query
On this page

attrNames

builtins.attrNames

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


Nix manual

Takes 1 arguments

set

Return the names of the attributes in the set set in an alphabetically sorted list. For instance, builtins.attrNames { y = 1; x = "foo"; } evaluates to [ "x" "y" ].

Time Complexity

  • O(n log n), where:

n = number of attributes in the set

Noogle detected

Aliases

Detected Type
attrNames :: AttrSet -> [String]

Implementation

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

src/libexpr/primops.cc:3070

static void prim_attrNames(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    state.forceAttrs(*args[0], pos, "while evaluating the argument passed to builtins.attrNames");

    auto list = state.buildList(args[0]->attrs()->size());

    for (const auto & [n, i] : enumerate(*args[0]->attrs()))
        list[n] = Value::toPtr(state.symbols[i.name]);

    std::sort(list.begin(), list.end(), [](Value * v1, Value * v2) { return v1->string_view() < v2->string_view(); });

    v.mkList(list);
}