mirror of
https://github.com/NixOS/nix
synced 2025-06-25 06:31:14 +02:00
Put the builder context in a struct
This commit is contained in:
parent
40bbad3be5
commit
fe0124fe17
5 changed files with 40 additions and 51 deletions
|
@ -167,17 +167,15 @@ void buildProfile(const Path & out, Packages && pkgs)
|
|||
debug("created %d symlinks in user environment", state.symlinks);
|
||||
}
|
||||
|
||||
static void builtinBuildenv(
|
||||
const BasicDerivation & drv,
|
||||
const std::map<std::string, Path> & outputs)
|
||||
static void builtinBuildenv(const BuiltinBuilderContext & ctx)
|
||||
{
|
||||
auto getAttr = [&](const std::string & name) {
|
||||
auto i = drv.env.find(name);
|
||||
if (i == drv.env.end()) throw Error("attribute '%s' missing", name);
|
||||
auto i = ctx.drv.env.find(name);
|
||||
if (i == ctx.drv.env.end()) throw Error("attribute '%s' missing", name);
|
||||
return i->second;
|
||||
};
|
||||
|
||||
auto out = outputs.at("out");
|
||||
auto out = ctx.outputs.at("out");
|
||||
createDirs(out);
|
||||
|
||||
/* Convert the stuff we get from the environment back into a
|
||||
|
|
|
@ -6,33 +6,29 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
void builtinFetchurl(
|
||||
const BasicDerivation & drv,
|
||||
const std::map<std::string, Path> & outputs,
|
||||
const std::string & netrcData,
|
||||
const std::string & caFileData)
|
||||
static void builtinFetchurl(const BuiltinBuilderContext & ctx)
|
||||
{
|
||||
/* 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
|
||||
pass a pointer to the data. */
|
||||
if (netrcData != "") {
|
||||
if (ctx.netrcData != "") {
|
||||
settings.netrcFile = "netrc";
|
||||
writeFile(settings.netrcFile, netrcData, 0600);
|
||||
writeFile(settings.netrcFile, ctx.netrcData, 0600);
|
||||
}
|
||||
|
||||
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)
|
||||
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");
|
||||
|
||||
auto storePath = outputs.at("out");
|
||||
auto mainUrl = drv.env.at("url");
|
||||
bool unpack = getOr(drv.env, "unpack", "") == "1";
|
||||
auto storePath = ctx.outputs.at("out");
|
||||
auto mainUrl = ctx.drv.env.at("url");
|
||||
bool unpack = getOr(ctx.drv.env, "unpack", "") == "1";
|
||||
|
||||
/* Note: have to use a fresh fileTransfer here because we're in
|
||||
a forked process. */
|
||||
|
@ -56,8 +52,8 @@ void builtinFetchurl(
|
|||
else
|
||||
writeFile(storePath, *source);
|
||||
|
||||
auto executable = drv.env.find("executable");
|
||||
if (executable != drv.env.end() && executable->second == "1") {
|
||||
auto executable = ctx.drv.env.find("executable");
|
||||
if (executable != ctx.drv.env.end() && executable->second == "1") {
|
||||
if (chmod(storePath.c_str(), 0755) == -1)
|
||||
throw SysError("making '%1%' executable", storePath);
|
||||
}
|
||||
|
@ -79,4 +75,6 @@ void builtinFetchurl(
|
|||
fetch(mainUrl);
|
||||
}
|
||||
|
||||
static RegisterBuiltinBuilder registerFetchurl("fetchurl", builtinFetchurl);
|
||||
|
||||
}
|
||||
|
|
|
@ -3,17 +3,15 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
static void builtinUnpackChannel(
|
||||
const BasicDerivation & drv,
|
||||
const std::map<std::string, Path> & outputs)
|
||||
static void builtinUnpackChannel(const BuiltinBuilderContext & ctx)
|
||||
{
|
||||
auto getAttr = [&](const std::string & name) -> const std::string & {
|
||||
auto i = drv.env.find(name);
|
||||
if (i == drv.env.end()) throw Error("attribute '%s' missing", name);
|
||||
auto i = ctx.drv.env.find(name);
|
||||
if (i == ctx.drv.env.end()) throw Error("attribute '%s' missing", name);
|
||||
return i->second;
|
||||
};
|
||||
|
||||
std::filesystem::path out{outputs.at("out")};
|
||||
std::filesystem::path out{ctx.outputs.at("out")};
|
||||
auto & channelName = getAttr("channelName");
|
||||
auto & src = getAttr("src");
|
||||
|
||||
|
|
|
@ -5,15 +5,15 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
// TODO: make pluggable.
|
||||
void builtinFetchurl(
|
||||
const BasicDerivation & drv,
|
||||
const std::map<std::string, Path> & outputs,
|
||||
const std::string & netrcData,
|
||||
const std::string & caFileData);
|
||||
struct BuiltinBuilderContext
|
||||
{
|
||||
const BasicDerivation & drv;
|
||||
std::map<std::string, Path> outputs;
|
||||
std::string netrcData;
|
||||
std::string caFileData;
|
||||
};
|
||||
|
||||
using BuiltinBuilder =
|
||||
std::function<void(const BasicDerivation & drv, const std::map<std::string, Path> & outputs)>;
|
||||
using BuiltinBuilder = std::function<void(const BuiltinBuilderContext &)>;
|
||||
|
||||
struct RegisterBuiltinBuilder
|
||||
{
|
||||
|
|
|
@ -1860,15 +1860,15 @@ void DerivationBuilderImpl::runChild()
|
|||
/* Make the contents of netrc and the CA certificate bundle
|
||||
available to builtin:fetchurl (which may run under a
|
||||
different uid and/or in a sandbox). */
|
||||
std::string netrcData;
|
||||
std::string caFileData;
|
||||
BuiltinBuilderContext ctx{.drv = drv};
|
||||
|
||||
if (drv.isBuiltin() && drv.builder == "builtin:fetchurl") {
|
||||
try {
|
||||
netrcData = readFile(settings.netrcFile);
|
||||
ctx.netrcData = readFile(settings.netrcFile);
|
||||
} catch (SystemError &) { }
|
||||
|
||||
try {
|
||||
caFileData = readFile(settings.caFile);
|
||||
ctx.caFileData = readFile(settings.caFile);
|
||||
} catch (SystemError &) { }
|
||||
}
|
||||
|
||||
|
@ -2289,21 +2289,16 @@ void DerivationBuilderImpl::runChild()
|
|||
try {
|
||||
logger = makeJSONLogger(getStandardError());
|
||||
|
||||
std::map<std::string, Path> 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)));
|
||||
|
||||
if (drv.builder == "builtin:fetchurl")
|
||||
builtinFetchurl(drv, outputs, netrcData, caFileData);
|
||||
else {
|
||||
std::string builtinName = drv.builder.substr(8);
|
||||
assert(RegisterBuiltinBuilder::builtinBuilders);
|
||||
if (auto builtin = get(*RegisterBuiltinBuilder::builtinBuilders, builtinName))
|
||||
(*builtin)(drv, outputs);
|
||||
else
|
||||
throw Error("unsupported builtin builder '%1%'", builtinName);
|
||||
}
|
||||
std::string builtinName = drv.builder.substr(8);
|
||||
assert(RegisterBuiltinBuilder::builtinBuilders);
|
||||
if (auto builtin = get(*RegisterBuiltinBuilder::builtinBuilders, builtinName))
|
||||
(*builtin)(ctx);
|
||||
else
|
||||
throw Error("unsupported builtin builder '%1%'", builtinName);
|
||||
_exit(0);
|
||||
} catch (std::exception & e) {
|
||||
writeFull(STDERR_FILENO, e.what() + std::string("\n"));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue