infrastructure/meta/isp/module.nix

146 lines
3.7 KiB
Nix
Raw Normal View History

2025-02-07 15:35:42 +01:00
# SPDX-FileCopyrightText: 2024 Tom Hubrecht <tom.hubrecht@dgnum.eu>
#
# SPDX-License-Identifier: EUPL-1.2
2025-03-02 18:49:09 +01:00
{ lib, config, ... }:
2025-02-07 15:35:42 +01:00
let
inherit (lib)
2025-03-02 18:49:09 +01:00
attrValues
genAttrs
2025-02-07 15:35:42 +01:00
mkDefault
mkIf
2025-03-02 18:49:09 +01:00
mkMerge
2025-02-07 15:35:42 +01:00
mkOption
2025-03-02 18:49:09 +01:00
optional
2025-02-07 15:35:42 +01:00
;
inherit (lib.types)
attrs
attrsOf
bool
ints
2025-03-02 18:49:09 +01:00
listOf
2025-02-07 15:35:42 +01:00
nullOr
submodule
str
;
2025-03-02 18:49:09 +01:00
cfg = config.isp;
2025-02-07 15:35:42 +01:00
in
{
options.isp = {
vlans = mkOption {
type = attrsOf (
submodule (
{ config, ... }:
{
options = {
2025-03-02 18:49:09 +01:00
flags = mkOption {
type = listOf str;
default = optional config.userOnly "users";
defaultText = ''optional config.userOnly "users"'';
description = ''
Groups of VLANs this VLAN belong to.
'';
};
2025-02-07 15:35:42 +01:00
id = mkOption {
type = ints.between 0 (4096 - 1);
description = ''
The VLAN id to use.
'';
};
settings = mkOption {
type = attrs;
default = { };
description = ''
Settings for the configuration of networkd.
'';
};
internal = {
network = mkOption {
type = nullOr str;
description = ''
The internal network address of this VLAN.
'';
};
prefix = mkOption {
type = nullOr (ints.between 0 32);
default = if config.userOnly then 27 else null;
description = ''
The prefix length of the network associated to this VLAN.
'';
};
address = mkOption {
type = nullOr str;
description = ''
The router address in the VLAN. It should be the first ipv4 in the network.
'';
};
cidr = mkOption {
type = nullOr str;
default =
with config.internal;
if (prefix != null && network != null) then "${network}/${builtins.toString prefix}" else null;
description = ''
The CIDR notation of the network associated to the VLAN.
'';
};
};
userOnly = mkOption {
type = bool;
default = false;
description = ''
Whether this VLAN is only used by a user in the context of the IPS.
I.e. this is not an administration VLAN.
'';
};
};
# The address is null by default when not on a user VLAN
config.internal = mkIf (!config.userOnly) {
address = mkDefault null;
network = mkDefault null;
};
}
)
);
2025-03-02 18:49:09 +01:00
default = { };
2025-02-07 15:35:42 +01:00
description = ''
The list of VLANs known to our ISP.
'';
};
2025-03-02 18:49:09 +01:00
vlans-groups = mkOption {
type = attrsOf (submodule {
options.id-list = mkOption {
type = listOf (ints.between 0 (4096 - 1));
description = ''
List of VLANs IDs inside this group.
'';
};
});
default = { };
description = ''
The list of groups of VLANs known to our ISP.
'';
};
2025-02-07 15:35:42 +01:00
};
2025-03-02 18:49:09 +01:00
config.isp.vlans-groups = mkMerge (
map (
{ flags, id, ... }:
genAttrs flags (_: {
id-list = [ id ];
})
) (attrValues cfg.vlans)
);
2025-02-07 15:35:42 +01:00
}