diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 3001e4ec93b56..e77ee6914a664 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -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; /** @@ -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 {} }: + { + 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. @@ -2070,9 +2116,7 @@ rec { ::: */ - recurseIntoAttrs = - attrs: - attrs // { recurseForDerivations = true; }; + recurseIntoAttrs = recurseIntoAttrsIf true; /** Undo the effect of recurseIntoAttrs. @@ -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 diff --git a/maintainers/scripts/rebuild-amount.sh b/maintainers/scripts/rebuild-amount.sh index 0058e8c04ba03..2606affa40a42 100755 --- a/maintainers/scripts/rebuild-amount.sh +++ b/maintainers/scripts/rebuild-amount.sh @@ -48,7 +48,7 @@ nixexpr() { { supportedSystems = cfg.systems or [ "x86_64-linux" "x86_64-darwin" ]; }; cfg = (import $1 {}).config.rebuild-amount or {}; - recurseIntoAttrs = attrs: attrs // { recurseForDerivations = true; }; + inherit (lib) recurseIntoAttrs; # hydraJobs leaves recurseForDerivations as empty attrmaps; # that would break nix-env and we also need to recurse everywhere. diff --git a/pkgs/top-level/release-outpaths.nix b/pkgs/top-level/release-outpaths.nix index 0b70ea631244f..82e00e46ff5f5 100644 --- a/pkgs/top-level/release-outpaths.nix +++ b/pkgs/top-level/release-outpaths.nix @@ -59,7 +59,7 @@ let }; }; }; - recurseIntoAttrs = attrs: attrs // { recurseForDerivations = true; }; + inherit (lib) recurseIntoAttrs; # hydraJobs leaves recurseForDerivations as empty attrmaps; # that would break nix-env and we also need to recurse everywhere.