mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 23:42:43 +01:00
Add external builders
These are helper programs that execute derivations for specified
system types (e.g. using QEMU to emulate another system type).
To use, set `external-builders`:
external-builders = [{"systems": ["aarch64-linux"], "program": "/path/to/external-builder.py"}]
The external builder gets one command line argument, the path to a JSON file containing all necessary information about the derivation:
{
"args": [...],
"builder": "/nix/store/kwcyvgdg98n98hqapaz8sw92pc2s78x6-bash-5.2p37/bin/bash",
"env": {
"HOME": "/homeless-shelter",
...
},
"realStoreDir": "/tmp/nix/nix/store",
"storeDir": "/nix/store",
"tmpDir": "/tmp/nix-shell.dzQ2hE/nix-build-patchelf-0.14.3.drv-46/build",
"tmpDirInSandbox": "/build"
}
Co-authored-by: Cole Helbling <cole.helbling@determinate.systems>
This commit is contained in:
parent
76ac3758d7
commit
584ef0ffd3
6 changed files with 274 additions and 18 deletions
|
|
@ -229,6 +229,12 @@ protected:
|
|||
return acquireUserLock(1, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw an exception if we can't do this derivation because of
|
||||
* missing system features.
|
||||
*/
|
||||
virtual void checkSystem();
|
||||
|
||||
/**
|
||||
* Return the paths that should be made available in the sandbox.
|
||||
* This includes:
|
||||
|
|
@ -666,21 +672,8 @@ static bool checkNotWorldWritable(std::filesystem::path path)
|
|||
return true;
|
||||
}
|
||||
|
||||
std::optional<Descriptor> DerivationBuilderImpl::startBuild()
|
||||
void DerivationBuilderImpl::checkSystem()
|
||||
{
|
||||
if (useBuildUsers()) {
|
||||
if (!buildUser)
|
||||
buildUser = getBuildUser();
|
||||
|
||||
if (!buildUser)
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
/* Make sure that no other processes are executing under the
|
||||
sandbox uids. This must be done before any chownToBuilder()
|
||||
calls. */
|
||||
prepareUser();
|
||||
|
||||
/* Right platform? */
|
||||
if (!drvOptions.canBuildLocally(store, drv)) {
|
||||
auto msg =
|
||||
|
|
@ -704,6 +697,24 @@ std::optional<Descriptor> DerivationBuilderImpl::startBuild()
|
|||
|
||||
throw BuildError(BuildResult::Failure::InputRejected, msg);
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<Descriptor> DerivationBuilderImpl::startBuild()
|
||||
{
|
||||
if (useBuildUsers()) {
|
||||
if (!buildUser)
|
||||
buildUser = getBuildUser();
|
||||
|
||||
if (!buildUser)
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
checkSystem();
|
||||
|
||||
/* Make sure that no other processes are executing under the
|
||||
sandbox uids. This must be done before any chownToBuilder()
|
||||
calls. */
|
||||
prepareUser();
|
||||
|
||||
auto buildDir = store.config->getBuildDir();
|
||||
|
||||
|
|
@ -1909,12 +1920,16 @@ StorePath DerivationBuilderImpl::makeFallbackPath(const StorePath & path)
|
|||
#include "chroot-derivation-builder.cc"
|
||||
#include "linux-derivation-builder.cc"
|
||||
#include "darwin-derivation-builder.cc"
|
||||
#include "external-derivation-builder.cc"
|
||||
|
||||
namespace nix {
|
||||
|
||||
std::unique_ptr<DerivationBuilder> makeDerivationBuilder(
|
||||
LocalStore & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params)
|
||||
{
|
||||
if (auto builder = ExternalDerivationBuilder::newIfSupported(store, miscMethods, params))
|
||||
return builder;
|
||||
|
||||
bool useSandbox = false;
|
||||
|
||||
/* Are we doing a sandboxed build? */
|
||||
|
|
|
|||
110
src/libstore/unix/build/external-derivation-builder.cc
Normal file
110
src/libstore/unix/build/external-derivation-builder.cc
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
namespace nix {
|
||||
|
||||
struct ExternalDerivationBuilder : DerivationBuilderImpl
|
||||
{
|
||||
Settings::ExternalBuilder externalBuilder;
|
||||
|
||||
ExternalDerivationBuilder(
|
||||
LocalStore & store,
|
||||
std::unique_ptr<DerivationBuilderCallbacks> miscMethods,
|
||||
DerivationBuilderParams params,
|
||||
Settings::ExternalBuilder externalBuilder)
|
||||
: DerivationBuilderImpl(store, std::move(miscMethods), std::move(params))
|
||||
, externalBuilder(std::move(externalBuilder))
|
||||
{
|
||||
experimentalFeatureSettings.require(Xp::ExternalBuilders);
|
||||
}
|
||||
|
||||
static std::unique_ptr<ExternalDerivationBuilder> newIfSupported(
|
||||
LocalStore & store, std::unique_ptr<DerivationBuilderCallbacks> & miscMethods, DerivationBuilderParams & params)
|
||||
{
|
||||
for (auto & handler : settings.externalBuilders.get()) {
|
||||
for (auto & system : handler.systems)
|
||||
if (params.drv.platform == system)
|
||||
return std::make_unique<ExternalDerivationBuilder>(
|
||||
store, std::move(miscMethods), std::move(params), handler);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Path tmpDirInSandbox() override
|
||||
{
|
||||
/* In a sandbox, for determinism, always use the same temporary
|
||||
directory. */
|
||||
return "/build";
|
||||
}
|
||||
|
||||
void setBuildTmpDir() override
|
||||
{
|
||||
tmpDir = topTmpDir + "/build";
|
||||
createDir(tmpDir, 0700);
|
||||
}
|
||||
|
||||
void checkSystem() override {}
|
||||
|
||||
void startChild() override
|
||||
{
|
||||
if (drvOptions.getRequiredSystemFeatures(drv).count("recursive-nix"))
|
||||
throw Error("'recursive-nix' is not supported yet by external derivation builders");
|
||||
|
||||
auto json = nlohmann::json::object();
|
||||
|
||||
json.emplace("builder", drv.builder);
|
||||
{
|
||||
auto l = nlohmann::json::array();
|
||||
for (auto & i : drv.args)
|
||||
l.push_back(rewriteStrings(i, inputRewrites));
|
||||
json.emplace("args", std::move(l));
|
||||
}
|
||||
{
|
||||
auto j = nlohmann::json::object();
|
||||
for (auto & [name, value] : env)
|
||||
j.emplace(name, rewriteStrings(value, inputRewrites));
|
||||
json.emplace("env", std::move(j));
|
||||
}
|
||||
json.emplace("topTmpDir", topTmpDir);
|
||||
json.emplace("tmpDir", tmpDir);
|
||||
json.emplace("tmpDirInSandbox", tmpDirInSandbox());
|
||||
json.emplace("storeDir", store.storeDir);
|
||||
json.emplace("realStoreDir", store.config->realStoreDir.get());
|
||||
json.emplace("system", drv.platform);
|
||||
|
||||
// TODO(cole-h): writing this to stdin is too much effort right now, if we want to revisit
|
||||
// that, see this comment by Eelco about how to make it not suck:
|
||||
// https://github.com/DeterminateSystems/nix-src/pull/141#discussion_r2205493257
|
||||
auto jsonFile = std::filesystem::path{topTmpDir} / "build.json";
|
||||
writeFile(jsonFile, json.dump());
|
||||
|
||||
pid = startProcess([&]() {
|
||||
openSlave();
|
||||
try {
|
||||
commonChildInit();
|
||||
|
||||
Strings args = {externalBuilder.program};
|
||||
|
||||
if (!externalBuilder.args.empty()) {
|
||||
args.insert(args.end(), externalBuilder.args.begin(), externalBuilder.args.end());
|
||||
}
|
||||
|
||||
args.insert(args.end(), jsonFile);
|
||||
|
||||
if (chdir(tmpDir.c_str()) == -1)
|
||||
throw SysError("changing into '%1%'", tmpDir);
|
||||
|
||||
chownToBuilder(topTmpDir);
|
||||
|
||||
setUser();
|
||||
|
||||
debug("executing external builder: %s", concatStringsSep(" ", args));
|
||||
execv(externalBuilder.program.c_str(), stringsToCharPtrs(args).data());
|
||||
|
||||
throw SysError("executing '%s'", externalBuilder.program);
|
||||
} catch (...) {
|
||||
handleChildException(true);
|
||||
_exit(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace nix
|
||||
Loading…
Add table
Add a link
Reference in a new issue