Compare commits

...

1 commit

Author SHA1 Message Date
5d32574fb9
broken! feat(xml)!: use xml generator 2024-12-13 16:03:36 +01:00
9 changed files with 221 additions and 174 deletions

View file

@ -1,13 +1,13 @@
{
pkgs ? (import <nixpkgs> { }),
}:
let
sources = import ./npins;
pkgs = import sources.kat-pkgs { };
lib = pkgs.lib;
hive_mod =
{
lib,
config,
name,
xml,
...
}:
let
@ -32,21 +32,17 @@ let
};
};
config.deployment = rec {
rpc = pkgs.writeText "config-${name}_rpc.xml" ''
<rpc>
<edit-config>
<config>
${config.netconf.xmls.configuration}
</config>
<target>
<candidate/>
</target>
</edit-config>
</rpc>
<rpc>
<commit/>
</rpc>
'';
rpc = xml.generate "config-${name}_rpc.xml" {
rpc = [
{
edit-config = {
config.configuration = config.netconf.xml;
target.candidate = "";
};
}
{ commit = { }; }
];
};
cmd = pkgs.writeShellApplication {
name = "deploy-${name}.sh";
runtimeInputs = with pkgs; [ openssh ];
@ -60,6 +56,7 @@ let
cfg = pkgs.lib.evalModules {
specialArgs = {
inherit name;
xml = pkgs.formats.xml { };
};
modules = [
./junos

View file

@ -1,6 +1,7 @@
{
lib,
config,
xml,
...
}:
let
@ -10,7 +11,6 @@ let
;
inherit (lib.types)
bool
str
attrsOf
submodule
;
@ -33,9 +33,8 @@ in
./system.nix
];
options = {
netconf.xmls.configuration = mkOption {
type = str;
readOnly = true;
netconf.xml = mkOption {
type = xml.type;
description = ''
The full configuration to send to a JunOS.
'';
@ -65,13 +64,4 @@ in
mkIntf = _: _: { };
in
mapAttrs mkIntf config.netconf.mandatoryInterfaces;
config.netconf.xmls.configuration = ''
<configuration>
${config.netconf.xmls.system}
${config.netconf.xmls.interfaces}
${config.netconf.xmls.protocols}
${config.netconf.xmls.vlans}
${config.netconf.xmls.poe}
</configuration>
'';
}

View file

@ -1,10 +1,17 @@
{ lib, config, ... }:
{
lib,
config,
xml,
...
}:
let
inherit (lib)
mkEnableOption
mkOption
optionalString
mapAttrsToList
mkMerge
mkIf
;
inherit (lib.types)
enum
@ -19,7 +26,6 @@ let
interface =
{ name, config, ... }:
let
intf-name = name;
unit =
{ name, config, ... }:
{
@ -74,46 +80,36 @@ let
};
};
xml = mkOption {
type = str;
type = xml.type;
visible = false;
readOnly = true;
};
};
config.xml =
let
members = map (
vlan: "<members>${builtins.toString vlan}</members>"
) config.family.ethernet-switching.vlans;
eth = optionalString config.family.ethernet-switching.enable ''
<ethernet-switching>
<interface-mode>${config.family.ethernet-switching.interface-mode}</interface-mode>
<vlan>${builtins.concatStringsSep "" members}</vlan>
<storm-control><profile-name>default</profile-name></storm-control>
</ethernet-switching>
'';
eth = mkIf config.family.ethernet-switching.enable {
family.ethernet-switching = {
interface-mode = config.family.ethernet-switching.interface-mode;
vlan.members = map toString config.family.ethernet-switching.vlans;
storm-control.profile-name = "default";
};
};
addr4 = map (addr: "<name>${addr}</name>") config.family.inet.addresses;
inet = optionalString config.family.inet.enable ''
<inet>
<address>${builtins.concatStringsSep "" addr4}</address>
</inet>
'';
addr4 = map (name: { inherit name; }) config.family.inet.addresses;
inet = mkIf config.family.inet.enable { family.inet.address = addr4; };
addr6 = map (addr: "<name>${addr}</name>") config.family.inet6.addresses;
inet6 = optionalString config.family.inet6.enable ''
<inet6>
<address>${builtins.concatStringsSep "" addr6}</address>
</inet6>
'';
addr6 = map (name: { inherit name; }) config.family.inet6.addresses;
inet6 = mkIf config.family.inet6.enable { family.inet6.address = addr6; };
in
''
<unit>
<name>${name}</name>
${optionalString (!config.enable) "<disable/>"}
<family>
${eth}${inet}${inet6}
</family>
</unit>'';
mkMerge [
{
inherit name;
}
(mkIf (!config.enable) { disable = { }; })
eth
inet
inet6
];
};
in
{
@ -127,22 +123,21 @@ let
'';
};
xml = mkOption {
type = str;
type = xml.type;
visible = false;
readOnly = true;
};
};
config.xml =
let
units = mapAttrsToList (_: unit: unit.xml) config.unit;
unit = mapAttrsToList (_: unit: unit.xml) config.unit;
in
''
<interface>
<name>${name}</name>
${optionalString (!config.enable) "<disable/>"}
${builtins.concatStringsSep "" units}
</interface>
'';
mkMerge [
{
inherit name unit;
}
(mkIf (!config.enable) { disable = { }; })
];
};
in
{
@ -153,15 +148,9 @@ in
The interfaces configuration.
'';
};
netconf.xmls.interfaces = mkOption {
type = str;
visible = false;
readOnly = true;
};
};
config.netconf.xmls.interfaces = ''
<interfaces operation="replace">
${builtins.concatStringsSep "" (mapAttrsToList (_: intf: intf.xml) config.interfaces)}
</interfaces>
'';
config.netconf.xml.interfaces = {
"@operation" = "replace";
interface = mapAttrsToList (_: intf: intf.xml) config.interfaces;
};
}

