2020-10-12 20:08:52 +03:00
|
|
|
#include "machines.hh"
|
|
|
|
#include "worker.hh"
|
|
|
|
#include "substitution-goal.hh"
|
|
|
|
#include "derivation-goal.hh"
|
2004-06-18 21:09:32 +03:00
|
|
|
|
2020-10-11 19:25:25 +03:00
|
|
|
namespace nix {
|
2004-06-18 21:09:32 +03:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
static void primeCache(Store & store, const std::vector<StorePathWithOutputs> & paths)
|
2017-08-31 17:02:36 +03:00
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
StorePathSet willBuild, willSubstitute, unknown;
|
2020-07-30 14:10:49 +03:00
|
|
|
uint64_t downloadSize, narSize;
|
2017-08-31 17:02:36 +03:00
|
|
|
store.queryMissing(paths, willBuild, willSubstitute, unknown, downloadSize, narSize);
|
2019-04-01 22:09:49 +03:00
|
|
|
|
|
|
|
if (!willBuild.empty() && 0 == settings.maxBuildJobs && getMachines().empty())
|
|
|
|
throw Error(
|
|
|
|
"%d derivations need to be built, but neither local builds ('--max-jobs') "
|
|
|
|
"nor remote builds ('--builders') are enabled", willBuild.size());
|
2017-08-31 17:02:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
void LocalStore::buildPaths(const std::vector<StorePathWithOutputs> & drvPaths, BuildMode buildMode)
|
2003-07-29 13:43:12 +03:00
|
|
|
{
|
2008-06-09 16:52:45 +03:00
|
|
|
Worker worker(*this);
|
2005-01-19 17:02:02 +02:00
|
|
|
|
2017-08-31 17:02:36 +03:00
|
|
|
primeCache(*this, drvPaths);
|
|
|
|
|
2005-01-19 17:02:02 +02:00
|
|
|
Goals goals;
|
2019-12-05 20:11:09 +02:00
|
|
|
for (auto & path : drvPaths) {
|
|
|
|
if (path.path.isDerivation())
|
|
|
|
goals.insert(worker.makeDerivationGoal(path.path, path.outputs, buildMode));
|
2012-06-27 23:58:15 +03:00
|
|
|
else
|
2019-12-05 20:11:09 +02:00
|
|
|
goals.insert(worker.makeSubstitutionGoal(path.path, buildMode == bmRepair ? Repair : NoRepair));
|
2012-11-26 16:39:10 +02:00
|
|
|
}
|
2005-02-23 13:19:27 +02:00
|
|
|
|
|
|
|
worker.run(goals);
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
StorePathSet failed;
|
2020-06-15 20:25:35 +03:00
|
|
|
std::optional<Error> ex;
|
2016-12-07 14:16:06 +02:00
|
|
|
for (auto & i : goals) {
|
2020-06-15 20:25:35 +03:00
|
|
|
if (i->ex) {
|
|
|
|
if (ex)
|
|
|
|
logError(i->ex->info());
|
|
|
|
else
|
|
|
|
ex = i->ex;
|
|
|
|
}
|
|
|
|
if (i->exitCode != Goal::ecSuccess) {
|
2015-07-17 20:24:28 +03:00
|
|
|
DerivationGoal * i2 = dynamic_cast<DerivationGoal *>(i.get());
|
2012-06-27 23:58:15 +03:00
|
|
|
if (i2) failed.insert(i2->getDrvPath());
|
2015-07-17 20:24:28 +03:00
|
|
|
else failed.insert(dynamic_cast<SubstitutionGoal *>(i.get())->getStorePath());
|
2005-02-23 13:19:27 +02:00
|
|
|
}
|
2016-12-07 14:16:06 +02:00
|
|
|
}
|
2012-07-27 16:59:18 +03:00
|
|
|
|
2020-06-15 20:25:35 +03:00
|
|
|
if (failed.size() == 1 && ex) {
|
|
|
|
ex->status = worker.exitStatus();
|
|
|
|
throw *ex;
|
|
|
|
} else if (!failed.empty()) {
|
|
|
|
if (ex) logError(ex->info());
|
2016-11-08 21:19:02 +02:00
|
|
|
throw Error(worker.exitStatus(), "build of %s failed", showPaths(failed));
|
2020-06-15 20:25:35 +03:00
|
|
|
}
|
2003-07-29 13:43:12 +03:00
|
|
|
}
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
BuildResult LocalStore::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 18:57:40 +03:00
|
|
|
BuildMode buildMode)
|
|
|
|
{
|
|
|
|
Worker worker(*this);
|
2020-08-22 23:44:47 +03: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 18:57:40 +03:00
|
|
|
|
|
|
|
BuildResult result;
|
|
|
|
|
|
|
|
try {
|
|
|
|
worker.run(Goals{goal});
|
2015-07-20 04:15:45 +03: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 18:57:40 +03:00
|
|
|
} catch (Error & e) {
|
|
|
|
result.status = BuildResult::MiscFailure;
|
|
|
|
result.errorMsg = e.msg();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
void LocalStore::ensurePath(const StorePath & path)
|
2003-07-20 22:29:38 +03:00
|
|
|
{
|
2004-06-18 21:09:32 +03:00
|
|
|
/* If the path is already valid, we're done. */
|
2008-06-09 16:52:45 +03:00
|
|
|
if (isValidPath(path)) return;
|
2004-06-18 21:09:32 +03:00
|
|
|
|
2020-06-16 23:20:18 +03:00
|
|
|
primeCache(*this, {{path}});
|
2017-08-31 17:02:36 +03:00
|
|
|
|
2008-06-09 16:52:45 +03:00
|
|
|
Worker worker(*this);
|
2005-02-23 13:19:27 +02:00
|
|
|
GoalPtr goal = worker.makeSubstitutionGoal(path);
|
2016-05-04 17:04:52 +03:00
|
|
|
Goals goals = {goal};
|
2005-02-23 13:19:27 +02:00
|
|
|
|
|
|
|
worker.run(goals);
|
|
|
|
|
2020-06-15 20:25:35 +03: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 22:29:38 +03:00
|
|
|
}
|
2006-09-05 00:06:23 +03:00
|
|
|
|
2012-07-27 16:59:18 +03:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
void LocalStore::repairPath(const StorePath & path)
|
2012-10-02 21:08:59 +03:00
|
|
|
{
|
|
|
|
Worker worker(*this);
|
2017-06-28 19:11:01 +03:00
|
|
|
GoalPtr goal = worker.makeSubstitutionGoal(path, Repair);
|
2016-05-04 17:04:52 +03:00
|
|
|
Goals goals = {goal};
|
2012-10-02 21:08:59 +03:00
|
|
|
|
|
|
|
worker.run(goals);
|
|
|
|
|
2020-06-15 20:25:35 +03:00
|
|
|
if (goal->exitCode != Goal::ecSuccess) {
|
2016-01-06 23:07:59 +02:00
|
|
|
/* Since substituting the path didn't work, if we have a valid
|
|
|
|
deriver, then rebuild the deriver. */
|
2019-12-05 20:11:09 +02:00
|
|
|
auto info = queryPathInfo(path);
|
|
|
|
if (info->deriver && isValidPath(*info->deriver)) {
|
2016-01-06 23:07:59 +02:00
|
|
|
goals.clear();
|
2019-12-05 20:11:09 +02:00
|
|
|
goals.insert(worker.makeDerivationGoal(*info->deriver, StringSet(), bmRepair));
|
2016-01-06 23:07:59 +02:00
|
|
|
worker.run(goals);
|
|
|
|
} else
|
2019-12-05 20:11:09 +02:00
|
|
|
throw Error(worker.exitStatus(), "cannot repair path '%s'", printStorePath(path));
|
2016-01-06 23:07:59 +02:00
|
|
|
}
|
2012-10-02 21:08:59 +03:00
|
|
|
}
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|