refactor: modularize topology config

This commit is contained in:
oddlama 2024-03-14 16:49:56 +01:00
parent ac18f2d07d
commit 621d725af3
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A
6 changed files with 104 additions and 12 deletions

View file

@ -175,10 +175,20 @@
}; };
# XXX: WIP: only testing # XXX: WIP: only testing
topology = import ./topology { topology =
inherit pkgs; import ./topology inputs
nixosConfigurations = self.nodes; /*
}; <-- move into topology flake
*/
{
inherit pkgs;
modules = [
{
renderer = "d2";
nixosConfigurations = self.nodes;
}
];
};
# For each major system, we provide a customized installer image that # For each major system, we provide a customized installer image that
# has ssh and some other convenience stuff preconfigured. # has ssh and some other convenience stuff preconfigured.

View file

@ -1,2 +1,12 @@
{renderer ? "d2", ...} @ attrs: inputs: {
import ./renderers/${renderer} attrs pkgs,
modules ? [],
}:
inputs.nixpkgs.lib.evalModules {
prefix = ["topology"];
modules = [./modules] ++ modules;
specialArgs = {
modulesPath = builtins.toString ./modules;
inherit pkgs;
};
}

View file

@ -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);
};
}

View file

@ -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
'';
}

View file

@ -76,6 +76,6 @@
d2ForNodes = mapAttrs' (_: node: nameValuePair node.config.topology.id (toD2 node)) nodesById; d2ForNodes = mapAttrs' (_: node: nameValuePair node.config.topology.id (toD2 node)) nodesById;
in in
pkgs.writeText "topology.d2" '' pkgs.writeText "network.d2" ''
${concatLines (map (x: d2ForNodes.${x}) (attrNames rootNodes))} ${concatLines (map (x: d2ForNodes.${x}) (attrNames rootNodes))}
'' ''

View file

@ -1,5 +0,0 @@
{pkgs, ...} @ attrs:
pkgs.runCommand "build-d2-topology" {} ''
mkdir -p $out
cp ${import ./network.nix attrs} $out/network.d2
''