From 47101bad5c83c064b8d26906d5510f110f85d9cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tristan=20Dani=C3=ABl=20Maat?= <tm@tlater.net>
Date: Mon, 17 Oct 2022 14:22:08 +0100
Subject: [PATCH 1/2] flake.nix: Move vm out of `nixosConfigurations` so that
 checks work

---
 flake.nix       | 46 +++++++++++++++++-----------------------------
 lib/default.nix | 22 +++++++++++++++++++++-
 2 files changed, 38 insertions(+), 30 deletions(-)

diff --git a/flake.nix b/flake.nix
index 35e3792..811a8c4 100644
--- a/flake.nix
+++ b/flake.nix
@@ -31,35 +31,11 @@
     ##################
     # Configurations #
     ##################
-    nixosConfigurations = let
-      # Modules that should be generic to all systems
-      genericModule = {...}: {
-        imports = [
-          # Inject flake dependencies
-          sops-nix.nixosModules.sops
-          tlaternet-webserver.nixosModules.default
-
-          # Import actual configuration
-          (import ./configuration)
-        ];
-      };
-    in {
+    nixosConfigurations = {
       # The actual system definition
-      tlaternet = nixpkgs.lib.nixosSystem {
+      tlaternet = self.lib.makeNixosSystem {
         inherit system;
-        modules = [
-          genericModule
-          (import ./configuration/hardware-specific/linode)
-        ];
-      };
-
-      # A qemu VM to test the above with
-      vm = nixpkgs.lib.nixosSystem {
-        inherit system;
-        modules = [
-          genericModule
-          (import ./configuration/hardware-specific/vm.nix)
-        ];
+        extraModules = [(import ./configuration/hardware-specific/linode)];
       };
     };
 
@@ -89,7 +65,10 @@
     ####################
     # Helper functions #
     ####################
-    lib = import ./lib {lib = nixpkgs.lib;};
+    lib = import ./lib {
+      inherit nixpkgs sops-nix tlaternet-webserver;
+      lib = nixpkgs.lib;
+    };
 
     ####################
     # VM launch script #
@@ -98,7 +77,16 @@
       inherit (nixpkgs.legacyPackages.${system}) writeShellScript;
     in {
       default = let
-        inherit (self.nixosConfigurations.vm.config.system.build) vm;
+        vm =
+          (self.lib.makeNixosSystem {
+            inherit system;
+            extraModules = [(import ./configuration/hardware-specific/vm.nix)];
+          })
+          .config
+          .system
+          .build
+          .vm;
+
         qemuNetOpts = self.lib.makeQemuNetOpts {
           "2222" = "2222";
           "3080" = "80";
diff --git a/lib/default.nix b/lib/default.nix
index 20d874d..5f6b68f 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -1,4 +1,9 @@
-{lib}: let
+{
+  lib,
+  nixpkgs,
+  sops-nix,
+  tlaternet-webserver,
+}: let
   inherit (lib.attrsets) mapAttrsToList;
   inherit (lib.strings) concatStringsSep;
 in {
@@ -7,4 +12,19 @@ in {
     (mapAttrsToList
       (host: vm: "hostfwd=::${host}-:${vm}")
       portMapping);
+
+  makeNixosSystem = {
+    system,
+    extraModules,
+  }:
+    nixpkgs.lib.nixosSystem {
+      inherit system;
+      modules =
+        [
+          sops-nix.nixosModules.sops
+          tlaternet-webserver.nixosModules.default
+          (import ../configuration)
+        ]
+        ++ extraModules;
+    };
 }

From 59a44261b80edc1934de2d3fd2b5d62dd921eca2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tristan=20Dani=C3=ABl=20Maat?= <tm@tlater.net>
Date: Mon, 17 Oct 2022 14:22:08 +0100
Subject: [PATCH 2/2] flake.nix: Move vm out of `nixosConfigurations` so that
 checks work

---
 flake.nix       | 43 +++++++++++++------------------------------
 lib/default.nix | 22 +++++++++++++++++++++-
 2 files changed, 34 insertions(+), 31 deletions(-)

diff --git a/flake.nix b/flake.nix
index 35e3792..144e69f 100644
--- a/flake.nix
+++ b/flake.nix
@@ -31,35 +31,11 @@
     ##################
     # Configurations #
     ##################
-    nixosConfigurations = let
-      # Modules that should be generic to all systems
-      genericModule = {...}: {
-        imports = [
-          # Inject flake dependencies
-          sops-nix.nixosModules.sops
-          tlaternet-webserver.nixosModules.default
-
-          # Import actual configuration
-          (import ./configuration)
-        ];
-      };
-    in {
+    nixosConfigurations = {
       # The actual system definition
-      tlaternet = nixpkgs.lib.nixosSystem {
+      tlaternet = self.lib.makeNixosSystem {
         inherit system;
-        modules = [
-          genericModule
-          (import ./configuration/hardware-specific/linode)
-        ];
-      };
-
-      # A qemu VM to test the above with
-      vm = nixpkgs.lib.nixosSystem {
-        inherit system;
-        modules = [
-          genericModule
-          (import ./configuration/hardware-specific/vm.nix)
-        ];
+        extraModules = [(import ./configuration/hardware-specific/linode)];
       };
     };
 
@@ -89,7 +65,10 @@
     ####################
     # Helper functions #
     ####################
-    lib = import ./lib {lib = nixpkgs.lib;};
+    lib = import ./lib {
+      inherit nixpkgs sops-nix tlaternet-webserver;
+      lib = nixpkgs.lib;
+    };
 
     ####################
     # VM launch script #
@@ -98,7 +77,11 @@
       inherit (nixpkgs.legacyPackages.${system}) writeShellScript;
     in {
       default = let
-        inherit (self.nixosConfigurations.vm.config.system.build) vm;
+        vm = self.lib.makeNixosSystem {
+          inherit system;
+          extraModules = [(import ./configuration/hardware-specific/vm.nix)];
+        };
+
         qemuNetOpts = self.lib.makeQemuNetOpts {
           "2222" = "2222";
           "3080" = "80";
@@ -110,7 +93,7 @@
         program = builtins.toString (writeShellScript "run-vm" ''
           export QEMU_OPTS="-m 3941 -smp 2 -display curses"
           export QEMU_NET_OPTS="${qemuNetOpts}"
-          "${vm}/bin/run-tlaternet-vm"
+          "${vm.config.system.build.vm}/bin/run-tlaternet-vm"
         '');
       };
 
diff --git a/lib/default.nix b/lib/default.nix
index 20d874d..5f6b68f 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -1,4 +1,9 @@
-{lib}: let
+{
+  lib,
+  nixpkgs,
+  sops-nix,
+  tlaternet-webserver,
+}: let
   inherit (lib.attrsets) mapAttrsToList;
   inherit (lib.strings) concatStringsSep;
 in {
@@ -7,4 +12,19 @@ in {
     (mapAttrsToList
       (host: vm: "hostfwd=::${host}-:${vm}")
       portMapping);
+
+  makeNixosSystem = {
+    system,
+    extraModules,
+  }:
+    nixpkgs.lib.nixosSystem {
+      inherit system;
+      modules =
+        [
+          sops-nix.nixosModules.sops
+          tlaternet-webserver.nixosModules.default
+          (import ../configuration)
+        ]
+        ++ extraModules;
+    };
 }