chore: remove with lib;

This commit is contained in:
catvayor 2024-12-13 14:37:43 +01:00
parent 8f58ee21fe
commit b791058a88
Signed by: lbailly
GPG key ID: CE3E645251AC63F3
8 changed files with 171 additions and 66 deletions

View file

@ -10,16 +10,24 @@ let
name, name,
... ...
}: }:
with lib; let
inherit (lib)
mkOption
;
inherit (lib.types)
str
package
;
in
{ {
options.deployment = { options.deployment = {
targetHost = mkOption { type = types.str; }; targetHost = mkOption { type = str; };
rpc = mkOption { rpc = mkOption {
type = types.package; type = package;
readOnly = true; readOnly = true;
}; };
cmd = mkOption { cmd = mkOption {
type = types.package; type = package;
readOnly = true; readOnly = true;
}; };
}; };

View file

@ -1,6 +1,23 @@
{ config, lib, ... }: { config, lib, ... }:
with lib;
let let
inherit (lib)
mkEnableOption
mkOption
filterAttrs
mapAttrs
attrNames
mkIf
;
inherit (lib.types)
nullOr
enum
listOf
either
str
ints
attrsOf
submodule
;
intf-mod = intf-mod =
{ config, ... }: { config, ... }:
{ {
@ -20,12 +37,10 @@ let
defaultText = ''config.ethernet-switching.enable''; defaultText = ''config.ethernet-switching.enable'';
}; };
interface-mode = mkOption { interface-mode = mkOption {
type = types.nullOr ( type = nullOr (enum [
types.enum [
"trunk" "trunk"
"access" "access"
] ]);
);
default = null; default = null;
description = '' description = ''
Mode of operation for vlan addressing of this interface. Mode of operation for vlan addressing of this interface.
@ -35,7 +50,7 @@ let
''; '';
}; };
vlans = mkOption { vlans = mkOption {
type = types.listOf (types.either types.str types.ints.unsigned); type = listOf (either str ints.unsigned);
default = [ ]; default = [ ];
description = '' description = ''
Vlans that can be used on this interface. Vlans that can be used on this interface.
@ -49,7 +64,7 @@ let
defaultText = ''config.inet.addresses != [ ]''; defaultText = ''config.inet.addresses != [ ]'';
}; };
addresses = mkOption { addresses = mkOption {
type = types.listOf types.str; type = listOf str;
default = [ ]; default = [ ];
description = '' description = ''
ipv4 addresses of this interface. ipv4 addresses of this interface.
@ -62,7 +77,7 @@ let
defaultText = ''config.inet6.addresses != [ ]''; defaultText = ''config.inet6.addresses != [ ]'';
}; };
addresses = mkOption { addresses = mkOption {
type = types.listOf types.str; type = listOf str;
default = [ ]; default = [ ];
description = '' description = ''
ipv6 addresses of this interface. ipv6 addresses of this interface.
@ -76,7 +91,7 @@ let
in in
{ {
options.dgn-interfaces = mkOption { options.dgn-interfaces = mkOption {
type = types.attrsOf (types.submodule intf-mod); type = attrsOf (submodule intf-mod);
default = { }; default = { };
description = '' description = ''
Unified configuration of interfaces adapted to DGNum usage: Unified configuration of interfaces adapted to DGNum usage:
@ -91,7 +106,9 @@ in
inherit (intf) enable; inherit (intf) enable;
unit."0".family = { unit."0".family = {
inherit (intf) inet inet6; inherit (intf) inet inet6;
ethernet-switching = mkIf intf.ethernet-switching.enable (removeAttrs intf.ethernet-switching [ "rstp" ]); ethernet-switching = mkIf intf.ethernet-switching.enable (
removeAttrs intf.ethernet-switching [ "rstp" ]
);
}; };
}) cfg; }) cfg;
poe.interfaces = filterAttrs ( poe.interfaces = filterAttrs (

View file

@ -3,11 +3,20 @@
config, config,
... ...
}: }:
with lib;
let let
inherit (lib)
mkOption
mapAttrs
;
inherit (lib.types)
bool
str
attrsOf
submodule
;
mandatory.options = { mandatory.options = {
supportPoE = mkOption { supportPoE = mkOption {
type = types.bool; type = bool;
example = true; example = true;
description = '' description = ''
Wether this interface supports PoE. Wether this interface supports PoE.
@ -25,14 +34,14 @@ in
]; ];
options = { options = {
netconf.xmls.configuration = mkOption { netconf.xmls.configuration = mkOption {
type = types.str; type = str;
readOnly = true; readOnly = true;
description = '' description = ''
The full configuration to send to a JunOS. The full configuration to send to a JunOS.
''; '';
}; };
netconf.mandatoryInterfaces = mkOption { netconf.mandatoryInterfaces = mkOption {
type = types.attrsOf (types.submodule mandatory); type = attrsOf (submodule mandatory);
example = { example = {
"ge-0/0/0" = { "ge-0/0/0" = {
supportPoE = true; supportPoE = true;

View file

@ -1,6 +1,21 @@
{ lib, config, ... }: { lib, config, ... }:
with lib;
let let
inherit (lib)
mkEnableOption
mkOption
optionalString
mapAttrsToList
;
inherit (lib.types)
enum
listOf
either
str
ints
submodule
attrsOf
;
interface = interface =
{ name, config, ... }: { name, config, ... }:
let let
@ -17,8 +32,7 @@ let
ethernet-switching = { ethernet-switching = {
enable = mkEnableOption "the ethernet switching on this logical interface"; enable = mkEnableOption "the ethernet switching on this logical interface";
interface-mode = mkOption { interface-mode = mkOption {
type = type = enum [
types.enum [
"trunk" "trunk"
"access" "access"
]; ];
@ -29,7 +43,7 @@ let
''; '';
}; };
vlans = mkOption { vlans = mkOption {
type = types.listOf (types.either types.str types.ints.unsigned); type = listOf (either str ints.unsigned);
default = [ ]; default = [ ];
description = '' description = ''
Vlans that can be used on this interface. Vlans that can be used on this interface.
@ -41,7 +55,7 @@ let
inet = { inet = {
enable = mkEnableOption "the IPv4 configuration of this logical interface"; enable = mkEnableOption "the IPv4 configuration of this logical interface";
addresses = mkOption { addresses = mkOption {
type = types.listOf types.str; type = listOf str;
default = [ ]; default = [ ];
description = '' description = ''
ipv4 addresses of this interface. ipv4 addresses of this interface.
@ -51,7 +65,7 @@ let
inet6 = { inet6 = {
enable = mkEnableOption "the IPv6 configuration of this logical interface"; enable = mkEnableOption "the IPv6 configuration of this logical interface";
addresses = mkOption { addresses = mkOption {
type = types.listOf types.str; type = listOf str;
default = [ ]; default = [ ];
description = '' description = ''
ipv6 addresses of this interface. ipv6 addresses of this interface.
@ -60,7 +74,7 @@ let
}; };
}; };
xml = mkOption { xml = mkOption {
type = types.str; type = str;
visible = false; visible = false;
readOnly = true; readOnly = true;
}; };
@ -106,21 +120,21 @@ let
options = { options = {
enable = mkEnableOption "this physical interface"; enable = mkEnableOption "this physical interface";
unit = mkOption { unit = mkOption {
type = types.attrsOf (types.submodule unit); type = attrsOf (submodule unit);
default = { }; default = { };
description = '' description = ''
Configuration of the logical interfaces on this physical interface. Configuration of the logical interfaces on this physical interface.
''; '';
}; };
xml = mkOption { xml = mkOption {
type = types.str; type = str;
visible = false; visible = false;
readOnly = true; readOnly = true;
}; };
}; };
config.xml = config.xml =
let let
units = attrsets.mapAttrsToList (_: unit: unit.xml) config.unit; units = mapAttrsToList (_: unit: unit.xml) config.unit;
in in
'' ''
<interface> <interface>
@ -134,20 +148,20 @@ in
{ {
options = { options = {
interfaces = mkOption { interfaces = mkOption {
type = types.attrsOf (types.submodule interface); type = attrsOf (submodule interface);
description = '' description = ''
The interfaces configuration. The interfaces configuration.
''; '';
}; };
netconf.xmls.interfaces = mkOption { netconf.xmls.interfaces = mkOption {
type = types.str; type = str;
visible = false; visible = false;
readOnly = true; readOnly = true;
}; };
}; };
config.netconf.xmls.interfaces = '' config.netconf.xmls.interfaces = ''
<interfaces operation="replace"> <interfaces operation="replace">
${builtins.concatStringsSep "" (attrsets.mapAttrsToList (_: intf: intf.xml) config.interfaces)} ${builtins.concatStringsSep "" (mapAttrsToList (_: intf: intf.xml) config.interfaces)}
</interfaces> </interfaces>
''; '';
} }

View file

@ -1,13 +1,24 @@
{ lib, config, ... }: { lib, config, ... }:
with lib;
let let
inherit (lib)
mkOption
mkEnableOption
mapAttrsToList
optionalString
;
inherit (lib.types)
str
attrsOf
submodule
;
interface-module = interface-module =
{ name, config, ... }: { name, config, ... }:
{ {
options = { options = {
enable = mkEnableOption "the PoE for this interface"; enable = mkEnableOption "the PoE for this interface";
xml = mkOption { xml = mkOption {
type = types.str; type = str;
visible = false; visible = false;
readOnly = true; readOnly = true;
}; };
@ -20,23 +31,21 @@ in
{ {
options = { options = {
poe.interfaces = mkOption { poe.interfaces = mkOption {
type = types.attrsOf (types.submodule interface-module); type = attrsOf (submodule interface-module);
default = { }; default = { };
description = '' description = ''
PoE configuration of interfaces. PoE configuration of interfaces.
''; '';
}; };
netconf.xmls.poe = mkOption { netconf.xmls.poe = mkOption {
type = types.str; type = str;
visible = false; visible = false;
readOnly = true; readOnly = true;
}; };
}; };
config.netconf.xmls.poe = '' config.netconf.xmls.poe = ''
<poe operation="replace"> <poe operation="replace">
${ ${builtins.concatStringsSep "" (mapAttrsToList (_: intf: intf.xml) config.poe.interfaces)}
builtins.concatStringsSep "" (attrsets.mapAttrsToList (_: intf: intf.xml) config.poe.interfaces)
}
</poe> </poe>
''; '';
} }

View file

@ -1,15 +1,24 @@
{ lib, config, ... }: { lib, config, ... }:
with lib; let
inherit (lib)
mkOption
concatStringsSep
;
inherit (lib.types)
listOf
str
;
in
{ {
options = { options = {
protocols.rstp = mkOption { protocols.rstp = mkOption {
type = types.listOf types.str; type = listOf str;
description = '' description = ''
List of interfaces on which Rapid Spanning Tree Protocol should be enabled. List of interfaces on which Rapid Spanning Tree Protocol should be enabled.
''; '';
}; };
netconf.xmls.protocols = mkOption { netconf.xmls.protocols = mkOption {
type = types.str; type = str;
visible = false; visible = false;
readOnly = true; readOnly = true;
}; };

View file

@ -1,26 +1,42 @@
{ lib, config, ... }: { lib, config, ... }:
with lib; let
inherit (lib)
mkOption
splitString
length
hasPrefix
filter
concatStrings
concatStringsSep
;
inherit (lib.types)
str
listOf
enum
port
;
in
{ {
options = { options = {
system = { system = {
host-name = mkOption { host-name = mkOption {
type = types.str; type = str;
description = "The hostname of the switch."; description = "The hostname of the switch.";
}; };
root-authentication = { root-authentication = {
hashedPasswd = mkOption { hashedPasswd = mkOption {
type = types.str; type = str;
description = "Hashed password for root."; description = "Hashed password for root.";
}; };
ssh-keys = mkOption { ssh-keys = mkOption {
type = types.listOf types.str; type = listOf str;
description = "ssh keys for root user."; description = "ssh keys for root user.";
default = [ ]; default = [ ];
}; };
}; };
services = { services = {
ssh.root-login = mkOption { ssh.root-login = mkOption {
type = types.enum [ type = enum [
"allow" "allow"
"deny" "deny"
"deny-password" "deny-password"
@ -28,14 +44,14 @@ with lib;
description = "Login policy to use for root."; description = "Login policy to use for root.";
}; };
netconf.port = mkOption { netconf.port = mkOption {
type = types.port; type = port;
description = "Port to use for netconf."; description = "Port to use for netconf.";
default = 830; default = 830;
}; };
}; };
}; };
netconf.xmls.system = mkOption { netconf.xmls.system = mkOption {
type = types.str; type = str;
visible = false; visible = false;
readOnly = true; readOnly = true;
}; };

View file

@ -1,12 +1,29 @@
{ lib, config, ... }: { lib, config, ... }:
with lib;
let let
inherit (lib)
mkOption
optionalString
assertMsg
concatStringsSep
mapAttrsToList
;
inherit (lib.types)
nullOr
ints
unspecified
listOf
either
submodule
str
attrsOf
;
vlan = vlan =
{ name, config, ... }: { name, config, ... }:
{ {
options = { options = {
id = mkOption { id = mkOption {
type = types.nullOr types.ints.unsigned; type = nullOr ints.unsigned;
default = null; default = null;
description = '' description = ''
The ID of this vlan, `null` means no ID. The ID of this vlan, `null` means no ID.
@ -22,27 +39,30 @@ let
config.__toString = _: "${toString config.begin}-${toString config.end}"; config.__toString = _: "${toString config.begin}-${toString config.end}";
options = { options = {
begin = mkOption { begin = mkOption {
type = types.ints.unsigned; type = ints.unsigned;
visible = false; visible = false;
}; };
end = mkOption { end = mkOption {
type = types.ints.unsigned; type = ints.unsigned;
visible = false; visible = false;
}; };
__toString = mkOption { __toString = mkOption {
visible = false; visible = false;
internal = true; internal = true;
readOnly = true; readOnly = true;
type = types.unspecified; type = unspecified;
}; };
}; };
}; };
in in
types.listOf (types.either types.ints.unsigned (types.submodule range_type)); listOf (either ints.unsigned (submodule range_type));
default = [ ]; default = [ ];
example = [ example = [
42 42
{ begin = 100; end = 200; } {
begin = 100;
end = 200;
}
]; ];
description = '' description = ''
List of IDs or IDs range to classify as this vlan. List of IDs or IDs range to classify as this vlan.
@ -50,7 +70,7 @@ let
''; '';
}; };
l3-interface = mkOption { l3-interface = mkOption {
type = types.nullOr types.str; type = nullOr str;
default = null; default = null;
example = "irb.0"; example = "irb.0";
description = '' description = ''
@ -60,7 +80,7 @@ let
''; '';
}; };
xml = mkOption { xml = mkOption {
type = types.str; type = str;
readOnly = true; readOnly = true;
visible = false; visible = false;
}; };
@ -75,7 +95,10 @@ let
!isNull config.l3-interface !isNull config.l3-interface
) "<l3-interface>${config.l3-interface}</l3-interface>"; ) "<l3-interface>${config.l3-interface}</l3-interface>";
in in
assert assertMsg (config.id == null || config.id-list == [ ]) "vlans.${name}.id and vlans.${name}.id-list are incompatible."; '' assert assertMsg (
config.id == null || config.id-list == [ ]
) "vlans.${name}.id and vlans.${name}.id-list are incompatible.";
''
<vlan> <vlan>
<name>${name}</name> <name>${name}</name>
${id}${id-list}${l3-intf} ${id}${id-list}${l3-intf}
@ -86,21 +109,21 @@ in
{ {
options = { options = {
vlans = mkOption { vlans = mkOption {
type = types.attrsOf (types.submodule vlan); type = attrsOf (submodule vlan);
description = '' description = ''
Named vlans configuration. Allows to name vlans inside interface configuration, Named vlans configuration. Allows to name vlans inside interface configuration,
instead of just using their IDs. instead of just using their IDs.
''; '';
}; };
netconf.xmls.vlans = mkOption { netconf.xmls.vlans = mkOption {
type = types.str; type = str;
visible = false; visible = false;
readOnly = true; readOnly = true;
}; };
}; };
config.netconf.xmls.vlans = '' config.netconf.xmls.vlans = ''
<vlans operation="replace"> <vlans operation="replace">
${builtins.concatStringsSep "" (attrsets.mapAttrsToList (_: vlan: vlan.xml) config.vlans)} ${builtins.concatStringsSep "" (mapAttrsToList (_: vlan: vlan.xml) config.vlans)}
</vlans> </vlans>
''; '';
} }