2024-07-19 00:06:58 +02:00
|
|
|
{ lib, self, ... }:
|
|
|
|
|
|
|
|
rec {
|
|
|
|
# Boolean helpers
|
|
|
|
and = lib.all lib.id;
|
|
|
|
or = lib.any lib.id;
|
|
|
|
eq = x: y: x == y;
|
|
|
|
|
|
|
|
# Directory walking helpers
|
|
|
|
recurseDir = dir:
|
|
|
|
lib.mapAttrs
|
|
|
|
(file: type:
|
|
|
|
if type == "directory"
|
|
|
|
then recurseDir "${dir}/${file}"
|
|
|
|
else type)
|
|
|
|
(builtins.readDir dir);
|
|
|
|
|
|
|
|
allSatisfy = predicate: attrs: attrset:
|
|
|
|
lib.all
|
|
|
|
(attr:
|
|
|
|
and [
|
|
|
|
(builtins.hasAttr attr attrset)
|
|
|
|
(predicate (builtins.getAttr attr attrset))
|
|
|
|
])
|
|
|
|
attrs;
|
|
|
|
|
|
|
|
# NOTE: Implying last argument is the output of `recurseDir`
|
|
|
|
hasFiles = allSatisfy (eq "regular");
|
|
|
|
|
|
|
|
# NOTE: Implying last argument is the output of `recurseDir`
|
|
|
|
hasDirectories = allSatisfy lib.isAttrs;
|
|
|
|
|
2024-07-24 23:45:23 +02:00
|
|
|
camelToKebab =
|
|
|
|
lib.stringAsChars
|
|
|
|
(c: if c == lib.toUpper c then "-${lib.toLower c}" else c);
|
|
|
|
|
|
|
|
# NOTE: from Tweag's Nix Hour 76 - <https://github.com/tweag/nix-hour/blob/c4fd0f2fc3059f057571bbfd74f3c5e4021f526c/code/76/default.nix#L4-L22>
|
|
|
|
mutFirstChar =
|
|
|
|
f: s:
|
|
|
|
let
|
|
|
|
firstChar = f (lib.substring 0 1 s);
|
|
|
|
rest = lib.substring 1 (-1) s;
|
|
|
|
in firstChar + rest;
|
|
|
|
|
|
|
|
kebabToCamel =
|
|
|
|
s:
|
|
|
|
mutFirstChar lib.toLower (
|
|
|
|
lib.concatMapStrings (mutFirstChar lib.toUpper) (
|
|
|
|
lib.splitString "-" s
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2024-07-19 00:33:09 +02:00
|
|
|
gen-configuration-type-to = mappings: mkError: configuration-type:
|
|
|
|
mappings.${configuration-type} or
|
2024-07-19 00:06:58 +02:00
|
|
|
(builtins.throw
|
2024-07-19 00:33:09 +02:00
|
|
|
(mkError configuration-type));
|
2024-07-19 00:06:58 +02:00
|
|
|
|
2024-07-19 00:33:09 +02:00
|
|
|
configuration-type-to-outputs-machines =
|
|
|
|
gen-configuration-type-to
|
2024-07-19 00:06:58 +02:00
|
|
|
{
|
|
|
|
nixos = "nixosMachines";
|
|
|
|
nix-on-droid = "nixOnDroidMachines";
|
|
|
|
nix-darwin = "nixDarwinMachines";
|
|
|
|
home-manager = "homeMachines";
|
|
|
|
}
|
2024-07-19 00:33:09 +02:00
|
|
|
(configuration-type:
|
2024-07-19 00:06:58 +02:00
|
|
|
builtins.throw
|
2024-07-19 00:33:09 +02:00
|
|
|
"Invaild configuration-type \"${configuration-type}\" for flake outputs' machines");
|
2024-07-19 00:06:58 +02:00
|
|
|
|
2024-07-19 00:33:09 +02:00
|
|
|
configuration-type-to-outputs-configurations =
|
|
|
|
gen-configuration-type-to
|
2024-07-19 00:06:58 +02:00
|
|
|
{
|
|
|
|
nixos = "nixosConfigurations";
|
|
|
|
nix-on-droid = "nixOnDroidConfigurations";
|
|
|
|
nix-darwin = "darwinConfigurations";
|
|
|
|
home-manager = "homeConfigurations";
|
|
|
|
}
|
2024-07-19 00:33:09 +02:00
|
|
|
(configuration-type:
|
2024-07-19 00:06:58 +02:00
|
|
|
builtins.throw
|
2024-07-19 00:33:09 +02:00
|
|
|
"Invaild configuration-type \"${configuration-type}\" for flake outputs' configurations");
|
2024-07-19 00:06:58 +02:00
|
|
|
|
2024-07-19 00:33:09 +02:00
|
|
|
configuration-type-to-deploy-type =
|
|
|
|
gen-configuration-type-to
|
2024-07-19 00:06:58 +02:00
|
|
|
{
|
|
|
|
nixos = "nixos";
|
|
|
|
nix-darwin = "darwin";
|
|
|
|
}
|
2024-07-19 00:33:09 +02:00
|
|
|
(configuration-type:
|
2024-07-19 00:06:58 +02:00
|
|
|
builtins.throw
|
2024-07-19 00:33:09 +02:00
|
|
|
"Invaild configuration-type \"${configuration-type}\" for deploy-rs deployment");
|
2024-07-19 00:06:58 +02:00
|
|
|
|
2024-07-19 00:33:09 +02:00
|
|
|
accumulateMachines = configuration-types: host-system-configuration-type-configuration-fn:
|
2024-07-19 00:06:58 +02:00
|
|
|
lib.flip lib.concatMapAttrs
|
|
|
|
(lib.genAttrs
|
2024-07-19 00:33:09 +02:00
|
|
|
configuration-types
|
|
|
|
(configuration-type:
|
2024-07-19 00:06:58 +02:00
|
|
|
let
|
2024-07-19 00:33:09 +02:00
|
|
|
machines = configuration-type-to-outputs-machines configuration-type;
|
2024-07-19 00:06:58 +02:00
|
|
|
in
|
|
|
|
self.${machines}))
|
2024-07-19 00:33:09 +02:00
|
|
|
(configuration-type: machines:
|
2024-07-19 00:06:58 +02:00
|
|
|
lib.pipe
|
|
|
|
machines
|
|
|
|
[
|
|
|
|
# Filter out nondirectories
|
|
|
|
(lib.filterAttrs
|
2024-07-19 00:33:09 +02:00
|
|
|
(system: configurations:
|
|
|
|
builtins.isAttrs configurations))
|
2024-07-19 00:06:58 +02:00
|
|
|
# Convert non-template configs into `system-and-config` pairs
|
|
|
|
(lib.concatMapAttrs
|
2024-07-19 00:33:09 +02:00
|
|
|
(system: configurations:
|
2024-07-19 00:06:58 +02:00
|
|
|
(lib.concatMapAttrs
|
2024-07-19 00:33:09 +02:00
|
|
|
(host: configuration:
|
2024-07-19 00:06:58 +02:00
|
|
|
lib.optionalAttrs
|
|
|
|
(host != "__template__")
|
|
|
|
{
|
|
|
|
${host} = {
|
|
|
|
inherit system;
|
2024-07-19 00:33:09 +02:00
|
|
|
configuration =
|
2024-07-19 00:06:58 +02:00
|
|
|
let
|
2024-07-19 00:33:09 +02:00
|
|
|
configurations = configuration-type-to-outputs-configurations configuration-type;
|
2024-07-19 00:06:58 +02:00
|
|
|
in
|
|
|
|
self.${configurations}.${host};
|
|
|
|
};
|
|
|
|
})
|
2024-07-19 00:33:09 +02:00
|
|
|
configurations)))
|
|
|
|
# Convert each `system-and-config` pair into a *whatever*
|
2024-07-19 00:06:58 +02:00
|
|
|
(lib.concatMapAttrs
|
2024-07-19 00:33:09 +02:00
|
|
|
(host: { system, configuration }:
|
|
|
|
host-system-configuration-type-configuration-fn { inherit host system configuration-type configuration; }))
|
2024-07-19 00:06:58 +02:00
|
|
|
]);
|
|
|
|
}
|