query
On this page

pathExists

builtins.pathExists

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


Nix manual

Takes 1 arguments

path

Return true if the path path exists at evaluation time, and false otherwise.

Noogle detected

Aliases

Detected Type
pathExists :: Path -> Bool

Implementation

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

src/libexpr/primops.cc:1984

static void prim_pathExists(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    try {
        auto & arg = *args[0];

        /* SourcePath doesn't know about trailing slash. */
        state.forceValue(arg, pos);
        auto mustBeDir =
            arg.type() == nString && (arg.string_view().ends_with("/") || arg.string_view().ends_with("/."));

        auto symlinkResolution = mustBeDir ? SymlinkResolution::Full : SymlinkResolution::Ancestors;
        auto path = state.realisePath(pos, arg, symlinkResolution);

        auto st = path.maybeLstat();
        auto exists = st && (!mustBeDir || st->type == SourceAccessor::tDirectory);
        v.mkBool(exists);
    } catch (RestrictedPathError & e) {
        v.mkBool(false);
    }
}