Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/flakes: move eachSystem from flake-utils #171235

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}