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