mirror of
https://github.com/nix-community/flakelight.git
synced 2025-02-13 07:59:24 +01:00
Refactoring and naming cleanup
This commit is contained in:
parent
bafb3c33ec
commit
55493becb8
189
default.nix
189
default.nix
@ -5,18 +5,19 @@
|
|||||||
nixpkgs:
|
nixpkgs:
|
||||||
let
|
let
|
||||||
inherit (builtins) functionArgs isFunction isList isPath isString readDir;
|
inherit (builtins) functionArgs isFunction isList isPath isString readDir;
|
||||||
inherit (nixpkgs.lib) attrNames attrVals composeManyExtensions filter
|
inherit (nixpkgs.lib) attrNames attrVals callPackageWith composeManyExtensions
|
||||||
filterAttrs foldAttrs foldl genAttrs hasSuffix listToAttrs mapAttrs
|
filter filterAttrs foldAttrs foldl genAttrs hasSuffix 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
|
||||||
removeSuffix zipAttrsWith;
|
removeSuffix zipAttrsWith;
|
||||||
|
|
||||||
exports = {
|
exports = {
|
||||||
inherit mkFlake loadNixDir systems autoloadAttr ensureFn mergeListFns
|
inherit mkFlake systems importDir autoImport autoImportAttrs defaultPkgName
|
||||||
mergeAttrFns mergeModules callWith genPackages supportedSystem;
|
supportedSystem mergeModules moduleAttrs rootAttrs ensureFn fnConcat
|
||||||
|
fnUpdate callFn callAuto callAttrsAuto;
|
||||||
};
|
};
|
||||||
|
|
||||||
baseModule = src: inputs: root: {
|
builtinModule = src: inputs: root: {
|
||||||
withOverlays = params: [
|
withOverlays = params: [
|
||||||
(final: prev: {
|
(final: prev: {
|
||||||
flakelite = params // {
|
flakelite = params // {
|
||||||
@ -50,7 +51,7 @@ let
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
loadNixDir = path: genAttrs
|
importDir = path: genAttrs
|
||||||
(pipe (readDir path) [
|
(pipe (readDir path) [
|
||||||
attrNames
|
attrNames
|
||||||
(filter (s: s != "default.nix"))
|
(filter (s: s != "default.nix"))
|
||||||
@ -61,16 +62,16 @@ let
|
|||||||
(p: import (path + (if pathExists
|
(p: import (path + (if pathExists
|
||||||
(path + "/_${p}.nix") then "/_${p}.nix" else "/${p}.nix")));
|
(path + "/_${p}.nix") then "/_${p}.nix" else "/${p}.nix")));
|
||||||
|
|
||||||
autoloadAttr = nixDir: attr:
|
autoImport = dir: attr:
|
||||||
if pathExists (nixDir + "/${attr}.nix")
|
if pathExists (dir + "/${attr}.nix")
|
||||||
then import (nixDir + "/${attr}.nix")
|
then import (dir + "/${attr}.nix")
|
||||||
else if pathExists (nixDir + "/${attr}/default.nix")
|
else if pathExists (dir + "/${attr}/default.nix")
|
||||||
then import (nixDir + "/${attr}")
|
then import (dir + "/${attr}")
|
||||||
else if pathExists (nixDir + "/${attr}")
|
else if pathExists (dir + "/${attr}")
|
||||||
then loadNixDir (nixDir + "/${attr}")
|
then importDir (dir + "/${attr}")
|
||||||
else null;
|
else null;
|
||||||
|
|
||||||
autoAttrs = [
|
moduleAttrs = [
|
||||||
"withOverlay"
|
"withOverlay"
|
||||||
"withOverlays"
|
"withOverlays"
|
||||||
"package"
|
"package"
|
||||||
@ -90,55 +91,74 @@ let
|
|||||||
"template"
|
"template"
|
||||||
"templates"
|
"templates"
|
||||||
"formatters"
|
"formatters"
|
||||||
|
];
|
||||||
|
|
||||||
|
rootAttrs = moduleAttrs ++ [
|
||||||
"systems"
|
"systems"
|
||||||
"perSystem"
|
"perSystem"
|
||||||
"outputs"
|
"outputs"
|
||||||
|
"nixDir"
|
||||||
];
|
];
|
||||||
|
|
||||||
genAutoAttrs = nixDir:
|
autoImportAttrs = dir: attrs:
|
||||||
filterAttrs (_: v: v != null) (genAttrs autoAttrs (autoloadAttr nixDir));
|
filterAttrs (_: v: v != null) (genAttrs attrs (autoImport dir));
|
||||||
|
|
||||||
ensureFn = v: if isFunction v then v else _: v;
|
ensureFn = v: if isFunction v then v else _: v;
|
||||||
|
|
||||||
mergeListFns = f1: f2: args: (f1 args) ++ (f2 args);
|
fnConcat = f1: f2: args: (f1 args) ++ (f2 args);
|
||||||
mergeAttrFns = f1: f2: args: (f1 args) // (f2 args);
|
fnUpdate = 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;
|
||||||
devTools = mergeListFns m1.devTools m2.devTools;
|
devTools = fnConcat m1.devTools m2.devTools;
|
||||||
devShells = mergeAttrFns m1.devShells m2.devShells;
|
devShells = fnUpdate m1.devShells m2.devShells;
|
||||||
env = mergeAttrFns m1.env m2.env;
|
env = fnUpdate m1.env m2.env;
|
||||||
overlays = zipAttrsWith (_: composeManyExtensions)
|
overlays = zipAttrsWith (_: composeManyExtensions)
|
||||||
[ m1.overlays m2.overlays ];
|
[ m1.overlays m2.overlays ];
|
||||||
apps = mergeAttrFns m1.apps m2.apps;
|
apps = fnUpdate m1.apps m2.apps;
|
||||||
checks = mergeAttrFns m1.checks m2.checks;
|
checks = fnUpdate m1.checks m2.checks;
|
||||||
nixosModules = m1.nixosModules // m2.nixosModules;
|
nixosModules = m1.nixosModules // m2.nixosModules;
|
||||||
nixosConfigurations = m1.nixosConfigurations // m2.nixosConfigurations;
|
nixosConfigurations = m1.nixosConfigurations // m2.nixosConfigurations;
|
||||||
templates = m1.templates // m2.templates;
|
templates = m1.templates // m2.templates;
|
||||||
formatters = mergeAttrFns m1.formatters m2.formatters;
|
formatters = fnUpdate m1.formatters m2.formatters;
|
||||||
};
|
};
|
||||||
|
|
||||||
callWith = pkgs: x:
|
callFn = args: f:
|
||||||
|
if functionArgs f == { }
|
||||||
|
then f args
|
||||||
|
else
|
||||||
|
if args ? callPackage
|
||||||
|
then args.callPackage f { }
|
||||||
|
else callPackageWith args f { };
|
||||||
|
|
||||||
|
callAuto = args: x:
|
||||||
let
|
let
|
||||||
x' = ensureFn (if (isPath x) || (isString x) then import x else x);
|
x' = ensureFn (if (isPath x) || (isString x) then import x else x);
|
||||||
in
|
in
|
||||||
callFn pkgs x';
|
callFn args x';
|
||||||
|
|
||||||
callFn = pkgs: f:
|
callAttrsAuto = args: mapAttrs (_: callAuto args);
|
||||||
if functionArgs f == { }
|
|
||||||
then f pkgs
|
|
||||||
else pkgs.callPackage f { };
|
|
||||||
|
|
||||||
genPackages = pkgs: mapAttrs (_: callWith pkgs);
|
defaultPkgName = root: pkg: root.name or pkg.pname or (parseDrvName pkg).name;
|
||||||
|
|
||||||
getName = root: pkg: root.name or pkg.pname or (parseDrvName pkg).name;
|
|
||||||
|
|
||||||
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
|
||||||
else true;
|
else true;
|
||||||
|
|
||||||
|
systems = rec {
|
||||||
|
linuxDefault = [
|
||||||
|
"x86_64-linux"
|
||||||
|
"aarch64-linux"
|
||||||
|
];
|
||||||
|
linuxAll = linuxDefault ++ [
|
||||||
|
"armv6l-linux"
|
||||||
|
"armv7l-linux"
|
||||||
|
"i686-linux"
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
mkFlake = src: inputs: root:
|
mkFlake = src: inputs: root:
|
||||||
let
|
let
|
||||||
modules = root.modules or pipe (inputs // { self = { }; }) [
|
modules = root.modules or pipe (inputs // { self = { }; }) [
|
||||||
@ -146,10 +166,20 @@ let
|
|||||||
(mapAttrsToList (_: v: v.flakeliteModule))
|
(mapAttrsToList (_: v: v.flakeliteModule))
|
||||||
];
|
];
|
||||||
|
|
||||||
params = exports // { inherit src inputs root'; };
|
inputs' = { inherit nixpkgs; } // inputs;
|
||||||
applyParams = v: ensureFn v params;
|
|
||||||
|
|
||||||
moduleDefaults = {
|
nonSysArgs = exports // {
|
||||||
|
args = nonSysArgs;
|
||||||
|
flakelite = exports;
|
||||||
|
inherit src;
|
||||||
|
inputs = inputs';
|
||||||
|
root = root';
|
||||||
|
inherit (inputs.nixpkgs) lib;
|
||||||
|
};
|
||||||
|
|
||||||
|
applyNonSysArgs = x: ensureFn x nonSysArgs;
|
||||||
|
|
||||||
|
moduleAttrDefaults = {
|
||||||
withOverlays = [ ];
|
withOverlays = [ ];
|
||||||
packages = { };
|
packages = { };
|
||||||
devTools = _: [ ];
|
devTools = _: [ ];
|
||||||
@ -166,36 +196,36 @@ let
|
|||||||
|
|
||||||
normalizeModule = module:
|
normalizeModule = module:
|
||||||
let
|
let
|
||||||
module' = moduleDefaults // module;
|
module' = moduleAttrDefaults // module;
|
||||||
in
|
in
|
||||||
module' // {
|
module' // {
|
||||||
withOverlays = (applyParams module'.withOverlays)
|
withOverlays = (applyNonSysArgs module'.withOverlays)
|
||||||
++ optional (module' ? withOverlay) module'.withOverlay;
|
++ optional (module' ? withOverlay) module'.withOverlay;
|
||||||
packages = (applyParams module'.packages)
|
packages = (applyNonSysArgs module'.packages)
|
||||||
// optionalAttrs (module' ? package) {
|
// optionalAttrs (module' ? package) {
|
||||||
default = module'.package;
|
default = module'.package;
|
||||||
};
|
};
|
||||||
devTools = ensureFn module'.devTools;
|
devTools = ensureFn module'.devTools;
|
||||||
devShells = mergeAttrFns (ensureFn module'.devShells)
|
devShells = fnUpdate (ensureFn module'.devShells)
|
||||||
(_: optionalAttrs (module' ? devShell) {
|
(_: optionalAttrs (module' ? devShell) {
|
||||||
default = module'.devShell;
|
default = module'.devShell;
|
||||||
});
|
});
|
||||||
env = ensureFn module'.env;
|
env = ensureFn module'.env;
|
||||||
overlays = (applyParams module'.overlays)
|
overlays = (applyNonSysArgs module'.overlays)
|
||||||
// optionalAttrs (module' ? overlay) {
|
// optionalAttrs (module' ? overlay) {
|
||||||
default = module'.overlay;
|
default = module'.overlay;
|
||||||
};
|
};
|
||||||
apps = mergeAttrFns (ensureFn module'.apps)
|
apps = fnUpdate (ensureFn module'.apps)
|
||||||
(_: optionalAttrs (module' ? app) {
|
(_: optionalAttrs (module' ? app) {
|
||||||
default = module'.app;
|
default = module'.app;
|
||||||
});
|
});
|
||||||
checks = ensureFn module'.checks;
|
checks = ensureFn module'.checks;
|
||||||
nixosModules = (applyParams module'.nixosModules)
|
nixosModules = (applyNonSysArgs module'.nixosModules)
|
||||||
// optionalAttrs (module' ? nixosModule) {
|
// optionalAttrs (module' ? nixosModule) {
|
||||||
default = module'.nixosModule;
|
default = module'.nixosModule;
|
||||||
};
|
};
|
||||||
nixosConfigurations = applyParams module'.nixosConfigurations;
|
nixosConfigurations = applyNonSysArgs module'.nixosConfigurations;
|
||||||
templates = (applyParams module'.templates)
|
templates = (applyNonSysArgs module'.templates)
|
||||||
// optionalAttrs (module' ? template) {
|
// optionalAttrs (module' ? template) {
|
||||||
default = module'.template;
|
default = module'.template;
|
||||||
};
|
};
|
||||||
@ -205,24 +235,24 @@ let
|
|||||||
root' =
|
root' =
|
||||||
let
|
let
|
||||||
nixDir = root.nixDir or (src + ./nix);
|
nixDir = root.nixDir or (src + ./nix);
|
||||||
rootWithAuto = (genAutoAttrs nixDir) // root;
|
fullRoot = (autoImportAttrs nixDir rootAttrs) // root;
|
||||||
in
|
in
|
||||||
normalizeModule rootWithAuto // {
|
normalizeModule fullRoot // {
|
||||||
systems = applyParams rootWithAuto.systems or systems.linuxDefault;
|
systems = applyNonSysArgs fullRoot.systems or systems.linuxDefault;
|
||||||
perSystem = ensureFn rootWithAuto.perSystem or (_: { });
|
perSystem = ensureFn fullRoot.perSystem or (_: { });
|
||||||
outputs = applyParams rootWithAuto.outputs or { };
|
outputs = applyNonSysArgs fullRoot.outputs or { };
|
||||||
inherit nixDir;
|
inherit nixDir;
|
||||||
raw = root;
|
raw = root;
|
||||||
};
|
};
|
||||||
|
|
||||||
merged = foldl mergeModules moduleDefaults
|
merged = foldl mergeModules moduleAttrDefaults
|
||||||
((map (m: normalizeModule (m src inputs root'))
|
((map (m: normalizeModule (m src inputs' root'))
|
||||||
([ baseModule ] ++ modules)) ++ [ root' ]);
|
([ builtinModule ] ++ modules)) ++ [ root' ]);
|
||||||
|
|
||||||
pkgsFor = system: import (inputs.nixpkgs or nixpkgs) {
|
pkgsFor = system: import inputs'.nixpkgs {
|
||||||
inherit system;
|
inherit system;
|
||||||
overlays = merged.withOverlays ++ [
|
overlays = merged.withOverlays ++ [
|
||||||
(final: _: genPackages final merged.packages)
|
(final: _: callAttrsAuto final merged.packages)
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -230,10 +260,12 @@ let
|
|||||||
(system: nameValuePair system (pkgsFor system))
|
(system: nameValuePair system (pkgsFor system))
|
||||||
root'.systems);
|
root'.systems);
|
||||||
|
|
||||||
getPackagesFrom = pkgs: packageSet:
|
replaceAttrsFrom = source: attrset:
|
||||||
genAttrs (attrNames packageSet) (p: pkgs.${p});
|
genAttrs (attrNames attrset) (n: source.${n});
|
||||||
|
|
||||||
mkCheck = pkgs: name: cmd: pkgs.runCommand "check-${name}" { } ''
|
mkCheck = pkgs: name: cmd:
|
||||||
|
if pkgs.lib.isDerivation cmd then cmd else
|
||||||
|
pkgs.runCommand "check-${name}" { } ''
|
||||||
cp --no-preserve=mode -r ${src} src
|
cp --no-preserve=mode -r ${src} src
|
||||||
cd src
|
cd src
|
||||||
${cmd}
|
${cmd}
|
||||||
@ -255,25 +287,25 @@ let
|
|||||||
(fn systemPkgs.${system}))
|
(fn systemPkgs.${system}))
|
||||||
root'.systems);
|
root'.systems);
|
||||||
|
|
||||||
mergeOutputs = foldl
|
recUpdateSets = foldl
|
||||||
(acc: new: recursiveUpdate acc ((ensureFn new) acc))
|
(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" ]) //
|
||||||
{ ${getName root' set.default} = set.default; }
|
{ ${defaultPkgName root' set.default} = set.default; }
|
||||||
else set;
|
else set;
|
||||||
in
|
in
|
||||||
mergeOutputs [
|
recUpdateSets [
|
||||||
(optionalAttrs (merged.packages != { }) ({
|
(optionalAttrs (merged.packages != { }) ({
|
||||||
overlays.default = final: _: genPackages
|
overlays.default = final: _: callAttrsAuto
|
||||||
(final.appendOverlays merged.withOverlays)
|
(final.appendOverlays merged.withOverlays)
|
||||||
(replaceDefault merged.packages);
|
(replaceDefault merged.packages);
|
||||||
} // eachSystem (pkgs: rec {
|
} // eachSystem (pkgs: rec {
|
||||||
packages = filterAttrs (_: supportedSystem pkgs)
|
packages = filterAttrs (_: supportedSystem pkgs)
|
||||||
(getPackagesFrom pkgs merged.packages);
|
(replaceAttrsFrom pkgs merged.packages);
|
||||||
checks = mapAttrs' (k: nameValuePair ("packages-" + k)) packages;
|
checks = mapAttrs' (n: nameValuePair ("packages-" + n)) packages;
|
||||||
})))
|
})))
|
||||||
(prev: optionalAttrs (merged.overlays != { }) ({
|
(prev: optionalAttrs (merged.overlays != { }) ({
|
||||||
overlays = zipAttrsWith (_: composeManyExtensions)
|
overlays = zipAttrsWith (_: composeManyExtensions)
|
||||||
@ -288,7 +320,7 @@ let
|
|||||||
${pkgs.fd}/bin/fd "$f" -Htf -x "$0"
|
${pkgs.fd}/bin/fd "$f" -Htf -x "$0"
|
||||||
else
|
else
|
||||||
case "$(${pkgs.coreutils}/bin/basename "$f")" in
|
case "$(${pkgs.coreutils}/bin/basename "$f")" in
|
||||||
${toString (mapAttrsToList (k: v: "${k}) ${v} \"$f\";;")
|
${toString (mapAttrsToList (n: v: "${n}) ${v} \"$f\";;")
|
||||||
(merged.formatters pkgs))}
|
(merged.formatters pkgs))}
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
@ -300,11 +332,9 @@ let
|
|||||||
sed 's/Files .* and \(.*\) differ/File \1 not formatted/g'
|
sed 's/Files .* and \(.*\) differ/File \1 not formatted/g'
|
||||||
'';
|
'';
|
||||||
}))
|
}))
|
||||||
(eachSystem ({ pkgs, lib, ... }:
|
(eachSystem (pkgs:
|
||||||
let
|
let
|
||||||
checks = mapAttrs
|
checks = mapAttrs (mkCheck pkgs) (merged.checks pkgs);
|
||||||
(k: v: if lib.isDerivation v then v else mkCheck pkgs k v)
|
|
||||||
(merged.checks pkgs);
|
|
||||||
in
|
in
|
||||||
optionalAttrs (checks != { }) { inherit checks; }))
|
optionalAttrs (checks != { }) { inherit checks; }))
|
||||||
(eachSystem (pkgs:
|
(eachSystem (pkgs:
|
||||||
@ -312,15 +342,14 @@ let
|
|||||||
apps = mapAttrs (_: mkApp pkgs) (merged.apps pkgs);
|
apps = mapAttrs (_: mkApp pkgs) (merged.apps pkgs);
|
||||||
in
|
in
|
||||||
optionalAttrs (apps != { }) { inherit apps; }))
|
optionalAttrs (apps != { }) { inherit apps; }))
|
||||||
|
|
||||||
(optionalAttrs (merged.nixosModules != { }) {
|
(optionalAttrs (merged.nixosModules != { }) {
|
||||||
inherit (merged) nixosModules;
|
inherit (merged) nixosModules;
|
||||||
})
|
})
|
||||||
(optionalAttrs (merged.nixosConfigurations != { }) {
|
(optionalAttrs (merged.nixosConfigurations != { }) {
|
||||||
inherit (merged) nixosConfigurations;
|
inherit (merged) nixosConfigurations;
|
||||||
checks = mergeOutputs (mapAttrsToList
|
checks = recUpdateSets (mapAttrsToList
|
||||||
(k: v: {
|
(n: v: {
|
||||||
${v.config.nixpkgs.system}."nixos-${k}" =
|
${v.config.nixpkgs.system}."nixos-${n}" =
|
||||||
v.config.system.build.toplevel;
|
v.config.system.build.toplevel;
|
||||||
})
|
})
|
||||||
merged.nixosConfigurations);
|
merged.nixosConfigurations);
|
||||||
@ -334,21 +363,9 @@ let
|
|||||||
prev.packages.${system}.default;
|
prev.packages.${system}.default;
|
||||||
packages = merged.devTools pkgs;
|
packages = merged.devTools pkgs;
|
||||||
});
|
});
|
||||||
} // (genPackages pkgs (merged.devShells pkgs))))
|
} // (callAttrsAuto pkgs (merged.devShells pkgs))))
|
||||||
(eachSystem root'.perSystem)
|
(eachSystem root'.perSystem)
|
||||||
root'.outputs
|
root'.outputs
|
||||||
];
|
];
|
||||||
|
|
||||||
systems = rec {
|
|
||||||
linuxDefault = [
|
|
||||||
"x86_64-linux"
|
|
||||||
"aarch64-linux"
|
|
||||||
];
|
|
||||||
linuxAll = linuxDefault ++ [
|
|
||||||
"armv6l-linux"
|
|
||||||
"armv7l-linux"
|
|
||||||
"i686-linux"
|
|
||||||
];
|
|
||||||
};
|
|
||||||
in
|
in
|
||||||
exports
|
exports
|
||||||
|
Loading…
Reference in New Issue
Block a user