1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "lix/libexpr/primops.hh"
#include "lix/libexpr/extra-primops.hh"
#include "lix/libstore/derivations.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libutil/types.hh"
#include "lix/libexpr/value.hh"

namespace nix {

Value prim_unsafeDiscardStringContext(EvalState & state, Value ** args)
{
    NixStringContext context;
    auto s = state.coerceToString(noPos, *args[0], context, "while evaluating the argument passed to builtins.unsafeDiscardStringContext");
    return {NewValueAs::string, *s};
}

Value prim_hasContext(EvalState & state, Value ** args)
{
    NixStringContext context;
    state.forceString(*args[0], context, noPos, "while evaluating the argument passed to builtins.hasContext");
    return {NewValueAs::boolean, !context.empty()};
}

Value prim_unsafeDiscardOutputDependency(EvalState & state, Value ** args)
{
    NixStringContext context;
    auto s = state.coerceToString(noPos, *args[0], context, "while evaluating the argument passed to builtins.unsafeDiscardOutputDependency");

    NixStringContext context2;
    for (auto && c : context) {
        if (auto * ptr = std::get_if<NixStringContextElem::DrvDeep>(&c.raw)) {
            context2.emplace(NixStringContextElem::Opaque {
                .path = ptr->drvPath
            });
        } else {
            /* Can reuse original item */
            context2.emplace(std::move(c).raw);
        }
    }

    return {NewValueAs::string, *s, context2};
}

Value prim_addDrvOutputDependencies(EvalState & state, Value ** args)
{
    NixStringContext context;
    auto s = state.coerceToString(noPos, *args[0], context, "while evaluating the argument passed to builtins.addDrvOutputDependencies");

	auto contextSize = context.size();
    if (contextSize != 1) {
        state.ctx.errors.make<EvalError>(
            "context of string '%s' must have exactly one element, but has %d",
            *s,
            contextSize
        ).debugThrow();
    }
    NixStringContext context2 {
        (NixStringContextElem { std::visit(overloaded {
            [&](const NixStringContextElem::Opaque & c) -> NixStringContextElem::DrvDeep {
                if (!c.path.isDerivation()) {
                    state.ctx.errors.make<EvalError>(
                        "path '%s' is not a derivation",
                        state.ctx.store->printStorePath(c.path)
                    ).debugThrow(always_progresses);
                }
                return NixStringContextElem::DrvDeep {
                    .drvPath = c.path,
                };
            },
            [&](const NixStringContextElem::Built & c) -> NixStringContextElem::DrvDeep {
                state.ctx.errors.make<EvalError>(
                    "`addDrvOutputDependencies` can only act on derivations, not on a derivation output such as '%1%'",
                    c.output
                ).debugThrow(always_progresses);
            },
            [&](const NixStringContextElem::DrvDeep & c) -> NixStringContextElem::DrvDeep {
                /* Reuse original item because we want this to be idempotent. */
                return std::move(c);
            },
        }, context.begin()->raw) }),
    };

    return {NewValueAs::string, *s, context2};
}


/* 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
   may be present.
*/
Value prim_getContext(EvalState & state, Value ** args)
{
    struct ContextInfo {
        bool path = false;
        bool allOutputs = false;
        Strings outputs;
    };
    NixStringContext context;
    state.forceString(*args[0], context, noPos, "while evaluating the argument passed to builtins.getContext");
    auto contextInfos = std::map<StorePath, ContextInfo>();
    for (auto && i : context) {
        std::visit(overloaded {
            [&](NixStringContextElem::DrvDeep && d) {
                contextInfos[std::move(d.drvPath)].allOutputs = true;
            },
            [&](NixStringContextElem::Built && b) {
                auto drvPath = b.drvPath.path;
                contextInfos[std::move(drvPath)].outputs.emplace_back(std::move(b.output));
            },
            [&](NixStringContextElem::Opaque && o) {
                contextInfos[std::move(o.path)].path = true;
            },
        }, ((NixStringContextElem &&) i).raw);
    }

    auto attrs = state.ctx.buildBindings(contextInfos.size());

    auto sAllOutputs = state.ctx.symbols.create("allOutputs");
    for (const auto & info : contextInfos) {
        auto infoAttrs = state.ctx.buildBindings(3);
        if (info.second.path)
            infoAttrs.insert(state.ctx.symbols.sym_path, {NewValueAs::boolean, true});
        if (info.second.allOutputs)
            infoAttrs.insert(sAllOutputs, {NewValueAs::boolean, true});
        if (!info.second.outputs.empty()) {
            auto content = state.ctx.mem.newList(info.second.outputs.size());
            infoAttrs.insert(state.ctx.symbols.sym_outputs, {NewValueAs::list, content});
            for (const auto & [i, output] : enumerate(info.second.outputs))
                content->elems[i] = {NewValueAs::string, output};
        }
        attrs.insert(state.ctx.store->printStorePath(info.first), {NewValueAs::attrs, infoAttrs});
    }

    return {NewValueAs::attrs, attrs};
}


/* Append the given context to a given string.

   See the commentary above unsafeGetContext for details of the
   context representation.
*/
Value prim_appendContext(EvalState & state, Value ** args)
{
    NixStringContext context;
    auto orig = state.forceString(*args[0], context, noPos, "while evaluating the first argument passed to builtins.appendContext");

    state.forceAttrs(*args[1], noPos, "while evaluating the second argument passed to builtins.appendContext");

    auto sAllOutputs = state.ctx.symbols.create("allOutputs");
    for (auto & i : *args[1]->attrs()) {
        const auto & name = state.ctx.symbols[i.name];
        if (!state.ctx.store->isStorePath(name))
            state.ctx.errors.make<EvalError>(
                "context key '%s' is not a store path",
                name
            ).atPos(i.pos).debugThrow();
        auto namePath = state.ctx.store->parseStorePath(name);
        if (!settings.readOnlyMode)
            state.aio.blockOn(state.ctx.store->ensurePath(namePath));
        state.forceAttrs(i.value, i.pos, "while evaluating the value of a string context");
        auto a = i.value.attrs()->get(state.ctx.symbols.sym_path);
        if (a) {
            if (state.forceBool(
                    a->value, a->pos, "while evaluating the `path` attribute of a string context"
                ))
            {
                context.emplace(NixStringContextElem::Opaque{
                    .path = namePath,
                });
            }
        }

        a = i.value.attrs()->get(sAllOutputs);
        if (a) {
            if (state.forceBool(
                    a->value,
                    a->pos,
                    "while evaluating the `allOutputs` attribute of a string context"
                ))
            {
                if (!isDerivation(name)) {
                    state.ctx.errors.make<EvalError>(
                        "tried to add all-outputs context of %s, which is not a derivation, to a string",
                        name
                    ).atPos(i.pos).debugThrow();
                }
                context.emplace(NixStringContextElem::DrvDeep {
                    .drvPath = namePath,
                });
            }
        }

        a = i.value.attrs()->get(state.ctx.symbols.sym_outputs);
        if (a) {
            state.forceList(
                a->value, a->pos, "while evaluating the `outputs` attribute of a string context"
            );
            if (a->value.listSize() && !isDerivation(name)) {
                state.ctx.errors.make<EvalError>(
                    "tried to add derivation output context of %s, which is not a derivation, to a string",
                    name
                ).atPos(i.pos).debugThrow();
            }
            for (auto & elem : a->value.listItems()) {
                auto outputName = state.forceStringNoCtx(
                    elem, a->pos, "while evaluating an output name within a string context"
                );
                context.emplace(NixStringContextElem::Built {
                    .drvPath = makeConstantStorePath(namePath),
                    .output = std::string { outputName },
                });
            }
        }
    }

    return {NewValueAs::string, orig, context};
}
}