From 25a864b1e88c8f769930f22f7eeaf69af06c315b Mon Sep 17 00:00:00 2001 From: oddlama Date: Sat, 16 Mar 2024 13:49:59 +0100 Subject: [PATCH] feat(topology): render network addresses --- modules/config/topology.nix | 3 ++ modules/default.nix | 1 + topology/nixos/extractors/disko.nix | 2 - .../nixos/extractors/network-interfaces.nix | 2 - topology/nixos/extractors/node.nix | 2 - topology/nixos/extractors/wireguard.nix | 2 +- topology/options/interfaces.nix | 2 +- topology/topology/renderers/d2/network.nix | 40 ++++++++++++------- 8 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 modules/config/topology.nix delete mode 100644 topology/nixos/extractors/disko.nix delete mode 100644 topology/nixos/extractors/network-interfaces.nix delete mode 100644 topology/nixos/extractors/node.nix diff --git a/modules/config/topology.nix b/modules/config/topology.nix new file mode 100644 index 0000000..df939d5 --- /dev/null +++ b/modules/config/topology.nix @@ -0,0 +1,3 @@ +{config, ...}: { + topology.id = config.node.name; +} diff --git a/modules/default.nix b/modules/default.nix index 22eb8ec..0ceaeed 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -24,6 +24,7 @@ ./config/secrets.nix ./config/ssh.nix ./config/system.nix + ./config/topology.nix ./config/users.nix ./acme-wildcard.nix diff --git a/topology/nixos/extractors/disko.nix b/topology/nixos/extractors/disko.nix deleted file mode 100644 index 2c63c08..0000000 --- a/topology/nixos/extractors/disko.nix +++ /dev/null @@ -1,2 +0,0 @@ -{ -} diff --git a/topology/nixos/extractors/network-interfaces.nix b/topology/nixos/extractors/network-interfaces.nix deleted file mode 100644 index 2c63c08..0000000 --- a/topology/nixos/extractors/network-interfaces.nix +++ /dev/null @@ -1,2 +0,0 @@ -{ -} diff --git a/topology/nixos/extractors/node.nix b/topology/nixos/extractors/node.nix deleted file mode 100644 index 2c63c08..0000000 --- a/topology/nixos/extractors/node.nix +++ /dev/null @@ -1,2 +0,0 @@ -{ -} diff --git a/topology/nixos/extractors/wireguard.nix b/topology/nixos/extractors/wireguard.nix index 9002412..dc17f1f 100644 --- a/topology/nixos/extractors/wireguard.nix +++ b/topology/nixos/extractors/wireguard.nix @@ -73,7 +73,7 @@ in { network = networkId wgName; virtual = true; physicalConnections = flip map connectedPeers (peer: { - node = peer; + node = inputs.self.nodes.${peer}.config.topology.id; interface = (wgCfgOf peer).linkName; }); }; diff --git a/topology/options/interfaces.nix b/topology/options/interfaces.nix index f0d1d92..5f00e72 100644 --- a/topology/options/interfaces.nix +++ b/topology/options/interfaces.nix @@ -89,7 +89,7 @@ in interface: [ { - assertion = config.networks ? ${interface.network}; + assertion = interface.network != null -> config.networks ? ${interface.network}; message = "topology: nodes.${node.id}.interfaces.${interface.id} refers to an unknown network '${interface.network}'"; } ] diff --git a/topology/topology/renderers/d2/network.nix b/topology/topology/renderers/d2/network.nix index 23f2902..2452dda 100644 --- a/topology/topology/renderers/d2/network.nix +++ b/topology/topology/renderers/d2/network.nix @@ -8,6 +8,7 @@ (lib) attrValues concatLines + optionalString ; #toD2 = _nodeName: node: '' @@ -29,25 +30,34 @@ #''; netToD2 = net: '' - ${net.id}: |md - # ${net.name} - ${net.cidrv4} - ${net.cidrv6} - | + ${net.id}: ${net.name} { + info: |md + ${net.cidrv4} + ${net.cidrv6} + | + } ''; - nodeInterfaceToD2 = node: interface: '' - ${node.id}.${interface.id}: |md - ## ${interface.id} - | - - ${node.id}.${interface.id} -> ${interface.network} - ''; + nodeInterfaceToD2 = node: interface: + '' + ${node.id}.${interface.id}: ${interface.id} { + info: |md + ${toString interface.mac} + ${toString interface.addresses} + ${toString interface.gateways} + | + } + '' + + optionalString (interface.network != null) '' + ${node.id}.${interface.id} -- ${interface.network} + ''; + # TODO: deduplicate first + #+ concatLines (flip map interface.physicalConnections (x: '' + # ${node.id}.${interface.id} -- ${x.node}.${x.interface} + #'')); nodeToD2 = node: '' - ${node.id}: |md - # ${node.name} - | + ${node.id}: ${node.name} {} ${concatLines (map (nodeInterfaceToD2 node) (attrValues node.interfaces))} '';