From 3be13404aeeae190ad802f2edf2d139621e9f70d Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar <3998+srid@users.noreply.github.com> Date: Tue, 21 Oct 2025 18:20:29 -0400 Subject: [PATCH 1/3] wip --- flake.nix | 107 +++++++++++++++++++++++++----------------------------- 1 file changed, 49 insertions(+), 58 deletions(-) diff --git a/flake.nix b/flake.nix index c14d903..15c0d68 100644 --- a/flake.nix +++ b/flake.nix @@ -17,67 +17,58 @@ build-systems = let systems = import inputs.systems; in if systems == [ ] then [ system ] else systems; - shouldBuildOn = s: lib.elem s build-systems; - getSystem = cfg: - cfg.pkgs.stdenv.hostPlatform.system; - configForCurrentSystem = cfg: - shouldBuildOn (getSystem cfg); - # Given a flake output key, how to get the buildable derivation for - # any of its attr values? - flakeSchema = { - perSystem = { - # -> [ path ] - lookupFlake = k: flake: - lib.flip builtins.map build-systems (sys: - lib.attrByPath [ k sys ] { } flake - ); - getDrv = { - packages = _: x: [ x ]; - checks = _: x: [ x ]; - devShells = _: x: [ x ]; - apps = _: app: [ app.program ]; - legacyPackages = k: v: - if k == "homeConfigurations" - then - lib.mapAttrsToList (_: cfg: cfg.activationPackage) v - else [ ]; - }; - }; - flake = { - lookupFlake = k: flake: [ (lib.attrByPath [ k ] { } flake) ]; - getDrv = { - nixosConfigurations = _: cfg: - lib.optional (configForCurrentSystem cfg) cfg.config.system.build.toplevel; - darwinConfigurations = _: cfg: - lib.optional (configForCurrentSystem cfg) cfg.config.system.build.toplevel; - }; - }; - }; - paths = - lib.flip lib.mapAttrsToList flakeSchema (_: lvlSchema: - lib.flip lib.mapAttrsToList lvlSchema.getDrv (kind: getDrv: - builtins.concatMap - (attr: lib.mapAttrsToList getDrv attr) - (lvlSchema.lookupFlake kind inputs.flake) - ) - ); + getSystem = cfg: cfg.pkgs.stdenv.hostPlatform.system; nameForStorePath = path: if builtins.typeOf path == "set" - then path.pname or path.name or null - else null; - result = rec { - outPaths = lib.lists.flatten paths; - # Indexed by the path's unique name - # Paths without such a name will be ignored. Hence, you must rely on `out_paths` for comprehensive list of outputs. - byName = lib.foldl' (acc: path: - let name = nameForStorePath path; - in if name == null then acc else acc // { "${name}" = path; } - ) { } outPaths; - }; + then path.pname or path.name or null + else null; + + # Collect paths for a specific system + collectPathsForSystem = sys: + let + # Helper to extract derivations from attrs + getDrvs = kind: attrs: + if kind == "packages" || kind == "checks" || kind == "devShells" then + lib.attrValues attrs + else if kind == "apps" then + map (app: app.program) (lib.attrValues attrs) + else if kind == "legacyPackages" then + lib.optionals (attrs ? homeConfigurations) + (lib.mapAttrsToList (_: cfg: cfg.activationPackage) attrs.homeConfigurations) + else [ ]; + + # Per-system outputs + perSystemPaths = lib.concatMap (kind: + getDrvs kind (lib.attrByPath [ kind sys ] { } inputs.flake) + ) [ "packages" "checks" "devShells" "apps" "legacyPackages" ]; + + # Flake-level outputs (nixosConfigurations, darwinConfigurations) + flakePaths = lib.concatMap (kind: + let + configs = lib.attrByPath [ kind ] { } inputs.flake; + configsForSys = lib.filterAttrs (_: cfg: + getSystem cfg == sys + ) configs; + in + map (cfg: cfg.config.system.build.toplevel) (lib.attrValues configsForSys) + ) [ "nixosConfigurations" "darwinConfigurations" ]; + in + perSystemPaths ++ flakePaths; + + # Build result organized by system + result = builtins.listToAttrs (map (sys: + let + outPaths = lib.sort (a: b: "${a}" < "${b}") (collectPathsForSystem sys); + byName = lib.foldl' (acc: path: + let name = nameForStorePath path; + in if name == null then acc else acc // { "${name}" = path; } + ) { } outPaths; + in + { name = sys; value = { inherit outPaths byName; }; } + ) build-systems); in - rec { - json = pkgs.writeText "devour-output.json" (builtins.toJSON result); - default = pkgs.writeText "devour-output" (lib.strings.concatLines result.outPaths); + { + default = pkgs.writeText "devour-output.json" (builtins.toJSON result); } ); }; From eb480ac8a6de8f2457ac1faf94cf38f08ecafeed Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar <3998+srid@users.noreply.github.com> Date: Tue, 21 Oct 2025 18:22:55 -0400 Subject: [PATCH 2/3] dedup --- flake.nix | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 15c0d68..ab252eb 100644 --- a/flake.nix +++ b/flake.nix @@ -58,7 +58,9 @@ # Build result organized by system result = builtins.listToAttrs (map (sys: let - outPaths = lib.sort (a: b: "${a}" < "${b}") (collectPathsForSystem sys); + allPaths = collectPathsForSystem sys; + uniquePaths = lib.unique allPaths; + outPaths = lib.sort (a: b: "${a}" < "${b}") uniquePaths; byName = lib.foldl' (acc: path: let name = nameForStorePath path; in if name == null then acc else acc // { "${name}" = path; } From 62c2a43a1dfff61a04e609ee412ae426827c5326 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar <3998+srid@users.noreply.github.com> Date: Tue, 21 Oct 2025 18:28:37 -0400 Subject: [PATCH 3/3] test --- test/expected | 5 +---- test/test.sh | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/test/expected b/test/expected index 26baa9c..5df9de5 100644 --- a/test/expected +++ b/test/expected @@ -1,4 +1 @@ -/nix/store/3k5m5hpb07bchbdymlwkn0n06vn09src-foo-0.1.0.0 -/nix/store/y5gw89k3ch5gx9aqxazxqll6g8da1j26-ghc-shell-for-packages-0 -/nix/store/zfh0052wf0ndn0033lg5x7kf4s72kfgw-bar-0.1.0.0 -/nix/store/zfh0052wf0ndn0033lg5x7kf4s72kfgw-bar-0.1.0.0/bin/bar +{"x86_64-linux":{"byName":{"bar":"/nix/store/zfh0052wf0ndn0033lg5x7kf4s72kfgw-bar-0.1.0.0","foo":"/nix/store/3k5m5hpb07bchbdymlwkn0n06vn09src-foo-0.1.0.0","ghc-shell-for-packages-0":"/nix/store/y5gw89k3ch5gx9aqxazxqll6g8da1j26-ghc-shell-for-packages-0"},"outPaths":["/nix/store/3k5m5hpb07bchbdymlwkn0n06vn09src-foo-0.1.0.0","/nix/store/y5gw89k3ch5gx9aqxazxqll6g8da1j26-ghc-shell-for-packages-0","/nix/store/zfh0052wf0ndn0033lg5x7kf4s72kfgw-bar-0.1.0.0","/nix/store/zfh0052wf0ndn0033lg5x7kf4s72kfgw-bar-0.1.0.0/bin/bar"]}} \ No newline at end of file diff --git a/test/test.sh b/test/test.sh index 6af7007..194c004 100755 --- a/test/test.sh +++ b/test/test.sh @@ -3,8 +3,7 @@ set -euxo pipefail cd "$(dirname "$0")" rm -f result -nix build .. --override-input flake github:srid/haskell-multi-nix/c85563721c388629fa9e538a1d97274861bc8321 -L --no-link --print-out-paths | xargs cat | sort | uniq \ - > result +nix build .. --override-input flake github:srid/haskell-multi-nix/c85563721c388629fa9e538a1d97274861bc8321 -L --no-link --print-out-paths | xargs cat > result diff expected result