query
On this page

getAttr

builtins.getAttr

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


Nix manual

Takes 2 arguments

s, set

getAttr returns the attribute named s from set. Evaluation aborts if the attribute doesn’t exist. This is a dynamic version of the . operator, since s is an expression rather than an identifier.

Time Complexity

O(log n) where n = number of attributes in the set

Noogle detected

Aliases

Detected Type
getAttr :: String -> AttrSet -> a

Implementation

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

src/libexpr/primops.cc:3140

void prim_getAttr(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    auto attr = state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.getAttr");
    state.forceAttrs(*args[1], pos, "while evaluating the second argument passed to builtins.getAttr");
    auto i = state.getAttr(state.symbols.create(attr), args[1]->attrs(), "in the attribute set under consideration");
    // !!! add to stack trace?
    if (state.countCalls && i->pos)
        state.attrSelects[i->pos]++;
    state.forceValue(*i->value, pos);
    v = *i->value;
}