mirror of https://gitlab.crans.org/nounous/nixos
82 lines
1.9 KiB
Nix
82 lines
1.9 KiB
Nix
{ lib, inputs, ... }:
|
||
|
||
let
|
||
inherit (lib)
|
||
attrNames
|
||
attrsToList
|
||
isInt
|
||
isString
|
||
join
|
||
length
|
||
listToAttrs
|
||
filter
|
||
filterAttrs
|
||
mapAttrs
|
||
nameValuePair
|
||
nixosSystem
|
||
readDir
|
||
warn
|
||
warnIf
|
||
warnIfNot
|
||
;
|
||
in
|
||
rec {
|
||
|
||
infoRequired = {
|
||
name = isString;
|
||
description = isString;
|
||
id = isInt;
|
||
};
|
||
verifyAttrs =
|
||
info:
|
||
let
|
||
missing = filter (n: !(info ? ${n})) (attrNames infoRequired);
|
||
wrongType = filter (n: info ? ${n.name} && !(n.value info.${n.name})) (attrsToList infoRequired);
|
||
in
|
||
(warnIf (
|
||
length missing > 0
|
||
) "${info.name}/info.nix : les attributs « ${join ", " missing} » sont manquants")
|
||
(warnIf (length wrongType > 0)
|
||
"${info.name}/info.nix : les attributs « ${join ", " (map (i: i.name) wrongType)} » sont mal typés"
|
||
)
|
||
(warnIfNot (
|
||
(info.isDebian or false) || info ? enable
|
||
) "${info.name}/info.nix : cette machine nix ne définit pas l’attribut enable")
|
||
info;
|
||
|
||
get_hosts_names = path: attrNames (filterAttrs (name: type: type == "directory") (readDir path));
|
||
get_info =
|
||
path:
|
||
filter (x: x != null) (
|
||
map (
|
||
name:
|
||
let
|
||
filePath = path + "/${name}/info.nix";
|
||
in
|
||
if builtins.pathExists filePath then
|
||
verifyAttrs (import filePath)
|
||
else
|
||
warn "${toString filePath} not found" null
|
||
) (get_hosts_names path)
|
||
);
|
||
|
||
listInfoToAttrs = infos: add: listToAttrs (map (info: nameValuePair info.name (info // add)) infos);
|
||
|
||
attrsToNixosSystem =
|
||
path: all_hosts: hosts: baseModules:
|
||
mapAttrs (
|
||
name: info:
|
||
nixosSystem {
|
||
specialArgs = inputs // {
|
||
hosts = all_hosts;
|
||
};
|
||
modules = [
|
||
(path + "/${name}")
|
||
({ ... }: { config.crans = info; })
|
||
]
|
||
++ baseModules;
|
||
}
|
||
) (filterAttrs (name: info: !(info.isDebian or false)) hosts);
|
||
|
||
}
|