1
1
forked from extern/flakelight

Apply optional parameters to non-per-system attrs

This allows attrs that previously did not take args to receive args
including the flakelite lib attrs and module parameters.

`withOverlay` and `withPackage` are excluded as it would be ambiguous
since those are already functions. They are also short forms; if args
are needed, the regular plural attr can be used.

These args are also added to the flakelite pkgs attr for per-system
attrs to take advantage of.
This commit is contained in:
Archit Gupta 2023-04-14 20:16:50 -07:00
parent cd655770d6
commit b1fe8e8e17

View File

@ -11,26 +11,29 @@ let
optionalAttrs optionalString parseDrvName pathExists pipe recursiveUpdate optionalAttrs optionalString parseDrvName pathExists pipe recursiveUpdate
removeSuffix zipAttrsWith; removeSuffix zipAttrsWith;
exports = { inherit mkFlake loadNixDir systems; };
baseModule = src: inputs: root: { baseModule = src: inputs: root: {
withOverlay = final: prev: { withOverlays = params: [
flakelite = exports // { (final: prev: {
inherit inputs; flakelite = params // {
inputs' = mapAttrs inputs' = mapAttrs
(_: mapAttrs (_: mapAttrs
(_: v: v.${prev.system} or { })) (_: v: v.${prev.system} or { }))
inputs; inputs;
meta = { meta = {
platforms = root.systems; platforms = root.systems;
} // optionalAttrs (root ? description) { } // optionalAttrs (root ? description) {
inherit (root) description; inherit (root) description;
} // optionalAttrs (root ? license) { } // optionalAttrs (root ? license) {
license = license =
if isList root.license if isList root.license
then attrVals root.license final.lib.licenses then attrVals root.license final.lib.licenses
else final.lib.licenses.${root.license}; else final.lib.licenses.${root.license};
};
}; };
}; })
}; ];
checks = { pkgs, lib, ... }: checks = { pkgs, lib, ... }:
(optionalAttrs (pathExists (src + /.editorconfig)) { (optionalAttrs (pathExists (src + /.editorconfig)) {
editorconfig = "${lib.getExe pkgs.editorconfig-checker}" editorconfig = "${lib.getExe pkgs.editorconfig-checker}"
@ -51,6 +54,11 @@ let
(mapAttrsToList (_: v: v.flakeliteModule)) (mapAttrsToList (_: v: v.flakeliteModule))
]; ];
mkFunc = v: if isFunction v then v else _: v;
params = exports // { inherit src inputs root'; };
applyParams = v: mkFunc v params;
defaultModule = { defaultModule = {
withOverlays = [ ]; withOverlays = [ ];
packages = { }; packages = { };
@ -65,31 +73,40 @@ let
formatters = _: { }; formatters = _: { };
}; };
mergeListFns = f1: f2: args: (f1 args) ++ (f2 args);
mergeAttrFns = f1: f2: args: (f1 args) // (f2 args);
mkFunc = v: if isFunction v then v else _: v;
normalizeModule = module: normalizeModule = module:
let let
module' = defaultModule // module; module' = defaultModule // module;
in in
module' // { module' // {
withOverlays = module'.withOverlays withOverlays = (applyParams module'.withOverlays)
++ optional (module' ? withOverlay) module'.withOverlay; ++ optional (module' ? withOverlay) module'.withOverlay;
packages = module'.packages // optionalAttrs (module' ? package) { packages = (applyParams module'.packages)
// optionalAttrs (module' ? package) {
default = module'.package; default = module'.package;
}; };
devTools = mkFunc module'.devTools; devTools = mkFunc module'.devTools;
env = mkFunc module'.env; env = mkFunc module'.env;
overlays = module'.overlays // optionalAttrs (module' ? overlay) { overlays = (applyParams module'.overlays)
// optionalAttrs (module' ? overlay) {
default = module'.overlay; default = module'.overlay;
}; };
apps = mkFunc module'.apps; apps = mkFunc module'.apps;
checks = mkFunc module'.checks; checks = mkFunc module'.checks;
nixosModules = applyParams module'.nixosModules;
nixosConfigurations = applyParams module'.nixosConfigurations;
templates = applyParams module'.templates;
formatters = mkFunc module'.formatters; formatters = mkFunc module'.formatters;
}; };
root' = normalizeModule root // {
systems = applyParams root.systems or systems.linuxDefault;
perSystem = mkFunc root.perSystem or (_: { });
outputs = applyParams root.outputs or { };
};
mergeListFns = f1: f2: args: (f1 args) ++ (f2 args);
mergeAttrFns = f1: f2: args: (f1 args) // (f2 args);
mergeModules = m1: m2: { mergeModules = m1: m2: {
withOverlays = m1.withOverlays ++ m2.withOverlays; withOverlays = m1.withOverlays ++ m2.withOverlays;
packages = m1.packages // m2.packages; packages = m1.packages // m2.packages;
@ -105,12 +122,6 @@ let
formatters = mergeAttrFns m1.formatters m2.formatters; formatters = mergeAttrFns m1.formatters m2.formatters;
}; };
root' = normalizeModule root // {
systems = root.systems or systems.linuxDefault;
perSystem = mkFunc root.perSystem or (_: { });
outputs = root.outputs or { };
};
merged = foldl mergeModules defaultModule merged = foldl mergeModules defaultModule
((map (m: normalizeModule (m src inputs root')) ((map (m: normalizeModule (m src inputs root'))
([ baseModule ] ++ modules)) ++ [ root' ]); ([ baseModule ] ++ modules)) ++ [ root' ]);
@ -276,7 +287,5 @@ let
"i686-linux" "i686-linux"
]; ];
}; };
exports = { inherit mkFlake loadNixDir systems; };
in in
exports exports