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,
...
}:
with lib;
let
inherit (lib)
mkOption
;
inherit (lib.types)
str
package
;
in
{
options.deployment = {
targetHost = mkOption { type = types.str; };
targetHost = mkOption { type = str; };
rpc = mkOption {
type = types.package;
type = package;
readOnly = true;
};
cmd = mkOption {
type = types.package;
type = package;
readOnly = true;
};
};

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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