Improve performance of per-system attributes

Using `perSystem` to implement per-system attributes ties all the
per-system attributes together; all the `perSystem` functions must run
to determine output attrs. By generating them separately, the generation
can be done lazily.
This commit is contained in:
Archit Gupta 2024-01-10 01:16:24 -08:00
parent bcaa85757e
commit 82f9fe67c3
8 changed files with 44 additions and 42 deletions

View File

@ -2,7 +2,7 @@
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
# SPDX-License-Identifier: MIT
{ config, lib, flakelight, ... }:
{ config, lib, flakelight, genSystems, ... }:
let
inherit (lib) isFunction mapAttrs mkIf mkMerge mkOption;
inherit (lib.types) lazyAttrsOf nullOr raw;
@ -35,9 +35,8 @@ in
})
(mkIf (config.apps != null) {
perSystem = pkgs: {
apps = mapAttrs (_: mkApp pkgs) (config.apps pkgs);
};
outputs.apps = genSystems (pkgs:
mapAttrs (_: mkApp pkgs) (config.apps pkgs));
})
];
}

View File

@ -2,7 +2,7 @@
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
# SPDX-License-Identifier: MIT
{ config, lib, flakelight, ... }:
{ config, lib, flakelight, genSystems, ... }:
let
inherit (lib) isFunction mapAttrs mkMerge mkOption mkIf;
inherit (lib.types) lazyAttrsOf nullOr;
@ -30,9 +30,8 @@ in
})
(mkIf (config.bundlers != null) {
perSystem = pkgs: {
bundlers = mapAttrs (_: wrapBundler pkgs) (config.bundlers pkgs);
};
outputs.bundlers = genSystems (pkgs:
mapAttrs (_: wrapBundler pkgs) (config.bundlers pkgs));
})
];
}

View File

@ -2,7 +2,7 @@
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
# SPDX-License-Identifier: MIT
{ config, src, lib, flakelight, ... }:
{ config, src, lib, flakelight, genSystems, ... }:
let
inherit (lib) isDerivation isFunction mkOption mkIf mapAttrs;
inherit (lib.types) lazyAttrsOf nullOr raw;
@ -26,7 +26,8 @@ in
default = null;
};
config.perSystem = mkIf (config.checks != null) (pkgs: {
checks = mapAttrs (mkCheck pkgs src) (config.checks pkgs);
});
config.outputs = mkIf (config.checks != null) {
checks = genSystems (pkgs:
mapAttrs (mkCheck pkgs src) (config.checks pkgs));
};
}

View File

@ -26,6 +26,8 @@ let
inherit (config.nixpkgs) config;
overlays = config.withOverlays ++ [ config.packageOverlay ];
});
genSystems = f: genAttrs config.systems (system: f pkgsFor.${system});
in
{
options = {
@ -62,7 +64,7 @@ in
config = {
_module.args = {
inherit (config) inputs outputs;
inherit pkgsFor;
inherit pkgsFor genSystems;
};
outputs = foldAttrs mergeAttrs { } (map

View File

@ -2,7 +2,7 @@
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
# SPDX-License-Identifier: MIT
{ config, lib, flakelight, ... }:
{ config, lib, flakelight, genSystems, ... }:
let
inherit (lib) filterAttrs mapAttrs mkDefault mkIf mkMerge mkOption;
inherit (lib.types) coercedTo functionTo lazyAttrsOf lines listOf nullOr
@ -71,10 +71,9 @@ in
})
(mkIf (config.devShells != { }) {
perSystem = pkgs: {
devShells = filterAttrs (_: supportedSystem pkgs)
(mapAttrs (_: v: pkgs.callPackage v { }) config.devShells);
};
outputs.devShells = genSystems (pkgs:
filterAttrs (_: supportedSystem pkgs)
(mapAttrs (_: v: pkgs.callPackage v { }) config.devShells));
})
];
}

View File

@ -2,7 +2,7 @@
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
# SPDX-License-Identifier: MIT
{ config, src, lib, flakelight, ... }:
{ config, src, lib, flakelight, genSystems, ... }:
let
inherit (lib) mkDefault mkMerge mkOption mkIf mapAttrsToList;
inherit (lib.types) lazyAttrsOf nullOr package str;
@ -22,28 +22,26 @@ in
config = mkMerge [
(mkIf (config.formatter != null) {
perSystem = pkgs: {
formatter = config.formatter pkgs;
};
outputs.formatter = genSystems config.formatter;
})
(mkIf (config.formatters != null) {
perSystem = { pkgs, lib, fd, coreutils, ... }: {
formatter = mkDefault (pkgs.writeShellScriptBin "formatter" ''
PATH=${lib.makeBinPath ((config.devShell.packages or (_: [ ])) pkgs)}
for f in "$@"; do
if [ -d "$f" ]; then
${fd}/bin/fd "$f" -Htf -x "$0" &
else
case "$(${coreutils}/bin/basename "$f")" in
${toString (mapAttrsToList
(n: v: "${n}) ${v} \"$f\" & ;;") (config.formatters pkgs))}
esac
fi
done &>/dev/null
wait
'');
};
outputs.formatter = mkDefault (genSystems
({ pkgs, lib, fd, coreutils, ... }:
pkgs.writeShellScriptBin "formatter" ''
PATH=${lib.makeBinPath (config.devShell.packages or (_: [ ]) pkgs)}
for f in "$@"; do
if [ -d "$f" ]; then
${fd}/bin/fd "$f" -Htf -x "$0" &
else
case "$(${coreutils}/bin/basename "$f")" in
${toString (mapAttrsToList
(n: v: "${n}) ${v} \"$f\" & ;;") (config.formatters pkgs))}
esac
fi
done &>/dev/null
wait
''));
})
(mkIf ((config.formatters != null) || (config.formatter != null)) {

View File

@ -10,6 +10,7 @@
, inputs
, outputs
, pkgsFor
, genSystems
, specialArgs
, modulesPath
, moduleArgs

View File

@ -2,7 +2,7 @@
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
# SPDX-License-Identifier: MIT
{ config, lib, inputs, flakelight, ... }:
{ config, lib, inputs, flakelight, genSystems, ... }:
let
inherit (builtins) parseDrvName;
inherit (lib) filterAttrs mapAttrs mapAttrs' mkIf mkMerge mkOption
@ -56,10 +56,13 @@ in
(config.packageOverlay (final.appendOverlays config.withOverlays) prev)
[ "default" ];
perSystem = pkgs: rec {
packages = filterAttrs (_: supportedSystem pkgs) (genPkgs pkgs);
outputs = rec {
packages = genSystems (pkgs:
filterAttrs (_: supportedSystem pkgs) (genPkgs pkgs));
checks = mapAttrs' (n: nameValuePair ("packages-" + n)) packages;
checks = mapAttrs
(_: mapAttrs' (n: nameValuePair ("packages-" + n)))
packages;
};
})
];