nixos/lib/info.nix

83 lines
2.1 KiB
Nix
Raw Permalink Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{ lib, inputs, ... }:
let
inherit (lib)
attrNames
attrsToList
isInt
isString
join
length
listToAttrs
filter
filterAttrs
mapAttrs
nameValuePair
nixosSystem
readDir
warn
warnIf
warnIfNot
;
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);
in
rec {
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);
attrs_vm_info = listInfoToAttrs (get_info ../hosts/vm) { isVm = true; };
attrs_physique_info = listInfoToAttrs (get_info ../hosts/physiques) { isVm = false; };
attrs_all_info = attrs_physique_info // attrs_vm_info;
}