tests: refactor

This commit is contained in:
Qyriad 2026-02-11 13:19:59 +01:00
parent 8a6bd41baa
commit d7a0cbefe5
12 changed files with 184 additions and 127 deletions

View file

@ -11,5 +11,5 @@ indent_style = space
indent_size = 4 indent_size = 4
[*.nix] [*.nix]
indent_style = tab indent_style = space
indent_size = 2 indent_size = 2

View file

@ -5,7 +5,8 @@
in import src { inherit pkgs; }, in import src { inherit pkgs; },
}: let }: let
inherit (qpkgs) lib; inherit (qpkgs) lib;
dynix = qpkgs.callPackage ./package.nix { }; dynix = qpkgs.callPackage ./package.nix { }
|> qpkgs.stdlib.mkStdenvPretty;
byStdenv = lib.mapAttrs (stdenvName: stdenv: let byStdenv = lib.mapAttrs (stdenvName: stdenv: let
withStdenv = dynix.override { inherit stdenv; }; withStdenv = dynix.override { inherit stdenv; };
dynix' = withStdenv.overrideAttrs (prev: { dynix' = withStdenv.overrideAttrs (prev: {

View file

@ -2,9 +2,12 @@
lib, lib,
stdenvNoCC, stdenvNoCC,
callPackage, callPackage,
linkFarm,
}: let }: let
stdenv = stdenvNoCC; stdenv = stdenvNoCC;
in stdenv.mkDerivation (self: { in stdenv.mkDerivation (finalAttrs: let
self = finalAttrs.finalPackage;
in {
name = "dynix-modules"; name = "dynix-modules";
strictDeps = true; strictDeps = true;
@ -21,12 +24,15 @@ in stdenv.mkDerivation (self: {
phases = [ "unpackPhase" "patchPhase" "installPhase" ]; phases = [ "unpackPhase" "patchPhase" "installPhase" ];
modulesOut = "${placeholder "modules"}/share/nixos/modules/dynix";
installPhase = lib.dedent '' installPhase = lib.dedent ''
runHook preInstall
mkdir -p "$out" mkdir -p "$out"
cp -r * "$out/" cp -r * "$out/"
mkdir -p "$modules/share/nixos/modules/dynix" mkdir -p "$modules/share/nixos/modules/dynix"
cp --reflink=auto -r "$out/"* "$modules/share/nixos/modules/dynix/" cp --reflink=auto -r "$out/"* "$modulesOut/"
''; '';
passthru.mkDevShell = { passthru.mkDevShell = {
@ -39,7 +45,7 @@ in stdenv.mkDerivation (self: {
p.beartype p.beartype
]); ]);
in mkShell' { in mkShell' {
name = "devshell-for-${self.finalPackage.name}"; name = "devshell-for-${self.name}";
packages = [ pyEnv ]; packages = [ pyEnv ];
env.PYTHONPATH = [ env.PYTHONPATH = [
"${pyEnv}/${pyEnv.sitePackages}" "${pyEnv}/${pyEnv.sitePackages}"
@ -48,13 +54,19 @@ in stdenv.mkDerivation (self: {
] |> lib.concatStringsSep ":"; ] |> lib.concatStringsSep ":";
}; };
passthru.modulesPath = self.finalPackage.modules + "/share/nixos/modules"; passthru.modulesPath = self.modules + "/share/nixos/modules";
passthru.tests = lib.fix (callPackage ./tests { passthru.tests = lib.fix (callPackage ./tests {
dynix = self.finalPackage; dynix = self;
}).packages; }).packages;
passthru.allTests = linkFarm "dynix-all-tests" self.tests;
meta = { meta = {
longDescription = lib.dedent ''
Default output contains the modules at top-level, meant for `import`.
The `modules` output contains the modules prefixed under `/share/nixos/modules/dynix`.
'';
outputsToInstall = [ "modules" ]; outputsToInstall = [ "modules" ];
}; };
}) })

View file

@ -3,23 +3,35 @@
qpkgs ? let qpkgs ? let
src = fetchTree (builtins.parseFlakeRef "github:Qyriad/nur-packages"); src = fetchTree (builtins.parseFlakeRef "github:Qyriad/nur-packages");
in import src { inherit pkgs; }, in import src { inherit pkgs; },
callPackage ? qpkgs.callPackage,
lib ? qpkgs.lib, lib ? qpkgs.lib,
dynix ? qpkgs.callPackage ../package.nix { }, dynix ? qpkgs.callPackage ../package.nix { },
}: let }: let
mkDynixConfigurationDotNix = callPackage ./mk-test-configuration-dot-nix.nix { };
runDynixTest = testModule: pkgs.testers.runNixOSTest { runDynixTest = testModule: pkgs.testers.runNixOSTest {
imports = [ testModule ]; imports = [ testModule ];
# NOTE: these are arguments to each *test module*.
# Not the NixOS modules of the test's nodes.
_module.args = { inherit mkDynixConfigurationDotNix; };
# Why is this argument called "extraBaseModule**s**" but take a single module argument... # Why is this argument called "extraBaseModule**s**" but take a single module argument...
# Also note this is an extra base module for each node of the test, # Also note this is an extra base module for each node of the test,
# not an extra test module. # not an extra test module.
extraBaseModules = { name, config, options, modulesPath, ... }: { extraBaseModules = { name, config, options, modulesPath, ... }: {
/**
* Everything in this module will disappear once nixos-rebuild switch happens.
* So each test will need to use `mkDynixConfigurationDotNix` to get
* ./dynix-vm-configuration included in the in-VM configuration.
*/
imports = (import "${modulesPath}/module-list.nix") ++ [ imports = (import "${modulesPath}/module-list.nix") ++ [
./module-allow-rebuild-in-vm.nix ./module-allow-rebuild-in-vm.nix
"${modulesPath}/testing/test-instrumentation.nix" ./dynix-vm-configuration.nix
(toString dynix) (toString dynix)
]; ];
environment.systemPackages = [ dynix ];
systemd.services."install-dynix" = { systemd.services."install-dynix" = {
enable = true; enable = true;
@ -27,9 +39,18 @@
serviceConfig.RemainAfterExit = true; serviceConfig.RemainAfterExit = true;
path = [ config.system.path ]; path = [ config.system.path ];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
serviceConfig.requisteOf = [ "multi-user.target" ];
after = [ "default.target" ]; after = [ "default.target" ];
script = '' script = ''
nix profile install -vv "$(realpath /run/current-system/sw/share/nixos/modules/dynix/)" nix profile install -vv "${dynix.modules}"
mkdir -vp /etc/nixos
nixos-generate-config
cp -rv --dereference /run/current-system/sw/share/nixos/*.nix /etc/nixos/
if ! [[ -e /etc/nixos/dynix-vm-configuration.nix ]]; then
echo "FAILURE"
echo "FAILURE" >&2
fi
''; '';
}; };

View file

@ -0,0 +1,59 @@
{ pkgs, lib, modulesPath, ... }:
let
moduleList = import "${modulesPath}/module-list.nix";
dynixFromSearchPath = let
res = builtins.tryEval <dynix>;
in lib.optional res.success res.value;
in
{
imports = [
"${modulesPath}/testing/test-instrumentation.nix"
] ++ lib.concatLists [
dynixFromSearchPath
moduleList
];
system.switch.enable = true;
system.includeBuildDependencies = true;
documentation.enable = false;
boot.loader.grub = {
enable = true;
device = "/dev/vda";
forceInstall = true;
};
nix = {
package = pkgs.lixPackageSets.latest.lix;
nixPath = [
"nixpkgs=${pkgs.path}"
"/nix/var/nix/profiles/per-user/root/profile/share/nixos/modules"
];
settings = {
experimental-features = [ "nix-command" "pipe-operator" ];
substituters = lib.mkForce [ ];
hashed-mirrors = null;
connect-timeout = 1;
# For my debugging purposes.
show-trace = true;
};
};
environment.pathsToLink = [ "/share" ];
environment.extraOutputsToInstall = [ "modules" ];
environment.variables = {
"NIXOS_CONFIG" = "/etc/nixos/configuration.nix";
};
environment.shellAliases = {
ls = "eza --long --header --group --group-directories-first --classify --binary";
};
environment.systemPackages = with pkgs; [
eza
fd
ripgrep
];
}

View file

@ -1,48 +1,9 @@
{ pkgs, lib, config, modulesPath, ... }: { pkgs, lib, config, modulesPath, ... }:
let let
name = config.networking.hostName; name = config.networking.hostName;
moduleList = import (modulesPath + "/module-list.nix");
dynixFromSearchPath = let
res = builtins.tryEval <dynix>;
in lib.optional res.success res.value;
in in
{ {
imports = moduleList ++ [
"${modulesPath}/testing/test-instrumentation.nix"
./hardware-configuration.nix
] ++ lib.concatLists [
dynixFromSearchPath
];
system.switch.enable = true;
documentation.enable = false;
networking.hostName = "gotosocial-machine"; networking.hostName = "gotosocial-machine";
boot.loader.grub = {
enable = true;
device = "/dev/vda";
forceInstall = true;
};
nix = {
package = pkgs.lixPackageSets.latest.lix;
nixPath = [
"nixpkgs=${pkgs.path}"
"/nix/var/nix/profiles/per-user/root/profile/share/nixos/modules"
];
settings = {
experimental-features = [ "nix-command" "pipe-operator" ];
substituters = lib.mkForce [ ];
hashed-mirrors = null;
connect-timeout = 1;
# For my debugging purposes.
show-trace = true;
};
};
services.gotosocial = { services.gotosocial = {
enable = true; enable = true;
setupPostgresqlDB = true; setupPostgresqlDB = true;
@ -53,19 +14,4 @@ in
}; };
dynamicism.for.gotosocial.enable = true; dynamicism.for.gotosocial.enable = true;
environment.pathsToLink = [ "/share" ];
environment.extraOutputsToInstall = [ "modules" ];
environment.variables = {
"NIXOS_CONFIG" = "/etc/nixos/configuration.nix";
};
environment.shellAliases = {
ls = "eza --long --header --group --group-directories-first --classify --binary";
};
environment.systemPackages = with pkgs; [
eza
fd
ripgrep
];
} }

View file

@ -52,12 +52,6 @@ def get_config_file() -> str:
machine.wait_for_unit("default.target") machine.wait_for_unit("default.target")
assert "lix" in machine.succeed("nix --version").lower() assert "lix" in machine.succeed("nix --version").lower()
machine.log("INIT") machine.log("INIT")
run_log(machine, "journalctl --no-pager -eu install-dynix.service")
machine.succeed("nixos-generate-config")
machine.succeed("mkdir -vp /etc/nixos")
# Dereference is required since that configuration.nix is probably a symlink to the store.
machine.succeed("cp -rv --dereference /run/current-system/sw/share/nixos/configuration.nix /etc/nixos/")
machine.log("REBUILDING configuration inside VM") machine.log("REBUILDING configuration inside VM")
machine.succeed("env PAGER= nixos-rebuild switch --log-format raw-with-logs --fallback") machine.succeed("env PAGER= nixos-rebuild switch --log-format raw-with-logs --fallback")

View file

@ -1,4 +1,4 @@
{ ... }: { mkDynixConfigurationDotNix, config, ... }:
{ {
name = "nixos-test-dynamicism-gotosocial"; name = "nixos-test-dynamicism-gotosocial";
@ -17,7 +17,10 @@
imports = [ ./configuration.nix ]; imports = [ ./configuration.nix ];
environment.systemPackages = let environment.systemPackages = let
configFileTree = pkgs.callPackage ./configuration-package.nix { }; configFileTree = mkDynixConfigurationDotNix {
inherit (config) name;
configuration = ./configuration.nix;
};
in [ in [
configFileTree configFileTree
]; ];

View file

@ -1,4 +1,4 @@
{ pkgs, lib, config, modulesPath, ... }: { lib, modulesPath, ... }:
let let
moduleList = import "${modulesPath}/module-list.nix"; moduleList = import "${modulesPath}/module-list.nix";
@ -8,10 +8,10 @@ let
in in
{ {
imports = [ imports = [
"${modulesPath}/testing/test-instrumentation.nix" #"${modulesPath}/testing/test-instrumentation.nix"
./hardware-configuration.nix #./hardware-configuration.nix
] ++ lib.concatLists [ ] ++ lib.concatLists [
dynixFromSearchPath #dynixFromSearchPath
moduleList moduleList
]; ];
@ -26,47 +26,5 @@ in
}; };
}; };
system.switch.enable = true;
documentation.enable = false;
networking.hostName = "harmonia-machine"; networking.hostName = "harmonia-machine";
boot.loader.grub = {
enable = true;
device = "/dev/vda";
forceInstall = true;
};
nix = {
package = pkgs.lixPackageSets.latest.lix;
nixPath = [
"nixpkgs=${pkgs.path}"
"/nix/var/nix/profiles/per-user/root/profile/share/nixos/modules"
];
settings = {
experimental-features = [ "nix-command" "pipe-operator" ];
substituters = lib.mkForce [ ];
hashed-mirrors = null;
connect-timeout = 1;
# For my debugging purposes.
show-trace = true;
};
};
environment.pathsToLink = [ "/share" ];
environment.extraOutputsToInstall = [ "modules" ];
environment.variables = {
"NIXOS_CONFIG" = "/etc/nixos/configuration.nix";
};
environment.shellAliases = {
ls = "eza --long --header --group --group-directories-first --classify --binary";
};
environment.systemPackages = with pkgs; [
eza
fd
ripgrep
];
} }

View file

@ -66,10 +66,6 @@ assert int(config_toml['workers']) == 4, f"{config_toml['workers']=} != 4"
assert int(config_toml['max_connection_rate']) == 256, f"{config_toml['max_connection_rate']=} != 256" assert int(config_toml['max_connection_rate']) == 256, f"{config_toml['max_connection_rate']=} != 256"
with machine.nested("must succeed: initial nixos-rebuild switch"): with machine.nested("must succeed: initial nixos-rebuild switch"):
machine.succeed("nixos-generate-config")
machine.succeed("mkdir -vp /etc/nixos")
# Dereference is required since that configuration.nix is probably a symlink to the store.
machine.succeed("cp -rv --dereference /run/current-system/sw/share/nixos/configuration.nix /etc/nixos/")
machine.succeed("env PAGER= nixos-rebuild switch --log-format raw-with-logs --fallback") machine.succeed("env PAGER= nixos-rebuild switch --log-format raw-with-logs --fallback")
# Config should not have changed. # Config should not have changed.

View file

@ -1,4 +1,4 @@
{ ... }: { mkDynixConfigurationDotNix, config, ... }:
{ {
name = "nixos-test-dynamicism-harmonia"; name = "nixos-test-dynamicism-harmonia";
@ -10,10 +10,14 @@
imports = [ ./configuration.nix ]; imports = [ ./configuration.nix ];
environment.systemPackages = let environment.systemPackages = let
configFileTree = pkgs.runCommand "${name}-configuration-dot-nix" { } '' #configFileTree = pkgs.runCommand "${name}-configuration-dot-nix" { } ''
set -euo pipefail # set -euo pipefail
install -Dm a=r ${./configuration.nix} "$out/share/nixos/configuration.nix" # install -Dm a=r ${./configuration.nix} "$out/share/nixos/configuration.nix"
''; #'';
configFileTree = mkDynixConfigurationDotNix {
inherit (config) name;
configuration = ./configuration.nix;
};
in [ in [
configFileTree configFileTree
]; ];

View file

@ -0,0 +1,63 @@
{
lib,
stdenvNoCC,
}: let
stdenv = stdenvNoCC;
mkDynixConfigurationDotNix = finalAttrs: {
name,
configuration,
}: assert lib.isStringLike configuration; let
self = finalAttrs.finalPackage;
in {
name = "configuration-dot-nix-for-${name}";
strictDeps = true;
__structuredAttrs = true;
preferLocalBuild = true;
phases = [ "installPhase" ];
#outputs = [ "out" "modules" ];
outputs = [ "out" ];
modulesOut = "${placeholder "out"}/share/nixos";
#modulesOut = "${placeholder "modules"}/share/nixos/modules";
baseConfiguration = configuration;
installPhase = ''
runHook preInstall
install -Dm a=r "$baseConfiguration" "$modulesOut/test-configuration.nix"
install -Dm a=r "${./dynix-vm-configuration.nix}" "$modulesOut/dynix-vm-configuration.nix"
echo "/** GENERATED BY mk-test-configuration-dot-nix! */" >> "$modulesOut/configuration.nix"
echo "{ ... }:" >> "$modulesOut/configuration.nix"
echo >> "$modulesOut/configuration.nix"
echo >> "$modulesOut/configuration.nix"
echo "{" >> "$modulesOut/configuration.nix"
echo " imports = [" >> "$modulesOut/configuration.nix"
echo " ./test-configuration.nix" >> "$modulesOut/configuration.nix"
echo " ./dynix-vm-configuration.nix" >> "$modulesOut/configuration.nix"
echo " ./hardware-configuration.nix" >> "$modulesOut/configuration.nix"
echo " ];" >> "$modulesOut/configuration.nix"
echo "}" >> "$modulesOut/configuration.nix"
#mkdir -p "$out"
#cp -r --reflink=auto "$modulesOut/"* "$out/"
runHook postInstall
'';
passthru = {
modulesPath = self.out + "/share/nixos";
configuration = self.out + "/share/nixos/configuration.nix";
};
meta = {
#outputsToInstall = [ "modules" ];
};
};
in lib.extendMkDerivation {
constructDrv = stdenv.mkDerivation;
extendDrvArgs = mkDynixConfigurationDotNix;
}