Apply some cleanup

This commit is contained in:
Archit Gupta 2023-04-15 18:37:48 -07:00
parent b9ee864785
commit 53843042f2

View File

@ -6,7 +6,7 @@ localInputs:
let let
inherit (builtins) intersectAttrs isPath readDir; inherit (builtins) intersectAttrs isPath readDir;
inherit (localInputs.nixpkgs.lib) attrNames attrVals callPackageWith inherit (localInputs.nixpkgs.lib) attrNames attrVals callPackageWith
composeManyExtensions filter filterAttrs foldAttrs foldl functionArgs composeManyExtensions concat filter filterAttrs foldAttrs foldl functionArgs
genAttrs hasSuffix isFunction isList isString listToAttrs mapAttrs genAttrs hasSuffix isFunction isList isString listToAttrs mapAttrs
mapAttrsToList mapAttrs' mergeAttrs nameValuePair optional optionalAttrs mapAttrsToList mapAttrs' mergeAttrs nameValuePair optional optionalAttrs
optionalString parseDrvName pathExists pipe recursiveUpdate removePrefix optionalString parseDrvName pathExists pipe recursiveUpdate removePrefix
@ -14,8 +14,9 @@ let
exports = { exports = {
inherit mkFlake systems importDir autoImport autoImportAttrs defaultPkgName inherit mkFlake systems importDir autoImport autoImportAttrs defaultPkgName
supportedSystem mergeModules moduleAttrs rootAttrs ensureFn fnConcat supportedSystem mergeModules moduleAttrs rootAttrs ensureFn callFn
fnUpdate callFn filterArgs callPkg callPkgs tryImport; filterArgs callPkg callPkgs tryImport mkApp mkCheck liftFn2 fnConcat
fnMergeAttrs;
}; };
builtinModule = { src, inputs, root }: { builtinModule = { src, inputs, root }: {
@ -108,24 +109,27 @@ let
ensureFn = v: if isFunction v then v else _: v; ensureFn = v: if isFunction v then v else _: v;
fnConcat = f1: f2: args: (f1 args) ++ (f2 args); liftFn2 = fn: a: b: args: fn (a args) (b args);
fnUpdate = f1: f2: args: (f1 args) // (f2 args);
mergeModules = m1: m2: { fnConcat = liftFn2 concat;
inputs = m1.inputs // m2.inputs; fnMergeAttrs = liftFn2 mergeAttrs;
withOverlays = m1.withOverlays ++ m2.withOverlays;
packages = m1.packages // m2.packages; mergeOverlayAttrs = a: b: zipAttrsWith (_: composeManyExtensions) [ a b ];
devTools = fnConcat m1.devTools m2.devTools;
devShells = fnUpdate m1.devShells m2.devShells; mergeModules = a: b: mapAttrs (n: v: v a.${n} b.${n}) {
env = fnUpdate m1.env m2.env; inputs = mergeAttrs;
overlays = zipAttrsWith (_: composeManyExtensions) withOverlays = concat;
[ m1.overlays m2.overlays ]; packages = mergeAttrs;
apps = fnUpdate m1.apps m2.apps; devTools = fnConcat;
checks = fnUpdate m1.checks m2.checks; devShells = fnMergeAttrs;
nixosModules = m1.nixosModules // m2.nixosModules; env = fnMergeAttrs;
nixosConfigurations = m1.nixosConfigurations // m2.nixosConfigurations; overlays = mergeOverlayAttrs;
templates = m1.templates // m2.templates; apps = fnMergeAttrs;
formatters = fnUpdate m1.formatters m2.formatters; checks = fnMergeAttrs;
nixosModules = mergeAttrs;
nixosConfigurations = mergeAttrs;
templates = mergeAttrs;
formatters = fnMergeAttrs;
}; };
callFn = args: f: callFn = args: f:
@ -150,6 +154,26 @@ let
defaultPkgName = root: pkg: root.name or pkg.pname or (parseDrvName pkg).name; defaultPkgName = root: pkg: root.name or pkg.pname or (parseDrvName pkg).name;
recUpdateSets = foldl (acc: x: recursiveUpdate acc ((ensureFn x) acc)) { };
isApp = x: (x ? type) && (x.type == "app") && (x ? program);
mkApp = pkgs: app:
let
app' = callFn pkgs app;
in
if isApp app' then app'
else { type = "app"; program = "${app'}"; };
mkCheck = pkgs: src: name: cmd:
if pkgs.lib.isDerivation cmd then cmd else
pkgs.runCommand "check-${name}" { } ''
cp --no-preserve=mode -r ${src} src
cd src
${cmd}
touch $out
'';
supportedSystem = { lib, stdenv, ... }: pkg: supportedSystem = { lib, stdenv, ... }: pkg:
if pkg ? meta.platforms if pkg ? meta.platforms
then lib.meta.availableOn stdenv.hostPlatform pkg then lib.meta.availableOn stdenv.hostPlatform pkg
@ -169,11 +193,6 @@ let
mkFlake = src: root: mkFlake = src: root:
let let
modules = root.modules or (pipe (removeAttrs root'.inputs [ "self" ]) [
(filterAttrs (_: v: v ? flakeliteModule))
(mapAttrsToList (_: v: v.flakeliteModule))
]);
nonSysArgs = exports // { nonSysArgs = exports // {
args = nonSysArgs; args = nonSysArgs;
flakelite = exports; flakelite = exports;
@ -213,7 +232,7 @@ let
default = module'.package; default = module'.package;
}; };
devTools = filterArgs module'.devTools; devTools = filterArgs module'.devTools;
devShells = fnUpdate (filterArgs module'.devShells) devShells = fnMergeAttrs (filterArgs module'.devShells)
(_: optionalAttrs (module' ? devShell) { (_: optionalAttrs (module' ? devShell) {
default = module'.devShell; default = module'.devShell;
}); });
@ -222,7 +241,7 @@ let
// optionalAttrs (module' ? overlay) { // optionalAttrs (module' ? overlay) {
default = module'.overlay; default = module'.overlay;
}; };
apps = fnUpdate (filterArgs module'.apps) apps = fnMergeAttrs (filterArgs module'.apps)
(_: optionalAttrs (module' ? app) { (_: optionalAttrs (module' ? app) {
default = module'.app; default = module'.app;
}); });
@ -245,6 +264,11 @@ let
fullRoot = (autoImportAttrs nixDir rootAttrs) // root; fullRoot = (autoImportAttrs nixDir rootAttrs) // root;
in in
normalizeModule fullRoot // { normalizeModule fullRoot // {
modules = fullRoot.modules or
(pipe (removeAttrs root'.inputs [ "self" ]) [
(filterAttrs (_: v: v ? flakeliteModule))
(mapAttrsToList (_: v: v.flakeliteModule))
]);
systems = applyNonSysArgs (fullRoot.systems or systems.linuxDefault); systems = applyNonSysArgs (fullRoot.systems or systems.linuxDefault);
perSystem = filterArgs (fullRoot.perSystem or { }); perSystem = filterArgs (fullRoot.perSystem or { });
outputs = applyNonSysArgs (fullRoot.outputs or { }); outputs = applyNonSysArgs (fullRoot.outputs or { });
@ -254,7 +278,7 @@ let
merged = foldl mergeModules moduleAttrDefaults merged = foldl mergeModules moduleAttrDefaults
((map (m: normalizeModule (applyNonSysArgs m)) ((map (m: normalizeModule (applyNonSysArgs m))
([ builtinModule ] ++ modules)) ++ [ root' ]); ([ builtinModule ] ++ root'.modules)) ++ [ root' ]);
pkgsFor = system: import merged.inputs.nixpkgs { pkgsFor = system: import merged.inputs.nixpkgs {
inherit system; inherit system;
@ -267,34 +291,12 @@ let
(system: nameValuePair system (pkgsFor system)) (system: nameValuePair system (pkgsFor system))
root'.systems); root'.systems);
mkCheck = pkgs: name: cmd:
if pkgs.lib.isDerivation cmd then cmd else
pkgs.runCommand "check-${name}" { } ''
cp --no-preserve=mode -r ${src} src
cd src
${cmd}
touch $out
'';
isApp = x: (x ? type) && (x.type == "app") && (x ? program);
mkApp = pkgs: app:
let
app' = callFn pkgs app;
in
if isApp app' then app'
else { type = "app"; program = "${app'}"; };
eachSystem = fn: foldAttrs mergeAttrs { } (map eachSystem = fn: foldAttrs mergeAttrs { } (map
(system: mapAttrs (system: mapAttrs
(_: v: { ${system} = v; }) (_: v: { ${system} = v; })
(fn systemPkgs.${system})) (fn systemPkgs.${system}))
root'.systems); root'.systems);
recUpdateSets = foldl
(acc: new: recursiveUpdate acc ((ensureFn new) acc))
{ };
replaceDefault = set: replaceDefault = set:
if set ? default if set ? default
then (removeAttrs set [ "default" ]) // then (removeAttrs set [ "default" ]) //
@ -312,8 +314,7 @@ let
checks = mapAttrs' (n: nameValuePair ("packages-" + n)) packages; checks = mapAttrs' (n: nameValuePair ("packages-" + n)) packages;
}))) })))
(prev: optionalAttrs (merged.overlays != { }) ({ (prev: optionalAttrs (merged.overlays != { }) ({
overlays = zipAttrsWith (_: composeManyExtensions) overlays = mergeOverlayAttrs (prev.overlays or { }) merged.overlays;
[ (prev.overlays or { }) merged.overlays ];
})) }))
(eachSystem ({ pkgs, lib, ... }: (eachSystem ({ pkgs, lib, ... }:
optionalAttrs (merged.formatters pkgs != { }) rec { optionalAttrs (merged.formatters pkgs != { }) rec {
@ -330,7 +331,7 @@ let
fi fi
done &>/dev/null done &>/dev/null
''; '';
checks.formatting = mkCheck pkgs "formatting" '' checks.formatting = mkCheck pkgs src "formatting" ''
${lib.getExe formatter} . ${lib.getExe formatter} .
${pkgs.diffutils}/bin/diff -qr ${src} . |\ ${pkgs.diffutils}/bin/diff -qr ${src} . |\
sed 's/Files .* and \(.*\) differ/File \1 not formatted/g' sed 's/Files .* and \(.*\) differ/File \1 not formatted/g'
@ -338,7 +339,7 @@ let
})) }))
(eachSystem (pkgs: (eachSystem (pkgs:
let let
checks = mapAttrs (mkCheck pkgs) (merged.checks pkgs); checks = mapAttrs (mkCheck pkgs src) (merged.checks pkgs);
in in
optionalAttrs (checks != { }) { inherit checks; })) optionalAttrs (checks != { }) { inherit checks; }))
(eachSystem (pkgs: (eachSystem (pkgs: