2022-03-21 12:31:01 +02:00
|
|
|
#include "primops.hh"
|
|
|
|
#include "store-api.hh"
|
2022-03-22 22:14:58 +02:00
|
|
|
#include "make-content-addressed.hh"
|
2022-03-22 23:47:33 +02:00
|
|
|
#include "url.hh"
|
2022-03-21 12:31:01 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
static void prim_fetchClosure(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
|
|
{
|
|
|
|
state.forceAttrs(*args[0], pos);
|
|
|
|
|
2022-03-21 15:34:45 +02:00
|
|
|
std::optional<std::string> fromStoreUrl;
|
|
|
|
std::optional<StorePath> fromPath;
|
2022-03-22 22:14:58 +02:00
|
|
|
bool toCA = false;
|
|
|
|
std::optional<StorePath> toPath;
|
2022-03-21 12:31:01 +02:00
|
|
|
|
|
|
|
for (auto & attr : *args[0]->attrs) {
|
2022-03-21 15:34:45 +02:00
|
|
|
if (attr.name == "fromPath") {
|
2022-03-21 12:31:01 +02:00
|
|
|
PathSet context;
|
2022-03-21 15:34:45 +02:00
|
|
|
fromPath = state.coerceToStorePath(*attr.pos, *attr.value, context);
|
2022-03-21 12:31:01 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 22:14:58 +02:00
|
|
|
else if (attr.name == "toPath") {
|
|
|
|
state.forceValue(*attr.value, *attr.pos);
|
|
|
|
toCA = true;
|
|
|
|
if (attr.value->type() != nString || attr.value->string.s != std::string("")) {
|
|
|
|
PathSet context;
|
|
|
|
toPath = state.coerceToStorePath(*attr.pos, *attr.value, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 15:34:45 +02:00
|
|
|
else if (attr.name == "fromStore")
|
|
|
|
fromStoreUrl = state.forceStringNoCtx(*attr.value, *attr.pos);
|
2022-03-21 12:31:01 +02:00
|
|
|
|
|
|
|
else
|
|
|
|
throw Error({
|
|
|
|
.msg = hintfmt("attribute '%s' isn't supported in call to 'fetchClosure'", attr.name),
|
|
|
|
.errPos = pos
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-21 15:34:45 +02:00
|
|
|
if (!fromPath)
|
2022-03-21 12:31:01 +02:00
|
|
|
throw Error({
|
2022-03-21 15:34:45 +02:00
|
|
|
.msg = hintfmt("attribute '%s' is missing in call to 'fetchClosure'", "fromPath"),
|
2022-03-21 12:31:01 +02:00
|
|
|
.errPos = pos
|
|
|
|
});
|
|
|
|
|
2022-03-21 15:34:45 +02:00
|
|
|
if (!fromStoreUrl)
|
2022-03-21 12:31:01 +02:00
|
|
|
throw Error({
|
2022-03-21 15:34:45 +02:00
|
|
|
.msg = hintfmt("attribute '%s' is missing in call to 'fetchClosure'", "fromStore"),
|
2022-03-21 12:31:01 +02:00
|
|
|
.errPos = pos
|
|
|
|
});
|
|
|
|
|
2022-03-22 23:47:33 +02:00
|
|
|
auto parsedURL = parseURL(*fromStoreUrl);
|
|
|
|
|
|
|
|
if (parsedURL.scheme != "http" && parsedURL.scheme != "https")
|
|
|
|
throw Error({
|
|
|
|
.msg = hintfmt("'fetchClosure' only supports http:// and https:// stores"),
|
|
|
|
.errPos = pos
|
|
|
|
});
|
|
|
|
|
|
|
|
auto fromStore = openStore(parsedURL.to_string());
|
2022-03-21 12:31:01 +02:00
|
|
|
|
2022-03-22 22:14:58 +02:00
|
|
|
if (toCA) {
|
2022-03-22 23:01:20 +02:00
|
|
|
if (!toPath || !state.store->isValidPath(*toPath)) {
|
|
|
|
auto remappings = makeContentAddressed(*fromStore, *state.store, { *fromPath });
|
|
|
|
auto i = remappings.find(*fromPath);
|
|
|
|
assert(i != remappings.end());
|
|
|
|
if (toPath && *toPath != i->second)
|
|
|
|
throw Error({
|
|
|
|
.msg = hintfmt("rewriting '%s' to content-addressed form yielded '%s', while '%s' was expected",
|
|
|
|
state.store->printStorePath(*fromPath),
|
|
|
|
state.store->printStorePath(i->second),
|
|
|
|
state.store->printStorePath(*toPath)),
|
|
|
|
.errPos = pos
|
|
|
|
});
|
|
|
|
if (!toPath)
|
|
|
|
throw Error({
|
|
|
|
.msg = hintfmt(
|
|
|
|
"rewriting '%s' to content-addressed form yielded '%s'; "
|
|
|
|
"please set this in the 'toPath' attribute passed to 'fetchClosure'",
|
|
|
|
state.store->printStorePath(*fromPath),
|
|
|
|
state.store->printStorePath(i->second)),
|
|
|
|
.errPos = pos
|
|
|
|
});
|
|
|
|
}
|
2022-03-22 22:14:58 +02:00
|
|
|
} else {
|
|
|
|
copyClosure(*fromStore, *state.store, RealisedPath::Set { *fromPath });
|
|
|
|
toPath = fromPath;
|
|
|
|
}
|
2022-03-21 12:31:01 +02:00
|
|
|
|
2022-03-21 15:00:54 +02:00
|
|
|
/* In pure mode, require a CA path. */
|
|
|
|
if (evalSettings.pureEval) {
|
2022-03-22 22:14:58 +02:00
|
|
|
auto info = state.store->queryPathInfo(*toPath);
|
2022-03-21 15:00:54 +02:00
|
|
|
if (!info->isContentAddressed(*state.store))
|
|
|
|
throw Error({
|
|
|
|
.msg = hintfmt("in pure mode, 'fetchClosure' requires a content-addressed path, which '%s' isn't",
|
2022-03-22 22:14:58 +02:00
|
|
|
state.store->printStorePath(*toPath)),
|
2022-03-21 15:00:54 +02:00
|
|
|
.errPos = pos
|
|
|
|
});
|
|
|
|
}
|
2022-03-21 12:31:01 +02:00
|
|
|
|
2022-03-22 22:14:58 +02:00
|
|
|
auto toPathS = state.store->printStorePath(*toPath);
|
|
|
|
v.mkString(toPathS, {toPathS});
|
2022-03-21 12:31:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static RegisterPrimOp primop_fetchClosure({
|
|
|
|
.name = "__fetchClosure",
|
|
|
|
.args = {"args"},
|
|
|
|
.doc = R"(
|
|
|
|
)",
|
|
|
|
.fun = prim_fetchClosure,
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|