mirror of
https://github.com/oddlama/nix-config.git
synced 2025-10-11 07:10:39 +02:00
refactor: modularize topology config
This commit is contained in:
parent
ac18f2d07d
commit
621d725af3
6 changed files with 104 additions and 12 deletions
18
flake.nix
18
flake.nix
|
@ -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.
|
||||||
|
|
|
@ -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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
49
topology/modules/default.nix
Normal file
49
topology/modules/default.nix
Normal 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);
|
||||||
|
};
|
||||||
|
}
|
28
topology/modules/renderers/d2/default.nix
Normal file
28
topology/modules/renderers/d2/default.nix
Normal 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
|
||||||
|
'';
|
||||||
|
}
|
|
@ -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))}
|
||||||
''
|
''
|
|
@ -1,5 +0,0 @@
|
||||||
{pkgs, ...} @ attrs:
|
|
||||||
pkgs.runCommand "build-d2-topology" {} ''
|
|
||||||
mkdir -p $out
|
|
||||||
cp ${import ./network.nix attrs} $out/network.d2
|
|
||||||
''
|
|
Loading…
Add table
Add a link
Reference in a new issue