1
1
Fork 1
mirror of https://github.com/oddlama/nix-config.git synced 2025-10-10 23:00:39 +02:00

chore: PoC to generate d2 network graph

This commit is contained in:
oddlama 2024-01-05 12:35:55 +01:00
parent e1231f8da1
commit 490e2ce1a5
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A
3 changed files with 99 additions and 0 deletions

View file

@ -152,6 +152,18 @@
nixosConfigurationsMinimal
;
# XXX: WIP: only testing
d2diag = let
inherit
(nixpkgs.lib)
attrValues
concatLines
;
in
self.pkgs.x86_64-linux.writeText "test.d2" (
concatLines (map (x: x.config.d2diag.text) (attrValues self.nixosConfigurations))
);
# All nixosSystem instanciations are collected here, so that we can refer
# to any system via nodes.<name>
nodes = self.nixosConfigurations // self.guestConfigs;

85
modules/d2diag.nix Normal file
View file

@ -0,0 +1,85 @@
{
config,
lib,
nodes,
...
}: let
inherit
(lib)
attrNames
concatMap
getAttrFromPath
mkMerge
mkOption
optionals
types
;
nodeName = config.node.name;
in {
options.d2diag.text = mkOption {
# TODO readonly, _text
description = "TODO";
type = types.lines;
};
options.d2diag.services = mkOption {
description = "TODO";
type = types.attrsOf (types.submodule {
options = {
};
});
};
config = {
d2diag.text =
''
${nodeName}: ${nodeName} {
disks: Disks {
shape: sql_table
${lib.concatLines (map (x: "${x}: 8TB") (lib.attrNames config.disko.devices.disk))}
}
net: Interfaces {
shape: sql_table
${lib.concatLines (lib.mapAttrsToList (n: v: ''${n}: ${v.mac}'') (config.repo.secrets.local.networking.interfaces or {lan.mac = "?";}))}
}
''
+ (lib.optionalString (config.guests != {}) ''
guests: {
${
lib.concatLines (
lib.flip lib.mapAttrsToList config.guests (
guestName: guestDef:
(
if guestDef.backend == "microvm"
then config.microvm.vms.${guestName}.config
else config.containers.${guestName}.nixosConfiguration
)
.config
.d2diag
.text
)
)
}
}
${
lib.concatLines (
lib.flip lib.mapAttrsToList config.guests (
guestName: guestDef: "net.lan -> guests.${(
if guestDef.backend == "microvm"
then config.microvm.vms.${guestName}.config
else config.containers.${guestName}.nixosConfiguration
)
.config
.node
.name}.net.lan"
)
)
}
'')
+ ''
}
'';
};
}

View file

@ -40,6 +40,8 @@
./telegraf.nix
./wireguard-proxy.nix
./wireguard.nix
./d2diag.nix
];
nixpkgs.overlays = [