1
0
Fork 0
mirror of https://github.com/numtide/flake-utils.git synced 2024-12-14 11:47:31 +00:00

Merge pull request #97 from input-output-hk/jbgi/revert-hydraJobs

Revert special handling of hydraJobs
This commit is contained in:
Jonas Chevalier 2023-08-23 16:11:51 +02:00 committed by GitHub
commit f9e7cf8183
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 41 deletions

View file

@ -0,0 +1,56 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 0,
"narHash": "sha256-omjHh3LT883xERMxVEXH/oeAFI2pAAy30mhZb0eN5G4=",
"path": "../..",
"type": "path"
},
"original": {
"path": "../..",
"type": "path"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1685498995,
"narHash": "sha256-rdyjnkq87tJp+T2Bm1OD/9NXKSsh/vLlPeqCc/mm7qs=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9cfaa8a1a00830d17487cb60a19bb86f96f09b27",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View file

@ -1,19 +1,28 @@
{
description = "Flake utils demo";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.flake-utils.url = "path:../..";
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
inherit (flake-utils.lib.check-utils system) isEqual hasKey;
testDataset = { key1 = "value1"; key2 = 123; key3 = "some>value with^invalid&characters"; };
mkHydraJobs = system: {
toplevel = derivation { name = "toplevel"; builder = "mybuilder"; inherit system; };
nested = {
attribute = derivation { name = "nested-attribute"; builder = "mybuilder"; inherit system; };
};
};
in
rec {
hydraJobs = mkHydraJobs system;
checks = {
# Successful cases
success_isEqual = isEqual testDataset.key1 "value1";
success_hasKey = hasKey testDataset "key2";
# ensure no special handling of hydraJobs
success_hydraJobs = isEqual self.hydraJobs (flake-utils.lib.eachDefaultSystemMap mkHydraJobs);
# Failing cases
failure_isEqual = isEqual testDataset.key1 "failing-data";

46
lib.nix
View file

@ -26,53 +26,19 @@ let
# eachSystem using defaultSystems
eachDefaultSystem = eachSystem defaultSystems;
# 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.
# Builds a map from <attr>=value to <attr>.<system>=value for each system
#
eachSystem = systems: f:
let
# Taken from <nixpkgs/lib/attrsets.nix>
isDerivation = x: builtins.isAttrs x && x ? type && x.type == "derivation";
# 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 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);
}
op = attrs: key: attrs //
{
${key} = (attrs.${key} or { })
// { ${system} = ret.${key}; };
}
;
in
builtins.foldl' op attrs (builtins.attrNames ret);