1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 10:41:16 +02:00

Merge pull request #13138 from NixOS/register-builtin-builders

Register builtin builders
This commit is contained in:
Jörg Thalheim 2025-05-13 08:50:19 +02:00 committed by GitHub
commit 0f985fea11
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 63 additions and 53 deletions

View file

@ -1,4 +1,5 @@
#include "nix/store/builtins/buildenv.hh" #include "nix/store/builtins/buildenv.hh"
#include "nix/store/builtins.hh"
#include "nix/store/derivations.hh" #include "nix/store/derivations.hh"
#include "nix/util/signals.hh" #include "nix/util/signals.hh"
@ -166,17 +167,15 @@ void buildProfile(const Path & out, Packages && pkgs)
debug("created %d symlinks in user environment", state.symlinks); debug("created %d symlinks in user environment", state.symlinks);
} }
void builtinBuildenv( static void builtinBuildenv(const BuiltinBuilderContext & ctx)
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs)
{ {
auto getAttr = [&](const std::string & name) { auto getAttr = [&](const std::string & name) {
auto i = drv.env.find(name); auto i = ctx.drv.env.find(name);
if (i == drv.env.end()) throw Error("attribute '%s' missing", name); if (i == ctx.drv.env.end()) throw Error("attribute '%s' missing", name);
return i->second; return i->second;
}; };
auto out = outputs.at("out"); auto out = ctx.outputs.at("out");
createDirs(out); createDirs(out);
/* Convert the stuff we get from the environment back into a /* Convert the stuff we get from the environment back into a
@ -203,4 +202,6 @@ void builtinBuildenv(
createSymlink(getAttr("manifest"), out + "/manifest.nix"); createSymlink(getAttr("manifest"), out + "/manifest.nix");
} }
static RegisterBuiltinBuilder registerBuildenv("buildenv", builtinBuildenv);
} }

View file

@ -6,33 +6,29 @@
namespace nix { namespace nix {
void builtinFetchurl( static void builtinFetchurl(const BuiltinBuilderContext & ctx)
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs,
const std::string & netrcData,
const std::string & caFileData)
{ {
/* Make the host's netrc data available. Too bad curl requires /* Make the host's netrc data available. Too bad curl requires
this to be stored in a file. It would be nice if we could just this to be stored in a file. It would be nice if we could just
pass a pointer to the data. */ pass a pointer to the data. */
if (netrcData != "") { if (ctx.netrcData != "") {
settings.netrcFile = "netrc"; settings.netrcFile = "netrc";
writeFile(settings.netrcFile, netrcData, 0600); writeFile(settings.netrcFile, ctx.netrcData, 0600);
} }
settings.caFile = "ca-certificates.crt"; settings.caFile = "ca-certificates.crt";
writeFile(settings.caFile, caFileData, 0600); writeFile(settings.caFile, ctx.caFileData, 0600);
auto out = get(drv.outputs, "out"); auto out = get(ctx.drv.outputs, "out");
if (!out) if (!out)
throw Error("'builtin:fetchurl' requires an 'out' output"); throw Error("'builtin:fetchurl' requires an 'out' output");
if (!(drv.type().isFixed() || drv.type().isImpure())) if (!(ctx.drv.type().isFixed() || ctx.drv.type().isImpure()))
throw Error("'builtin:fetchurl' must be a fixed-output or impure derivation"); throw Error("'builtin:fetchurl' must be a fixed-output or impure derivation");
auto storePath = outputs.at("out"); auto storePath = ctx.outputs.at("out");
auto mainUrl = drv.env.at("url"); auto mainUrl = ctx.drv.env.at("url");
bool unpack = getOr(drv.env, "unpack", "") == "1"; bool unpack = getOr(ctx.drv.env, "unpack", "") == "1";
/* Note: have to use a fresh fileTransfer here because we're in /* Note: have to use a fresh fileTransfer here because we're in
a forked process. */ a forked process. */
@ -56,8 +52,8 @@ void builtinFetchurl(
else else
writeFile(storePath, *source); writeFile(storePath, *source);
auto executable = drv.env.find("executable"); auto executable = ctx.drv.env.find("executable");
if (executable != drv.env.end() && executable->second == "1") { if (executable != ctx.drv.env.end() && executable->second == "1") {
if (chmod(storePath.c_str(), 0755) == -1) if (chmod(storePath.c_str(), 0755) == -1)
throw SysError("making '%1%' executable", storePath); throw SysError("making '%1%' executable", storePath);
} }
@ -79,4 +75,6 @@ void builtinFetchurl(
fetch(mainUrl); fetch(mainUrl);
} }
static RegisterBuiltinBuilder registerFetchurl("fetchurl", builtinFetchurl);
} }

View file

