query
On this page

filterAttrs

lib.attrsets.filterAttrs

Docs pulled from | This Revision | about 2 hours ago


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 true to include the attribute, or false to exclude the attribute.

If possible, decide on name first and on value only 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.)

filterAttrs is occasionally the cause of infinite recursion in configuration systems that allow self-references. To support the widest range of user-provided logic, perform the filterAttrs call 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

Aliases

Implementation

The following is the current implementation of this function.

filterAttrs = pred: set: removeAttrs set (filter (name: !pred name set.${name}) (attrNames set));