{ lib, config, ... }:
let
inherit (lib)
mkEnableOption
mkOption
optionalString
mapAttrsToList
;
inherit (lib.types)
enum
listOf
either
str
ints
submodule
attrsOf
;
interface =
{ name, config, ... }:
let
intf-name = name;
unit =
{ name, config, ... }:
{
options = {
enable = mkEnableOption "this logical interface" // {
default = true;
example = false;
};
family = {
ethernet-switching = {
enable = mkEnableOption "the ethernet switching on this logical interface";
interface-mode = mkOption {
type = enum [
"trunk"
"access"
];
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.
'';
};
vlans = mkOption {
type = listOf (either str ints.unsigned);
default = [ ];
description = ''
Vlans that can be used on this interface.
Only one ID should be here for "access" mode of operation.
'';
};
};
#TODO : DHCP
inet = {
enable = mkEnableOption "the IPv4 configuration of this logical interface";
addresses = mkOption {
type = listOf str;
default = [ ];
description = ''
ipv4 addresses of this interface.
'';
};
};
inet6 = {
enable = mkEnableOption "the IPv6 configuration of this logical interface";
addresses = mkOption {
type = listOf str;
default = [ ];
description = ''
ipv6 addresses of this interface.
'';
};
};
};
xml = mkOption {
type = str;
visible = false;
readOnly = true;
};
};
config.xml =
let
members = map (
vlan: "${builtins.toString vlan}"
) config.family.ethernet-switching.vlans;
eth = optionalString config.family.ethernet-switching.enable ''
${config.family.ethernet-switching.interface-mode}
${builtins.concatStringsSep "" members}
default
'';
addr4 = map (addr: "${addr}") config.family.inet.addresses;
inet = optionalString config.family.inet.enable ''
${builtins.concatStringsSep "" addr4}
'';
addr6 = map (addr: "${addr}") config.family.inet6.addresses;
inet6 = optionalString config.family.inet6.enable ''
${builtins.concatStringsSep "" addr6}
'';
in
''
${name}
${optionalString (!config.enable) ""}
${eth}${inet}${inet6}
'';
};
in
{
options = {
enable = mkEnableOption "this physical interface";
unit = mkOption {
type = attrsOf (submodule unit);
default = { };
description = ''
Configuration of the logical interfaces on this physical interface.
'';
};
xml = mkOption {
type = str;
visible = false;
readOnly = true;
};
};
config.xml =
let
units = mapAttrsToList (_: unit: unit.xml) config.unit;
in
''
${name}
${optionalString (!config.enable) ""}
${builtins.concatStringsSep "" units}
'';
};
in
{
options = {
interfaces = mkOption {
type = attrsOf (submodule interface);
description = ''
The interfaces configuration.
'';
};
netconf.xmls.interfaces = mkOption {
type = str;
visible = false;
readOnly = true;
};
};
config.netconf.xmls.interfaces = ''
${builtins.concatStringsSep "" (mapAttrsToList (_: intf: intf.xml) config.interfaces)}
'';
}