diff --git a/README.md b/README.md index 5c40629..65b4f94 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ system = { ``` It's mainly useful to detect typos and auto-complete if you use [rnix-lsp](https://github.com/nix-community/rnix-lsp). - + Eg: instead of typing `"x86_64-linux"`, use `system.x86_64-linux`. @@ -83,6 +83,11 @@ eachSystem allSystems (system: { hello = 42; }) } ``` +### `eachSystemPassThrough :: [] -> ( -> attrs)` + +Unlike `eachSystem`, this function does not inject the `${system}` key by merely +providing the system argument to the function. + ### `eachDefaultSystem :: ( -> attrs)` `eachSystem` pre-populated with `defaultSystems`. @@ -113,6 +118,24 @@ eachSystem allSystems (system: { hello = 42; }) } ``` +### `eachDefaultSystemPassThrough :: ( -> attrs)` + +`eachSystemPassThrough` pre-populated with `defaultSystems`. + +#### Example + +```nix +inputs.flake-utils.lib.eachDefaultSystem (system: { + checks./*.*/"" = /* ... */; + devShells./*.*/"" = /* ... */; + packages./*.*/"" = /* ... */; +}) +// inputs.flake-utils.lib.eachDefaultSystemPassThrough (system: { + homeConfigurations."" = /* ... */; + nixosConfigurations."" = /* ... */; +}) +``` + ### `meld :: attrs -> [ path ] -> attrs` Meld merges subflakes using common inputs. Useful when you want to diff --git a/lib.nix b/lib.nix index b8ff993..2c2e009 100644 --- a/lib.nix +++ b/lib.nix @@ -26,33 +26,47 @@ let # eachSystem using defaultSystems eachDefaultSystem = eachSystem defaultSystems; - # Builds a map from =value to .=value for each system - # - eachSystem = systems: f: + # eachSystemPassThrough using defaultSystems + eachDefaultSystemPassThrough = eachSystemPassThrough defaultSystems; + + # Builds a map from =value to .=value for each system. + eachSystem = eachSystemOp ( + # Merge outputs for each system. + f: attrs: system: let - # Merge together the outputs for all systems. - op = attrs: system: - let - ret = f system; - op = attrs: key: attrs // - { - ${key} = (attrs.${key} or { }) - // { ${system} = ret.${key}; }; - } - ; - in - builtins.foldl' op attrs (builtins.attrNames ret); + ret = f system; in - builtins.foldl' op { } - (systems - ++ # add the current system if --impure is used - (if builtins?currentSystem then - if builtins.elem builtins.currentSystem systems - then [] - else [ builtins.currentSystem ] - else - [])) - ; + builtins.foldl' ( + attrs: key: + attrs + // { + ${key} = (attrs.${key} or { }) // { + ${system} = ret.${key}; + }; + } + ) attrs (builtins.attrNames ret) + ); + + # Applies a merge operation accross systems. + eachSystemOp = + op: systems: f: + builtins.foldl' (op f) { } ( + if + !builtins ? currentSystem || builtins.elem builtins.currentSystem systems + then + systems + else + # Add the current system if the --impure flag is used. + systems ++ [ builtins.currentSystem ] + ); + + # Merely provides the system argument to the function. + # + # Unlike eachSystem, this function does not inject the `${system}` key. + eachSystemPassThrough = eachSystemOp ( + f: attrs: system: + attrs // (f system) + ); # eachSystemMap using defaultSystems eachDefaultSystemMap = eachSystemMap defaultSystems; @@ -198,9 +212,11 @@ let check-utils defaultSystems eachDefaultSystem - eachSystem eachDefaultSystemMap + eachDefaultSystemPassThrough + eachSystem eachSystemMap + eachSystemPassThrough filterPackages flattenTree meld