significantly improve purity

This commit is contained in:
Qyriad 2026-02-09 14:32:56 +01:00
parent 45a7d43f77
commit 68e9b9a1e4
12 changed files with 139 additions and 230 deletions

View file

@ -5,71 +5,13 @@
in import src { inherit pkgs; }, in import src { inherit pkgs; },
}: let }: let
inherit (qpkgs) lib; inherit (qpkgs) lib;
dynix = qpkgs.callPackage ./modules-package.nix { };
dynix = qpkgs.callPackage ./package.nix { };
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: {
pname = "${prev.pname}-${stdenvName}"; pname = "${prev.pname}-${stdenvName}";
}); });
in dynix') qpkgs.validStdenvs; in dynix') qpkgs.validStdenvs;
in dynix.overrideAttrs (prev: lib.recursiveUpdate prev {
evalNixos = import (pkgs.path + "/nixos"); passthru = { inherit byStdenv; };
doChange = {
option,
value,
}:
assert lib.isList option;
assert lib.all lib.isString option;
let
nixosBefore = evalNixos {
configuration = ./configuration.nix;
};
dynamicBefore = nixosBefore.config.dynamicism.finalSettings;
nixosAfter = evalNixos {
configuration = { ... }: {
imports = [
./configuration.nix
(lib.setAttrByPath option (lib.mkOverride (-999) value))
];
};
};
withActivationScripts = evalNixos {
configuration = ({ ... }: {
imports = [ ./configuration.nix ];
config.environment.systemPackages = [ nixosAfter.config.dynamicism.for.gotosocial.activate ];
});
};
in {
inherit nixosBefore nixosAfter withActivationScripts;
};
in dynix.overrideAttrs (final: prev: let
self = final.finalPackage;
in lib.recursiveUpdate prev {
passthru = {
ts = let
scope = pkgs.callPackage ./modules/tests.nix { };
in scope.packages scope;
dync = self.nixos.config.dynamicism;
dyno = self.nixos.options.dynamicism;
gotosocial = self.nixos.options.dynamicism.for.valueMeta.attrs.gotosocial.configuration;
inherit byStdenv;
nixos = evalNixos {
configuration = ./configuration.nix;
};
c = self.nixos;
nixos-vm = self.nixos.config.system.build.vm;
doChange = builtins.seq self.nixos.config.dynamicism doChange;
withVox = self.doChange {
option = lib.splitString "." "services.gotosocial.settings.application-name";
value = "Vox is an asshole";
};
};
}) })

61
modules-package.nix Normal file
View file

@ -0,0 +1,61 @@
{
lib,
stdenvNoCC,
callPackage,
}: let
stdenv = stdenvNoCC;
in stdenv.mkDerivation (self: {
name = "dynix-modules";
strictDeps = true;
__structuredAttrs = true;
outputs = [ "out" "modules" ];
src = lib.fileset.toSource {
root = ./modules/dynamicism;
fileset = lib.fileset.unions [
./modules/dynamicism
];
};
phases = [ "unpackPhase" "patchPhase" "installPhase "];
installPhase = lib.dedent ''
mkdir -vp "$out"
cp -rv * "$out/"
#mkdir -vp "$modules/share/nix/modules/dynix"
#cp --reflink=auto -rv * "$modules/share/nix/modules/dynix/"
mkdir -vp "$modules/share/nixos/modules/dynix"
cp --reflink=auto -rv * "$modules/share/nixos/modules/dynix/"
'';
passthru.mkDevShell = {
path,
mkShell,
python3Packages,
}: let
mkShell' = mkShell.override { inherit stdenv; };
pyEnv = python3Packages.python.withPackages (p: [
p.beartype
]);
in mkShell' {
name = "devshell-for-${self.finalPackage.name}";
packages = [ pyEnv ];
env.PYTHONPATH = [
"${pyEnv}/${pyEnv.sitePackages}"
# Cursed.
"${path}/nixos/lib/test-driver/src"
] |> lib.concatStringsSep ":";
};
passthru.tests = lib.fix (callPackage ./tests {
dynix = self.finalPackage;
}).packages;
meta = {
outputsToInstall = [ "modules" ];
};
})

