From 3aa02e85240ad8de59e2e83e9b4a3816cd4a83b6 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 6 May 2024 16:13:38 +0300 Subject: [PATCH 1/4] flake: add machine tests --- flake.nix | 2 ++ flake/tests/checks/homeManagerModule.nix | 28 +++++++++++++++++++++ flake/tests/checks/nixosModule.nix | 19 +++++++++++++++ flake/tests/default.nix | 31 ++++++++++++++++++++++++ flake/tests/profiles/minimal.nix | 17 +++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 flake/tests/checks/homeManagerModule.nix create mode 100644 flake/tests/checks/nixosModule.nix create mode 100644 flake/tests/default.nix create mode 100644 flake/tests/profiles/minimal.nix diff --git a/flake.nix b/flake.nix index e63f7ca..a0281a3 100644 --- a/flake.nix +++ b/flake.nix @@ -14,6 +14,8 @@ imports = [ # add lib to module args {_module.args = {inherit (nixpkgs) lib;};} + + ./flake/tests # machine tests for nvf ./flake/apps.nix ./flake/legacyPackages.nix ./flake/overlays.nix diff --git a/flake/tests/checks/homeManagerModule.nix b/flake/tests/checks/homeManagerModule.nix new file mode 100644 index 0000000..55a08b5 --- /dev/null +++ b/flake/tests/checks/homeManagerModule.nix @@ -0,0 +1,28 @@ +{ + inputs, + nixosTest, + homeManagerModules, + ... +}: +nixosTest { + name = "home-manager-test"; + + nodes.machine = { + imports = [ + inputs.home-manager.nixosModules.home-manager + ../profiles/minimal.nix + ]; + + home-manager = { + sharedModules = [ + homeManagerModules.nvf + ]; + + users.test = { + programs.nvf.enable = true; + }; + }; + }; + + testScript = ""; +} diff --git a/flake/tests/checks/nixosModule.nix b/flake/tests/checks/nixosModule.nix new file mode 100644 index 0000000..437d70c --- /dev/null +++ b/flake/tests/checks/nixosModule.nix @@ -0,0 +1,19 @@ +{ + nixosTest, + nixosModules, + ... +}: +nixosTest { + name = "home-manager-test"; + + nodes.machine = { + imports = [ + nixosModules.nvf + ../profiles/minimal.nix + ]; + + programs.nvf.enable = true; + }; + + testScript = ""; +} diff --git a/flake/tests/default.nix b/flake/tests/default.nix new file mode 100644 index 0000000..9fd8ca1 --- /dev/null +++ b/flake/tests/default.nix @@ -0,0 +1,31 @@ +{ + inputs, + config, + lib, + ... +}: { + perSystem = { + pkgs, + self', + ... + }: let + inherit (lib.filesystem) packagesFromDirectoryRecursive; + inherit (lib.customisation) callPackageWith; + inherit (lib.attrsets) recursiveUpdate; + + defaultInherits = { + inherit (config.flake) homeManagerModules nixosModules; + inherit inputs; + }; + + callPackage = callPackageWith (recursiveUpdate pkgs defaultInherits); + in { + checks = packagesFromDirectoryRecursive { + inherit callPackage; + directory = ./checks; + }; + + # expose checks as packages to be built + packages.test = self'.checks.home-manager-test.driverInteractive; + }; +} diff --git a/flake/tests/profiles/minimal.nix b/flake/tests/profiles/minimal.nix new file mode 100644 index 0000000..357cafb --- /dev/null +++ b/flake/tests/profiles/minimal.nix @@ -0,0 +1,17 @@ +{ + # he's a thicc boi + virtualisation = { + cores = 2; + memorySize = 2048; + qemu.options = ["-vga none -enable-kvm -device virtio-gpu-pci,xres=720,yres=1440"]; + }; + + users.users.test = { + isNormalUser = true; + password = ""; + }; + + home-manager.sharedModules = [ + {home.stateVersion = "24.05";} + ]; +} From 198a36f3d47004492270d2a2551e66b0f5c0d060 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 6 May 2024 16:43:05 +0300 Subject: [PATCH 2/4] flake/tests: add missing imports --- flake.lock | 21 +++++++++++++++++++++ flake.nix | 6 ++++++ flake/tests/checks/homeManagerModule.nix | 15 +++++++++------ flake/tests/checks/nixosModule.nix | 6 ++++-- flake/tests/default.nix | 5 ++++- flake/tests/profiles/minimal.nix | 4 ---- 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/flake.lock b/flake.lock index f590525..d0ab7e3 100644 --- a/flake.lock +++ b/flake.lock @@ -84,6 +84,26 @@ "type": "github" } }, + "home-manager": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1714981474, + "narHash": "sha256-b3/U21CJjCjJKmA9WqUbZGZgCvospO3ArOUTgJugkOY=", + "owner": "nix-community", + "repo": "home-manager", + "rev": "6ebe7be2e67be7b9b54d61ce5704f6fb466c536f", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "home-manager", + "type": "github" + } + }, "naersk": { "inputs": { "nixpkgs": [ @@ -1804,6 +1824,7 @@ "inputs": { "flake-parts": "flake-parts", "flake-utils": "flake-utils", + "home-manager": "home-manager", "nil": "nil", "nixpkgs": "nixpkgs", "nmd": "nmd", diff --git a/flake.nix b/flake.nix index a0281a3..a0e2989 100644 --- a/flake.nix +++ b/flake.nix @@ -89,6 +89,12 @@ flake = false; }; + # Primarily used for testing nvf. + home-manager = { + url = "github:nix-community/home-manager"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + # TODO: get zig from the zig overlay instead of nixpkgs zig.url = "github:mitchellh/zig-overlay"; diff --git a/flake/tests/checks/homeManagerModule.nix b/flake/tests/checks/homeManagerModule.nix index 55a08b5..f6a29f9 100644 --- a/flake/tests/checks/homeManagerModule.nix +++ b/flake/tests/checks/homeManagerModule.nix @@ -13,13 +13,16 @@ nixosTest { ../profiles/minimal.nix ]; - home-manager = { - sharedModules = [ - homeManagerModules.nvf - ]; + config = { + home-manager = { + sharedModules = [ + homeManagerModules.nvf + ]; - users.test = { - programs.nvf.enable = true; + users.test = { + home.stateVersion = "24.05"; + programs.nvf.enable = true; + }; }; }; }; diff --git a/flake/tests/checks/nixosModule.nix b/flake/tests/checks/nixosModule.nix index 437d70c..961c991 100644 --- a/flake/tests/checks/nixosModule.nix +++ b/flake/tests/checks/nixosModule.nix @@ -4,7 +4,7 @@ ... }: nixosTest { - name = "home-manager-test"; + name = "nixos-test"; nodes.machine = { imports = [ @@ -12,7 +12,9 @@ nixosTest { ../profiles/minimal.nix ]; - programs.nvf.enable = true; + config = { + programs.nvf.enable = true; + }; }; testScript = ""; diff --git a/flake/tests/default.nix b/flake/tests/default.nix index 9fd8ca1..4db2ffa 100644 --- a/flake/tests/default.nix +++ b/flake/tests/default.nix @@ -26,6 +26,9 @@ }; # expose checks as packages to be built - packages.test = self'.checks.home-manager-test.driverInteractive; + packages = { + test-home-manager-module = self'.checks.homeManagerModule.driverInteractive; + test-nixos-module = self'.checks.nixosModule.driverInteractive; + }; }; } diff --git a/flake/tests/profiles/minimal.nix b/flake/tests/profiles/minimal.nix index 357cafb..a26e844 100644 --- a/flake/tests/profiles/minimal.nix +++ b/flake/tests/profiles/minimal.nix @@ -10,8 +10,4 @@ isNormalUser = true; password = ""; }; - - home-manager.sharedModules = [ - {home.stateVersion = "24.05";} - ]; } From c3c76afda4aa3fca2cb0365626f52b55fda98510 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 6 May 2024 17:25:58 +0300 Subject: [PATCH 3/4] flake/tests: initial test script --- .gitignore | 1 + flake/tests/checks/homeManagerModule.nix | 37 +++++++++++++++++++++++- flake/tests/default.nix | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 972ffd5..ab94f28 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ result result/ .direnv +.nixos-test-history # Ignore files generated by common IDEs .vscode/* diff --git a/flake/tests/checks/homeManagerModule.nix b/flake/tests/checks/homeManagerModule.nix index f6a29f9..f568243 100644 --- a/flake/tests/checks/homeManagerModule.nix +++ b/flake/tests/checks/homeManagerModule.nix @@ -6,6 +6,7 @@ }: nixosTest { name = "home-manager-test"; + skipLint = true; nodes.machine = { imports = [ @@ -27,5 +28,39 @@ nixosTest { }; }; - testScript = ""; + testScript = '' + import subprocess + machine.wait_for_unit("default.target") + + def check_errs(process): + # Check for errors + print("Connecting to Neovim process") + + # Capture stdout and stderr + stdout, stderr = process.communicate() + + # Print captured stdout and stderr + if stdout: + print("Captured stdout:") + print(stdout.decode('utf-8')) + if stderr: + print("Captured stderr:") + print(stderr.decode('utf-8')) + + def run_neovim_headless(): + print("Running Neovim in headless mode.") + + # Run Neovim in headless mode + nvim_process = subprocess.Popen(['nvim', '--headless'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + check_errs(nvim_process) + + # Load configuration file + nvim_process.stdin.write(b':NonExistentCommand\n') + nvim_process.stdin.flush() + + # run Neovim in headless mode + # and expect it to return sucessfully + machine.succeed(run_neovim_headless()) + ''; } diff --git a/flake/tests/default.nix b/flake/tests/default.nix index 4db2ffa..11463e4 100644 --- a/flake/tests/default.nix +++ b/flake/tests/default.nix @@ -22,6 +22,7 @@ in { checks = packagesFromDirectoryRecursive { inherit callPackage; + testProfile = ../profiles/minimal.nix; directory = ./checks; }; From aa62e904d67c55ef81bf40cd2fa369cd19dbd60c Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 6 May 2024 23:23:52 +0300 Subject: [PATCH 4/4] flake/tests: pass testProfile as a shared arg --- flake/tests/checks/homeManagerModule.nix | 39 ++---------------------- flake/tests/checks/nixosModule.nix | 39 ++++++++++++++++++++++-- flake/tests/default.nix | 4 +-- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/flake/tests/checks/homeManagerModule.nix b/flake/tests/checks/homeManagerModule.nix index f568243..a14fa03 100644 --- a/flake/tests/checks/homeManagerModule.nix +++ b/flake/tests/checks/homeManagerModule.nix @@ -2,6 +2,7 @@ inputs, nixosTest, homeManagerModules, + testProfile, ... }: nixosTest { @@ -10,8 +11,8 @@ nixosTest { nodes.machine = { imports = [ + testProfile inputs.home-manager.nixosModules.home-manager - ../profiles/minimal.nix ]; config = { @@ -28,39 +29,5 @@ nixosTest { }; }; - testScript = '' - import subprocess - machine.wait_for_unit("default.target") - - def check_errs(process): - # Check for errors - print("Connecting to Neovim process") - - # Capture stdout and stderr - stdout, stderr = process.communicate() - - # Print captured stdout and stderr - if stdout: - print("Captured stdout:") - print(stdout.decode('utf-8')) - if stderr: - print("Captured stderr:") - print(stderr.decode('utf-8')) - - def run_neovim_headless(): - print("Running Neovim in headless mode.") - - # Run Neovim in headless mode - nvim_process = subprocess.Popen(['nvim', '--headless'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - - check_errs(nvim_process) - - # Load configuration file - nvim_process.stdin.write(b':NonExistentCommand\n') - nvim_process.stdin.flush() - - # run Neovim in headless mode - # and expect it to return sucessfully - machine.succeed(run_neovim_headless()) - ''; + testScript = ""; } diff --git a/flake/tests/checks/nixosModule.nix b/flake/tests/checks/nixosModule.nix index 961c991..087245a 100644 --- a/flake/tests/checks/nixosModule.nix +++ b/flake/tests/checks/nixosModule.nix @@ -1,6 +1,7 @@ { nixosTest, nixosModules, + testProfile, ... }: nixosTest { @@ -8,8 +9,8 @@ nixosTest { nodes.machine = { imports = [ + testProfile nixosModules.nvf - ../profiles/minimal.nix ]; config = { @@ -17,5 +18,39 @@ nixosTest { }; }; - testScript = ""; + testScript = '' + import subprocess + machine.wait_for_unit("default.target") + + def check_errs(process): + # Check for errors + print("Connecting to Neovim process") + + # Capture stdout and stderr + stdout, stderr = process.communicate() + + # Print captured stdout and stderr + if stdout: + print("Captured stdout:") + print(stdout.decode('utf-8')) + if stderr: + print("Captured stderr:") + print(stderr.decode('utf-8')) + + def run_neovim_headless(): + print("Running Neovim in headless mode.") + + # Run Neovim in headless mode + nvim_process = subprocess.Popen(['nvim', '--headless'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + check_errs(nvim_process) + + # Load configuration file + nvim_process.stdin.write(b':NonExistentCommand\n') + nvim_process.stdin.flush() + + # run Neovim in headless mode + # and expect it to return sucessfully + machine.succeed(run_neovim_headless()) + ''; } diff --git a/flake/tests/default.nix b/flake/tests/default.nix index 11463e4..aa0a40a 100644 --- a/flake/tests/default.nix +++ b/flake/tests/default.nix @@ -16,20 +16,20 @@ defaultInherits = { inherit (config.flake) homeManagerModules nixosModules; inherit inputs; + testProfile = ./profiles/minimal.nix; }; callPackage = callPackageWith (recursiveUpdate pkgs defaultInherits); in { checks = packagesFromDirectoryRecursive { inherit callPackage; - testProfile = ../profiles/minimal.nix; directory = ./checks; }; # expose checks as packages to be built packages = { test-home-manager-module = self'.checks.homeManagerModule.driverInteractive; - test-nixos-module = self'.checks.nixosModule.driverInteractive; + # test-nixos-module = self'.checks.nixosModule.driverInteractive; }; }; }