Files
komorebi/scripts/vm-mac
T
LGUG2Z 3c51b9b4bf build(nix): a NixOS test framework for headless KWin
The unit tests run against a fake desktop and cannot say whether KWin does what
the daemon asks. This adds the other half: NixOS machines with Plasma in them, a
real KWin, and a Python library to drive it.

Every directory under `nix/tests/vm/suites` is a test. The generator lists the
directory, so a new suite appears as `checks.<system>.vm-NAME` on Linux and as
`legacyPackages.<system>.vm.NAME` everywhere, with no edit to the flake. A suite
names its tier and its node and has a `test.py`;
`legacyPackages.<system>.vmTiers` lists the names of each tier as JSON for the
CI matrix, `vmGuests` the guests to build on a Linux builder, and `vmTcg` the
same suites for a machine with no KVM. `scale` multiplies every wait: 1 for KVM,
1.5 for HVF, 8 for TCG.

The headless node runs `kwin_wayland --virtual` as user units of a lingering
user, one pair of units per session (`session-bus@NAME`, `kwin-virtual@NAME`),
so a test can run several sessions with their own bus and socket. A virtual KWin
has no output to draw on, so anything that needs pixels or a pointer needs the
session node.

The library, `komorebi_vm`, has `Checks` (counted checks that fail once, at the
end) and `Kde` (start a session, run clients and tools inside it, read windows
through kdotool, poll with `wait_for`). It depends on a small `Machine` protocol
rather than on the test driver. The `vm-lib` check runs its unit tests against a
fake machine, with ruff and ty. The driver type checks each `test.py` with ty,
which is how a protocol that did not match `QemuMachine.execute` was caught.

`vm-kwin` starts a KWin with two outputs, spawns a foot window, reads it back,
starts a second session and checks that it cannot see the first.

On a Mac the guest is built on a Linux builder and the driver run directly,
because the macOS nix sandbox cannot give QEMU the hypervisor: `scripts/vm-mac`,
`just vm NAME`. docs/dev/vm-tests.md covers this, adding a test, and the
builder.
2026-10-27 14:06:19 -07:00

39 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Runs one VM test on a Mac, where the guest is Linux and the host is not.
#
# scripts/vm-mac NAME [driver arguments]
#
# The guest's closure is built on a Linux builder, copied back, and the test driver then runs
# on the Mac under HVF. The driver is run directly, not through `nix build`, because the nix
# sandbox on macOS cannot give QEMU the hypervisor. Artifacts (screenshots, logs) land in
# `.vm/out/NAME`.
#
# The builder is yours: set KOMOREBI_BUILDER to its store URL (for example
# `ssh-ng://builder@localhost`) and, if it needs them, NIX_SSHOPTS. docs/dev/vm-tests.md
# says what a builder has to be.
set -euo pipefail
name="${1:?usage: scripts/vm-mac NAME [driver arguments]}"
shift
builder="${KOMOREBI_BUILDER:?set KOMOREBI_BUILDER to the store URL of a Linux builder}"
cd "$(dirname "$0")/.."
test="path:$PWD#legacyPackages.aarch64-darwin.vm.$name"
[ -d .git ] && test="git+file://$PWD#legacyPackages.aarch64-darwin.vm.$name"
echo "== building the guests on $builder"
nix eval --json "$test.nodes" --apply 'nodes: builtins.mapAttrs (_: node: node.system.build.toplevel.drvPath) nodes' |
jq -r '.[]' |
while read -r drv; do
out=$(nix build "$drv^out" --eval-store auto --store "$builder" --no-link --print-out-paths)
nix copy --from "$builder" --substitute-on-destination --no-check-sigs "$out"
done
echo "== building the driver"
driver=$(nix build "$test.driver" --builders '' --no-link --print-out-paths)
out="$PWD/.vm/out/$name"
mkdir -p "$out"
echo "== running $name (artifacts in $out)"
cd "$out"
exec "$driver/bin/nixos-test-driver" -o . "$@"