query
On this page

hasAttr

builtins.hasAttr

Primop
Docs pulled from | This Revision | 3 days ago


Nix manual

Takes 2 arguments

s, set

hasAttr returns true if set has an attribute named s, and false otherwise. 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
hasAttr :: String -> AttrSet -> Bool

Implementation

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

src/libexpr/primops.cc:3238

static void prim_hasAttr(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    auto attr = state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.hasAttr");
    state.forceAttrs(*args[1], pos, "while evaluating the second argument passed to builtins.hasAttr");
    v.mkBool(args[1]->attrs()->get(state.symbols.create(attr)));
}