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);
|
|
|
|
|
2024-07-25 00:25:21 +02:00
|
|
|
# NOTE: from Tweag's Nix Hour 76 - <https://github.com/tweag/nix-hour/blob/c4fd0f2fc3059f057571bbfd74f3c5e4021f526c/code/76/default.nix#L4-L22>
|
2024-07-24 23:45:23 +02:00
|
|
|
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-27 12:04:48 +02:00
|
|
|
# TODO: abstract away `_Hosts` and `_Modules`
|
2024-07-25 00:25:21 +02:00
|
|
|
|
2024-07-27 12:04:48 +02:00
|
|
|
configuration-type-to-outputs-hosts =
|
2024-07-19 00:33:09 +02:00
|
|
|
gen-configuration-type-to
|
2024-07-19 00:06:58 +02:00
|
|
|
{
|
2024-07-27 12:04:48 +02:00
|
|
|
nixos = "nixosHosts";
|
|
|
|
nix-on-droid = "nixOnDroidHosts";
|
|
|
|
nix-darwin = "darwinHosts";
|
|
|
|
home-manager = "homeManagerHosts";
|
2024-07-19 00:06:58 +02:00
|
|
|
}
|
2024-07-19 00:33:09 +02:00
|
|
|
(configuration-type:
|
2024-07-19 00:06:58 +02:00
|
|
|
builtins.throw
|
2024-07-27 12:04:48 +02:00
|
|
|
"Invaild configuration-type \"${configuration-type}\" for flake outputs' hosts");
|
2024-07-19 00:06:58 +02:00
|
|
|
|
2024-07-25 00:25:21 +02:00
|
|
|
configuration-type-to-outputs-modules =
|
|
|
|
gen-configuration-type-to
|
|
|
|
{
|
|
|
|
nixos = "nixosModules";
|
|
|
|
nix-on-droid = "nixOnDroidModules";
|
2024-07-25 11:21:59 +02:00
|
|
|
nix-darwin = "darwinModules";
|
2024-07-25 00:25:21 +02:00
|
|
|
home-manager = "homeManagerModules";
|
|
|
|
flake = "flakeModules";
|
|
|
|
}
|
|
|
|
(configuration-type:
|
|
|
|
builtins.throw
|
|
|
|
"Invaild configuration-type \"${configuration-type}\" for flake outputs' modules");
|
|
|
|
|
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-27 12:04:48 +02:00
|
|
|
accumulateHosts = 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-27 12:04:48 +02:00
|
|
|
hosts = configuration-type-to-outputs-hosts configuration-type;
|
2024-07-19 00:06:58 +02:00
|
|
|
in
|
2024-07-27 12:04:48 +02:00
|
|
|
self.${hosts}))
|
|
|
|
(configuration-type: hosts:
|
2024-07-19 00:06:58 +02:00
|
|
|
lib.pipe
|
2024-07-27 12:04:48 +02:00
|
|
|
hosts
|
2024-07-19 00:06:58 +02:00
|
|
|
[
|
|
|
|
# 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
|
|
|
]);
|
|
|
|
}
|