query
On this page

path

builtins.path

Primop
Docs pulled from | This Revision | about 4 hours ago


Nix manual

Takes 1 arguments

args

An enrichment of the built-in path type, based on the attributes present in args. All are optional except path:

  • path
    The underlying path.

  • name
    The name of the path when added to the store. This can used to reference paths that have nix-illegal characters in their names, like @.

  • filter
    A function of the type expected by builtins.filterSource, with the same semantics.

  • recursive
    When false, when path is added to the store it is with a flat hash, rather than a hash of the NAR serialization of the file. Thus, path must refer to a regular file, not a directory. This allows similar behavior to fetchurl. Defaults to true.

  • sha256
    When provided, this is the expected content hash of the path. Evaluation fails if the hash is incorrect, and providing a hash allows builtins.path to be used even when the pure-eval nix config option is on.

Implementation

This function is implemented in c++ and is part of the native nix runtime.

src/libexpr/primops.cc:2979

static void prim_path(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    std::optional<SourcePath> path;
    std::string_view name;
    Value * filterFun = nullptr;
    auto method = ContentAddressMethod::Raw::NixArchive;
    std::optional<Hash> expectedHash;
    NixStringContext context;

    state.forceAttrs(*args[0], pos, "while evaluating the argument passed to 'builtins.path'");

    for (auto & attr : *args[0]->attrs()) {
        auto n = state.symbols[attr.name];
        if (n == "path")
            path.emplace(state.coerceToPath(
                attr.pos, *attr.value, context, "while evaluating the 'path' attribute passed to 'builtins.path'"));
        else if (attr.name == state.s.name)
            name = state.forceStringNoCtx(
                *attr.value, attr.pos, "while evaluating the `name` attribute passed to builtins.path");
        else if (n == "filter")
            state.forceFunction(
                *(filterFun = attr.value), attr.pos, "while evaluating the `filter` parameter passed to builtins.path");
        else if (n == "recursive")
            method = state.forceBool(
                         *attr.value, attr.pos, "while evaluating the `recursive` attribute passed to builtins.path")
                         ? ContentAddressMethod::Raw::NixArchive
                         : ContentAddressMethod::Raw::Flat;
        else if (n == "sha256")
            expectedHash = newHashAllowEmpty(
                state.forceStringNoCtx(
                    *attr.value, attr.pos, "while evaluating the `sha256` attribute passed to builtins.path"),
                HashAlgorithm::SHA256);
        else
            state.error<EvalError>("unsupported argument '%1%' to 'builtins.path'", state.symbols[attr.name])
                .atPos(attr.pos)
                .debugThrow();
    }
    if (!path)
        state.error<EvalError>("missing required 'path' attribute in the first argument to 'builtins.path'")
            .atPos(pos)
            .debugThrow();
    if (name.empty())
        name = path->baseName();

    addPath(state, pos, name, *path, filterFun, method, expectedHash, v, context);
}