Netconf-Module/dgn-module.nix

120 lines
3.7 KiB
Nix
Raw Permalink Normal View History

2024-08-31 22:37:30 +02:00
{ config, lib, ... }:
let
2024-12-13 14:37:43 +01:00
inherit (lib)
mkEnableOption
mkOption
filterAttrs
mapAttrs
attrNames
mkIf
;
inherit (lib.types)
nullOr
enum
listOf
either
str
ints
attrsOf
submodule
;
2024-08-31 22:37:30 +02:00
intf-mod =
2024-12-02 15:45:56 +01:00
{ config, ... }:
2024-08-31 22:37:30 +02:00
{
options = {
enable = mkEnableOption "this interface" // {
default = config.inet.enable || config.inet6.enable || config.ethernet-switching.enable;
defaultText = ''config.inet.enable || config.inet6.enable || config.ethernet-switching.enable'';
};
poe = mkEnableOption "the PoE on this interface";
2024-08-31 22:37:30 +02:00
ethernet-switching = {
enable = mkEnableOption "the ethernet switching on this interface" // {
default = config.ethernet-switching.interface-mode != null;
defaultText = ''config.ethernet-switching.interface-mode != null'';
};
2024-12-02 15:45:56 +01:00
rstp = mkEnableOption "RSTP on this interface" // {
default = config.ethernet-switching.enable;
defaultText = ''config.ethernet-switching.enable'';
};
2024-08-31 22:37:30 +02:00
interface-mode = mkOption {
2024-12-13 14:37:43 +01:00
type = nullOr (enum [
"trunk"
"access"
]);
default = null;
description = ''
Mode of operation for vlan addressing of this interface.
"trunk" means that the traffic is tagged, "access" means the
traffic is tagged by the switch.
Use null to desactivate the switching.
'';
2024-08-31 22:37:30 +02:00
};
vlans = mkOption {
2024-12-13 14:37:43 +01:00
type = listOf (either str ints.unsigned);
2024-08-31 22:37:30 +02:00
default = [ ];
description = ''
Vlans that can be used on this interface.
Only one ID should be here for "access" mode of operation.
'';
2024-08-31 22:37:30 +02:00
};
};
inet = {
enable = mkEnableOption "the ipv4 on this interface" // {
default = config.inet.addresses != [ ];
defaultText = ''config.inet.addresses != [ ]'';
};
addresses = mkOption {
2024-12-13 14:37:43 +01:00
type = listOf str;
2024-08-31 22:37:30 +02:00
default = [ ];
description = ''
ipv4 addresses of this interface.
'';
2024-08-31 22:37:30 +02:00
};
};
inet6 = {
enable = mkEnableOption "the ipv6 on this interface" // {
default = config.inet6.addresses != [ ];
defaultText = ''config.inet6.addresses != [ ]'';
};
addresses = mkOption {
2024-12-13 14:37:43 +01:00
type = listOf str;
2024-08-31 22:37:30 +02:00
default = [ ];
description = ''
ipv6 addresses of this interface.
'';
2024-08-31 22:37:30 +02:00
};
};
};
};
cfg = config.dgn-interfaces;
in
{
options.dgn-interfaces = mkOption {
2024-12-13 14:37:43 +01:00
type = attrsOf (submodule intf-mod);
2024-08-31 22:37:30 +02:00
default = { };
description = ''
Unified configuration of interfaces adapted to DGNum usage:
- each interfaces have only one logical subinterface;
- enabling ethernet-switching also enable RSTP;
- automatic enabling interface and relevant config family when configuring;
- allows enabling PoE along other configurations.
'';
2024-08-31 22:37:30 +02:00
};
config = {
interfaces = mapAttrs (_: intf: {
inherit (intf) enable;
unit."0".family = {
inherit (intf) inet inet6;
2024-12-13 14:37:43 +01:00
ethernet-switching = mkIf intf.ethernet-switching.enable (
removeAttrs intf.ethernet-switching [ "rstp" ]
);
2024-08-31 22:37:30 +02:00
};
}) cfg;
poe.interfaces = filterAttrs (
name: _: config.netconf.mandatoryInterfaces.${name}.supportPoE or false
) (mapAttrs (_: intf: { enable = intf.poe; }) cfg);
2024-12-02 15:45:56 +01:00
protocols.rstp = attrNames (filterAttrs (_: intf: intf.ethernet-switching.rstp) cfg);
2024-08-31 22:37:30 +02:00
};
}