View file

@ -1,4 +0,0 @@
{ pkgs, ... }:
{
nix.package = pkgs.lixPackageSets.latest.lix;
}

View file

@ -1,76 +0,0 @@
#import re
from pathlib import Path
from typing import cast, TYPE_CHECKING
from test_driver.machine import Machine
from test_driver.errors import RequestedAssertionFailed
DEFAULT_NIX = "@DEFAULT_NIX@"
CONFIGURATION_NIX = "@CONFIGURATION_NIX@"
DYNAMICISM = "@DYNAMICISM@"
if TYPE_CHECKING:
global machine
machine = cast(Machine, ...)
def run_log(machine: Machine, *commands: str, timeout: int | None = 60) -> str:
output = ""
for command in commands:
with machine.nested(f"must succeed: {command}"):
(status, out) = machine.execute(f"{command} | tee /dev/stderr", timeout=timeout)
if status != 0:
machine.log(f"output: {out}")
raise RequestedAssertionFailed(
f"command `{command}` failed (exit code {status})",
)
output += out
return output
def get_config_file() -> Path:
machine.wait_for_unit("gotosocial.service")
gotosocial_pid = int(machine.get_unit_property("gotosocial.service", "MainPID"))
print(f"{gotosocial_pid=}")
cmdline = machine.succeed(f"cat /proc/{gotosocial_pid}/cmdline")
cmdline_args = cmdline.split("\0")
config_file_idx = cmdline_args.index("--config-path") + 1
config_file = Path(cmdline_args[config_file_idx])
machine.log(f"copying from VM: {config_file=}")
machine.copy_from_vm(config_file.as_posix())
return machine.out_dir / config_file.name
machine.wait_for_unit("default.target")
assert "lix" in run_log(machine, "nix --version").lower()
print(f"{CONFIGURATION_NIX=}")
machine.succeed("mkdir -vp /etc/nixos")
machine.copy_from_host(CONFIGURATION_NIX, "/etc/nixos")
machine.copy_from_host(DYNAMICISM, "/etc/nixos")
run_log(machine, f"nix build --log-format multiline-with-logs --impure -E 'import <nixpkgs/nixos> {{ configuration = {CONFIGURATION_NIX}; }}'")
run_log(machine, f"nixos-rebuild switch --file {CONFIGURATION_NIX} --verbose --print-build-logs")
config_file_local = get_config_file()
machine.log(f"opening copied file: {config_file_local=}")
with open(config_file_local, "r") as f:
text = f.read()
lines = text.splitlines()
application_name = next(line for line in lines if line.startswith("application-name:"))
assert "gotosocial-for-machine" in application_name, f"'gotosocial-for-machine' should be in {application_name=}"
print(f"{DEFAULT_NIX=}")
run_log(machine, "eza -lah --color=always --group-directories-first --tree /etc/")
#exec_start = machine.succeed("systemctl show gotosocial.service --property=ExecStart --value")
#exec_start = machine.succeed("systemctl show gotosocial.service --property=ExecStart --value")
#service_text = machine.succeed("systemctl show gotosocial.service")
#service_props = dict(line.split("=", maxsplit=1) for line in service_text.splitlines())
#exec_start = service_props['ExecStart']
#print(f"{exec_start=}")

View file

@ -1,52 +0,0 @@
{
pkgs,
lib,
testers,
}:
let
inherit (testers) runNixOSTest;
in lib.makeScope lib.callPackageWith (self: {
main = runNixOSTest {
name = "nixos-test-dynamicism-main";
defaults = { pkgs, ... }: {
imports = [ ./dynamicism ];
nix = {
package = pkgs.lixPackageSets.latest.lix;
settings.experimental-features = [ "nix-command" ];
nixPath = [ "nixpkgs=${pkgs.path}" ];
};
environment.shellAliases = {
ls = "eza --long --header --group --group-directories-first --classify --binary";
};
environment.systemPackages = with pkgs; [
eza
fd
ripgrep
];
};
nodes.machine = { name, ... }: {
#services.gotosocial = {
# enable = true;
# setupPostgresqlDB = true;
# settings = {
# application-name = "gotosocial-for-${name}";
# host = "${name}.local";
# };
#};
#
#dynamicism.for.gotosocial.enable = true;
};
# What's a little IFD between friends?
testScript = pkgs.replaceVars ./tests-main.py {
DEFAULT_NIX = ../default.nix;
CONFIGURATION_NIX = ./tests-configuration.nix;
DYNAMICISM = ./dynamicism;
}
|> builtins.readFile;
};
})

