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

Put the builder context in a struct

This commit is contained in:
Eelco Dolstra 2025-05-01 12:46:40 +02:00
parent 40bbad3be5
commit fe0124fe17
5 changed files with 40 additions and 51 deletions

View file

@ -167,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);
} }
static 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

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 {
static 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");

View file

@ -5,15 +5,15 @@
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;
};
using BuiltinBuilder = using BuiltinBuilder = std::function<void(const BuiltinBuilderContext &)>;
std::function<void(const BasicDerivation & drv, const std::map<std::string, Path> & outputs)>;
struct RegisterBuiltinBuilder struct RegisterBuiltinBuilder
{ {

View file

@ -1860,15 +1860,15 @@ 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{.drv = drv};
std::string caFileData;
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 &) { }
} }
@ -2289,21 +2289,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")
builtinFetchurl(drv, outputs, netrcData, caFileData);
else {
std::string builtinName = drv.builder.substr(8); std::string builtinName = drv.builder.substr(8);
assert(RegisterBuiltinBuilder::builtinBuilders); assert(RegisterBuiltinBuilder::builtinBuilders);
if (auto builtin = get(*RegisterBuiltinBuilder::builtinBuilders, builtinName)) if (auto builtin = get(*RegisterBuiltinBuilder::builtinBuilders, builtinName))
(*builtin)(drv, outputs); (*builtin)(ctx);
else else
throw Error("unsupported builtin builder '%1%'", builtinName); 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"));