Skip to content

Commit

Permalink
lib/attrsets: add recurseIntoAttrsIf, a generalization of recurseInto…
Browse files Browse the repository at this point in the history
…Attrs and dontRecurseIntoAttrs

Signed-off-by: lucasew <[email protected]>
  • Loading branch information
lucasew committed Dec 22, 2024
1 parent 70f2687 commit 1e5e58a
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions lib/attrsets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let
in

rec {
inherit (builtins) attrNames listToAttrs hasAttr isAttrs getAttr removeAttrs intersectAttrs;
inherit (builtins) attrNames listToAttrs hasAttr isAttrs getAttr removeAttrs intersectAttrs isBool;


/**
Expand Down Expand Up @@ -2035,6 +2035,52 @@ rec {
*/
chooseDevOutputs = builtins.map getDev;

/**
Make various Nix tools consider the contents of the resulting
attribute set when looking for what to build, find, etc if a
condition is true.
This function only affects a single attribute set; it does not
apply itself recursively for nested attribute sets.
# Inputs
`condition`
:A boolean that says whether a tool should consider the attrset
values.
`attrs`
:An attribute set to scan for derivations.
# Type
````
recurseIntoAttrsIf :: Bool -> AttrSet -> AttrSet
```
# Examples
:::{.example}
## `lib.attrsets.recurseIntoAttrsIf` usage example
```nix
{ pkgs ? import <nixpkgs> {} }:
{
myTools = pkgs.lib.recurseIntoAttrsIf true {
inherit (pkgs) hello figlet;
};
}
```
:::
*/
recurseIntoAttrsIf =
condition:
attrs:
assert isBool condition;
attrs // { recurseForDerivations = condition; };


/**
Make various Nix tools consider the contents of the resulting
attribute set when looking for what to build, find, etc.
Expand Down Expand Up @@ -2070,9 +2116,7 @@ rec {
:::
*/
recurseIntoAttrs =
attrs:
attrs // { recurseForDerivations = true; };
recurseIntoAttrs = recurseIntoAttrsIf true;

/**
Undo the effect of recurseIntoAttrs.
Expand All @@ -2090,9 +2134,7 @@ rec {
dontRecurseIntoAttrs :: AttrSet -> AttrSet
```
*/
dontRecurseIntoAttrs =
attrs:
attrs // { recurseForDerivations = false; };
dontRecurseIntoAttrs = recurseIntoAttrsIf false;

/**
`unionOfDisjoint x y` is equal to `x // y // z` where the
Expand Down

0 comments on commit 1e5e58a

Please sign in to comment.