2023-04-09 02:56:06 +02:00
|
|
|
# flakelite -- Framework for making flakes simple
|
|
|
|
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
nixpkgs:
|
|
|
|
let
|
2023-04-15 10:26:00 +02:00
|
|
|
inherit (builtins) functionArgs isFunction isList isPath isString readDir;
|
2023-04-09 02:56:06 +02:00
|
|
|
inherit (nixpkgs.lib) attrNames attrVals composeManyExtensions filter
|
2023-04-15 10:26:00 +02:00
|
|
|
filterAttrs foldAttrs foldl genAttrs hasSuffix listToAttrs mapAttrs
|
|
|
|
mapAttrsToList mapAttrs' mergeAttrs nameValuePair optional optionalAttrs
|
|
|
|
optionalString parseDrvName pathExists pipe recursiveUpdate removeSuffix
|
|
|
|
zipAttrsWith;
|
2023-04-09 02:56:06 +02:00
|
|
|
|
2023-04-15 05:16:50 +02:00
|
|
|
exports = { inherit mkFlake loadNixDir systems; };
|
|
|
|
|
2023-04-09 02:56:06 +02:00
|
|
|
baseModule = src: inputs: root: {
|
2023-04-15 05:16:50 +02:00
|
|
|
withOverlays = params: [
|
|
|
|
(final: prev: {
|
|
|
|
flakelite = params // {
|
|
|
|
inputs' = mapAttrs
|
|
|
|
(_: mapAttrs
|
|
|
|
(_: v: v.${prev.system} or { }))
|
|
|
|
inputs;
|
|
|
|
meta = {
|
|
|
|
platforms = root.systems;
|
|
|
|
} // optionalAttrs (root ? description) {
|
|
|
|
inherit (root) description;
|
|
|
|
} // optionalAttrs (root ? license) {
|
|
|
|
license =
|
|
|
|
if isList root.license
|
|
|
|
then attrVals root.license final.lib.licenses
|
|
|
|
else final.lib.licenses.${root.license};
|
|
|
|
};
|
2023-04-14 19:42:50 +02:00
|
|
|
};
|
2023-04-15 05:16:50 +02:00
|
|
|
})
|
|
|
|
];
|
2023-04-09 02:56:06 +02:00
|
|
|
checks = { pkgs, lib, ... }:
|
|
|
|
(optionalAttrs (pathExists (src + /.editorconfig)) {
|
|
|
|
editorconfig = "${lib.getExe pkgs.editorconfig-checker}"
|
|
|
|
+ optionalString (!pathExists (src + /.ecrc))
|
|
|
|
" -disable-indent-size -disable-max-line-length";
|
|
|
|
});
|
2023-04-14 19:43:39 +02:00
|
|
|
devTools = pkgs: with pkgs; [ nixpkgs-fmt nodePackages.prettier ];
|
2023-04-09 02:56:06 +02:00
|
|
|
formatters = {
|
|
|
|
"*.nix" = "nixpkgs-fmt";
|
2023-04-14 19:43:39 +02:00
|
|
|
"*.md | *.json | *.yml" = "prettier --write";
|
2023-04-09 02:56:06 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkFlake = src: inputs: root:
|
|
|
|
let
|
|
|
|
modules = root.modules or pipe (inputs // { self = { }; }) [
|
|
|
|
(filterAttrs (_: v: v ? flakeliteModule))
|
|
|
|
(mapAttrsToList (_: v: v.flakeliteModule))
|
|
|
|
];
|
|
|
|
|
2023-04-15 05:16:50 +02:00
|
|
|
mkFunc = v: if isFunction v then v else _: v;
|
|
|
|
|
2023-04-15 10:29:54 +02:00
|
|
|
mergeListFns = f1: f2: args: (f1 args) ++ (f2 args);
|
|
|
|
mergeAttrFns = f1: f2: args: (f1 args) // (f2 args);
|
|
|
|
|
2023-04-15 05:16:50 +02:00
|
|
|
params = exports // { inherit src inputs root'; };
|
|
|
|
applyParams = v: mkFunc v params;
|
|
|
|
|
2023-04-15 10:29:54 +02:00
|
|
|
moduleDefaults = {
|
2023-04-09 02:56:06 +02:00
|
|
|
withOverlays = [ ];
|
|
|
|
packages = { };
|
|
|
|
devTools = _: [ ];
|
2023-04-15 10:29:54 +02:00
|
|
|
devShells = _: { };
|
2023-04-09 02:56:06 +02:00
|
|
|
env = _: { };
|
|
|
|
overlays = { };
|
|
|
|
apps = _: { };
|
|
|
|
checks = _: { };
|
|
|
|
nixosModules = { };
|
|
|
|
nixosConfigurations = { };
|
|
|
|
templates = { };
|
|
|
|
formatters = _: { };
|
|
|
|
};
|
|
|
|
|
|
|
|
normalizeModule = module:
|
|
|
|
let
|
2023-04-15 10:29:54 +02:00
|
|
|
module' = moduleDefaults // module;
|
2023-04-09 02:56:06 +02:00
|
|
|
in
|
|
|
|
module' // {
|
2023-04-15 05:16:50 +02:00
|
|
|
withOverlays = (applyParams module'.withOverlays)
|
2023-04-09 02:56:06 +02:00
|
|
|
++ optional (module' ? withOverlay) module'.withOverlay;
|
2023-04-15 05:16:50 +02:00
|
|
|
packages = (applyParams module'.packages)
|
|
|
|
// optionalAttrs (module' ? package) {
|
2023-04-09 02:56:06 +02:00
|
|
|
default = module'.package;
|
|
|
|
};
|
|
|
|
devTools = mkFunc module'.devTools;
|
2023-04-15 10:29:54 +02:00
|
|
|
devShells = mergeAttrFns (mkFunc module'.devShells)
|
|
|
|
(_: optionalAttrs (module' ? devShell) {
|
|
|
|
default = module'.devShell;
|
|
|
|
});
|
2023-04-09 02:56:06 +02:00
|
|
|
env = mkFunc module'.env;
|
2023-04-15 05:16:50 +02:00
|
|
|
overlays = (applyParams module'.overlays)
|
|
|
|
// optionalAttrs (module' ? overlay) {
|
2023-04-09 02:56:06 +02:00
|
|
|
default = module'.overlay;
|
|
|
|
};
|
|
|
|
apps = mkFunc module'.apps;
|
|
|
|
checks = mkFunc module'.checks;
|
2023-04-15 05:16:50 +02:00
|
|
|
nixosModules = applyParams module'.nixosModules;
|
|
|
|
nixosConfigurations = applyParams module'.nixosConfigurations;
|
|
|
|
templates = applyParams module'.templates;
|
2023-04-09 02:56:06 +02:00
|
|
|
formatters = mkFunc module'.formatters;
|
|
|
|
};
|
|
|
|
|
2023-04-15 05:16:50 +02:00
|
|
|
root' = normalizeModule root // {
|
|
|
|
systems = applyParams root.systems or systems.linuxDefault;
|
|
|
|
perSystem = mkFunc root.perSystem or (_: { });
|
|
|
|
outputs = applyParams root.outputs or { };
|
|
|
|
};
|
|
|
|
|
2023-04-09 02:56:06 +02:00
|
|
|
mergeModules = m1: m2: {
|
|
|
|
withOverlays = m1.withOverlays ++ m2.withOverlays;
|
|
|
|
packages = m1.packages // m2.packages;
|
|
|
|
devTools = mergeListFns m1.devTools m2.devTools;
|
2023-04-15 10:29:54 +02:00
|
|
|
devShells = mergeAttrFns m1.devShells m2.devShells;
|
2023-04-09 02:56:06 +02:00
|
|
|
env = mergeAttrFns m1.env m2.env;
|
|
|
|
overlays = zipAttrsWith (_: composeManyExtensions)
|
|
|
|
[ m1.overlays m2.overlays ];
|
|
|
|
apps = mergeAttrFns m1.apps m2.apps;
|
|
|
|
checks = mergeAttrFns 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;
|
|
|
|
};
|
|
|
|
|
2023-04-15 10:29:54 +02:00
|
|
|
merged = foldl mergeModules moduleDefaults
|
2023-04-09 02:56:06 +02:00
|
|
|
((map (m: normalizeModule (m src inputs root'))
|
|
|
|
([ baseModule ] ++ modules)) ++ [ root' ]);
|
|
|
|
|
2023-04-15 10:26:00 +02:00
|
|
|
callWith = pkgs: x:
|
|
|
|
let
|
|
|
|
x' = if (isPath x) || (isString x) then import x else x;
|
|
|
|
in
|
|
|
|
if ! isFunction x' then x'
|
|
|
|
else
|
|
|
|
if functionArgs x' == { }
|
|
|
|
then x' pkgs
|
|
|
|
else pkgs.callPackage x' { };
|
|
|
|
|
|
|
|
genPackages = pkgs: mapAttrs (_: callWith pkgs);
|
2023-04-14 04:19:58 +02:00
|
|
|
|
2023-04-09 02:56:06 +02:00
|
|
|
pkgsFor = system: import (inputs.nixpkgs or nixpkgs) {
|
|
|
|
inherit system;
|
2023-04-14 04:19:58 +02:00
|
|
|
overlays = merged.withOverlays ++ [
|
|
|
|
(final: _: genPackages final merged.packages)
|
|
|
|
];
|
2023-04-09 02:56:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
systemPkgs = listToAttrs (map
|
|
|
|
(system: nameValuePair system (pkgsFor system))
|
|
|
|
root'.systems);
|
|
|
|
|
2023-04-14 04:19:58 +02:00
|
|
|
getPackagesFrom = pkgs: packageSet:
|
|
|
|
genAttrs (attrNames packageSet) (p: pkgs.${p});
|
2023-04-09 02:56:06 +02:00
|
|
|
|
|
|
|
mkCheck = pkgs: name: cmd: pkgs.runCommand "check-${name}" { } ''
|
|
|
|
cp --no-preserve=mode -r ${src} src
|
|
|
|
cd src
|
|
|
|
${cmd}
|
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
|
2023-04-14 07:57:23 +02:00
|
|
|
isApp = x: (x ? type) && (x.type == "app") && (x ? program);
|
|
|
|
|
|
|
|
mkApp = lib: app:
|
|
|
|
if isApp app then app
|
2023-04-15 06:00:59 +02:00
|
|
|
else { type = "app"; program = "${app}"; };
|
2023-04-09 02:56:06 +02:00
|
|
|
|
|
|
|
eachSystem = fn: foldAttrs mergeAttrs { } (map
|
|
|
|
(system: mapAttrs
|
|
|
|
(_: v: { ${system} = v; })
|
|
|
|
(fn systemPkgs.${system}))
|
|
|
|
root'.systems);
|
|
|
|
|
|
|
|
mergeOutputs = foldl
|
|
|
|
(acc: new: recursiveUpdate acc ((mkFunc new) acc))
|
|
|
|
{ };
|
|
|
|
|
|
|
|
getName = pkg: root.name or pkg.pname or (parseDrvName pkg).name;
|
|
|
|
|
|
|
|
replaceDefault = set:
|
|
|
|
if set ? default
|
|
|
|
then (removeAttrs set [ "default" ]) //
|
|
|
|
{ ${getName set.default} = set.default; }
|
|
|
|
else set;
|
|
|
|
|
|
|
|
supportedSystem = { lib, stdenv, ... }: pkg:
|
|
|
|
if pkg ? meta.platforms
|
|
|
|
then lib.meta.availableOn stdenv.hostPlatform pkg
|
|
|
|
else true;
|
|
|
|
in
|
|
|
|
mergeOutputs [
|
|
|
|
|
|
|
|
(optionalAttrs (merged.packages != { }) ({
|
|
|
|
overlays.default = final: _: genPackages
|
|
|
|
(final.appendOverlays merged.withOverlays)
|
|
|
|
(replaceDefault merged.packages);
|
|
|
|
} // eachSystem (pkgs: rec {
|
|
|
|
packages = filterAttrs (_: supportedSystem pkgs)
|
2023-04-14 04:19:58 +02:00
|
|
|
(getPackagesFrom pkgs merged.packages);
|
2023-04-09 02:56:06 +02:00
|
|
|
checks = mapAttrs' (k: nameValuePair ("packages-" + k)) packages;
|
|
|
|
})))
|
|
|
|
|
|
|
|
(prev: optionalAttrs (merged.overlays != { }) ({
|
|
|
|
overlays = zipAttrsWith (_: composeManyExtensions)
|
|
|
|
[ (prev.overlays or { }) merged.overlays ];
|
|
|
|
}))
|
|
|
|
|
|
|
|
(eachSystem ({ pkgs, lib, ... }:
|
|
|
|
optionalAttrs (merged.formatters pkgs != { }) rec {
|
|
|
|
formatter = pkgs.writeShellScriptBin "formatter" ''
|
|
|
|
PATH=${lib.makeBinPath (merged.devTools pkgs)}
|
|
|
|
for f in "$@"; do
|
|
|
|
if [ -d "$f" ]; then
|
|
|
|
${pkgs.fd}/bin/fd "$f" -Htf -x "$0"
|
|
|
|
else
|
|
|
|
case "$(${pkgs.coreutils}/bin/basename "$f")" in
|
|
|
|
${toString (mapAttrsToList (k: v: "${k}) ${v} \"$f\";;")
|
|
|
|
(merged.formatters pkgs))}
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
done &>/dev/null
|
|
|
|
'';
|
|
|
|
checks.formatting = mkCheck pkgs "formatting" ''
|
|
|
|
${lib.getExe formatter} .
|
|
|
|
${pkgs.diffutils}/bin/diff -qr ${src} . |\
|
|
|
|
sed 's/Files .* and \(.*\) differ/File \1 not formatted/g'
|
|
|
|
'';
|
|
|
|
}))
|
|
|
|
|
|
|
|
(eachSystem ({ pkgs, lib, ... }:
|
|
|
|
let
|
|
|
|
checks = mapAttrs
|
|
|
|
(k: v: if lib.isDerivation v then v else mkCheck pkgs k v)
|
|
|
|
(merged.checks pkgs);
|
|
|
|
in
|
|
|
|
optionalAttrs (checks != { }) { inherit checks; }))
|
|
|
|
|
|
|
|
(eachSystem ({ pkgs, lib, ... }:
|
|
|
|
let
|
|
|
|
apps = mapAttrs (_: mkApp lib) (merged.apps pkgs);
|
|
|
|
in
|
|
|
|
optionalAttrs (apps != { }) { inherit apps; }))
|
|
|
|
|
|
|
|
(optionalAttrs (merged.nixosModules != { }) {
|
|
|
|
inherit (merged) nixosModules;
|
|
|
|
})
|
|
|
|
|
|
|
|
(optionalAttrs (merged.nixosConfigurations != { }) {
|
|
|
|
inherit (merged) nixosConfigurations;
|
2023-04-15 05:04:31 +02:00
|
|
|
checks = mergeOutputs (mapAttrsToList
|
|
|
|
(k: v: {
|
|
|
|
${v.config.nixpkgs.system}."nixos-${k}" =
|
|
|
|
v.config.system.build.toplevel;
|
|
|
|
})
|
|
|
|
merged.nixosConfigurations);
|
2023-04-09 02:56:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
(optionalAttrs (merged.templates != { }) {
|
|
|
|
inherit (merged) templates;
|
|
|
|
})
|
|
|
|
|
|
|
|
(prev: eachSystem ({ pkgs, system, mkShell, ... }: {
|
|
|
|
devShells.default = mkShell (merged.env pkgs // {
|
|
|
|
inputsFrom = optional (prev ? packages.${system}.default)
|
|
|
|
prev.packages.${system}.default;
|
|
|
|
packages = merged.devTools pkgs;
|
|
|
|
});
|
2023-04-15 10:29:54 +02:00
|
|
|
} // (genPackages pkgs (merged.devShells pkgs))))
|
2023-04-09 02:56:06 +02:00
|
|
|
|
|
|
|
(eachSystem root'.perSystem)
|
|
|
|
|
|
|
|
root'.outputs
|
|
|
|
];
|
|
|
|
|
|
|
|
loadNixDir = path: genAttrs
|
|
|
|
(pipe (readDir path) [
|
|
|
|
attrNames
|
|
|
|
(filter (hasSuffix ".nix"))
|
|
|
|
(map (removeSuffix ".nix"))
|
|
|
|
])
|
|
|
|
(p: import (path + "/${p}.nix"));
|
|
|
|
|
|
|
|
systems = rec {
|
|
|
|
linuxDefault = [
|
|
|
|
"x86_64-linux"
|
|
|
|
"aarch64-linux"
|
|
|
|
];
|
|
|
|
linuxAll = linuxDefault ++ [
|
|
|
|
"armv6l-linux"
|
|
|
|
"armv7l-linux"
|
|
|
|
"i686-linux"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
in
|
2023-04-14 19:42:50 +02:00
|
|
|
exports
|