2020-10-12 20:08:52 +03:00
|
|
|
#include "worker.hh"
|
|
|
|
#include "substitution-goal.hh"
|
|
|
|
#include "derivation-goal.hh"
|
2021-02-26 17:20:33 +02:00
|
|
|
#include "local-store.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
|
|
|
|
2021-07-19 16:43:08 +03:00
|
|
|
void Store::buildPaths(const std::vector<DerivedPath> & reqs, BuildMode buildMode, std::shared_ptr<Store> evalStore)
|
2003-07-29 13:43:12 +03:00
|
|
|
{
|
2021-07-19 16:43:08 +03:00
|
|
|
Worker worker(*this, evalStore ? *evalStore : *this);
|
2005-01-19 17:02:02 +02:00
|
|
|
|
|
|
|
Goals goals;
|
2021-10-01 00:31:21 +03:00
|
|
|
for (const auto & br : reqs) {
|
2021-03-02 05:50:41 +02:00
|
|
|
std::visit(overloaded {
|
2021-10-01 00:31:21 +03:00
|
|
|
[&](const DerivedPath::Built & bfd) {
|
2021-03-02 05:50:41 +02:00
|
|
|
goals.insert(worker.makeDerivationGoal(bfd.drvPath, bfd.outputs, buildMode));
|
|
|
|
},
|
2021-10-01 00:31:21 +03:00
|
|
|
[&](const DerivedPath::Opaque & bo) {
|
2021-03-02 05:50:41 +02:00
|
|
|
goals.insert(worker.makePathSubstitutionGoal(bo.path, buildMode == bmRepair ? Repair : NoRepair));
|
|
|
|
},
|
2021-04-05 16:24:42 +03:00
|
|
|
}, br.raw());
|
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
|
2022-12-07 13:58:58 +02:00
|
|
|
ex = std::move(i->ex);
|
2020-06-15 20:25:35 +03:00
|
|
|
}
|
|
|
|
if (i->exitCode != Goal::ecSuccess) {
|
2020-11-18 15:36:15 +02:00
|
|
|
if (auto i2 = dynamic_cast<DerivationGoal *>(i.get())) failed.insert(i2->drvPath);
|
2020-11-09 14:47:06 +02:00
|
|
|
else if (auto i2 = dynamic_cast<PathSubstitutionGoal *>(i.get())) failed.insert(i2->storePath);
|
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();
|
2022-12-07 13:58:58 +02:00
|
|
|
throw std::move(*ex);
|
2020-06-15 20:25:35 +03:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2022-03-08 20:50:46 +02:00
|
|
|
std::vector<BuildResult> Store::buildPathsWithResults(
|
|
|
|
const std::vector<DerivedPath> & reqs,
|
|
|
|
BuildMode buildMode,
|
|
|
|
std::shared_ptr<Store> evalStore)
|
|
|
|
{
|
|
|
|
Worker worker(*this, evalStore ? *evalStore : *this);
|
|
|
|
|
|
|
|
Goals goals;
|
|
|
|
for (const auto & br : reqs) {
|
|
|
|
std::visit(overloaded {
|
|
|
|
[&](const DerivedPath::Built & bfd) {
|
|
|
|
goals.insert(worker.makeDerivationGoal(bfd.drvPath, bfd.outputs, buildMode));
|
|
|
|
},
|
|
|
|
[&](const DerivedPath::Opaque & bo) {
|
|
|
|
goals.insert(worker.makePathSubstitutionGoal(bo.path, buildMode == bmRepair ? Repair : NoRepair));
|
|
|
|
},
|
|
|
|
}, br.raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
worker.run(goals);
|
|
|
|
|
|
|
|
std::vector<BuildResult> results;
|
|
|
|
|
|
|
|
for (auto & i : goals)
|
|
|
|
results.push_back(i->buildResult);
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:54:57 +02: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 18:57:40 +03:00
|
|
|
BuildMode buildMode)
|
|
|
|
{
|
2021-07-19 16:43:08 +03:00
|
|
|
Worker worker(*this, *this);
|
2023-01-11 23:32:30 +02:00
|
|
|
auto goal = worker.makeBasicDerivationGoal(drvPath, drv, OutputsSpec::All {}, 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
|
|
|
|
|
|
|
try {
|
|
|
|
worker.run(Goals{goal});
|
2022-03-09 13:25:35 +02:00
|
|
|
return goal->buildResult;
|
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) {
|
2022-03-09 13:25:35 +02:00
|
|
|
return BuildResult {
|
|
|
|
.status = BuildResult::MiscFailure,
|
|
|
|
.errorMsg = e.msg(),
|
2023-01-11 23:32:30 +02:00
|
|
|
.path = DerivedPath::Built {
|
|
|
|
.drvPath = drvPath,
|
|
|
|
.outputs = OutputsSpec::All { },
|
|
|
|
},
|
2022-03-09 13:25:35 +02:00
|
|
|
};
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-20 19:54:57 +02:00
|
|
|
void Store::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
|
|
|
|
2021-07-19 16:43:08 +03:00
|
|
|
Worker worker(*this, *this);
|
2020-11-09 14:47:06 +02:00
|
|
|
GoalPtr goal = worker.makePathSubstitutionGoal(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();
|
2022-12-07 13:58:58 +02:00
|
|
|
throw std::move(*goal->ex);
|
2020-06-15 20:25:35 +03:00
|
|
|
} 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
|
|
|
{
|
2021-07-19 16:43:08 +03:00
|
|
|
Worker worker(*this, *this);
|
2020-11-09 14:47:06 +02:00
|
|
|
GoalPtr goal = worker.makePathSubstitutionGoal(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();
|
2023-01-12 01:57:18 +02:00
|
|
|
// FIXME: Should just build the specific output we need.
|
|
|
|
goals.insert(worker.makeDerivationGoal(*info->deriver, OutputsSpec::All { }, 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
|
|
|
}
|