summary refs log tree commit diff
path: root/pkgs/lib/attrsets.nix
blob: a394ff5bd1823fcddd4eca84367a6f8c8dfd3ea9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Operations on attribute sets.

with {
  inherit (builtins) head tail;
  inherit (import ./default.nix) fold;
};

rec {
  inherit (builtins) attrNames listToAttrs hasAttr isAttrs;


  # Return an attribute from nested attribute sets.  For instance ["x"
  # "y"] applied to some set e returns e.x.y, if it exists.  The
  # default value is returned otherwise.  !!! there is also
  # builtins.getAttr (is there a better name for this function?)
  getAttr = attrPath: default: e:
    let attr = head attrPath;
    in
      if attrPath == [] then e
      else if builtins ? hasAttr && hasAttr attr e
      then getAttr (tail attrPath) default (builtins.getAttr attr e)
      else default;

  # ordered by name
  attrValues = attrs: attrVals (__attrNames attrs) attrs;

  attrVals = nameList : attrSet :
    map (x: builtins.getAttr x attrSet) nameList;

  # iterates over a list of attributes collecting the attribute attr if it exists
  catAttrs = attr : l : fold ( s : l : if (hasAttr attr s) then [(builtins.getAttr attr s)] ++ l else l) [] l;


}