filterAttrs
lib.attrsets.filterAttrs
Filter an attribute set by removing all attributes for which the given predicate return false.
Inputs
pred-
Predicate taking an attribute name and an attribute value, which returns
trueto include the attribute, orfalseto exclude the attribute.If possible, decide on
namefirst and onvalueonly if necessary. This avoids evaluating the value if the name is already enough, making it possible, potentially, to have the argument reference the return value. (Depending on context, that could still be considered a self reference by users; a common pattern in Nix.)filterAttrsis occasionally the cause of infinite recursion in configuration systems that allow self-references. To support the widest range of user-provided logic, perform thefilterAttrscall as late as possible. Typically that's right before using it in a derivation, as opposed to an implicit conversion whose result is accessible to the user's expressions. set-
The attribute set to filter
Type
filterAttrs :: (String -> Any -> Bool) -> AttrSet -> AttrSet
Examples
lib.attrsets.filterAttrs usage example
filterAttrs (n: v: n == "foo") { foo = 1; bar = 2; }
=> { foo = 1; }
Noogle detected
Implementation
The following is the current implementation of this function.
filterAttrs = pred: set: removeAttrs set (filter (name: !pred name set.${name}) (attrNames set));