@ -3,17 +3,15 @@
namespace nix { namespace nix {
void builtinUnpackChannel( static void builtinUnpackChannel(const BuiltinBuilderContext & ctx)
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs)
{ {
auto getAttr = [&](const std::string & name) -> const std::string & { auto getAttr = [&](const std::string & name) -> const std::string & {
auto i = drv.env.find(name); auto i = ctx.drv.env.find(name);
if (i == drv.env.end()) throw Error("attribute '%s' missing", name); if (i == ctx.drv.env.end()) throw Error("attribute '%s' missing", name);
return i->second; return i->second;
}; };
std::filesystem::path out{outputs.at("out")}; std::filesystem::path out{ctx.outputs.at("out")};
auto & channelName = getAttr("channelName"); auto & channelName = getAttr("channelName");
auto & src = getAttr("src"); auto & src = getAttr("src");
@ -42,4 +40,6 @@ void builtinUnpackChannel(
} }
} }
static RegisterBuiltinBuilder registerUnpackChannel("unpack-channel", builtinUnpackChannel);
} }

View file

@ -5,15 +5,30 @@
namespace nix { namespace nix {
// TODO: make pluggable. struct BuiltinBuilderContext
void builtinFetchurl( {
const BasicDerivation & drv, const BasicDerivation & drv;
const std::map<std::string, Path> & outputs, std::map<std::string, Path> outputs;
const std::string & netrcData, std::string netrcData;
const std::string & caFileData); std::string caFileData;
Path tmpDirInSandbox;
};
void builtinUnpackChannel( using BuiltinBuilder = std::function<void(const BuiltinBuilderContext &)>;
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs); struct RegisterBuiltinBuilder
{
typedef std::map<std::string, BuiltinBuilder> BuiltinBuilders;
static BuiltinBuilders & builtinBuilders() {
static BuiltinBuilders builders;
return builders;
}
RegisterBuiltinBuilder(const std::string & name, BuiltinBuilder && fun)
{
builtinBuilders().insert_or_assign(name, std::move(fun));
}
};
} }

View file

@ -45,8 +45,4 @@ typedef std::vector<Package> Packages;
void buildProfile(const Path & out, Packages && pkgs); void buildProfile(const Path & out, Packages && pkgs);
void builtinBuildenv(
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs);
} }

View file

@ -1861,15 +1861,18 @@ void DerivationBuilderImpl::runChild()
/* Make the contents of netrc and the CA certificate bundle /* Make the contents of netrc and the CA certificate bundle
available to builtin:fetchurl (which may run under a available to builtin:fetchurl (which may run under a
different uid and/or in a sandbox). */ different uid and/or in a sandbox). */
std::string netrcData; BuiltinBuilderContext ctx{
std::string caFileData; .drv = drv,
.tmpDirInSandbox = tmpDirInSandbox,
};
if (drv.isBuiltin() && drv.builder == "builtin:fetchurl") { if (drv.isBuiltin() && drv.builder == "builtin:fetchurl") {
try { try {
netrcData = readFile(settings.netrcFile); ctx.netrcData = readFile(settings.netrcFile);
} catch (SystemError &) { } } catch (SystemError &) { }
try { try {
caFileData = readFile(settings.caFile); ctx.caFileData = readFile(settings.caFile);
} catch (SystemError &) { } } catch (SystemError &) { }
} }
@ -2290,19 +2293,16 @@ void DerivationBuilderImpl::runChild()
try { try {
logger = makeJSONLogger(getStandardError()); logger = makeJSONLogger(getStandardError());
std::map<std::string, Path> outputs;
for (auto & e : drv.outputs) for (auto & e : drv.outputs)
outputs.insert_or_assign(e.first, ctx.outputs.insert_or_assign(e.first,
store.printStorePath(scratchOutputs.at(e.first))); store.printStorePath(scratchOutputs.at(e.first)));
if (drv.builder == "builtin:fetchurl") std::string builtinName = drv.builder.substr(8);
builtinFetchurl(drv, outputs, netrcData, caFileData); assert(RegisterBuiltinBuilder::builtinBuilders);
else if (drv.builder == "builtin:buildenv") if (auto builtin = get(RegisterBuiltinBuilder::builtinBuilders(), builtinName))
builtinBuildenv(drv, outputs); (*builtin)(ctx);
else if (drv.builder == "builtin:unpack-channel")
builtinUnpackChannel(drv, outputs);
else else
throw Error("unsupported builtin builder '%1%'", drv.builder.substr(8)); throw Error("unsupported builtin builder '%1%'", builtinName);
_exit(0); _exit(0);
} catch (std::exception & e) { } catch (std::exception & e) {
writeFull(STDERR_FILENO, e.what() + std::string("\n")); writeFull(STDERR_FILENO, e.what() + std::string("\n"));