View file

@ -1,5 +1,10 @@
{ {
pkgs ? import <nixpkgs> { }, pkgs ? import <nixpkgs> {
config = {
checkMeta = true;
allowAliases = false;
};
},
qpkgs ? let qpkgs ? let
src = fetchTarball "https://github.com/Qyriad/nur-packages/archive/main.tar.gz"; src = fetchTarball "https://github.com/Qyriad/nur-packages/archive/main.tar.gz";
in import src { inherit pkgs; }, in import src { inherit pkgs; },
@ -12,12 +17,6 @@
byStdenv = lib.mapAttrs (lib.const mkDevShell) dynix.byStdenv; byStdenv = lib.mapAttrs (lib.const mkDevShell) dynix.byStdenv;
in devShell.overrideAttrs (prev: lib.recursiveUpdate prev { in devShell.overrideAttrs (prev: {
passthru = { inherit byStdenv; }; passthru = { inherit byStdenv; };
env.PYTHONPATH = [
"${pkgs.python3Packages.beartype}/${pkgs.python3.sitePackages}"
] |> lib.concatStringsSep ":";
packages = prev.packages or [ ] ++ [
pkgs.python3Packages.beartype
];
}) })

View file

@ -5,5 +5,5 @@
set -euo pipefail set -euo pipefail
mkdir -vp "$out/share/nixos" mkdir -vp "$out/share/nixos"
cp -rv ${./configuration.nix} "$out/share/nixos/configuration.nix" cp -rv ${./configuration.nix} "$out/share/nixos/configuration.nix"
cp -rv ${../../modules/dynamicism} "$out/share/nixos/dynamicism" #cp -rv ${../../modules/dynamicism} "$out/share/nixos/dynamicism"
'' ''

View file

@ -1,22 +1,18 @@
{ pkgs, lib, config, modulesPath, ... }: { pkgs, lib, config, modulesPath, ... }:
let let
name = config.networking.hostName; name = config.networking.hostName;
nixosLibPath = (modulesPath + "/../lib");
moduleList = import (modulesPath + "/module-list.nix"); moduleList = import (modulesPath + "/module-list.nix");
optionalPath = p: lib.optional (builtins.pathExists p) p; dynixFromSearchPath = let
res = builtins.tryEval <dynix>;
in lib.optional res.success res.value;
in in
assert builtins.pathExists nixosLibPath;
builtins.seq lib
builtins.seq modulesPath
builtins.seq moduleList
{ {
imports = moduleList ++ [ imports = moduleList ++ [
(modulesPath + "/testing/test-instrumentation.nix") "${modulesPath}/testing/test-instrumentation.nix"
./hardware-configuration.nix
] ++ lib.concatLists [ ] ++ lib.concatLists [
(optionalPath ./hardware-configuration.nix) dynixFromSearchPath
(optionalPath ./dynamicism)
(optionalPath ../../modules/dynamicism)
]; ];
system.switch.enable = true; system.switch.enable = true;
@ -32,13 +28,18 @@ builtins.seq moduleList
nix = { nix = {
package = pkgs.lixPackageSets.latest.lix; package = pkgs.lixPackageSets.latest.lix;
nixPath = [ "nixpkgs=${pkgs.path}" ]; nixPath = [
"nixpkgs=${pkgs.path}"
"/nix/var/nix/profiles/per-user/root/profile/share/nixos/modules"
];
settings = { settings = {
experimental-features = [ "nix-command" "pipe-operator" ]; experimental-features = [ "nix-command" "pipe-operator" ];
substituters = lib.mkForce [ ]; substituters = lib.mkForce [ ];
hashed-mirrors = null; hashed-mirrors = null;
connect-timeout = 1; connect-timeout = 1;
# For my debugging purposes.
show-trace = true;
}; };
}; };
@ -54,6 +55,7 @@ builtins.seq moduleList
dynamicism.for.gotosocial.enable = true; dynamicism.for.gotosocial.enable = true;
environment.pathsToLink = [ "/share" ]; environment.pathsToLink = [ "/share" ];
environment.extraOutputsToInstall = [ "modules" ];
environment.variables = { environment.variables = {
"NIXOS_CONFIG" = "/etc/nixos/configuration.nix"; "NIXOS_CONFIG" = "/etc/nixos/configuration.nix";
}; };

View file

@ -0,0 +1,7 @@
/** Dummy hardware configuration.
* Will be replaced with the real one in the test VM.
*/
{ ... }:
{
}

View file

@ -52,11 +52,17 @@ 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")
run_log(machine, "nixos-generate-config") machine.succeed("nix profile install -vv $(realpath /run/current-system/sw/share/nixos/modules/dynix)")
machine.succeed("nixos-generate-config")
machine.succeed("mkdir -vp /etc/nixos") machine.succeed("mkdir -vp /etc/nixos")
machine.succeed("cp -rv /run/current-system/sw/share/nixos/* /etc/nixos/") # Dereference is required since that configuration.nix is probably a symlink to the store.
machine.succeed("env PAGER= nixos-rebuild switch --log-format raw-with-logs -v --fallback >&2") machine.succeed("cp -rv --dereference /run/current-system/sw/share/nixos/configuration.nix /etc/nixos/")
machine.log("REBUILDING configuration inside VM")
machine.succeed("env PAGER= nixos-rebuild switch --log-format raw-with-logs --fallback")
config_text = get_config_file() config_text = get_config_file()
lines = config_text.splitlines() lines = config_text.splitlines()

View file

@ -1,22 +1,23 @@
{ { dynix, ... }:
pkgs,
lib,
config,
...
}:
{ {
name = "nixos-test-dynamicism-main"; name = "nixos-test-dynamicism-main";
defaults = { ... }: { }; defaults = { ... }: { };
#node.pkgsReadOnly = false;
extraPythonPackages = p: [ extraPythonPackages = p: [
p.beartype p.beartype
]; ];
nodes.machine = { pkgs, config, ... }: { nodes.machine = { pkgs, config, ... }: {
imports = [ ./configuration.nix ]; # NOTE: Anything in this `nodes.machine = ` module will not be included
# in the VM's NixOS configuration once it does `nixos-rebuild switch`,
# except for `./configuration.nix` which will be copied to `/etc/nixos/`.
# dynix will also be statefully installed to root's user profile.
imports = [
./configuration.nix
(toString dynix)
];
system.includeBuildDependencies = true; system.includeBuildDependencies = true;
system.switch.enable = true; system.switch.enable = true;

View file

@ -1,8 +1,31 @@
{ {
pkgs ? import <nixpkgs> { }, pkgs ? import <nixpkgs> { },
lib ? pkgs.lib, qpkgs ? let
}: lib.makeScope lib.callPackageWith (self: let src = fetchTree (builtins.parseFlakeRef "github:Qyriad/nur-packages");
inherit (pkgs.testers) runNixOSTest; in import src { inherit pkgs; },
in { lib ? qpkgs.lib,
basic = runNixOSTest ./basic/test.nix; dynix ? qpkgs.callPackage ../modules-package.nix { },
}: let
runDynixTest = testModule: pkgs.testers.runNixOSTest {
imports = [ testModule ];
# Note: these are arguments to the *test* modules.
# Not the NixOS modules for the NixOS configuration the test is testing.
# Wew.
_module.args = { inherit dynix; };
# Why is this argument called "extraBaseModule**s**" but take a single module argument...
extraBaseModules = { name, ... }: {
#imports = [ dynixInjectionModule ];
config.environment.systemPackages = [ dynix ];
# Just making something in this strict in `name`,
# which is only present as an argument for nodes and I don't want to
# confuse that with the test modules.
config.warnings = builtins.seq name [ ];
};
};
in lib.makeScope lib.callPackageWith (self: {
basic = runDynixTest ./basic/test.nix;
}) })