2019-01-13 17:43:46 +00:00
|
|
|
#include "primops.hh"
|
|
|
|
#include "eval-inline.hh"
|
2020-03-21 15:07:16 +00:00
|
|
|
#include "store-api.hh"
|
2019-01-13 17:43:46 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
static void prim_unsafeDiscardStringContext(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
|
|
{
|
|
|
|
PathSet context;
|
2022-01-04 17:24:42 +00:00
|
|
|
v.mkString(state.coerceToString(pos, *args[0], context));
|
2019-01-13 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static RegisterPrimOp primop_unsafeDiscardStringContext("__unsafeDiscardStringContext", 1, prim_unsafeDiscardStringContext);
|
2019-01-13 17:43:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void prim_hasContext(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
|
|
{
|
|
|
|
PathSet context;
|
|
|
|
state.forceString(*args[0], context, pos);
|
2022-01-04 17:40:39 +00:00
|
|
|
v.mkBool(!context.empty());
|
2019-01-13 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static RegisterPrimOp primop_hasContext("__hasContext", 1, prim_hasContext);
|
2019-01-13 17:43:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Sometimes we want to pass a derivation path (i.e. pkg.drvPath) to a
|
|
|
|
builder without causing the derivation to be built (for instance,
|
|
|
|
in the derivation that builds NARs in nix-push, when doing
|
|
|
|
source-only deployment). This primop marks the string context so
|
|
|
|
that builtins.derivation adds the path to drv.inputSrcs rather than
|
|
|
|
drv.inputDrvs. */
|
|
|
|
static void prim_unsafeDiscardOutputDependency(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
|
|
{
|
|
|
|
PathSet context;
|
|
|
|
string s = state.coerceToString(pos, *args[0], context);
|
|
|
|
|
|
|
|
PathSet context2;
|
|
|
|
for (auto & p : context)
|
|
|
|
context2.insert(p.at(0) == '=' ? string(p, 1) : p);
|
|
|
|
|
2022-01-04 17:24:42 +00:00
|
|
|
v.mkString(s, context2);
|
2019-01-13 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static RegisterPrimOp primop_unsafeDiscardOutputDependency("__unsafeDiscardOutputDependency", 1, prim_unsafeDiscardOutputDependency);
|
2019-01-13 17:43:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Extract the context of a string as a structured Nix value.
|
|
|
|
|
|
|
|
The context is represented as an attribute set whose keys are the
|
|
|
|
paths in the context set and whose values are attribute sets with
|
|
|
|
the following keys:
|
|
|
|
path: True if the relevant path is in the context as a plain store
|
|
|
|
path (i.e. the kind of context you get when interpolating
|
|
|
|
a Nix path (e.g. ./.) into a string). False if missing.
|
|
|
|
allOutputs: True if the relevant path is a derivation and it is
|
|
|
|
in the context as a drv file with all of its outputs
|
|
|
|
(i.e. the kind of context you get when referencing
|
|
|
|
.drvPath of some derivation). False if missing.
|
|
|
|
outputs: If a non-empty list, the relevant path is a derivation
|
|
|
|
and the provided outputs are referenced in the context
|
|
|
|
(i.e. the kind of context you get when referencing
|
|
|
|
.outPath of some derivation). Empty list if missing.
|
|
|
|
Note that for a given path any combination of the above attributes
|
2019-01-14 16:34:54 +00:00
|
|
|
may be present.
|
2019-01-13 17:43:46 +00:00
|
|
|
*/
|
|
|
|
static void prim_getContext(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
|
|
{
|
|
|
|
struct ContextInfo {
|
|
|
|
bool path = false;
|
|
|
|
bool allOutputs = false;
|
|
|
|
Strings outputs;
|
|
|
|
};
|
|
|
|
PathSet context;
|
|
|
|
state.forceString(*args[0], context, pos);
|
|
|
|
auto contextInfos = std::map<Path, ContextInfo>();
|
|
|
|
for (const auto & p : context) {
|
|
|
|
Path drv;
|
|
|
|
string output;
|
|
|
|
const Path * path = &p;
|
|
|
|
if (p.at(0) == '=') {
|
|
|
|
drv = string(p, 1);
|
|
|
|
path = &drv;
|
|
|
|
} else if (p.at(0) == '!') {
|
|
|
|
std::pair<string, string> ctx = decodeContext(p);
|
|
|
|
drv = ctx.first;
|
|
|
|
output = ctx.second;
|
|
|
|
path = &drv;
|
|
|
|
}
|
|
|
|
auto isPath = drv.empty();
|
|
|
|
auto isAllOutputs = (!drv.empty()) && output.empty();
|
|
|
|
|
|
|
|
auto iter = contextInfos.find(*path);
|
|
|
|
if (iter == contextInfos.end()) {
|
|
|
|
contextInfos.emplace(*path, ContextInfo{isPath, isAllOutputs, output.empty() ? Strings{} : Strings{std::move(output)}});
|
|
|
|
} else {
|
|
|
|
if (isPath)
|
|
|
|
iter->second.path = true;
|
|
|
|
else if (isAllOutputs)
|
|
|
|
iter->second.allOutputs = true;
|
|
|
|
else
|
|
|
|
iter->second.outputs.emplace_back(std::move(output));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 16:39:16 +00:00
|
|
|
auto attrs = state.buildBindings(contextInfos.size());
|
2019-01-13 17:43:46 +00:00
|
|
|
|
|
|
|
auto sPath = state.symbols.create("path");
|
|
|
|
auto sAllOutputs = state.symbols.create("allOutputs");
|
|
|
|
for (const auto & info : contextInfos) {
|
2022-01-04 16:39:16 +00:00
|
|
|
auto infoAttrs = state.buildBindings(3);
|
2019-01-13 17:43:46 +00:00
|
|
|
if (info.second.path)
|
2022-01-04 17:40:39 +00:00
|
|
|
infoAttrs.alloc(sPath).mkBool(true);
|
2019-01-13 17:43:46 +00:00
|
|
|
if (info.second.allOutputs)
|
2022-01-04 17:40:39 +00:00
|
|
|
infoAttrs.alloc(sAllOutputs).mkBool(true);
|
2019-01-13 17:43:46 +00:00
|
|
|
if (!info.second.outputs.empty()) {
|
2022-01-04 16:39:16 +00:00
|
|
|
auto & outputsVal = infoAttrs.alloc(state.sOutputs);
|
2019-01-13 17:43:46 +00:00
|
|
|
state.mkList(outputsVal, info.second.outputs.size());
|
2022-01-04 16:39:16 +00:00
|
|
|
for (const auto & [i, output] : enumerate(info.second.outputs))
|
|
|
|
(outputsVal.listElems()[i] = state.allocValue())->mkString(output);
|
2019-01-13 17:43:46 +00:00
|
|
|
}
|
2022-01-04 16:39:16 +00:00
|
|
|
attrs.alloc(info.first).mkAttrs(infoAttrs);
|
2019-01-13 17:43:46 +00:00
|
|
|
}
|
2022-01-04 16:39:16 +00:00
|
|
|
|
|
|
|
v.mkAttrs(attrs);
|
2019-01-13 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static RegisterPrimOp primop_getContext("__getContext", 1, prim_getContext);
|
2019-01-13 17:43:46 +00:00
|
|
|
|
2019-01-14 16:34:54 +00:00
|
|
|
|
|
|
|
/* Append the given context to a given string.
|
|
|
|
|
|
|
|
See the commentary above unsafeGetContext for details of the
|
|
|
|
context representation.
|
|
|
|
*/
|
|
|
|
static void prim_appendContext(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
|
|
{
|
|
|
|
PathSet context;
|
|
|
|
auto orig = state.forceString(*args[0], context, pos);
|
|
|
|
|
|
|
|
state.forceAttrs(*args[1], pos);
|
|
|
|
|
|
|
|
auto sPath = state.symbols.create("path");
|
|
|
|
auto sAllOutputs = state.symbols.create("allOutputs");
|
|
|
|
for (auto & i : *args[1]->attrs) {
|
|
|
|
if (!state.store->isStorePath(i.name))
|
2020-06-15 12:06:58 +00:00
|
|
|
throw EvalError({
|
2021-01-20 23:27:36 +00:00
|
|
|
.msg = hintfmt("Context key '%s' is not a store path", i.name),
|
2020-06-23 21:30:13 +00:00
|
|
|
.errPos = *i.pos
|
2020-06-15 12:06:58 +00:00
|
|
|
});
|
2019-01-14 16:34:54 +00:00
|
|
|
if (!settings.readOnlyMode)
|
2019-12-05 18:11:09 +00:00
|
|
|
state.store->ensurePath(state.store->parseStorePath(i.name));
|
2019-01-14 16:34:54 +00:00
|
|
|
state.forceAttrs(*i.value, *i.pos);
|
|
|
|
auto iter = i.value->attrs->find(sPath);
|
|
|
|
if (iter != i.value->attrs->end()) {
|
|
|
|
if (state.forceBool(*iter->value, *iter->pos))
|
|
|
|
context.insert(i.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
iter = i.value->attrs->find(sAllOutputs);
|
|
|
|
if (iter != i.value->attrs->end()) {
|
|
|
|
if (state.forceBool(*iter->value, *iter->pos)) {
|
|
|
|
if (!isDerivation(i.name)) {
|
2020-06-15 12:06:58 +00:00
|
|
|
throw EvalError({
|
2021-01-20 23:27:36 +00:00
|
|
|
.msg = hintfmt("Tried to add all-outputs context of %s, which is not a derivation, to a string", i.name),
|
2020-06-23 21:30:13 +00:00
|
|
|
.errPos = *i.pos
|
2020-06-15 12:06:58 +00:00
|
|
|
});
|
2019-01-14 16:34:54 +00:00
|
|
|
}
|
|
|
|
context.insert("=" + string(i.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iter = i.value->attrs->find(state.sOutputs);
|
|
|
|
if (iter != i.value->attrs->end()) {
|
|
|
|
state.forceList(*iter->value, *iter->pos);
|
|
|
|
if (iter->value->listSize() && !isDerivation(i.name)) {
|
2020-06-15 12:06:58 +00:00
|
|
|
throw EvalError({
|
2021-01-20 23:27:36 +00:00
|
|
|
.msg = hintfmt("Tried to add derivation output context of %s, which is not a derivation, to a string", i.name),
|
2020-06-23 21:30:13 +00:00
|
|
|
.errPos = *i.pos
|
2020-06-15 12:06:58 +00:00
|
|
|
});
|
2019-01-14 16:34:54 +00:00
|
|
|
}
|
2021-11-24 19:21:34 +00:00
|
|
|
for (auto elem : iter->value->listItems()) {
|
|
|
|
auto name = state.forceStringNoCtx(*elem, *iter->pos);
|
2019-01-14 16:34:54 +00:00
|
|
|
context.insert("!" + name + "!" + string(i.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 17:24:42 +00:00
|
|
|
v.mkString(orig, context);
|
2019-01-14 16:34:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static RegisterPrimOp primop_appendContext("__appendContext", 2, prim_appendContext);
|
2019-01-14 16:34:54 +00:00
|
|
|
|
2019-01-13 17:43:46 +00:00
|
|
|
}
|