View file

@ -1,13 +1,13 @@
{ lib, config, ... }:
{ lib, config, xml, ... }:
let
inherit (lib)
mkOption
mkEnableOption
mapAttrsToList
optionalString
mkIf
mkMerge
;
inherit (lib.types)
str
attrsOf
submodule
;
@ -18,14 +18,15 @@ let
options = {
enable = mkEnableOption "the PoE for this interface";
xml = mkOption {
type = str;
type = xml.type;
visible = false;
readOnly = true;
};
};
config.xml = ''
<interface><name>${name}</name>${optionalString (!config.enable) "<disable/>"}</interface>
'';
config.xml = mkMerge [
{ inherit name; }
(mkIf (!config.enable) { disable = { }; })
];
};
in
{
@ -37,15 +38,9 @@ in
PoE configuration of interfaces.
'';
};
netconf.xmls.poe = mkOption {
type = str;
visible = false;
readOnly = true;
};
};
config.netconf.xmls.poe = ''
<poe operation="replace">
${builtins.concatStringsSep "" (mapAttrsToList (_: intf: intf.xml) config.poe.interfaces)}
</poe>
'';
config.netconf.xml.poe = {
"@operation" = "replace";
interface = mapAttrsToList (_: intf: intf.xml) config.poe.interfaces;
};
}

View file

@ -2,7 +2,6 @@
let
inherit (lib)
mkOption
concatStringsSep
;
inherit (lib.types)
listOf
@ -17,21 +16,9 @@ in
List of interfaces on which Rapid Spanning Tree Protocol should be enabled.
'';
};
netconf.xmls.protocols = mkOption {
type = str;
visible = false;
readOnly = true;
};
};
config.netconf.xmls.protocols =
let
rstps = map (intf: "<interface><name>${intf}</name></interface>") config.protocols.rstp;
in
''
<protocols>
<rstp operation="replace">
${concatStringsSep "" rstps}
</rstp>
</protocols>
'';
config.netconf.xml.protocols.rstp = {
"@operation" = "replace";
interface = map (name: { inherit name; }) config.protocols.rstp;
};
}

View file

@ -6,7 +6,6 @@ let
length
hasPrefix
filter
concatStrings
concatStringsSep
;
inherit (lib.types)
@ -50,39 +49,34 @@ in
};
};
};
netconf.xmls.system = mkOption {
type = str;
visible = false;
readOnly = true;
};
};
config.netconf.xmls.system =
config.netconf.xml.system =
let
ssh-keys1 = map (splitString " ") config.system.root-authentication.ssh-keys;
ssh-keys2 = map (key: if length key < 3 then key ++ [ "foo@bar" ] else key) ssh-keys1;
ssh-keys = map (concatStringsSep " ") ssh-keys2;
edsca = map (key: "<ssh-edsca><name>${key}</name></ssh-edsca>") (
filter (hasPrefix "ssh-edsca ") ssh-keys
);
rsa = map (key: "<ssh-rsa><name>${key}</name></ssh-rsa>") (filter (hasPrefix "ssh-rsa ") ssh-keys);
ed25519 = map (key: "<ssh-ed25519><name>${key}</name></ssh-ed25519>") (
filter (hasPrefix "ssh-ed25519 ") ssh-keys
);
ssh-edsca = map (name: { inherit name; }) (filter (hasPrefix "ssh-edsca ") ssh-keys);
ssh-rsa = map (name: { inherit name; }) (filter (hasPrefix "ssh-rsa ") ssh-keys);
ssh-ed25519 = map (name: { inherit name; }) (filter (hasPrefix "ssh-ed25519 ") ssh-keys);
in
''
<system>
<host-name operation="replace">${config.system.host-name}</host-name>
<root-authentication operation="replace">
<encrypted-password>${config.system.root-authentication.hashedPasswd}</encrypted-password>
${concatStrings (edsca ++ rsa ++ ed25519)}
</root-authentication>
<services operation="replace">
<ssh><root-login>${config.system.services.ssh.root-login}</root-login></ssh>
<netconf>
<ssh><port>${toString config.system.services.netconf.port}</port></ssh>
<rfc-compliant/><yang-compliant/>
</netconf>
</services>
</system>
'';
{
host-name = {
"@operation" = "replace";
"#text" = config.system.host-name;
};
root-authentication = {
"@operation" = "replace";
encrypted-password = config.system.root-authentication.hashedPasswd;
inherit ssh-edsca ssh-rsa ssh-ed25519;
};
services = {
"@operation" = "replace";
ssh.root-login = config.system.services.ssh.root-login;
netconf = {
ssh.port = config.system.services.netconf.port;
rfc-compliant = {};
yang-compliant = {};
};
};
};
}

