From 7515d54d9ef899ea6551f58fc1ab92d5ac4da3e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 17 Jun 2025 18:38:49 +0200 Subject: [PATCH] introduce flake parts module for clan nixos tests --- clanServices/wifi/flake-module.nix | 18 ++---- clanServices/wifi/tests/vm/default.nix | 55 ++++++---------- flake.nix | 1 + lib/flake-parts/clan-nixos-test.nix | 90 ++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 48 deletions(-) create mode 100644 lib/flake-parts/clan-nixos-test.nix diff --git a/clanServices/wifi/flake-module.nix b/clanServices/wifi/flake-module.nix index 525b9a2e7..0faea677e 100644 --- a/clanServices/wifi/flake-module.nix +++ b/clanServices/wifi/flake-module.nix @@ -1,6 +1,5 @@ { self, - inputs, lib, ... }: @@ -14,7 +13,7 @@ in wifi = module; }; perSystem = - { pkgs, ... }: + { ... }: { /** 1. Prepare the test vars @@ -23,15 +22,10 @@ in 2. To run the test nix build .#checks.x86_64-linux.hello-service */ - checks = - # Currently we don't support nixos-integration tests on darwin - lib.optionalAttrs (pkgs.stdenv.isLinux) { - wifi-service = import ./tests/vm/default.nix { - inherit module; - inherit inputs pkgs; - clan-core = self; - nixosLib = import (self.inputs.nixpkgs + "/nixos/lib") { }; - }; - }; + clan.nixosTests.wifi-service = { + imports = [ ./tests/vm/default.nix ]; + + clan.modules."@clan/wifi" = module; + }; }; } diff --git a/clanServices/wifi/tests/vm/default.nix b/clanServices/wifi/tests/vm/default.nix index dec7743e0..0efd5e315 100644 --- a/clanServices/wifi/tests/vm/default.nix +++ b/clanServices/wifi/tests/vm/default.nix @@ -1,46 +1,29 @@ { - pkgs, - nixosLib, - clan-core, - module, - ... -}: -nixosLib.runTest ( - { ... }: - { - imports = [ - clan-core.modules.nixosVmTest.clanTest - ]; + name = "wifi-service"; - hostPkgs = pkgs; + clan = { + directory = ./.; + test.useContainers = false; + inventory = { - name = "wifi-service"; + machines.test = { }; - clan = { - directory = ./.; - test.useContainers = false; - modules."@clan/wifi" = module; - inventory = { + instances = { + wg-test-one = { + module.name = "@clan/wifi"; - machines.test = { }; - - instances = { - wg-test-one = { - module.name = "@clan/wifi"; - - roles.default.machines = { - test.settings.networks.one = { }; - }; + roles.default.machines = { + test.settings.networks.one = { }; }; }; }; }; + }; - testScript = '' - start_all() - test.wait_for_unit("NetworkManager.service") - psk = test.succeed("cat /run/NetworkManager/system-connections/one.nmconnection") - assert "password-eins" in psk, "Password is incorrect" - ''; - } -) + testScript = '' + start_all() + test.wait_for_unit("NetworkManager.service") + psk = test.succeed("cat /run/NetworkManager/system-connections/one.nmconnection") + assert "password-eins" in psk, "Password is incorrect" + ''; +} diff --git a/flake.nix b/flake.nix index e490d5b85..7907da570 100644 --- a/flake.nix +++ b/flake.nix @@ -71,6 +71,7 @@ ./flakeModules/demo_iso.nix ./lib/filter-clan-core/flake-module.nix ./lib/flake-module.nix + ./lib/flake-parts/clan-nixos-test.nix ./nixosModules/clanCore/vars/flake-module.nix ./nixosModules/flake-module.nix ./pkgs/flake-module.nix diff --git a/lib/flake-parts/clan-nixos-test.nix b/lib/flake-parts/clan-nixos-test.nix new file mode 100644 index 000000000..7125a3838 --- /dev/null +++ b/lib/flake-parts/clan-nixos-test.nix @@ -0,0 +1,90 @@ +{ + lib, + flake-parts-lib, + self, + inputs, + ... +}: +let + inherit (lib) + mkOption + types + ; + inherit (flake-parts-lib) + mkPerSystemOption + ; + nixosLib = import (inputs.nixpkgs + "/nixos/lib") { }; +in +{ + options = { + perSystem = mkPerSystemOption ( + { config, pkgs, ... }: + let + cfg = config.clan.nixosTests; + in + { + options.clan.nixosTests = mkOption { + description = "Clan NixOS tests configuration"; + type = types.attrsOf types.unspecified; + default = { }; + }; + + config.checks = lib.optionalAttrs (pkgs.stdenv.isLinux) ( + let + # Build all individual vars-check derivations + varsChecks = lib.mapAttrs' ( + name: testModule: + lib.nameValuePair "vars-check-${name}" ( + let + test = nixosLib.runTest ( + { ... }: + { + imports = [ + self.modules.nixosVmTest.clanTest + testModule + ]; + + hostPkgs = pkgs; + } + ); + in + test.config.result.vars-check + ) + ) cfg; + in + lib.mkMerge [ + # Add the VM tests as checks + (lib.mapAttrs ( + _name: testModule: + nixosLib.runTest ( + { ... }: + { + imports = [ + self.modules.nixosVmTest.clanTest + testModule + ]; + + hostPkgs = pkgs; + } + ) + ) cfg) + + # Add a single vars-check that depends on all others XXX if we ever + # optimize buildbot to perform better with many builds we can + # remove this and just run the individual vars-checks to speed up + # parallel evaluation. + (lib.optionalAttrs (varsChecks != {}) { + vars-check = pkgs.runCommand "vars-check-all" { + buildInputs = lib.attrValues varsChecks; + } '' + echo "All vars checks passed:" + ${lib.concatMapStringsSep "\n" (name: "echo ' ✓ ${name}'") (lib.attrNames varsChecks)} + touch $out + ''; + }) + ] + ); + } + ); + }; +}