Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/libstore/builtins/buildenv.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "nix/store/builtins/buildenv.hh"
#include "nix/store/builtins.hh"
#include "nix/store/derivations.hh"
#include "nix/util/signals.hh"

Expand Down Expand Up @@ -166,17 +167,15 @@ void buildProfile(const Path & out, Packages && pkgs)
debug("created %d symlinks in user environment", state.symlinks);
}

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
Expand All @@ -203,4 +202,6 @@ void builtinBuildenv(
createSymlink(getAttr("manifest"), out + "/manifest.nix");
}

static RegisterBuiltinBuilder registerBuildenv("buildenv", builtinBuildenv);

}
28 changes: 13 additions & 15 deletions src/libstore/builtins/fetchurl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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);
}
Expand All @@ -79,4 +75,6 @@ void builtinFetchurl(
fetch(mainUrl);
}

static RegisterBuiltinBuilder registerFetchurl("fetchurl", builtinFetchurl);

}
12 changes: 6 additions & 6 deletions src/libstore/builtins/unpack-channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@

namespace nix {

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

Expand Down Expand Up @@ -42,4 +40,6 @@ void builtinUnpackChannel(
}
}

static RegisterBuiltinBuilder registerUnpackChannel("unpack-channel", builtinUnpackChannel);

}
35 changes: 25 additions & 10 deletions src/libstore/include/nix/store/builtins.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,30 @@

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);

void builtinUnpackChannel(
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs);
struct BuiltinBuilderContext
{
const BasicDerivation & drv;
std::map<std::string, Path> outputs;
std::string netrcData;
std::string caFileData;
Path tmpDirInSandbox;
};

using BuiltinBuilder = std::function<void(const BuiltinBuilderContext &)>;

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));
}
};

}
4 changes: 0 additions & 4 deletions src/libstore/include/nix/store/builtins/buildenv.hh
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,4 @@ typedef std::vector<Package> Packages;

void buildProfile(const Path & out, Packages && pkgs);

void builtinBuildenv(
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs);

}
26 changes: 13 additions & 13 deletions src/libstore/unix/build/derivation-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1857,15 +1857,18 @@ 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,
.tmpDirInSandbox = tmpDirInSandbox,
};

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 &) { }
}

Expand Down Expand Up @@ -2286,19 +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 if (drv.builder == "builtin:buildenv")
builtinBuildenv(drv, outputs);
else if (drv.builder == "builtin:unpack-channel")
builtinUnpackChannel(drv, outputs);
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%'", drv.builder.substr(8));
throw Error("unsupported builtin builder '%1%'", builtinName);
_exit(0);
} catch (std::exception & e) {
writeFull(STDERR_FILENO, e.what() + std::string("\n"));
Expand Down
Loading