2020-02-12 00:53:46 +02:00
|
|
|
#include "primops.hh"
|
|
|
|
#include "eval-inline.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "fetchers/fetchers.hh"
|
|
|
|
#include "fetchers/registry.hh"
|
|
|
|
|
2020-03-09 16:28:41 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <iomanip>
|
|
|
|
|
2020-02-12 00:53:46 +02:00
|
|
|
namespace nix {
|
|
|
|
|
2020-03-09 16:28:41 +02:00
|
|
|
void emitTreeAttrs(
|
|
|
|
EvalState & state,
|
|
|
|
const fetchers::Tree & tree,
|
|
|
|
std::shared_ptr<const fetchers::Input> input,
|
|
|
|
Value & v)
|
|
|
|
{
|
|
|
|
state.mkAttrs(v, 8);
|
|
|
|
|
|
|
|
auto storePath = state.store->printStorePath(tree.storePath);
|
|
|
|
|
|
|
|
mkString(*state.allocAttr(v, state.sOutPath), storePath, PathSet({storePath}));
|
|
|
|
|
|
|
|
assert(tree.info.narHash);
|
|
|
|
mkString(*state.allocAttr(v, state.symbols.create("narHash")),
|
|
|
|
tree.info.narHash.to_string(SRI));
|
|
|
|
|
|
|
|
if (input->getRev()) {
|
|
|
|
mkString(*state.allocAttr(v, state.symbols.create("rev")), input->getRev()->gitRev());
|
|
|
|
mkString(*state.allocAttr(v, state.symbols.create("shortRev")), input->getRev()->gitShortRev());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree.info.revCount)
|
|
|
|
mkInt(*state.allocAttr(v, state.symbols.create("revCount")), *tree.info.revCount);
|
|
|
|
|
|
|
|
if (tree.info.lastModified)
|
|
|
|
mkString(*state.allocAttr(v, state.symbols.create("lastModified")),
|
|
|
|
fmt("%s", std::put_time(std::gmtime(&*tree.info.lastModified), "%Y%m%d%H%M%S")));
|
|
|
|
|
|
|
|
v.attrs->sort();
|
|
|
|
}
|
|
|
|
|
2020-02-12 00:53:46 +02:00
|
|
|
static void prim_fetchTree(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
|
|
{
|
2020-03-09 16:28:41 +02:00
|
|
|
settings.requireExperimentalFeature("flakes");
|
2020-02-20 14:36:16 +02:00
|
|
|
|
2020-02-12 00:53:46 +02:00
|
|
|
std::shared_ptr<const fetchers::Input> input;
|
|
|
|
PathSet context;
|
|
|
|
|
|
|
|
state.forceValue(*args[0]);
|
|
|
|
|
|
|
|
if (args[0]->type == tAttrs) {
|
|
|
|
state.forceAttrs(*args[0], pos);
|
|
|
|
|
2020-03-17 21:54:36 +02:00
|
|
|
fetchers::Attrs attrs;
|
2020-02-12 00:53:46 +02:00
|
|
|
|
|
|
|
for (auto & attr : *args[0]->attrs) {
|
|
|
|
state.forceValue(*attr.value);
|
|
|
|
if (attr.value->type == tString)
|
|
|
|
attrs.emplace(attr.name, attr.value->string.s);
|
2020-03-17 22:34:38 +02:00
|
|
|
else if (attr.value->type == tBool)
|
|
|
|
attrs.emplace(attr.name, attr.value->boolean);
|
2020-02-12 00:53:46 +02:00
|
|
|
else
|
2020-03-17 22:34:38 +02:00
|
|
|
throw TypeError("fetchTree argument '%s' is %s while a string or Boolean is expected",
|
2020-03-09 16:28:41 +02:00
|
|
|
attr.name, showType(*attr.value));
|
2020-02-12 00:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!attrs.count("type"))
|
|
|
|
throw Error("attribute 'type' is missing in call to 'fetchTree', at %s", pos);
|
|
|
|
|
|
|
|
input = fetchers::inputFromAttrs(attrs);
|
|
|
|
} else
|
|
|
|
input = fetchers::inputFromURL(state.coerceToString(pos, *args[0], context, false, false));
|
|
|
|
|
|
|
|
if (!evalSettings.pureEval && !input->isDirect())
|
2020-02-20 23:14:44 +02:00
|
|
|
input = lookupInRegistries(state.store, input).first;
|
2020-02-12 00:53:46 +02:00
|
|
|
|
|
|
|
if (evalSettings.pureEval && !input->isImmutable())
|
|
|
|
throw Error("in pure evaluation mode, 'fetchTree' requires an immutable input");
|
|
|
|
|
2020-03-10 20:21:47 +02:00
|
|
|
// FIXME: use fetchOrSubstituteTree
|
2020-02-12 00:53:46 +02:00
|
|
|
auto [tree, input2] = input->fetchTree(state.store);
|
|
|
|
|
|
|
|
if (state.allowedPaths)
|
|
|
|
state.allowedPaths->insert(tree.actualPath);
|
2020-03-09 16:28:41 +02:00
|
|
|
|
|
|
|
emitTreeAttrs(state, tree, input2, v);
|
2020-02-12 00:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static RegisterPrimOp r("fetchTree", 1, prim_fetchTree);
|
|
|
|
|
|
|
|
}
|