in nix how do I loop over all attributes in the lowest attrset in something like {a = {…
- User
in nix how do I loop over all attributes in the lowest attrset in something like {a = { b =1; c =2; }; d ={ e=2;};} ?
- Assistant
Here's one way to loop over all attributes in the lowest attrset in Nix:
codelet attrs = {a = { b =1; c =2; }; d ={ e=2;};}; in builtins.attrNames (builtins.head (builtins.attrValues attrs)) |> List.map name: attrs.${name}This code first gets the values in the top-level attrset using
builtins.attrValues, then selects the first value (which is the lowest attrset) usingbuiltins.head. It then gets the attribute names within the lowest attrset usingbuiltins.attrNames, and finally maps each name to the corresponding value using${name}syntax. This produces a list of all attributes in the lowest attrset. - User
i want to apply mapAttrs to b,c and e how do I do that
- Assistant
You can apply
builtins.mapAttrsfunction to the nested attrsets within the top-level attrset like this:code