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

View file

@ -1,6 +1,7 @@
{ {
lib, lib,
config, config,
xml,
... ...
}: }:
let let
@ -10,7 +11,6 @@ let
; ;
inherit (lib.types) inherit (lib.types)
bool bool
str
attrsOf attrsOf
submodule submodule
; ;
@ -33,9 +33,8 @@ in
./system.nix ./system.nix
]; ];
options = { options = {
netconf.xmls.configuration = mkOption { netconf.xml = mkOption {
type = str; type = xml.type;
readOnly = true;
description = '' description = ''
The full configuration to send to a JunOS. The full configuration to send to a JunOS.
''; '';
@ -65,13 +64,4 @@ in
mkIntf = _: _: { }; mkIntf = _: _: { };
in in
mapAttrs mkIntf config.netconf.mandatoryInterfaces; 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 let
inherit (lib) inherit (lib)
mkEnableOption mkEnableOption
mkOption mkOption
optionalString optionalString
mapAttrsToList mapAttrsToList
mkMerge
mkIf
; ;
inherit (lib.types) inherit (lib.types)
enum enum
@ -19,7 +26,6 @@ let
interface = interface =
{ name, config, ... }: { name, config, ... }:
let let
intf-name = name;
unit = unit =
{ name, config, ... }: { name, config, ... }:
{ {
@ -74,46 +80,36 @@ let
}; };
}; };
xml = mkOption { xml = mkOption {
type = str; type = xml.type;
visible = false; visible = false;
readOnly = true; readOnly = true;
}; };
}; };
config.xml = config.xml =
let let
members = map ( eth = mkIf config.family.ethernet-switching.enable {
vlan: "<members>${builtins.toString vlan}</members>" family.ethernet-switching = {
) config.family.ethernet-switching.vlans; interface-mode = config.family.ethernet-switching.interface-mode;
eth = optionalString config.family.ethernet-switching.enable '' vlan.members = map toString config.family.ethernet-switching.vlans;
<ethernet-switching> storm-control.profile-name = "default";
<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>
'';
addr4 = map (addr: "<name>${addr}</name>") config.family.inet.addresses; addr4 = map (name: { inherit name; }) config.family.inet.addresses;
inet = optionalString config.family.inet.enable '' inet = mkIf config.family.inet.enable { family.inet.address = addr4; };
<inet>
<address>${builtins.concatStringsSep "" addr4}</address>
</inet>
'';
addr6 = map (addr: "<name>${addr}</name>") config.family.inet6.addresses; addr6 = map (name: { inherit name; }) config.family.inet6.addresses;
inet6 = optionalString config.family.inet6.enable '' inet6 = mkIf config.family.inet6.enable { family.inet6.address = addr6; };
<inet6>
<address>${builtins.concatStringsSep "" addr6}</address>
</inet6>
'';
in in
'' mkMerge [
<unit> {
<name>${name}</name> inherit name;
${optionalString (!config.enable) "<disable/>"} }
<family> (mkIf (!config.enable) { disable = { }; })
${eth}${inet}${inet6} eth
</family> inet
</unit>''; inet6
];
}; };
in in
{ {
@ -127,22 +123,21 @@ let
''; '';
}; };
xml = mkOption { xml = mkOption {
type = str; type = xml.type;
visible = false; visible = false;
readOnly = true; readOnly = true;
}; };
}; };
config.xml = config.xml =
let let
units = mapAttrsToList (_: unit: unit.xml) config.unit; unit = mapAttrsToList (_: unit: unit.xml) config.unit;
in in
'' mkMerge [
<interface> {
<name>${name}</name> inherit name unit;
${optionalString (!config.enable) "<disable/>"} }
${builtins.concatStringsSep "" units} (mkIf (!config.enable) { disable = { }; })
</interface> ];
'';
}; };
in in
{ {
@ -153,15 +148,9 @@ in
The interfaces configuration. The interfaces configuration.
''; '';
}; };
netconf.xmls.interfaces = mkOption {
type = str;
visible = false;
readOnly = true;
};
}; };
config.netconf.xmls.interfaces = '' config.netconf.xml.interfaces = {
<interfaces operation="replace"> "@operation" = "replace";
${builtins.concatStringsSep "" (mapAttrsToList (_: intf: intf.xml) config.interfaces)} interface = mapAttrsToList (_: intf: intf.xml) config.interfaces;
</interfaces> };
'';
} }

View file

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

View file

@ -2,7 +2,6 @@
let let
inherit (lib) inherit (lib)
mkOption mkOption
concatStringsSep
; ;
inherit (lib.types) inherit (lib.types)
listOf listOf
@ -17,21 +16,9 @@ in
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 {
type = str;
visible = false;
readOnly = true;
};
}; };
config.netconf.xmls.protocols = config.netconf.xml.protocols.rstp = {
let "@operation" = "replace";
rstps = map (intf: "<interface><name>${intf}</name></interface>") config.protocols.rstp; interface = map (name: { inherit name; }) config.protocols.rstp;
in };
''
<protocols>
<rstp operation="replace">
${concatStringsSep "" rstps}
</rstp>
</protocols>
'';
} }

View file

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

View file

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