From b20376f2e49f2c28d6215df4714e6737af3d8cfc Mon Sep 17 00:00:00 2001 From: oddlama Date: Fri, 15 Mar 2024 19:07:25 +0100 Subject: [PATCH] refactor(topology): split node options --- topology/options/devices.nix | 7 -- topology/options/disks.nix | 32 +++++++++ topology/options/firewall.nix | 38 +++++++++++ topology/options/interfaces.nix | 53 +++++++++++++++ topology/options/networks.nix | 7 -- topology/options/nodes.nix | 79 +--------------------- topology/topology/renderers/d2/default.nix | 8 +-- topology/topology/renderers/d2/network.nix | 71 +++---------------- 8 files changed, 138 insertions(+), 157 deletions(-) delete mode 100644 topology/options/devices.nix create mode 100644 topology/options/disks.nix create mode 100644 topology/options/firewall.nix create mode 100644 topology/options/interfaces.nix delete mode 100644 topology/options/networks.nix diff --git a/topology/options/devices.nix b/topology/options/devices.nix deleted file mode 100644 index 6def3d5..0000000 --- a/topology/options/devices.nix +++ /dev/null @@ -1,7 +0,0 @@ -f: { - lib, - config, - ... -}: -f { -} diff --git a/topology/options/disks.nix b/topology/options/disks.nix new file mode 100644 index 0000000..18d7226 --- /dev/null +++ b/topology/options/disks.nix @@ -0,0 +1,32 @@ +f: { + lib, + config, + ... +}: let + inherit + (lib) + mkOption + types + ; +in + f { + options.nodes = mkOption { + type = types.attrsOf (types.submodule { + options = { + disks = mkOption { + default = {}; + type = types.attrsOf (types.submodule (submod: { + options = { + name = mkOption { + description = "The name of this disk"; + default = submod.config._module.args.name; + readOnly = true; + type = types.str; + }; + }; + })); + }; + }; + }); + }; + } diff --git a/topology/options/firewall.nix b/topology/options/firewall.nix new file mode 100644 index 0000000..67e4f4a --- /dev/null +++ b/topology/options/firewall.nix @@ -0,0 +1,38 @@ +f: { + lib, + config, + ... +}: let + inherit + (lib) + mkOption + types + ; +in + f { + options.nodes = mkOption { + type = types.attrsOf (types.submodule { + options = { + firewallRules = mkOption { + description = "TODO"; + default = {}; + type = types.attrsOf (types.submodule (submod: { + options = { + name = mkOption { + description = "The name of this firewall rule"; + type = types.str; + readOnly = true; + default = submod.config._module.args.name; + }; + + contents = mkOption { + description = "A human readable summary of this rule's effects"; + type = types.lines; + }; + }; + })); + }; + }; + }); + }; + } diff --git a/topology/options/interfaces.nix b/topology/options/interfaces.nix new file mode 100644 index 0000000..2f2e3f1 --- /dev/null +++ b/topology/options/interfaces.nix @@ -0,0 +1,53 @@ +f: { + lib, + config, + ... +}: let + inherit + (lib) + mkOption + types + ; +in + f { + options.nodes = mkOption { + type = types.attrsOf (types.submodule { + options = { + interfaces = mkOption { + description = "TODO"; + default = {}; + type = types.attrsOf (types.submodule (submod: { + options = { + name = mkOption { + description = "The name of this interface"; + type = types.str; + readOnly = true; + default = submod.config._module.args.name; + }; + + mac = mkOption { + description = "The MAC address of this interface, if known."; + default = null; + type = types.nullOr types.str; + }; + + addresses = mkOption { + description = "The configured address(es), or a descriptive string (like DHCP)."; + type = types.listOf types.str; + }; + + network = mkOption { + description = '' + The global name of the attached/spanned network. + If this is given, this interface can be shown in the network graph. + ''; + default = null; + type = types.nullOr types.str; + }; + }; + })); + }; + }; + }); + }; + } diff --git a/topology/options/networks.nix b/topology/options/networks.nix deleted file mode 100644 index 6def3d5..0000000 --- a/topology/options/networks.nix +++ /dev/null @@ -1,7 +0,0 @@ -f: { - lib, - config, - ... -}: -f { -} diff --git a/topology/options/nodes.nix b/topology/options/nodes.nix index 27fc70f..5a61f35 100644 --- a/topology/options/nodes.nix +++ b/topology/options/nodes.nix @@ -30,84 +30,11 @@ in }; parent = mkOption { - description = "TODO guests ids (topology.node..id) ensure exists"; - default = []; - type = types.listOf types.str; - }; - - disks = mkOption { - default = {}; - type = types.attrsOf (types.submodule (submod: { - options = { - name = mkOption { - description = "The name of this disk"; - default = submod.config._module.args.name; - readOnly = true; - type = types.str; - }; - }; - })); - }; - - interfaces = mkOption { - description = "TODO"; - default = {}; - type = types.attrsOf (types.submodule (submod: { - options = { - name = mkOption { - description = "The name of this interface"; - type = types.str; - readOnly = true; - default = submod.config._module.args.name; - }; - - mac = mkOption { - description = "The MAC address of this interface, if known."; - default = null; - type = types.nullOr types.str; - }; - - addresses = mkOption { - description = "The configured address(es), or a descriptive string (like DHCP)."; - type = types.listOf types.str; - }; - - network = mkOption { - description = '' - The global name of the attached/spanned network. - If this is given, this interface can be shown in the network graph. - ''; - default = null; - type = types.nullOr types.str; - }; - }; - })); - }; - - firewallRules = mkOption { - description = "TODO"; - default = {}; - type = types.attrsOf (types.submodule (submod: { - options = { - name = mkOption { - description = "The name of this firewall rule"; - type = types.str; - readOnly = true; - default = submod.config._module.args.name; - }; - - contents = mkOption { - description = "A human readable summary of this rule's effects"; - type = types.lines; - }; - }; - })); + description = "The id of the parent node, if this node has a parent."; + default = null; + type = types.nullOr types.str; }; }; })); }; - - config = { - # TODO: assertions = [] - }; } diff --git a/topology/topology/renderers/d2/default.nix b/topology/topology/renderers/d2/default.nix index 359435c..67b4107 100644 --- a/topology/topology/renderers/d2/default.nix +++ b/topology/topology/renderers/d2/default.nix @@ -1,9 +1,8 @@ { lib, - config, pkgs, ... -}: let +} @ args: let inherit (lib) mkOption @@ -20,9 +19,6 @@ in { config.renderers.d2.output = pkgs.runCommand "build-d2-topology" {} '' mkdir -p $out - cp ${import ./network.nix { - inherit pkgs; - inherit (config) nixosConfigurations; - }} $out/network.d2 + cp ${import ./network.nix args} $out/network.d2 ''; } diff --git a/topology/topology/renderers/d2/network.nix b/topology/topology/renderers/d2/network.nix index a9eab23..9b85742 100644 --- a/topology/topology/renderers/d2/network.nix +++ b/topology/topology/renderers/d2/network.nix @@ -1,81 +1,30 @@ { + lib, + config, pkgs, - nixosConfigurations, ... }: let inherit - (pkgs.lib) - any - attrNames - attrValues + (lib) concatLines - concatStringsSep - elem - escapeXML - flip - filterAttrs - imap0 - mapAttrs' - nameValuePair mapAttrsToList - optional - optionalAttrs - optionalString ; - # global = { - # # global entities; - # }; - - # asjson = builtins.toFile "topology.dot" ( - # builtins.toJSON (map (x: x.config.topology) (attrValues nixosConfigurations)) - # ); - - colors.base00 = "#101419"; - colors.base01 = "#171B20"; - colors.base02 = "#21262e"; - colors.base03 = "#242931"; - colors.base03b = "#353c48"; - colors.base04 = "#485263"; - colors.base05 = "#b6beca"; - colors.base06 = "#dee1e6"; - colors.base07 = "#e3e6eb"; - colors.base08 = "#e05f65"; - colors.base09 = "#f9a872"; - colors.base0A = "#f1cf8a"; - colors.base0B = "#78dba9"; - colors.base0C = "#74bee9"; - colors.base0D = "#70a5eb"; - colors.base0E = "#c68aee"; - colors.base0F = "#9378de"; - - nodesById = mapAttrs' (_: node: nameValuePair node.config.topology.id node) nixosConfigurations; - - isGuestOfAny = node: any (x: elem node x.config.topology.guests) (attrValues nodesById); - rootNodes = filterAttrs (n: _: !(isGuestOfAny n)) nodesById; - - toD2 = node: let - topo = node.config.topology; - in '' - ${topo.id}: |md - # ${topo.id} - - ## Guests: - ${concatLines (map (x: "- ${x}") topo.guests)} + toD2 = _nodeName: node: '' + ${node.name}: |md + # ${node.name} ## Disks: - ${concatLines (mapAttrsToList (_: v: "- ${v.name}") topo.disks)} + ${concatLines (mapAttrsToList (_: v: "- ${v.name}") node.disks)} ## Interfaces: - ${concatLines (mapAttrsToList (_: v: "- ${v.name}, mac ${toString v.mac}, addrs ${toString v.addresses}, network ${toString v.network}") topo.interfaces)} + ${concatLines (mapAttrsToList (_: v: "- ${v.name}, mac ${toString v.mac}, addrs ${toString v.addresses}, network ${toString v.network}") node.interfaces)} ## Firewall Zones: - ${concatLines (mapAttrsToList (_: v: "- ${v.name}, mac ${toString v.mac}, addrs ${toString v.addresses}, network ${toString v.network}") topo.firewallRules)} + ${concatLines (mapAttrsToList (_: v: "- ${v.name}, mac ${toString v.mac}, addrs ${toString v.addresses}, network ${toString v.network}") node.firewallRules)} | ''; - - d2ForNodes = mapAttrs' (_: node: nameValuePair node.config.topology.id (toD2 node)) nodesById; in pkgs.writeText "network.d2" '' - ${concatLines (map (x: d2ForNodes.${x}) (attrNames rootNodes))} + ${concatLines (mapAttrsToList toD2 config.nodes)} ''