Attribute-Set Functions
<function>lib.attrset.attrByPath</function> attrByPath :: [String] -> Any -> AttrSet -> Any Return an attribute from within nested attribute sets. attrPath A list of strings representing the path through the nested attribute set set. default Default value if attrPath does not resolve to an existing value. set The nested attributeset to select values from. Extracting a value from a nested attribute set 3 ]]> No value at the path, instead using the default 0 ]]>
<function>lib.attrsets.hasAttrByPath</function> hasAttrByPath :: [String] -> AttrSet -> Bool Determine if an attribute exists within a nested attribute set. attrPath A list of strings representing the path through the nested attribute set set. set The nested attributeset to check. A nested value does exist inside a set true ]]>
<function>lib.attrsets.setAttrByPath</function> setAttrByPath :: [String] -> Any -> AttrSet Create a new attribute set with value set at the nested attribute location specified in attrPath. attrPath A list of strings representing the path through the nested attribute set. value The value to set at the location described by attrPath. Creating a new nested attribute set { a = { b = 3; }; } ]]>
<function>lib.attrsets.getAttrFromPath</function> getAttrFromPath :: [String] -> AttrSet -> Value Like except without a default, and it will throw if the value doesn't exist. attrPath A list of strings representing the path through the nested attribute set set. set The nested attribute set to find the value in. Succesfully getting a value from an attribute set 3 ]]> Throwing after failing to get a value from an attribute set error: cannot find attribute `x.y' ]]>
<function>lib.attrsets.attrVals</function> attrVals :: [String] -> AttrSet -> [Any] Return the specified attributes from a set. All values must exist. nameList The list of attributes to fetch from set. Each attribute name must exist on the attrbitue set. set The set to get attribute values from. Getting several values from an attribute set [ 1 2 3 ] ]]> Getting missing values from an attribute set
<function>lib.attrsets.attrValues</function> attrValues :: AttrSet -> [Any] Get all the attribute values from an attribute set. Provides a backwards-compatible interface of builtins.attrValues for Nix version older than 1.8. attrs The attribute set. [ 1 2 3 ] ]]>
<function>lib.attrsets.catAttrs</function> catAttrs :: String -> [AttrSet] -> [Any] Collect each attribute named `attr' from the list of attribute sets, sets. Sets that don't contain the named attribute are ignored. Provides a backwards-compatible interface of builtins.catAttrs for Nix version older than 1.9. attr Attribute name to select from each attribute set in sets. sets The list of attribute sets to select attr from. Collect an attribute from a list of attribute sets. Attribute sets which don't have the attribute are ignored. [ 1 2 ] ]]>
<function>lib.attrsets.filterAttrs</function> filterAttrs :: (String -> Any -> Bool) -> AttrSet -> AttrSet Filter an attribute set by removing all attributes for which the given predicate return false. pred String -> Any -> Bool Predicate which returns true to include an attribute, or returns false to exclude it. name The attribute's name value The attribute's value Returns true to include the attribute, false to exclude the attribute. set The attribute set to filter Filtering an attributeset { foo = 1; } ]]>
<function>lib.attrsets.filterAttrsRecursive</function> filterAttrsRecursive :: (String -> Any -> Bool) -> AttrSet -> AttrSet Filter an attribute set recursively by removing all attributes for which the given predicate return false. pred String -> Any -> Bool Predicate which returns true to include an attribute, or returns false to exclude it. name The attribute's name value The attribute's value Returns true to include the attribute, false to exclude the attribute. set The attribute set to filter Recursively filtering an attribute set { levelA = { example = "hi"; levelB = { hello = "there"; this-one-is-present = { }; }; }; } ]]>
<function>lib.attrsets.foldAttrs</function> foldAttrs :: (Any -> Any -> Any) -> Any -> [AttrSets] -> Any Apply fold function to values grouped by key. op Any -> Any -> Any Given a value val and a collector col, combine the two. val An attribute's value col The result of previous op calls with other values and nul. nul The null-value, the starting value. list_of_attrs A list of attribute sets to fold together by key. Combining an attribute of lists in to one attribute set { a = [ 2 3 ]; b = [ 7 6 ]; } ]]>
<function>lib.attrsets.collect</function> collect :: (Any -> Bool) -> AttrSet -> [Any] Recursively collect sets that verify a given predicate named pred from the set attrs. The recursion stops when pred returns true. pred Any -> Bool Given an attribute's value, determine if recursion should stop. value The attribute set value. attrs The attribute set to recursively collect. Collecting all lists from an attribute set [["b"] [1]] ]]> Collecting all attribute-sets which contain the <literal>outPath</literal> attribute name. [{ outPath = "a/"; } { outPath = "b/"; }] ]]>
<function>lib.attrsets.nameValuePair</function> nameValuePair :: String -> Any -> AttrSet Utility function that creates a {name, value} pair as expected by builtins.listToAttrs. name The attribute name. value The attribute value. Creating a name value pair { name = "some"; value = 6; } ]]>
<function>lib.attrsets.mapAttrs</function> Apply a function to each element in an attribute set, creating a new attribute set. Provides a backwards-compatible interface of builtins.mapAttrs for Nix version older than 2.1. fn String -> Any -> Any Given an attribute's name and value, return a new value. name The name of the attribute. value The attribute's value. Modifying each value of an attribute set { x = "x-foo"; y = "y-bar"; } ]]>
<function>lib.attrsets.mapAttrs'</function> mapAttrs' :: (String -> Any -> { name = String; value = Any }) -> AttrSet -> AttrSet Like mapAttrs, but allows the name of each attribute to be changed in addition to the value. The applied function should return both the new name and value as a nameValuePair. fn String -> Any -> { name = String; value = Any } Given an attribute's name and value, return a new name value pair. name The name of the attribute. value The attribute's value. set The attribute set to map over. Change the name and value of each attribute of an attribute set { foo_x = "bar-a"; foo_y = "bar-b"; } ]]>
<function>lib.attrsets.mapAttrsToList</function> mapAttrsToList :: (String -> Any -> Any) -> AttrSet -> Any Call fn for each attribute in the given set and return the result in a list. fn String -> Any -> Any Given an attribute's name and value, return a new value. name The name of the attribute. value The attribute's value. set The attribute set to map over. Combine attribute values and names in to a list [ "x=a" "y=b" ] ]]>
<function>lib.attrsets.mapAttrsRecursive</function> mapAttrsRecursive :: ([String] > Any -> Any) -> AttrSet -> AttrSet Like mapAttrs, except that it recursively applies itself to attribute sets. Also, the first argument of the argument function is a list of the names of the containing attributes. f [ String ] -> Any -> Any Given a list of attribute names and value, return a new value. name_path The list of attribute names to this value. For example, the name_path for the example string in the attribute set { foo = { bar = "example"; }; } is [ "foo" "bar" ]. value The attribute's value. set The attribute set to recursively map over. A contrived example of using <function>lib.attrsets.mapAttrsRecursive</function> { n = { a = "n-a-A"; m = { b = "n-m-b-B"; c = "n-m-c-C"; }; }; d = "d-D"; } ]]>
<function>lib.attrsets.mapAttrsRecursiveCond</function> mapAttrsRecursiveCond :: (AttrSet -> Bool) -> ([ String ] -> Any -> Any) -> AttrSet -> AttrSet Like mapAttrsRecursive, but it takes an additional predicate function that tells it whether to recursive into an attribute set. If it returns false, mapAttrsRecursiveCond does not recurse, but does apply the map function. It is returns true, it does recurse, and does not apply the map function. cond (AttrSet -> Bool) Determine if mapAttrsRecursive should recurse deeper in to the attribute set. attributeset An attribute set. f [ String ] -> Any -> Any Given a list of attribute names and value, return a new value. name_path The list of attribute names to this value. For example, the name_path for the example string in the attribute set { foo = { bar = "example"; }; } is [ "foo" "bar" ]. value The attribute's value. set The attribute set to recursively map over. Only convert attribute values to JSON if the containing attribute set is marked for recursion { dorecur = { hello = "\"there\""; recurse = "true"; }; dontrecur = "{\"converted-to\":\"json\"}"; } ]]>
<function>lib.attrsets.genAttrs</function> genAttrs :: [ String ] -> (String -> Any) -> AttrSet Generate an attribute set by mapping a function over a list of attribute names. names Names of values in the resulting attribute set. f String -> Any Takes the name of the attribute and return the attribute's value. name The name of the attribute to generate a value for. Generate an attrset based on names only { foo = "x_foo"; bar = "x_bar"; } ]]>
<function>lib.attrsets.isDerivation</function> isDerivation :: Any -> Bool Check whether the argument is a derivation. Any set with { type = "derivation"; } counts as a derivation. value The value which is possibly a derivation. A package is a derivation {}).ruby => true ]]> Anything else is not a derivation false ]]>
<function>lib.attrsets.toDerivation</function> toDerivation :: Path -> Derivation Converts a store path to a fake derivation. path A store path to convert to a derivation.
<function>lib.attrsets.optionalAttrs</function> optionalAttrs :: Bool -> AttrSet Conditionally return an attribute set or an empty attribute set. cond Condition under which the as attribute set is returned. as The attribute set to return if cond is true. Return the provided attribute set when <varname>cond</varname> is true { my = "set"; } ]]> Return an empty attribute set when <varname>cond</varname> is false { } ]]>
<function>lib.attrsets.zipAttrsWithNames</function> zipAttrsWithNames :: [ String ] -> (String -> [ Any ] -> Any) -> [ AttrSet ] -> AttrSet Merge sets of attributes and use the function f to merge attribute values where the attribute name is in names. names A list of attribute names to zip. f (String -> [ Any ] -> Any Accepts an attribute name, all the values, and returns a combined value. name The name of the attribute each value came from. vs A list of values collected from the list of attribute sets. sets A list of attribute sets to zip together. Summing a list of attribute sets of numbers { a = "a 11"; b = "b 101"; } ]]>
<function>lib.attrsets.zipAttrsWith</function> zipAttrsWith :: (String -> [ Any ] -> Any) -> [ AttrSet ] -> AttrSet Merge sets of attributes and use the function f to merge attribute values. Similar to where all key names are passed for names. f (String -> [ Any ] -> Any Accepts an attribute name, all the values, and returns a combined value. name The name of the attribute each value came from. vs A list of values collected from the list of attribute sets. sets A list of attribute sets to zip together. Summing a list of attribute sets of numbers { a = "a 11"; b = "b 101"; c = "c 1001"; } ]]>
<function>lib.attrsets.zipAttrs</function> zipAttrsWith :: [ AttrSet ] -> AttrSet Merge sets of attributes and combine each attribute value in to a list. Similar to where the merge function returns a list of all values. sets A list of attribute sets to zip together. Combining a list of attribute sets { a = [ 1 10 ]; b = [ 1 100 ]; c = [ 1 1000 ]; } ]]>
<function>lib.attrsets.recursiveUpdateUntil</function> recursiveUpdateUntil :: ( [ String ] -> AttrSet -> AttrSet -> Bool ) -> AttrSet -> AttrSet -> AttrSet Does the same as the update operator // except that attributes are merged until the given predicate is verified. The predicate should accept 3 arguments which are the path to reach the attribute, a part of the first attribute set and a part of the second attribute set. When the predicate is verified, the value of the first attribute set is replaced by the value of the second attribute set. pred [ String ] -> AttrSet -> AttrSet -> Bool path The path to the values in the left and right hand sides. l The left hand side value. r The right hand side value. lhs The left hand attribute set of the merge. rhs The right hand attribute set of the merge. Recursively merging two attribute sets { foo.bar = 1; # 'foo.*' from the second set foo.quz = 2; # bar = 3; # 'bar' from the first set baz = 4; # 'baz' from the second set } ]]>
<function>lib.attrsets.recursiveUpdate</function> recursiveUpdate :: AttrSet -> AttrSet -> AttrSet A recursive variant of the update operator //. The recursion stops when one of the attribute values is not an attribute set, in which case the right hand side value takes precedence over the left hand side value. lhs The left hand attribute set of the merge. rhs The right hand attribute set of the merge. Recursively merging two attribute sets { boot.loader.grub.enable = true; boot.loader.grub.device = ""; } ]]>
<function>lib.attrsets.recurseIntoAttrs</function> recurseIntoAttrs :: AttrSet -> AttrSet Make various Nix tools consider the contents of the resulting attribute set when looking for what to build, find, etc. This function only affects a single attribute set; it does not apply itself recursively for nested attribute sets. attrs An attribute set to scan for derivations. Making Nix look inside an attribute set {} }: { myTools = pkgs.lib.recurseIntoAttrs { inherit (pkgs) hello figlet; }; } ]]>
<function>lib.attrsets.cartesianProductOfSets</function> cartesianProductOfSets :: AttrSet -> [ AttrSet ] Return the cartesian product of attribute set value combinations. set An attribute set with attributes that carry lists of values. Creating the cartesian product of a list of attribute values [ { a = 1; b = 10; } { a = 1; b = 20; } { a = 2; b = 10; } { a = 2; b = 20; } ] ]]>