Skip to content
Merged
Show file tree
Hide file tree
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
109 changes: 51 additions & 58 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,60 @@
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
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; }
) { } 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);
}
);
};
Expand Down
5 changes: 1 addition & 4 deletions test/expected
Original file line number Diff line number Diff line change
@@ -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"]}}
3 changes: 1 addition & 2 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down