2009-02-09 17:51:03 +01:00
|
|
|
# Operations on attribute sets.
|
|
|
|
|
2009-03-07 00:21:14 +01:00
|
|
|
with {
|
|
|
|
inherit (builtins) head tail;
|
|
|
|
inherit (import ./default.nix) fold;
|
|
|
|
};
|
2009-02-09 17:51:03 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2009-03-07 00:21:14 +01:00
|
|
|
# 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;
|
|
|
|
|
2009-02-09 17:51:03 +01:00
|
|
|
|
|
|
|
}
|