2020-10-12 17:08:52 +00:00
|
|
|
#include "machines.hh"
|
|
|
|
#include "worker.hh"
|
|
|
|
#include "substitution-goal.hh"
|
|
|
|
#include "derivation-goal.hh"
|
2021-02-26 15:20:33 +00:00
|
|
|
#include "local-store.hh"
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2020-10-11 16:25:25 +00:00
|
|
|
namespace nix {
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2021-07-19 13:43:08 +00:00
|
|
|
void Store::buildPaths(const std::vector<DerivedPath> & reqs, BuildMode buildMode, std::shared_ptr<Store> evalStore)
|
2003-07-29 10:43:12 +00:00
|
|
|
{
|
2021-07-19 13:43:08 +00:00
|
|
|
Worker worker(*this, evalStore ? *evalStore : *this);
|
2005-01-19 15:02:02 +00:00
|
|
|
|
|
|
|
Goals goals;
|
2021-03-02 03:50:41 +00:00
|
|
|
for (auto & br : reqs) {
|
|
|
|
std::visit(overloaded {
|
2021-04-05 13:48:18 +00:00
|
|
|
[&](DerivedPath::Built bfd) {
|
2021-03-02 03:50:41 +00:00
|
|
|
goals.insert(worker.makeDerivationGoal(bfd.drvPath, bfd.outputs, buildMode));
|
|
|
|
},
|
2021-04-05 13:48:18 +00:00
|
|
|
[&](DerivedPath::Opaque bo) {
|
2021-03-02 03:50:41 +00:00
|
|
|
goals.insert(worker.makePathSubstitutionGoal(bo.path, buildMode == bmRepair ? Repair : NoRepair));
|
|
|
|
},
|
2021-04-05 13:24:42 +00:00
|
|
|
}, br.raw());
|
2012-11-26 14:39:10 +00:00
|
|
|
}
|
2005-02-23 11:19:27 +00:00
|
|
|
|
|
|
|
worker.run(goals);
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet failed;
|
2020-06-15 17:25:35 +00:00
|
|
|
std::optional<Error> ex;
|
2016-12-07 12:16:06 +00:00
|
|
|
for (auto & i : goals) {
|
2020-06-15 17:25:35 +00:00
|
|
|
if (i->ex) {
|
|
|
|
if (ex)
|
|
|
|
logError(i->ex->info());
|
|
|
|
else
|
|
|
|
ex = i->ex;
|
|
|
|
}
|
|
|
|
if (i->exitCode != Goal::ecSuccess) {
|
2020-11-18 13:36:15 +00:00
|
|
|
if (auto i2 = dynamic_cast<DerivationGoal *>(i.get())) failed.insert(i2->drvPath);
|
2020-11-09 12:47:06 +00:00
|
|
|
else if (auto i2 = dynamic_cast<PathSubstitutionGoal *>(i.get())) failed.insert(i2->storePath);
|
2005-02-23 11:19:27 +00:00
|
|
|
}
|
2016-12-07 12:16:06 +00:00
|
|
|
}
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2020-06-15 17:25:35 +00:00
|
|
|
if (failed.size() == 1 && ex) {
|
|
|
|
ex->status = worker.exitStatus();
|
|
|
|
throw *ex;
|
|
|
|
} else if (!failed.empty()) {
|
|
|
|
if (ex) logError(ex->info());
|
2016-11-08 19:19:02 +00:00
|
|
|
throw Error(worker.exitStatus(), "build of %s failed", showPaths(failed));
|
2020-06-15 17:25:35 +00:00
|
|
|
}
|
2003-07-29 10:43:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 17:54:57 +00:00
|
|
|
BuildResult Store::buildDerivation(const StorePath & drvPath, const BasicDerivation & drv,
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
BuildMode buildMode)
|
|
|
|
{
|
2021-07-19 13:43:08 +00:00
|
|
|
Worker worker(*this, *this);
|
2020-08-22 20:44:47 +00:00
|
|
|
auto goal = worker.makeBasicDerivationGoal(drvPath, drv, {}, buildMode);
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
|
|
|
|
BuildResult result;
|
|
|
|
|
|
|
|
try {
|
|
|
|
worker.run(Goals{goal});
|
2015-07-20 01:15:45 +00:00
|
|
|
result = goal->getResult();
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
} catch (Error & e) {
|
|
|
|
result.status = BuildResult::MiscFailure;
|
|
|
|
result.errorMsg = e.msg();
|
|
|
|
}
|
2021-01-26 09:50:44 +00:00
|
|
|
// XXX: Should use `goal->queryPartialDerivationOutputMap()` once it's
|
|
|
|
// extended to return the full realisation for each output
|
|
|
|
auto staticDrvOutputs = drv.outputsAndOptPaths(*this);
|
|
|
|
auto outputHashes = staticOutputHashes(*this, drv);
|
|
|
|
for (auto & [outputName, staticOutput] : staticDrvOutputs) {
|
|
|
|
auto outputId = DrvOutput{outputHashes.at(outputName), outputName};
|
|
|
|
if (staticOutput.second)
|
|
|
|
result.builtOutputs.insert_or_assign(
|
|
|
|
outputId,
|
|
|
|
Realisation{ outputId, *staticOutput.second}
|
|
|
|
);
|
2021-02-26 15:34:33 +00:00
|
|
|
if (settings.isExperimentalFeatureEnabled("ca-derivations") && !derivationHasKnownOutputPaths(drv.type())) {
|
2021-01-26 09:50:44 +00:00
|
|
|
auto realisation = this->queryRealisation(outputId);
|
|
|
|
if (realisation)
|
|
|
|
result.builtOutputs.insert_or_assign(
|
|
|
|
outputId,
|
|
|
|
*realisation
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 15:57:40 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-20 17:54:57 +00:00
|
|
|
void Store::ensurePath(const StorePath & path)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
2004-06-18 18:09:32 +00:00
|
|
|
/* If the path is already valid, we're done. */
|
2008-06-09 13:52:45 +00:00
|
|
|
if (isValidPath(path)) return;
|
2004-06-18 18:09:32 +00:00
|
|
|
|
2021-07-19 13:43:08 +00:00
|
|
|
Worker worker(*this, *this);
|
2020-11-09 12:47:06 +00:00
|
|
|
GoalPtr goal = worker.makePathSubstitutionGoal(path);
|
2016-05-04 14:04:52 +00:00
|
|
|
Goals goals = {goal};
|
2005-02-23 11:19:27 +00:00
|
|
|
|
|
|
|
worker.run(goals);
|
|
|
|
|
2020-06-15 17:25:35 +00:00
|
|
|
if (goal->exitCode != Goal::ecSuccess) {
|
|
|
|
if (goal->ex) {
|
|
|
|
goal->ex->status = worker.exitStatus();
|
|
|
|
throw *goal->ex;
|
|
|
|
} else
|
|
|
|
throw Error(worker.exitStatus(), "path '%s' does not exist and cannot be created", printStorePath(path));
|
|
|
|
}
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|
2006-09-04 21:06:23 +00:00
|
|
|
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void LocalStore::repairPath(const StorePath & path)
|
2012-10-02 18:08:59 +00:00
|
|
|
{
|
2021-07-19 13:43:08 +00:00
|
|
|
Worker worker(*this, *this);
|
2020-11-09 12:47:06 +00:00
|
|
|
GoalPtr goal = worker.makePathSubstitutionGoal(path, Repair);
|
2016-05-04 14:04:52 +00:00
|
|
|
Goals goals = {goal};
|
2012-10-02 18:08:59 +00:00
|
|
|
|
|
|
|
worker.run(goals);
|
|
|
|
|
2020-06-15 17:25:35 +00:00
|
|
|
if (goal->exitCode != Goal::ecSuccess) {
|
2016-01-06 21:07:59 +00:00
|
|
|
/* Since substituting the path didn't work, if we have a valid
|
|
|
|
deriver, then rebuild the deriver. */
|
2019-12-05 18:11:09 +00:00
|
|
|
auto info = queryPathInfo(path);
|
|
|
|
if (info->deriver && isValidPath(*info->deriver)) {
|
2016-01-06 21:07:59 +00:00
|
|
|
goals.clear();
|
2019-12-05 18:11:09 +00:00
|
|
|
goals.insert(worker.makeDerivationGoal(*info->deriver, StringSet(), bmRepair));
|
2016-01-06 21:07:59 +00:00
|
|
|
worker.run(goals);
|
|
|
|
} else
|
2019-12-05 18:11:09 +00:00
|
|
|
throw Error(worker.exitStatus(), "cannot repair path '%s'", printStorePath(path));
|
2016-01-06 21:07:59 +00:00
|
|
|
}
|
2012-10-02 18:08:59 +00:00
|
|
|
}
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|