diff --git a/flake.nix b/flake.nix index 557d3a1..98d4faf 100644 --- a/flake.nix +++ b/flake.nix @@ -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. nodes = self.nixosConfigurations // self.guestConfigs; diff --git a/modules/d2diag.nix b/modules/d2diag.nix new file mode 100644 index 0000000..50e0a92 --- /dev/null +++ b/modules/d2diag.nix @@ -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" + ) + ) + } + '') + + '' + } + ''; + }; +} diff --git a/modules/default.nix b/modules/default.nix index 80860e8..87cda06 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -40,6 +40,8 @@ ./telegraf.nix ./wireguard-proxy.nix ./wireguard.nix + + ./d2diag.nix ]; nixpkgs.overlays = [