query
On this page

dirOf

lib.filesystem.dirOf

Primop
Docs pulled from | This Revision | about 1 hour ago


Nix manual

Takes 1 arguments

s

Return the directory part of the string s, that is, everything before the final slash in the string. This is similar to the GNU dirname command.

Noogle detected

Aliases

Detected Type
dirOf :: String -> String

Implementation

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

src/libexpr/primops.cc:2068

static void prim_dirOf(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    state.forceValue(*args[0], pos);
    if (args[0]->type() == nPath) {
        auto path = args[0]->path();
        v.mkPath(path.path.isRoot() ? path : path.parent(), state.mem);
    } else {
        NixStringContext context;
        auto path = state.coerceToString(
            pos, *args[0], context, "while evaluating the first argument passed to 'builtins.dirOf'", false, false);
        auto pos = path->rfind('/');
        if (pos == path->npos)
            v.mkStringMove("."_sds, context, state.mem);
        else if (pos == 0)
            v.mkStringMove("/"_sds, context, state.mem);
        else
            v.mkString(path->substr(0, pos), context, state.mem);
    }
}