View file

@ -1,11 +1,11 @@
{ lib, config, ... }:
{ lib, config, xml, ... }:
let
inherit (lib)
mkOption
optionalString
assertMsg
concatStringsSep
mapAttrsToList
mkMerge
mkIf
;
inherit (lib.types)
nullOr
@ -80,30 +80,28 @@ let
'';
};
xml = mkOption {
type = str;
type = xml.type;
readOnly = true;
visible = false;
};
};
config.xml =
let
id = optionalString (!isNull config.id) "<vlan-id>${toString config.id}</vlan-id>";
id-list = concatStringsSep "" (
map (vlan: "<vlan-id-list>${toString vlan}</vlan-id-list>") config.id-list
);
l3-intf = optionalString (
!isNull config.l3-interface
) "<l3-interface>${config.l3-interface}</l3-interface>";
id = mkIf (!isNull config.id) { vlan-id = toString config.id; };
id-list = mkIf (config.id-list != [ ]) {
vlan-id-list = map toString config.id-list;
};
l3-intf = mkIf (!isNull config.l3-interface) { inherit (config) l3-interface; };
in
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}
</vlan>
'';
mkMerge [
{ inherit name; }
id
id-list
l3-intf
];
};
in
{
@ -115,15 +113,9 @@ in
instead of just using their IDs.
'';
};
netconf.xmls.vlans = mkOption {
type = str;
visible = false;
readOnly = true;
};
};
config.netconf.xmls.vlans = ''
<vlans operation="replace">
${builtins.concatStringsSep "" (mapAttrsToList (_: vlan: vlan.xml) config.vlans)}
</vlans>
'';
config.netconf.xml.vlans = {
"@operation" = "replace";
vlan = mapAttrsToList (_: vlan: vlan.xml) config.vlans;
};
}

81
npins/default.nix Normal file
View file

@ -0,0 +1,81 @@
# Generated by npins. Do not modify; will be overwritten regularly
let
data = builtins.fromJSON (builtins.readFile ./sources.json);
version = data.version;
mkSource =
spec:
assert spec ? type;
let
path =
if spec.type == "Git" then
mkGitSource spec
else if spec.type == "GitRelease" then
mkGitSource spec
else if spec.type == "PyPi" then
mkPyPiSource spec
else if spec.type == "Channel" then
mkChannelSource spec
else
builtins.throw "Unknown source type ${spec.type}";
in
spec // { outPath = path; };
mkGitSource =
{
repository,
revision,
url ? null,
hash,
branch ? null,
...
}:
assert repository ? type;
# At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
# In the latter case, there we will always be an url to the tarball
if url != null then
(builtins.fetchTarball {
inherit url;
sha256 = hash; # FIXME: check nix version & use SRI hashes
})
else
assert repository.type == "Git";
let
urlToName =
url: rev:
let
matched = builtins.match "^.*/([^/]*)(\\.git)?$" repository.url;
short = builtins.substring 0 7 rev;
appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
in
"${if matched == null then "source" else builtins.head matched}${appendShort}";
name = urlToName repository.url revision;
in
builtins.fetchGit {
url = repository.url;
rev = revision;
inherit name;
allRefs = true;
# hash = hash;
};
mkPyPiSource =
{ url, hash, ... }:
builtins.fetchurl {
inherit url;
sha256 = hash;
};
mkChannelSource =
{ url, hash, ... }:
builtins.fetchTarball {
inherit url;
sha256 = hash;
};
in
if version == 3 then
builtins.mapAttrs (_: mkSource) data.pins
else
throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"

22
npins/sources.json Normal file
View file

@ -0,0 +1,22 @@
{
"pins": {
"kat-pkgs": {
"type": "Git",
"repository": {
"type": "Git",
"url": "https://git.dgnum.eu/lbailly/kat-pkgs.git"
},
"branch": "master",
"revision": "191cb1dacb273078455b86006941386642681644",
"url": null,
"hash": "14rwcg92mnyb0azac27v05k72lm62pgl43cqj0np711sssjkmhv1"
},
"nixpkgs": {
"type": "Channel",
"name": "nixpkgs-unstable",
"url": "https://releases.nixos.org/nixpkgs/nixpkgs-25.05pre720687.5a48e3c2e435/nixexprs.tar.xz",
"hash": "06g8b0ga935dnziyzhxznwcx1vb2clc84hcxwrcqb26lgjgwsgbf"
}
},
"version": 3
}