From 78b315871a1845faf1f6059e5daf5fa31dddf14f Mon Sep 17 00:00:00 2001 From: ilkecan Date: Wed, 7 Sep 2022 00:52:51 +0000 Subject: [PATCH 1/2] each{,Default}System: accept binary functions closes: #78 --- README.md | 10 ++++++++++ lib.nix | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 84c4bdc..2cc94ef 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,16 @@ eachSystem allSystems (system: { hello = 42; }) } ``` +If you pass a function rather than an attribute set after the `system` argument, +it will receive as its argument the final attribute set. For example: + +```nix +eachSystem [ system.x86_64-linux ] (system: self: { a = 2; b = self.a + 3; }) +``` + +`self` is not that useful in this case, as it can be replaced with the `rec` +keyword but there can be situations where it is preferred. + ### `eachDefaultSystem :: ( -> attrs)` `eachSystem` pre-populated with `defaultSystems`. diff --git a/lib.nix b/lib.nix index c007888..8e57e19 100644 --- a/lib.nix +++ b/lib.nix @@ -33,7 +33,13 @@ let # Merge together the outputs for all systems. op = attrs: system: let - ret = f system; + ret = + let + retOrFunc = f system; + in + if builtins.isFunction retOrFunc + then retOrFunc ret + else retOrFunc; op = attrs: key: attrs // { ${key} = (attrs.${key} or { }) From cdb61a09f720d0ed586200fea8e49bf5b4304770 Mon Sep 17 00:00:00 2001 From: ilkecan Date: Wed, 7 Sep 2022 00:56:06 +0000 Subject: [PATCH 2/2] each{,Default}SystemMap: accept binary functions related: #78 --- lib.nix | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib.nix b/lib.nix index 8e57e19..94e0a45 100644 --- a/lib.nix +++ b/lib.nix @@ -33,13 +33,7 @@ let # Merge together the outputs for all systems. op = attrs: system: let - ret = - let - retOrFunc = f system; - in - if builtins.isFunction retOrFunc - then retOrFunc ret - else retOrFunc; + ret = maybeFix (f system); op = attrs: key: attrs // { ${key} = (attrs.${key} or { }) @@ -56,7 +50,8 @@ let eachDefaultSystemMap = eachSystemMap defaultSystems; # Builds a map from =value to . = value. - eachSystemMap = systems: f: builtins.listToAttrs (builtins.map (system: { name = system; value = f system; }) systems); + eachSystemMap = systems: f: + builtins.listToAttrs (builtins.map (system: { name = system; value = maybeFix (f system); }) systems); # Nix flakes insists on having a flat attribute set of derivations in # various places like the `packages` and `checks` attributes. @@ -207,5 +202,11 @@ let system ; }; + + maybeFix = arg: + let + fix = f: let x = f x; in x; + in + if builtins.isFunction arg then fix arg else arg; in lib