Skip to content

Commit

Permalink
lib/flakes: move eachSystem from flake-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Artturin committed May 2, 2022
1 parent a5357d0 commit a7d5878
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/flakes.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,57 @@ rec {
in
self;


/* Builds a map from <attr>=value to <attr>.<system>=value for each system,
except for the `hydraJobs` attribute, where it maps the inner attributes,
from hydraJobs.<attr>=value to hydraJobs.<attr>.<system>=value.
Example:
eachSystem [ "x86_64-linux" "aarch64-linux" ] (system: { hello = 42; })
=> { hello = { aarch64-linux = 42; x86_64-linux = 42; }; }
*/
eachSystem = systems: f:
let
# Used to match Hydra's convention of how to define jobs. Basically transforms
#
# hydraJobs = {
# hello = <derivation>;
# haskellPackages.aeson = <derivation>;
# }
#
# to
#
# hydraJobs = {
# hello.x86_64-linux = <derivation>;
# haskellPackages.aeson.x86_64-linux = <derivation>;
# }
#
# if the given flake does `eachSystem [ "x86_64-linux" ] { ... }`.
pushDownSystem = system: merged:
builtins.mapAttrs
(name: value:
if ! (builtins.isAttrs value) then value
else if lib.isDerivation value then (merged.${name} or {}) // { ${system} = value; }
else pushDownSystem system (merged.${name} or {}) value);

# Merge together the outputs for all systems.
op = attrs: system:
let
ret = f system;
op = attrs: key:
let
appendSystem = key: system: ret:
if key == "hydraJobs"
then (pushDownSystem system (attrs.hydraJobs or {}) ret.hydraJobs)
else { ${system} = ret.${key}; };
in attrs //
{
${key} = (attrs.${key} or { })
// (appendSystem key system ret);
}
;
in
builtins.foldl' op attrs (builtins.attrNames ret);
in
builtins.foldl' op { } systems;
}

0 comments on commit a7d5878

Please sign in to comment.