From f6960010cdbebd80fbd18e8bfcb2107f8e73b20e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicolas=20=E2=80=9CNiols=E2=80=9D=20Jeannerod?=
 <nicolas.jeannerod@moduscreate.com>
Date: Mon, 24 Feb 2025 18:48:05 +0100
Subject: [PATCH] One deployment per machine; bundle test machines

---
 infra/README.md      |  17 ------
 infra/flake-part.nix | 127 ++++++++++++++++++++-----------------------
 2 files changed, 58 insertions(+), 86 deletions(-)

diff --git a/infra/README.md b/infra/README.md
index 830eabc..b34cd10 100644
--- a/infra/README.md
+++ b/infra/README.md
@@ -96,23 +96,6 @@ nixops4 apply
 
 See `infra/proxmox-remove.sh --help`.
 
-## Deployments
-
-default
-:   Contains everything
-
-`git`
-:   Machines hosting our Git infrastructure, eg. Forgejo and its actions runners
-
-`web`
-:   Machines hosting our online content, eg. the website or the wiki
-
-`other`
-:   Machines without a specific purpose
-
-`hans`
-:   Testing machines for Hans's work
-
 ## Machines
 
 These machines are hosted on the Procolix Proxmox instance,
diff --git a/infra/flake-part.nix b/infra/flake-part.nix
index 7e05a81..9b33d1f 100644
--- a/infra/flake-part.nix
+++ b/infra/flake-part.nix
@@ -5,18 +5,12 @@
 }:
 
 let
-  inherit (lib)
-    attrValues
-    concatLists
-    mapAttrs
-    mkOption
-    evalModules
-    ;
-  inherit (lib.attrsets) concatMapAttrs genAttrs;
-
-  addDefaultDeployment =
-    deployments: deployments // { default = concatLists (attrValues deployments); };
+  inherit (lib) mkOption evalModules;
+  inherit (lib.attrsets) genAttrs;
 
+  ## Given a machine's name, make a resource module, except for its missing
+  ## provider. (Depending on the use of that resource, we will provide a
+  ## different one.)
   makeResourceModule = vmName: {
     _module.args = { inherit inputs; };
     imports = [
@@ -26,8 +20,10 @@ let
     fediversityVm.name = vmName;
   };
 
-  makeDeployments = mapAttrs (
-    _: vmNames:
+  ## Given a list of machine names, make a deployment with those machines'
+  ## configurations as resources
+  makeDeployment =
+    vmNames:
     { providers, ... }:
     {
       providers.local = inputs.nixops4.modules.nixops4Provider.local;
@@ -38,8 +34,8 @@ let
           (makeResourceModule vmName)
         ];
       });
-    }
-  );
+    };
+  makeDeployment' = vmName: makeDeployment [ vmName ];
 
   nixops4ResourceNixosMockOptions = {
     ## NOTE: We allow the use of a few options from
@@ -65,65 +61,58 @@ let
       ];
     }).config;
 
-  makeConfigurations = concatMapAttrs (
-    _: vmNames:
-    genAttrs vmNames (
-      vmName:
-      inputs.nixpkgs.lib.nixosSystem {
-        modules = [
-          (makeResourceConfig vmName).nixos.module
-        ];
-      }
-    )
-  );
+  ## Given a VM name, make a NixOS configuration for this machine.
+  makeConfiguration =
+    vmName:
+    inputs.nixpkgs.lib.nixosSystem {
+      modules = [
+        (makeResourceConfig vmName).nixos.module
+      ];
+    };
 
-  makeVmOptions = concatMapAttrs (
-    _: vmNames:
-    genAttrs vmNames (vmName: {
-      inherit ((makeResourceConfig vmName).fediversityVm)
-        proxmox
-        vmId
-        sockets
-        cores
-        memory
-        hostPublicKey
-        unsafeHostPrivateKey
-        ;
-    })
-  );
-
-  machines = {
-    git = [
-      "vm02116"
-    ];
-    web = [ "vm02187" ];
-    other = [
-      "vm02179"
-      "vm02186"
-    ];
-    hans = [
-      "fedi200"
-    ];
-    kiara = [
-      "fedi201"
-    ];
-    test = [
-      "test01"
-      "test02"
-      "test03"
-      "test04"
-      "test05"
-    ];
+  makeVmOptions = vmName: {
+    inherit ((makeResourceConfig vmName).fediversityVm)
+      proxmox
+      vmId
+      sockets
+      cores
+      memory
+      hostPublicKey
+      unsafeHostPrivateKey
+      ;
   };
 
+  machines = [
+    "vm02116"
+    "vm02179"
+    "vm02186"
+    "vm02187"
+
+    "fedi200"
+    "fedi201"
+  ];
+
+  testMachines = [
+    "test01"
+    "test02"
+    "test03"
+    "test04"
+    "test05"
+  ];
+
 in
 {
   flake.lib.makeInstallerIso = import ./makeInstallerIso.nix;
 
-  ## REVIEW: It would probably make more sense to have the VM names as parent of
-  ## the corresponding resource, NixOS configuration, and VM options, rather
-  ## than the contrary.
-  nixops4Deployments = makeDeployments (addDefaultDeployment machines);
-  flake.nixosConfigurations = makeConfigurations machines;
-  flake.vmOptions = makeVmOptions machines;
+  ## - Each normal or test machine gets a NixOS configuration.
+  ## - Each normal or test machine gets a VM options entry.
+  ## - Each normal machine gets a deployment.
+  ## - We add a “default” deployment with all normal machines.
+  ## - We add a “test” deployment with all test machines.
+  nixops4Deployments = genAttrs machines makeDeployment' // {
+    default = makeDeployment machines;
+    test = makeDeployment testMachines;
+  };
+  flake.nixosConfigurations = genAttrs (machines ++ testMachines) makeConfiguration;
+  flake.vmOptions = genAttrs (machines ++ testMachines) makeVmOptions;
 }