mirror of
https://github.com/NixOS/nix.git
synced 2025-12-14 04:51:05 +01:00
Consolidate logic choosing where we can/should build a bit
I want to separate "policy" from "mechanism". Now the logic to decide how to build (a policy choice, though with some hard constraints) is all in derivation building goal, and all in the same spot. build hook, external builder, or local builder --- the choice between all three is made in the same spot --- pure policy. Now, if you want to use the external deriation builder, you simply provide the `ExternalBuilder` you wish to use, and there is no additional checking --- pure mechanism. It is the responsibility of the caller to choose an external builder that works for the derivation in question. Also, `checkSystem()` was the only thing throwing `BuildError` from `startBuilder`. Now that that is gone, we can now remove the `try...catch` around that.
This commit is contained in:
parent
2ff59ec3e0
commit
b57caaa1a2
8 changed files with 140 additions and 94 deletions
27
src/libstore/build/derivation-builder.cc
Normal file
27
src/libstore/build/derivation-builder.cc
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include "nix/util/json-utils.hh"
|
||||
#include "nix/store/build/derivation-builder.hh"
|
||||
|
||||
namespace nlohmann {
|
||||
|
||||
using namespace nix;
|
||||
|
||||
ExternalBuilder adl_serializer<ExternalBuilder>::from_json(const json & json)
|
||||
{
|
||||
auto obj = getObject(json);
|
||||
return {
|
||||
.systems = valueAt(obj, "systems"),
|
||||
.program = valueAt(obj, "program"),
|
||||
.args = valueAt(obj, "args"),
|
||||
};
|
||||
}
|
||||
|
||||
void adl_serializer<ExternalBuilder>::to_json(json & json, const ExternalBuilder & eb)
|
||||
{
|
||||
json = {
|
||||
{"systems", eb.systems},
|
||||
{"program", eb.program},
|
||||
{"args", eb.args},
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace nlohmann
|
||||
|
|
@ -491,6 +491,8 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
|
|||
|
||||
bool useHook;
|
||||
|
||||
const ExternalBuilder * externalBuilder = nullptr;
|
||||
|
||||
while (true) {
|
||||
trace("trying to build");
|
||||
|
||||
|
|
@ -584,7 +586,42 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
|
|||
co_await waitForAWhile();
|
||||
continue;
|
||||
case rpDecline:
|
||||
/* We should do it ourselves. */
|
||||
/* We should do it ourselves.
|
||||
|
||||
Now that we've decided we can't / won't do a remote build, check
|
||||
that we can in fact build locally. First see if there is an
|
||||
external builder for a "semi-local build". If there is, prefer to
|
||||
use that. If there is not, then check if we can do a "true" local
|
||||
build. */
|
||||
|
||||
externalBuilder = settings.findExternalDerivationBuilderIfSupported(*drv);
|
||||
|
||||
if (!externalBuilder && !drvOptions->canBuildLocally(worker.store, *drv)) {
|
||||
auto msg =
|
||||
fmt("Cannot build '%s'.\n"
|
||||
"Reason: " ANSI_RED "required system or feature not available" ANSI_NORMAL
|
||||
"\n"
|
||||
"Required system: '%s' with features {%s}\n"
|
||||
"Current system: '%s' with features {%s}",
|
||||
Magenta(worker.store.printStorePath(drvPath)),
|
||||
Magenta(drv->platform),
|
||||
concatStringsSep(", ", drvOptions->getRequiredSystemFeatures(*drv)),
|
||||
Magenta(settings.thisSystem),
|
||||
concatStringsSep<StringSet>(", ", worker.store.Store::config.systemFeatures));
|
||||
|
||||
// since aarch64-darwin has Rosetta 2, this user can actually run x86_64-darwin on their hardware -
|
||||
// we should tell them to run the command to install Darwin 2
|
||||
if (drv->platform == "x86_64-darwin" && settings.thisSystem == "aarch64-darwin")
|
||||
msg += fmt(
|
||||
"\nNote: run `%s` to run programs for x86_64-darwin",
|
||||
Magenta(
|
||||
"/usr/sbin/softwareupdate --install-rosetta && launchctl stop org.nixos.nix-daemon"));
|
||||
|
||||
builder.reset();
|
||||
outputLocks.unlock();
|
||||
worker.permanentFailure = true;
|
||||
co_return doneFailure({BuildResult::Failure::InputRejected, std::move(msg)});
|
||||
}
|
||||
useHook = false;
|
||||
break;
|
||||
}
|
||||
|
|
@ -771,36 +808,35 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
|
|||
co_return doneFailure(std::move(e));
|
||||
}
|
||||
|
||||
DerivationBuilderParams params{
|
||||
.drvPath = drvPath,
|
||||
.buildResult = buildResult,
|
||||
.drv = *drv,
|
||||
.drvOptions = *drvOptions,
|
||||
.inputPaths = inputPaths,
|
||||
.initialOutputs = initialOutputs,
|
||||
.buildMode = buildMode,
|
||||
.defaultPathsInChroot = std::move(defaultPathsInChroot),
|
||||
.systemFeatures = worker.store.config.systemFeatures.get(),
|
||||
.desugaredEnv = std::move(desugaredEnv),
|
||||
};
|
||||
|
||||
/* If we have to wait and retry (see below), then `builder` will
|
||||
already be created, so we don't need to create it again. */
|
||||
builder = makeDerivationBuilder(
|
||||
*localStoreP,
|
||||
std::make_unique<DerivationBuildingGoalCallbacks>(*this, builder),
|
||||
DerivationBuilderParams{
|
||||
.drvPath = drvPath,
|
||||
.buildResult = buildResult,
|
||||
.drv = *drv,
|
||||
.drvOptions = *drvOptions,
|
||||
.inputPaths = inputPaths,
|
||||
.initialOutputs = initialOutputs,
|
||||
.buildMode = buildMode,
|
||||
.defaultPathsInChroot = std::move(defaultPathsInChroot),
|
||||
.systemFeatures = worker.store.config.systemFeatures.get(),
|
||||
.desugaredEnv = std::move(desugaredEnv),
|
||||
});
|
||||
builder = externalBuilder ? makeExternalDerivationBuilder(
|
||||
*localStoreP,
|
||||
std::make_unique<DerivationBuildingGoalCallbacks>(*this, builder),
|
||||
std::move(params),
|
||||
*externalBuilder)
|
||||
: makeDerivationBuilder(
|
||||
*localStoreP,
|
||||
std::make_unique<DerivationBuildingGoalCallbacks>(*this, builder),
|
||||
std::move(params));
|
||||
}
|
||||
|
||||
std::optional<Descriptor> builderOutOpt;
|
||||
try {
|
||||
/* Okay, we have to build. */
|
||||
builderOutOpt = builder->startBuild();
|
||||
} catch (BuildError & e) {
|
||||
builder.reset();
|
||||
outputLocks.unlock();
|
||||
worker.permanentFailure = true;
|
||||
co_return doneFailure(std::move(e)); // InputRejected
|
||||
}
|
||||
if (!builderOutOpt) {
|
||||
if (auto builderOutOpt = builder->startBuild()) {
|
||||
builderOut = *std::move(builderOutOpt);
|
||||
} else {
|
||||
if (!actLock)
|
||||
actLock = std::make_unique<Activity>(
|
||||
*logger,
|
||||
|
|
@ -809,9 +845,7 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
|
|||
fmt("waiting for a free build user ID for '%s'", Magenta(worker.store.printStorePath(drvPath))));
|
||||
co_await waitForAWhile();
|
||||
continue;
|
||||
} else {
|
||||
builderOut = *std::move(builderOutOpt);
|
||||
};
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue