mirror of https://gitlab.crans.org/nounous/nixos
149 lines
4.0 KiB
Nix
149 lines
4.0 KiB
Nix
{
|
||
config,
|
||
crans,
|
||
lib,
|
||
...
|
||
}:
|
||
let
|
||
inherit (lib)
|
||
fixedWidthString
|
||
hasPrefix
|
||
listToAttrs
|
||
mapAttrs
|
||
mergeAttrsList
|
||
mod
|
||
mkEnableOption
|
||
mkOption
|
||
optional
|
||
optionalAttrs
|
||
nameValuePair
|
||
types
|
||
;
|
||
|
||
switchOptions = listToAttrs (
|
||
map (
|
||
n:
|
||
nameValuePair n (mkOption {
|
||
type = types.nullOr types.int;
|
||
description = "Port sur le switch ${n}";
|
||
default = null;
|
||
})
|
||
) crans.switchs
|
||
);
|
||
|
||
routesType = types.listOf (
|
||
types.submodule {
|
||
options = {
|
||
address = mkOption {
|
||
type = types.str;
|
||
description = "Réseau de destination";
|
||
};
|
||
prefixLength = mkOption {
|
||
type = types.int;
|
||
description = "Masque du réseau de destination";
|
||
};
|
||
via = mkOption {
|
||
type = types.str;
|
||
description = "Ip de la gateway";
|
||
};
|
||
};
|
||
}
|
||
);
|
||
in
|
||
{
|
||
options = {
|
||
ilo.id = mkOption {
|
||
type = types.nullOr types.int;
|
||
description = "Identifiant de l’ilo";
|
||
default = null;
|
||
};
|
||
ilo.switch = switchOptions;
|
||
|
||
# TODO: vérifier que les IPs déclarées sont bien dans le réseau vlan (masque)
|
||
networking = {
|
||
enable = mkEnableOption "Activation du réseau Crans";
|
||
switch = switchOptions;
|
||
}
|
||
// mapAttrs (
|
||
vname: vconf:
|
||
mergeAttrsList (
|
||
[
|
||
{
|
||
enable = mkEnableOption "Activation du réseau ${vname}";
|
||
|
||
interface = mkOption {
|
||
type = types.str;
|
||
description = "Interface pour le réseau ${vname}";
|
||
default = vconf.interface;
|
||
};
|
||
|
||
tag = mkOption {
|
||
type = types.int;
|
||
description = "Tag du vlan";
|
||
readOnly = true;
|
||
default = vconf.id;
|
||
};
|
||
}
|
||
]
|
||
++ optional ((vconf.ipv4 or { }) != null) {
|
||
ipv4.address =
|
||
mkOption {
|
||
type = types.str;
|
||
description = "Adresse IPv4 de la machine sur le réseau ${vname}";
|
||
}
|
||
// (optionalAttrs (hasPrefix "172.16" (vconf.ipv4.address or "172.16.0.0")) {
|
||
default = "172.16.${toString vconf.id}.${toString config.id}";
|
||
});
|
||
|
||
ipv4.prefixLength = mkOption {
|
||
type = types.int;
|
||
description = "Masque IPv4 de la machine sur le réseau ${vname}";
|
||
default = vconf.ipv4.prefixLength or 24;
|
||
};
|
||
|
||
ipv4.routes = mkOption {
|
||
type = routesType;
|
||
description = "Routes de l’interface";
|
||
default = vconf.ipv4.routes or [ ];
|
||
};
|
||
}
|
||
++ optional ((vconf.ipv6 or { }) != null) (
|
||
let
|
||
# XXX: Utilisation du masque pour le déterminer à la place ?
|
||
prefix =
|
||
if !(vconf ? ipv6) || lib.hasPrefix "fd00:0:0" vconf.ipv6.address then
|
||
"fd00:0:0"
|
||
else if lib.hasPrefix "2a0c:700" vconf.ipv6.address then
|
||
"2a0c:700"
|
||
else
|
||
(builtins.warn "Le prefixe de ${vconf.ipv6.address} n’est pas reconnu");
|
||
in
|
||
{
|
||
ipv6.address = mkOption {
|
||
type = types.str;
|
||
description = "Adresse IPv6 de la machine sur le réseau ${vname}";
|
||
default = "${prefix}:${toString vconf.id}::ff:fe0${toString (config.id / 100)}:${toString (mod config.id 100)}${
|
||
fixedWidthString 2 "0" (toString vconf.id)
|
||
}";
|
||
};
|
||
|
||
ipv6.prefixLength = mkOption {
|
||
type = types.int;
|
||
description = "Masque IPv6 de la machine sur le réseau ${vname}";
|
||
default = vconf.ipv6.prefixLength or 64;
|
||
};
|
||
|
||
ipv6.routes = mkOption {
|
||
type = routesType;
|
||
description = "Routes de l’interface";
|
||
default = vconf.ipv6.routes or [ ];
|
||
};
|
||
}
|
||
)
|
||
)
|
||
) crans.vlans
|
||
|
||
;
|
||
};
|
||
}
|