From 621d725af39f89f6974e68619e20424c42b2054a Mon Sep 17 00:00:00 2001 From: oddlama Date: Thu, 14 Mar 2024 16:49:56 +0100 Subject: [PATCH] refactor: modularize topology config --- flake.nix | 18 +++++-- topology/default.nix | 14 +++++- topology/modules/default.nix | 49 +++++++++++++++++++ topology/modules/renderers/d2/default.nix | 28 +++++++++++ .../{ => modules}/renderers/d2/network.nix | 2 +- topology/renderers/d2/default.nix | 5 -- 6 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 topology/modules/default.nix create mode 100644 topology/modules/renderers/d2/default.nix rename topology/{ => modules}/renderers/d2/network.nix (98%) delete mode 100644 topology/renderers/d2/default.nix diff --git a/flake.nix b/flake.nix index 79383ba..7a8a3ba 100644 --- a/flake.nix +++ b/flake.nix @@ -175,10 +175,20 @@ }; # XXX: WIP: only testing - topology = import ./topology { - inherit pkgs; - nixosConfigurations = self.nodes; - }; + topology = + import ./topology inputs + /* + <-- move into topology flake + */ + { + inherit pkgs; + modules = [ + { + renderer = "d2"; + nixosConfigurations = self.nodes; + } + ]; + }; # For each major system, we provide a customized installer image that # has ssh and some other convenience stuff preconfigured. diff --git a/topology/default.nix b/topology/default.nix index ffff00e..810011a 100644 --- a/topology/default.nix +++ b/topology/default.nix @@ -1,2 +1,12 @@ -{renderer ? "d2", ...} @ attrs: -import ./renderers/${renderer} attrs +inputs: { + pkgs, + modules ? [], +}: +inputs.nixpkgs.lib.evalModules { + prefix = ["topology"]; + modules = [./modules] ++ modules; + specialArgs = { + modulesPath = builtins.toString ./modules; + inherit pkgs; + }; +} diff --git a/topology/modules/default.nix b/topology/modules/default.nix new file mode 100644 index 0000000..8daf1e3 --- /dev/null +++ b/topology/modules/default.nix @@ -0,0 +1,49 @@ +{ + lib, + config, + ... +}: let + inherit + (lib) + attrNames + filterAttrs + literalExpression + mkDefault + mkIf + mkOption + types + ; + + availableRenderers = attrNames (filterAttrs (_: v: v == "directory") (builtins.readDir ./renderers)); +in { + imports = map (x: ./renderers/${x}) (attrNames (builtins.readDir ./renderers)); + + options = { + nixosConfigurations = mkOption { + description = '' + The list of nixos configurations to process for topology rendering. + All of these must include the relevant nixos topology module. + ''; + type = types.unspecified; + }; + + renderer = mkOption { + description = "Which renderer to use for the default output. Availble options: ${toString availableRenderers}"; + type = types.nullOr (types.enum availableRenderers); + default = "d2"; + }; + + output = mkOption { + description = "The derivation containing the rendered output"; + type = types.path; + readOnly = true; + defaultText = literalExpression ''config.renderers.${config.renderer}.output''; + }; + }; + + config = { + output = + mkIf (config.renderer != null) + (mkDefault config.renderers.${config.renderer}.output); + }; +} diff --git a/topology/modules/renderers/d2/default.nix b/topology/modules/renderers/d2/default.nix new file mode 100644 index 0000000..359435c --- /dev/null +++ b/topology/modules/renderers/d2/default.nix @@ -0,0 +1,28 @@ +{ + lib, + config, + pkgs, + ... +}: let + inherit + (lib) + mkOption + types + ; +in { + options.renderers.d2 = { + output = mkOption { + description = "The derivation containing the rendered output"; + type = types.path; + readOnly = true; + }; + }; + + config.renderers.d2.output = pkgs.runCommand "build-d2-topology" {} '' + mkdir -p $out + cp ${import ./network.nix { + inherit pkgs; + inherit (config) nixosConfigurations; + }} $out/network.d2 + ''; +} diff --git a/topology/renderers/d2/network.nix b/topology/modules/renderers/d2/network.nix similarity index 98% rename from topology/renderers/d2/network.nix rename to topology/modules/renderers/d2/network.nix index 7f40741..a9eab23 100644 --- a/topology/renderers/d2/network.nix +++ b/topology/modules/renderers/d2/network.nix @@ -76,6 +76,6 @@ d2ForNodes = mapAttrs' (_: node: nameValuePair node.config.topology.id (toD2 node)) nodesById; in - pkgs.writeText "topology.d2" '' + pkgs.writeText "network.d2" '' ${concatLines (map (x: d2ForNodes.${x}) (attrNames rootNodes))} '' diff --git a/topology/renderers/d2/default.nix b/topology/renderers/d2/default.nix deleted file mode 100644 index 0108a06..0000000 --- a/topology/renderers/d2/default.nix +++ /dev/null @@ -1,5 +0,0 @@ -{pkgs, ...} @ attrs: -pkgs.runCommand "build-d2-topology" {} '' - mkdir -p $out - cp ${import ./network.nix attrs} $out/network.d2 -''