diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 98a7f226..1dc844a8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -151,7 +151,11 @@ module, and carry nothing of the person who uses them: a key, a password or a configuration of yours goes into the guest when it runs, never into these files. The `plasma-guests-eval` check evaluates them, and the VM suite `rdp` runs their RDP server, [`nix/dev/krdp.nix`](nix/dev/krdp.nix). The demo guest has no -password and no key in its closure: the host shares a directory with them. +password and no key in its closure: the host shares a directory with them, and +`run-kde` ([`scripts/run-kde`](scripts/run-kde)) starts it. The script is +shellchecked when the package is built, and `run-kde-dry-run` and +`run-kde-doctor` compare what it prints for made-up machines with the texts in +[`nix/tests/run-kde`](nix/tests/run-kde): change one with the other. ## Continuous integration diff --git a/README.md b/README.md index 51b9e486..ebb17df5 100644 --- a/README.md +++ b/README.md @@ -183,9 +183,12 @@ to develop in. For an Apple silicon Mac there are two that QEMU runs, one with a session that can be reached over RDP, and a disk image for UTM, which renders on the Mac's GPU. For a Linux machine with KVM there are the same two QEMU guests, for `x86_64` and for `aarch64`, and a demo guest of the session guest, which -shows komorebi tiling a few windows in the first minute. +shows komorebi tiling a few windows in the first minute. On Linux, +`nix run .#run-kde` (or `just run-kde`) starts the demo in a window, and +`run-kde --doctor` says what the machine lacks. [`docs/dev/utm.md`](docs/dev/utm.md) says how to build and run them; -`plasma-guests-eval` evaluates them on every system. +`plasma-guests-eval` evaluates them on every system, and `run-kde-dry-run` and +`run-kde-doctor` run the command against machines that a test makes up. ## How it fits together diff --git a/docs/dev/utm.md b/docs/dev/utm.md index 2bc0676e..bf5d9b96 100644 --- a/docs/dev/utm.md +++ b/docs/dev/utm.md @@ -135,7 +135,7 @@ what a person's run needs comes from the environment of the runner: | `QEMU_OPTS` | the display device: `-vga none -device virtio-vga,xres=1280,yres=800`, or `-display gtk,gl=on -vga none -device virtio-vga-gl,xres=1280,yres=800` for a GPU | | `NIX_DISK_IMAGE` | the disk image, which the runner makes when it is not there | -`run-kde` sets them; to start the guest by hand, on a Linux machine: +[`run-kde`](#the-command) sets them; to start the guest by hand, on a Linux machine: ```sh nix build .#nixosConfigurations.run-kde-x86_64.config.system.build.vm -o .vm/result-run-kde @@ -167,6 +167,77 @@ the closure is built: `/run/run-kde-guest`, where the RDP unit (`komorebi.krdp.passwordFile`) and sshd (`AuthorizedKeysFile`) read them. +## The command + +`run-kde` starts the demo guest on a Linux machine, from a checkout: `nix run .#run-kde -- [options]`, +or `just run-kde [options]`. It is [`scripts/run-kde`](../../scripts/run-kde), which the flake wraps +with the runner of the machine's guest (`run-kde-x86_64` or `run-kde-aarch64`) written into it, as +`packages..run-kde` and `apps..run-kde`. The package names the runner and does not +depend on it, so `nix run` does not fetch 4 GB before the command can say what the machine lacks. + +In order, it: + +1. Checks the machine. `--doctor` prints every check, a tick or a cross or a `!`, with the fix under + each cross and each `!`: WSL 2 or not, Nix, KVM (`/dev/kvm`, the group `kvm`, the CPU's `svm` or + `vmx` flag, the module `kvm_amd` or `kvm_intel`, and on WSL 2 the line + `nestedVirtualization=true` of `.wslconfig`), virtiofs (a `virtiofsd`, the runner's own when it + is built, is started as this user with the runner's options and must make its socket: a cross + when it does not; `/dev/fuse` is a `!`, since `virtiofsd` never opens it), memory, cores, disk, a + window (WSLg: `WAYLAND_DISPLAY`, `DISPLAY`), Windows interop for `--rdp`, the GPU (`/dev/dxg` on + WSL 2, a render node elsewhere, and the OpenGL renderer of the host from `glxinfo`), and the + networking mode of `.wslconfig`. A cross stops the command and `--doctor` exits with 1; a `!` + does not. A missing KVM is a cross, unless `--tcg` allows software emulation, which is too slow + to try anything. +2. Builds the guest when its runner is not in the store, with `nix build` of the checkout's flake and + the build log shown. There is no binary cache: the first run builds komorebi and downloads about + 4 GB of Plasma, which are 11 GB in the Nix store. +3. Makes the state directory, `$XDG_STATE_HOME/komorebi-run-kde` (`~/.local/state/komorebi-run-kde`), + when it is not there: the guest's disk (20 GB, or `--size`, sparse), a random RDP password and an + ed25519 key for ssh, all of mode 0600 in a directory of mode 0700. `--reset` deletes it. Nothing + of it is in the Nix store. +4. Prints a banner and starts the runner with the environment of [the demo guest](#the-demo-guest): + `-cpu host` with KVM, the repository at `/repo` (the checkout, or `KOMOREBI_REPO`), the state + directory at `/run/run-kde`, the ports on `127.0.0.1` (ssh 2223, RDP 3389: `--ssh-port`, + `--rdp-port`), and `-m` and `-smp` from what the machine has (`--memory`, `--cores`): the + machine's memory less 2048 MB, between 4096 and 8192, and its cores up to 6. + +The terminal is the guest's console, logged in as `komorebi`. What else the guest shows depends on +the display and on the GPU. + +| `--display` | What it does | +|---|---| +| `gtk` | a window of QEMU; the default when `WAYLAND_DISPLAY` or `DISPLAY` is set | +| `rdp` | no window; the guest is reached over RDP; the default otherwise | +| `vnc` | no window; QEMU's VNC server on `127.0.0.1:5900` | +| `none` | no window; `--shell` is this with the GPU off | + +`--gpu auto` uses the GPU when the display is a window, the host has a GPU device and the OpenGL of +the host (`glxinfo`) is not llvmpipe. Otherwise it does not, and the banner says why. The GPU is +`-display gtk,gl=on` with `-device virtio-vga-gl`, which renders the guest's OpenGL on the host +through VirGL; without it the guest has `virtio-vga` and renders on its CPUs. `--gpu on` with no +window starts `-display egl-headless`, which needs a DRM render node. The guest says what KWin draws +with in its cheat sheet and on the console (`GPU (virgl)` or `software (llvmpipe)`): the guest's +driver, not the host's. + +On WSL 2, `--rdp` starts Windows' own RDP client once ssh says that the unit `krdp` is running: +`cmdkey.exe` stores the password for `TERMSRV/localhost`, `mstsc.exe` connects to +`localhost:3389`, and the credential is deleted when `run-kde` ends. + +`--dry-run` prints the environment and the command that would be started, and starts nothing. The +script reads the machine through variables, so that a test can make up a machine: +`RUN_KDE_KVM_DEVICE`, `RUN_KDE_DXG_DEVICE`, `RUN_KDE_DRI_DIR`, `RUN_KDE_MEMINFO`, `RUN_KDE_CPUINFO`, +`RUN_KDE_OSRELEASE`, `RUN_KDE_SYS_MODULE_DIR`, `RUN_KDE_STORE_DIR`, `RUN_KDE_FUSE_DEVICE`, +`RUN_KDE_VIRTIOFSD`, `RUN_KDE_FREE_GB`, `RUN_KDE_CPUS`, `RUN_KDE_HOST_RENDERER` and +`RUN_KDE_WSLCONFIG`, besides `WAYLAND_DISPLAY` and `DISPLAY`; and `RUN_KDE_GUEST` and +`RUN_KDE_RUNNER_BIN`, which the package sets, and `RUN_KDE_QEMU_OPTS`, which adds to the options of +QEMU. The checks `run-kde-dry-run` and `run-kde-doctor` +([`nix/tests/run-kde.nix`](../../nix/tests/run-kde.nix)) run it against a WSL 2 with WSLg and a GPU, +one with WSLg and a software OpenGL, one with WSLg and no GPU device, a Linux machine with no +window, `--tcg` with no KVM, and machines that cannot run the guest (no KVM, a KVM the user cannot +open, a `virtiofsd` that exits), a `/dev/fuse` that is missing or closed, and compare the output +with the texts in [`nix/tests/run-kde`](../../nix/tests/run-kde). The check `run-kde` builds the package, which +shellchecks the script. + ## The UTM disk UTM boots `plasma-utm` from a disk image. The image is made by installing the system onto a blank @@ -273,7 +344,12 @@ machine. Both guests reached the login on their serial console and had one DRM d node, and the session guest drew the Plasma desktop on its display (a screenshot taken through QEMU's monitor) with komorebi running in the session. The demo guest, `run-kde-x86_64`, was started under KVM with a state directory: ssh took the key from it, the RDP unit started with the -password from it, komorebi tiled the four windows, and the cheat sheet opened. The `aarch64` guests +password from it, komorebi tiled the four windows, and the cheat sheet opened. `run-kde --doctor`, +`--dry-run`, `--build-only` and a start with `--display none` were run on the same machine, which has +no WSL, no window and no GPU: the guest came up with the memory and cores asked for, and it said +`software (llvmpipe)`. The GPU path was started on that machine in QEMU's GTK window on an X server +whose OpenGL is Mesa's llvmpipe. Nothing was run on WSL 2, with WSLg, with a GPU, or with +`mstsc.exe`: what the command does there is not known yet. The `aarch64` guests for a Linux host were evaluated and not built. The RDP server is the exception to all of this: the VM suite `rdp` runs the same unit in the test session. That KWin 6.8 runs well on VirGL under UTM is not known either: the UTM and Mesa notes say VirGL is the way to render a Linux desktop, and diff --git a/flake.nix b/flake.nix index f2384766..417f04a2 100644 --- a/flake.nix +++ b/flake.nix @@ -160,6 +160,35 @@ inherit pkgs; rev = "nix-check"; }; + # `run-kde` starts the demo guest of the host's architecture (docs/dev/utm.md). The guest's + # runner is named in the program, not depended on: the command works out what is missing + # and builds it, with its progress shown, instead of `nix run` fetching 4 GB first. + runKde = + let + arch = if system == "x86_64-linux" then "x86_64" else "aarch64"; + guest = self.nixosConfigurations."run-kde-${arch}"; + runner = builtins.unsafeDiscardStringContext "${guest.config.system.build.vm}"; + in + pkgs.writeShellApplication { + name = "run-kde"; + runtimeInputs = with pkgs; [ + coreutils + e2fsprogs + gawk + git + gnugrep + gnused + mesa-demos + openssh + qemu-utils + virtiofsd + ]; + text = '' + : "''${RUN_KDE_GUEST:=run-kde-${arch}}" + : "''${RUN_KDE_RUNNER_BIN:=${runner}/bin/run-${guest.config.networking.hostName}-vm}" + '' + + builtins.readFile ./scripts/run-kde; + }; nightlyRustfmt = pkgs.rust-bin.nightly.latest.rustfmt; nightlyToolchain = pkgs.rust-bin.nightly.latest.default; rustToolchainWithNightlyRustfmt = pkgs.symlinkJoin { @@ -383,6 +412,16 @@ touch $out ''; } + # `run-kde` is shellchecked when it is built, and its output is what a test says it is + // lib.optionalAttrs isLinux ( + { + run-kde = config.packages.run-kde; + } + // import ./nix/tests/run-kde.nix { + inherit pkgs; + runKde = config.packages.run-kde; + } + ) # The VM tests need QEMU with KVM, so only Linux checks them. A Mac runs the same tests # through legacyPackages..vm, see docs/dev/vm-tests.md. // lib.optionalAttrs isLinux ( @@ -422,6 +461,7 @@ kwin-effect-geometry-change-komorebi = pkgs.callPackage ./nix/kwin-effect-geometry-change.nix { }; wsl2-probe = wsl2Probe.probe; wsl2-probe-guest = wsl2Probe.guest; + run-kde = runKde; }; apps = lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux { @@ -440,6 +480,11 @@ program = lib.getExe wsl2Probe.probe; meta.description = "Probes a NixOS-WSL2 machine for KVM, WSLg, a GPU and RDP"; }; + run-kde = { + type = "app"; + program = lib.getExe config.packages.run-kde; + meta.description = "Start the komorebi demo in a virtual machine"; + }; default = config.apps.komorebi; }; diff --git a/justfile b/justfile index 28b2eb2d..85738414 100644 --- a/justfile +++ b/justfile @@ -70,5 +70,10 @@ docgen: rm -f docs/cli/*.md cargo run --package komorebic --locked -- docgen --output docs/cli +# the demo guest in a virtual machine, with komorebi tiling a few windows (docs/dev/utm.md); +# `just run-kde --doctor` says what this machine is missing +run-kde *args: + nix run .#run-kde -- {{ args }} + run target *args: cargo run --bin {{ target }} --locked -- {{ args }} diff --git a/nix/tests/run-kde.nix b/nix/tests/run-kde.nix new file mode 100644 index 00000000..47fc64d9 --- /dev/null +++ b/nix/tests/run-kde.nix @@ -0,0 +1,101 @@ +# What `run-kde` says and starts, for machines that a test makes up from the variables the script +# reads (RUN_KDE_*, WAYLAND_DISPLAY, DISPLAY): a WSL 2 with WSLg and a GPU, one with WSLg and a +# software OpenGL or none, a Linux machine with no window, and machines that cannot run the guest. +# `--dry-run` prints what the script would start, which is the environment and the arguments the +# runner is given; `--doctor` prints what the machine lacks and how to fix it, and starts the +# virtiofsd of nixpkgs for real, as the sandbox's user, to see that it serves a directory. The +# sandbox has no KVM, no GPU and no WSL, which is the point: the script's answer does not depend on +# the machine it is run on. The expected texts are in `run-kde/`. +{ pkgs, runKde }: +let + exe = pkgs.lib.getExe runKde; + + # The files that stand for a machine, and a command that runs the script in an environment of its + # own, with the variables that follow it. @T@ is where the files are, which the expected texts do + # not keep. + setup = '' + t=$PWD/machine + mkdir -p $t/bin $t/repo $t/dri + touch $t/repo/flake.nix $t/kvm $t/kvm-denied $t/fuse $t/dxg + chmod 000 $t/kvm-denied + printf '#!/bin/sh\n' > $t/bin/nix + printf '#!/bin/sh\necho "[ERROR virtiofsd] Error creating the vhost-user socket: Address in use (os error 98)" >&2\nexit 1\n' > $t/bin/virtiofsd-broken + chmod +x $t/bin/nix $t/bin/virtiofsd-broken + printf 'MemTotal: 16777216 kB\n' > $t/meminfo + printf 'MemTotal: 6291456 kB\n' > $t/meminfo-small + printf '5.15.167.4-microsoft-standard-WSL2\n' > $t/osrelease-wsl + printf '6.18.0\n' > $t/osrelease-linux + printf 'flags : fpu vme svm sse\n' > $t/cpuinfo-svm + printf 'flags : fpu vme sse\n' > $t/cpuinfo-none + + machine() { + env -i PATH=$t/bin:$PATH HOME=/home/user XDG_STATE_HOME=/state \ + KOMOREBI_REPO=$t/repo RUN_KDE_GUEST=run-kde-x86_64 \ + RUN_KDE_RUNNER_BIN=/nix/store/fake-nixos-vm/bin/run-komorebi-demo-vm \ + RUN_KDE_CPUINFO=$t/cpuinfo-svm RUN_KDE_STORE_DIR=$t RUN_KDE_FREE_GB=100 \ + RUN_KDE_DRI_DIR=$t/dri RUN_KDE_CPUS=12 RUN_KDE_FUSE_DEVICE=$t/fuse \ + "$@" + } + plain() { sed "s|$t|@T@|g"; } + ''; + + # a WSL 2 with WSLg, 16 GB of memory and KVM; and a Linux machine with none of WSL + wslg = "WAYLAND_DISPLAY=wayland-0 DISPLAY=:0 RUN_KDE_OSRELEASE=$t/osrelease-wsl RUN_KDE_KVM_DEVICE=$t/kvm RUN_KDE_MEMINFO=$t/meminfo"; + linux = "RUN_KDE_OSRELEASE=$t/osrelease-linux RUN_KDE_KVM_DEVICE=$t/kvm RUN_KDE_MEMINFO=$t/meminfo RUN_KDE_HOST_RENDERER="; + + # runs `command`, which has to exit with `status` and print what `run-kde/NAME.expected` has + scenario = name: status: command: '' + echo "== ${name}" + set +e + ${command} > ${name}.raw 2>&1 + got=$? + set -e + plain < ${name}.raw > ${name}.out + if [ "$got" != ${toString status} ]; then + echo "${name}: the exit status is $got, not ${toString status}" + cat ${name}.out + exit 1 + fi + diff -u ${./run-kde + "/${name}.expected"} ${name}.out + ''; + + check = + name: scenarios: + pkgs.runCommand name { nativeBuildInputs = [ pkgs.diffutils ]; } '' + ${setup} + ${builtins.concatStringsSep "\n" scenarios} + touch $out + ''; +in +{ + run-kde-dry-run = check "run-kde-dry-run" [ + (scenario "dry-run-wslg-gpu" 0 + ''machine ${wslg} RUN_KDE_DXG_DEVICE=$t/dxg "RUN_KDE_HOST_RENDERER=D3D12 (AMD Radeon RX 7800 XT)" ${exe} --dry-run'' + ) + (scenario "dry-run-wslg-no-gpu" 0 + "machine ${wslg} RUN_KDE_DXG_DEVICE=$t/absent RUN_KDE_HOST_RENDERER= ${exe} --dry-run" + ) + (scenario "dry-run-wslg-software-gl" 0 + ''machine ${wslg} RUN_KDE_DXG_DEVICE=$t/dxg "RUN_KDE_HOST_RENDERER=llvmpipe (LLVM 21.1.8, 256 bits)" ${exe} --dry-run'' + ) + (scenario "dry-run-headless" 0 "machine ${linux} RUN_KDE_MEMINFO=$t/meminfo-small ${exe} --dry-run") + (scenario "dry-run-tcg" 0 "machine ${linux} RUN_KDE_KVM_DEVICE=$t/absent ${exe} --dry-run --tcg") + ]; + + run-kde-doctor = check "run-kde-doctor" [ + (scenario "doctor-wslg-gpu" 0 + ''machine ${wslg} RUN_KDE_DXG_DEVICE=$t/dxg "RUN_KDE_HOST_RENDERER=D3D12 (AMD Radeon RX 7800 XT)" RUN_KDE_WSLCONFIG=$t/none ${exe} --doctor'' + ) + (scenario "doctor-wsl-no-kvm" 1 + "machine WAYLAND_DISPLAY=w RUN_KDE_OSRELEASE=$t/osrelease-wsl RUN_KDE_KVM_DEVICE=$t/absent RUN_KDE_CPUINFO=$t/cpuinfo-none RUN_KDE_MEMINFO=$t/meminfo-small RUN_KDE_DXG_DEVICE=$t/absent RUN_KDE_HOST_RENDERER= RUN_KDE_WSLCONFIG=$t/none ${exe} --doctor" + ) + (scenario "doctor-kvm-denied" 1 "machine ${linux} RUN_KDE_KVM_DEVICE=$t/kvm-denied ${exe} --doctor") + (scenario "doctor-fuse-missing" 0 "machine ${linux} RUN_KDE_FUSE_DEVICE=$t/absent ${exe} --doctor") + (scenario "doctor-fuse-denied" 0 + "machine ${linux} RUN_KDE_FUSE_DEVICE=$t/kvm-denied ${exe} --doctor" + ) + (scenario "doctor-virtiofsd-broken" 1 + "machine ${linux} RUN_KDE_VIRTIOFSD=$t/bin/virtiofsd-broken ${exe} --doctor" + ) + ]; +} diff --git a/nix/tests/run-kde/doctor-fuse-denied.expected b/nix/tests/run-kde/doctor-fuse-denied.expected new file mode 100644 index 00000000..fa9a90a5 --- /dev/null +++ b/nix/tests/run-kde/doctor-fuse-denied.expected @@ -0,0 +1,15 @@ +run-kde: what the guest needs, and what this machine has + ✓ this is Linux, not WSL 2 (6.18.0) + ✓ nix is installed + ✓ KVM: @T@/kvm can be used + ! FUSE: @T@/kvm-denied is there and this user cannot open it; virtiofsd does not use it, so the guest can still start + on NixOS, services.udev.extraRules = ''KERNEL=="fuse", MODE="0666"''; + for now: sudo chmod 666 @T@/kvm-denied + ✓ virtiofsd: starts as this user and serves a directory + ✓ memory: this machine has 16384 MB, and the guest gets 8192 MB + ✓ cores: this machine has 12, and the guest gets 6 + ✓ disk: 100 GB are free for the Nix store (@T@) + ! there is no window to show the guest in (neither WAYLAND_DISPLAY nor DISPLAY is set), so the guest is reached over RDP + ! GPU: there is no render node in @T@/dri, so the guest renders on its CPUs + ✓ the guest will not use the GPU: there is no window to show it in (the display is rdp) +run-kde: 0 error(s), 3 warning(s) diff --git a/nix/tests/run-kde/doctor-fuse-missing.expected b/nix/tests/run-kde/doctor-fuse-missing.expected new file mode 100644 index 00000000..29ec050b --- /dev/null +++ b/nix/tests/run-kde/doctor-fuse-missing.expected @@ -0,0 +1,14 @@ +run-kde: what the guest needs, and what this machine has + ✓ this is Linux, not WSL 2 (6.18.0) + ✓ nix is installed + ✓ KVM: @T@/kvm can be used + ! FUSE: @T@/absent does not exist; virtiofsd does not use it, so the guest can still start + on NixOS, boot.kernelModules = [ "fuse" ]; + ✓ virtiofsd: starts as this user and serves a directory + ✓ memory: this machine has 16384 MB, and the guest gets 8192 MB + ✓ cores: this machine has 12, and the guest gets 6 + ✓ disk: 100 GB are free for the Nix store (@T@) + ! there is no window to show the guest in (neither WAYLAND_DISPLAY nor DISPLAY is set), so the guest is reached over RDP + ! GPU: there is no render node in @T@/dri, so the guest renders on its CPUs + ✓ the guest will not use the GPU: there is no window to show it in (the display is rdp) +run-kde: 0 error(s), 3 warning(s) diff --git a/nix/tests/run-kde/doctor-kvm-denied.expected b/nix/tests/run-kde/doctor-kvm-denied.expected new file mode 100644 index 00000000..2ab4b937 --- /dev/null +++ b/nix/tests/run-kde/doctor-kvm-denied.expected @@ -0,0 +1,15 @@ +run-kde: what the guest needs, and what this machine has + ✓ this is Linux, not WSL 2 (6.18.0) + ✓ nix is installed + ✗ KVM: @T@/kvm-denied is there and this user cannot open it + put this user in the group kvm: on NixOS, users.users..extraGroups = [ "kvm" ]; + then open a new login (on WSL: run `wsl --shutdown` in PowerShell and start it again) + ✓ FUSE: @T@/fuse can be opened + ✓ virtiofsd: starts as this user and serves a directory + ✓ memory: this machine has 16384 MB, and the guest gets 8192 MB + ✓ cores: this machine has 12, and the guest gets 6 + ✓ disk: 100 GB are free for the Nix store (@T@) + ! there is no window to show the guest in (neither WAYLAND_DISPLAY nor DISPLAY is set), so the guest is reached over RDP + ! GPU: there is no render node in @T@/dri, so the guest renders on its CPUs + ✓ the guest will not use the GPU: there is no window to show it in (the display is rdp) +run-kde: 1 thing(s) to fix first diff --git a/nix/tests/run-kde/doctor-virtiofsd-broken.expected b/nix/tests/run-kde/doctor-virtiofsd-broken.expected new file mode 100644 index 00000000..8a0a0d15 --- /dev/null +++ b/nix/tests/run-kde/doctor-virtiofsd-broken.expected @@ -0,0 +1,17 @@ +run-kde: what the guest needs, and what this machine has + ✓ this is Linux, not WSL 2 (6.18.0) + ✓ nix is installed + ✓ KVM: @T@/kvm can be used + ✓ FUSE: @T@/fuse can be opened + ✗ virtiofsd: does not start as this user, and the guest mounts its /nix/store through it + virtiofsd said: [ERROR virtiofsd] Error creating the vhost-user socket: Address in use (os error 98) + virtiofsd ran with the options of the guest's runner and could not serve a directory: the lines above say why. + A socket path is 107 bytes at most, so a long $TMPDIR is one cause: TMPDIR=/tmp run-kde + Say what virtiofsd printed when you ask for help: the guest cannot start without it + ✓ memory: this machine has 16384 MB, and the guest gets 8192 MB + ✓ cores: this machine has 12, and the guest gets 6 + ✓ disk: 100 GB are free for the Nix store (@T@) + ! there is no window to show the guest in (neither WAYLAND_DISPLAY nor DISPLAY is set), so the guest is reached over RDP + ! GPU: there is no render node in @T@/dri, so the guest renders on its CPUs + ✓ the guest will not use the GPU: there is no window to show it in (the display is rdp) +run-kde: 1 thing(s) to fix first diff --git a/nix/tests/run-kde/doctor-wsl-no-kvm.expected b/nix/tests/run-kde/doctor-wsl-no-kvm.expected new file mode 100644 index 00000000..543913b9 --- /dev/null +++ b/nix/tests/run-kde/doctor-wsl-no-kvm.expected @@ -0,0 +1,20 @@ +run-kde: what the guest needs, and what this machine has + ✓ this is WSL 2 (5.15.167.4-microsoft-standard-WSL2) + ✓ nix is installed + ✗ KVM: @T@/absent does not exist, and the guest needs it + the CPU's virtualization flags (svm or vmx) are not visible here, so nested virtualization is off + in %UserProfile%\.wslconfig, under [wsl2], set: nestedVirtualization=true + and turn on SVM (AMD) or VT-x (Intel) in the firmware setup of the machine + ✓ FUSE: @T@/fuse can be opened + ✓ virtiofsd: starts as this user and serves a directory + ! memory: this machine has 6144 MB, so the guest gets 4096 MB (8192 is comfortable) + in %UserProfile%\.wslconfig, under [wsl2], set: memory=12GB + ✓ cores: this machine has 12, and the guest gets 6 + ✓ disk: 100 GB are free for the Nix store (@T@) + ✓ a window can be shown (WAYLAND_DISPLAY=w) + ! Windows interop: mstsc.exe or cmdkey.exe cannot be run, which --rdp needs + on NixOS-WSL, wsl.interop.includePath = true; (and wsl.interop.register = true;) + ! GPU: there is no @T@/absent, so the guest renders on its CPUs + install the Windows GPU driver with WSL support; on NixOS-WSL, wsl.useWindowsDriver = true; + ✓ the guest will not use the GPU: there is no @T@/absent, so WSL has no GPU +run-kde: 1 thing(s) to fix first diff --git a/nix/tests/run-kde/doctor-wslg-gpu.expected b/nix/tests/run-kde/doctor-wslg-gpu.expected new file mode 100644 index 00000000..9828f4e7 --- /dev/null +++ b/nix/tests/run-kde/doctor-wslg-gpu.expected @@ -0,0 +1,16 @@ +run-kde: what the guest needs, and what this machine has + ✓ this is WSL 2 (5.15.167.4-microsoft-standard-WSL2) + ✓ nix is installed + ✓ KVM: @T@/kvm can be used + ✓ FUSE: @T@/fuse can be opened + ✓ virtiofsd: starts as this user and serves a directory + ✓ memory: this machine has 16384 MB, and the guest gets 8192 MB + ✓ cores: this machine has 12, and the guest gets 6 + ✓ disk: 100 GB are free for the Nix store (@T@) + ✓ a window can be shown (WAYLAND_DISPLAY=wayland-0 DISPLAY=:0) + ! Windows interop: mstsc.exe or cmdkey.exe cannot be run, which --rdp needs + on NixOS-WSL, wsl.interop.includePath = true; (and wsl.interop.register = true;) + ✓ GPU: @T@/dxg is there + ✓ GPU: the host's OpenGL renderer is D3D12 (AMD Radeon RX 7800 XT) + ✓ the guest will use the GPU: @T@/dxg is there and the OpenGL of the host is D3D12 (AMD Radeon RX 7800 XT) +run-kde: 0 error(s), 1 warning(s) diff --git a/nix/tests/run-kde/dry-run-headless.expected b/nix/tests/run-kde/dry-run-headless.expected new file mode 100644 index 00000000..693ff1bd --- /dev/null +++ b/nix/tests/run-kde/dry-run-headless.expected @@ -0,0 +1,12 @@ +run-kde would start run-kde-x86_64: + host: Linux, KVM yes + display: rdp + gpu: off, because there is no window to show it in (the display is rdp) + environment: + NIX_DISK_IMAGE=/state/komorebi-run-kde/disk.qcow2 + KOMOREBI_REPO=@T@/repo + RUN_KDE_STATE=/state/komorebi-run-kde + RUN_KDE_MEMORY=4096 + QEMU_NET_OPTS=hostfwd=tcp:127.0.0.1:2223-:22,hostfwd=tcp:127.0.0.1:3389-:3389 + QEMU_OPTS=-cpu host -display none -vga none -device virtio-vga,xres=1280,yres=800 + command: /nix/store/fake-nixos-vm/bin/run-komorebi-demo-vm -m 4096 -smp 6 diff --git a/nix/tests/run-kde/dry-run-tcg.expected b/nix/tests/run-kde/dry-run-tcg.expected new file mode 100644 index 00000000..19f0c3e5 --- /dev/null +++ b/nix/tests/run-kde/dry-run-tcg.expected @@ -0,0 +1,12 @@ +run-kde would start run-kde-x86_64: + host: Linux, KVM no + display: rdp + gpu: off, because there is no window to show it in (the display is rdp) + environment: + NIX_DISK_IMAGE=/state/komorebi-run-kde/disk.qcow2 + KOMOREBI_REPO=@T@/repo + RUN_KDE_STATE=/state/komorebi-run-kde + RUN_KDE_MEMORY=8192 + QEMU_NET_OPTS=hostfwd=tcp:127.0.0.1:2223-:22,hostfwd=tcp:127.0.0.1:3389-:3389 + QEMU_OPTS=-display none -vga none -device virtio-vga,xres=1280,yres=800 + command: /nix/store/fake-nixos-vm/bin/run-komorebi-demo-vm -m 8192 -smp 6 diff --git a/nix/tests/run-kde/dry-run-wslg-gpu.expected b/nix/tests/run-kde/dry-run-wslg-gpu.expected new file mode 100644 index 00000000..0c92fbf9 --- /dev/null +++ b/nix/tests/run-kde/dry-run-wslg-gpu.expected @@ -0,0 +1,12 @@ +run-kde would start run-kde-x86_64: + host: WSL 2, KVM yes + display: gtk + gpu: on, because @T@/dxg is there and the OpenGL of the host is D3D12 (AMD Radeon RX 7800 XT) + environment: + NIX_DISK_IMAGE=/state/komorebi-run-kde/disk.qcow2 + KOMOREBI_REPO=@T@/repo + RUN_KDE_STATE=/state/komorebi-run-kde + RUN_KDE_MEMORY=8192 + QEMU_NET_OPTS=hostfwd=tcp:127.0.0.1:2223-:22,hostfwd=tcp:127.0.0.1:3389-:3389 + QEMU_OPTS=-cpu host -display gtk,gl=on -vga none -device virtio-vga-gl,xres=1280,yres=800 + command: /nix/store/fake-nixos-vm/bin/run-komorebi-demo-vm -m 8192 -smp 6 diff --git a/nix/tests/run-kde/dry-run-wslg-no-gpu.expected b/nix/tests/run-kde/dry-run-wslg-no-gpu.expected new file mode 100644 index 00000000..6e28d3e6 --- /dev/null +++ b/nix/tests/run-kde/dry-run-wslg-no-gpu.expected @@ -0,0 +1,12 @@ +run-kde would start run-kde-x86_64: + host: WSL 2, KVM yes + display: gtk + gpu: off, because there is no @T@/absent, so WSL has no GPU + environment: + NIX_DISK_IMAGE=/state/komorebi-run-kde/disk.qcow2 + KOMOREBI_REPO=@T@/repo + RUN_KDE_STATE=/state/komorebi-run-kde + RUN_KDE_MEMORY=8192 + QEMU_NET_OPTS=hostfwd=tcp:127.0.0.1:2223-:22,hostfwd=tcp:127.0.0.1:3389-:3389 + QEMU_OPTS=-cpu host -display gtk -vga none -device virtio-vga,xres=1280,yres=800 + command: /nix/store/fake-nixos-vm/bin/run-komorebi-demo-vm -m 8192 -smp 6 diff --git a/nix/tests/run-kde/dry-run-wslg-software-gl.expected b/nix/tests/run-kde/dry-run-wslg-software-gl.expected new file mode 100644 index 00000000..b708fbda --- /dev/null +++ b/nix/tests/run-kde/dry-run-wslg-software-gl.expected @@ -0,0 +1,12 @@ +run-kde would start run-kde-x86_64: + host: WSL 2, KVM yes + display: gtk + gpu: off, because the host's OpenGL is software (llvmpipe (LLVM 21.1.8, 256 bits)) + environment: + NIX_DISK_IMAGE=/state/komorebi-run-kde/disk.qcow2 + KOMOREBI_REPO=@T@/repo + RUN_KDE_STATE=/state/komorebi-run-kde + RUN_KDE_MEMORY=8192 + QEMU_NET_OPTS=hostfwd=tcp:127.0.0.1:2223-:22,hostfwd=tcp:127.0.0.1:3389-:3389 + QEMU_OPTS=-cpu host -display gtk -vga none -device virtio-vga,xres=1280,yres=800 + command: /nix/store/fake-nixos-vm/bin/run-komorebi-demo-vm -m 8192 -smp 6 diff --git a/scripts/run-kde b/scripts/run-kde new file mode 100755 index 00000000..b695a94b --- /dev/null +++ b/scripts/run-kde @@ -0,0 +1,668 @@ +#!/usr/bin/env bash +# run-kde: start the komorebi demo guest (nix/dev/run-kde.nix) in a virtual machine, so that anyone +# can try tiling by hand. docs/dev/utm.md says what the guest takes from it; this is the program. +# +# nix run .#run-kde -- [options] or just run-kde [options] +# +# The flake wraps this file with the guest's runner baked in (RUN_KDE_RUNNER_BIN, RUN_KDE_GUEST) and +# the tools it needs on its PATH. Everything it reads from the machine it reads through a variable +# that a test can set (RUN_KDE_*, below), so that `--dry-run` gives the same answer on a machine +# that has none of it: the check `run-kde-dry-run` runs it in a sandbox. + +set -euo pipefail + +usage() { + cat <<'EOF' +Usage: run-kde [options] + +Starts a NixOS guest with a Plasma session in which komorebi tiles a few windows, and shows it in +a window (QEMU through WSLg, where there is one) or over RDP. The first run builds komorebi from +source and downloads Plasma: there is no binary cache. + + --doctor check every prerequisite and say how to fix what is missing; exit 1 if + one is, and always when there is no KVM + --dry-run print what would be started, and start nothing + --build-only build the guest and stop + --reset delete the state directory (the disk, the password and the key) and stop + --shell no window: this terminal is the guest's console (--display none --gpu off) + --rdp on WSL 2, open Windows' own RDP client (mstsc) when the guest is up + --display KIND gtk, vnc, rdp or none (default: gtk where there is a window, else rdp) + --gpu MODE auto, on or off (default: auto): on renders in the guest with the host's GPU + (virtio-vga-gl); off renders on the guest's CPUs (virtio-vga) + --tcg allow software emulation when there is no KVM (too slow to try anything) + --memory MB the guest's memory (default: what this machine has, 4096 to 8192) + --cores N the guest's cores (default: what this machine has, up to 6) + --size GB the size of the guest's disk when it is made (default: 20, sparse) + --resolution WxH the size of the guest's screen (default: 1280x800) + --ssh-port PORT the host's port for ssh into the guest (default: 2223) + --rdp-port PORT the host's port for RDP into the guest (default: 3389) + -h, --help this text + +The repository the guest mounts at /repo is the current checkout, or KOMOREBI_REPO. +EOF +} + +# what the flake bakes in, and a guess for a run of the file by hand +guest=${RUN_KDE_GUEST:-run-kde-$(uname -m)} +runner_bin=${RUN_KDE_RUNNER_BIN:-} + +# what this machine says about itself: each can be set by a test +kvm_device=${RUN_KDE_KVM_DEVICE:-/dev/kvm} +fuse_device=${RUN_KDE_FUSE_DEVICE:-/dev/fuse} +virtiofsd_bin=${RUN_KDE_VIRTIOFSD:-} +dxg_device=${RUN_KDE_DXG_DEVICE:-/dev/dxg} +dri_dir=${RUN_KDE_DRI_DIR:-/dev/dri} +meminfo=${RUN_KDE_MEMINFO:-/proc/meminfo} +cpuinfo=${RUN_KDE_CPUINFO:-/proc/cpuinfo} +osrelease=${RUN_KDE_OSRELEASE:-/proc/sys/kernel/osrelease} +sys_module_dir=${RUN_KDE_SYS_MODULE_DIR:-/sys/module} +store_dir=${RUN_KDE_STORE_DIR:-/nix} +wslconfig=${RUN_KDE_WSLCONFIG:-} + +mode=run +display=auto +gpu=auto +tcg=0 +rdp=0 +shell=0 +memory= +cores= +size=20 +resolution=1280x800 +ssh_port=2223 +rdp_port=3389 + +while [ $# -gt 0 ]; do + case $1 in + --doctor) mode=doctor ;; + --dry-run) mode=dry-run ;; + --build-only) mode=build-only ;; + --reset) mode=reset ;; + --shell) shell=1 ;; + --rdp) rdp=1 ;; + --tcg) tcg=1 ;; + --display | --gpu | --memory | --cores | --size | --resolution | --ssh-port | --rdp-port) + [ $# -ge 2 ] || { + echo "run-kde: $1 needs a value" >&2 + exit 2 + } + case $1 in + --display) display=$2 ;; + --gpu) gpu=$2 ;; + --memory) memory=$2 ;; + --cores) cores=$2 ;; + --size) size=$2 ;; + --resolution) resolution=$2 ;; + --ssh-port) ssh_port=$2 ;; + --rdp-port) rdp_port=$2 ;; + esac + shift + ;; + -h | --help) + usage + exit 0 + ;; + *) + echo "run-kde: unknown option $1 (see --help)" >&2 + exit 2 + ;; + esac + shift +done + +case $display in auto | gtk | vnc | rdp | none) ;; *) + echo "run-kde: --display is gtk, vnc, rdp or none" >&2 + exit 2 + ;; +esac +case $gpu in auto | on | off) ;; *) + echo "run-kde: --gpu is auto, on or off" >&2 + exit 2 + ;; +esac +for number in "$size" "$ssh_port" "$rdp_port" ${memory:+"$memory"} ${cores:+"$cores"}; do + case $number in '' | *[!0-9]*) + echo "run-kde: expected a number, got $number" >&2 + exit 2 + ;; + esac +done +case $resolution in [0-9]*x[0-9]*) ;; *) + echo "run-kde: --resolution is WIDTHxHEIGHT" >&2 + exit 2 + ;; +esac +if [ "$shell" = 1 ]; then + display=none + gpu=off +fi + +state_dir=${XDG_STATE_HOME:-$HOME/.local/state}/komorebi-run-kde + +if [ "$mode" = reset ]; then + if [ -e "$state_dir" ]; then + rm -rf "$state_dir" + echo "run-kde: deleted $state_dir" + else + echo "run-kde: there is no $state_dir" + fi + exit 0 +fi + +# --------------------------------------------------------------------------------------------- +# what the machine is + +wsl=0 +if grep -qi microsoft "$osrelease" 2> /dev/null || [ -n "${WSL_DISTRO_NAME:-}" ]; then + wsl=1 +fi + +kvm=missing +if [ -e "$kvm_device" ]; then + if [ -r "$kvm_device" ] && [ -w "$kvm_device" ]; then kvm=ok; else kvm=denied; fi +fi + +virt=none +if grep -qw svm "$cpuinfo" 2> /dev/null; then + virt=svm +elif grep -qw vmx "$cpuinfo" 2> /dev/null; then + virt=vmx +fi + +have_window=0 +if [ -n "${WAYLAND_DISPLAY:-}" ] || [ -n "${DISPLAY:-}" ]; then have_window=1; fi + +mem_mb=$(awk '/^MemTotal:/ { print int($2 / 1024) }' "$meminfo" 2> /dev/null || true) +mem_mb=${mem_mb:-0} +cpus=${RUN_KDE_CPUS:-$(nproc 2> /dev/null || echo 1)} + +# a device the host's GPU shows up as: WSL has /dev/dxg, a Linux machine a render node +gpu_device= +if [ "$wsl" = 1 ]; then + if [ -e "$dxg_device" ]; then gpu_device=$dxg_device; fi +else + for node in "$dri_dir"/renderD*; do + if [ -e "$node" ]; then + gpu_device=$node + break + fi + done +fi + +host_renderer() { + if [ "${RUN_KDE_HOST_RENDERER+set}" = set ]; then + printf '%s\n' "$RUN_KDE_HOST_RENDERER" + elif [ -n "${DISPLAY:-}" ] && command -v glxinfo > /dev/null 2>&1; then + glxinfo -B 2> /dev/null | sed -n 's/^OpenGL renderer string: //p' | head -n 1 || true + elif [ -n "${WAYLAND_DISPLAY:-}" ] && command -v eglinfo > /dev/null 2>&1; then + eglinfo -B 2> /dev/null | sed -n 's/^OpenGL core profile renderer: //p' | head -n 1 || true + fi +} + +free_gb() { + if [ -n "${RUN_KDE_FREE_GB:-}" ]; then + echo "$RUN_KDE_FREE_GB" + else + df -Pk "$1" 2> /dev/null | awk 'NR == 2 { print int($4 / 1048576) }' || echo 0 + fi +} + +# --------------------------------------------------------------------------------------------- +# what the guest gets + +guest_memory=$memory +if [ -z "$guest_memory" ]; then + guest_memory=$((mem_mb - 2048)) + [ "$guest_memory" -ge 4096 ] || guest_memory=4096 + [ "$guest_memory" -le 8192 ] || guest_memory=8192 +fi +guest_cores=$cores +if [ -z "$guest_cores" ]; then + guest_cores=$cpus + [ "$guest_cores" -le 6 ] || guest_cores=6 +fi + +if [ "$display" = auto ]; then + if [ "$have_window" = 1 ]; then display=gtk; else display=rdp; fi +fi + +renderer=$(host_renderer) +gpu_on=0 +case $gpu in + off) gpu_why="turned off with --gpu off" ;; + on) + gpu_on=1 + gpu_why="turned on with --gpu on" + ;; + auto) + if [ "$display" != gtk ]; then + gpu_why="there is no window to show it in (the display is $display)" + elif [ -z "$gpu_device" ]; then + if [ "$wsl" = 1 ]; then + gpu_why="there is no $dxg_device, so WSL has no GPU" + else + gpu_why="there is no render node in $dri_dir" + fi + else + case $renderer in + *llvmpipe* | *softpipe* | *swrast*) + gpu_why="the host's OpenGL is software ($renderer)" + ;; + *) + gpu_on=1 + gpu_why="$gpu_device is there${renderer:+ and the OpenGL of the host is $renderer}" + ;; + esac + fi + ;; +esac + +# the virtiofsd that the guest's runner starts for each of its shares (the runner names it by its +# store path), else the one on PATH +find_virtiofsd() { + local named= + if [ -n "$virtiofsd_bin" ]; then + printf '%s\n' "$virtiofsd_bin" + return 0 + fi + if [ -n "$runner_bin" ] && [ -r "$runner_bin" ]; then + named=$(grep -o '/nix/store/[a-z0-9]*-virtiofsd-[^/ ]*/bin/virtiofsd' "$runner_bin" 2> /dev/null | head -n 1 || true) + fi + if [ -n "$named" ] && [ -x "$named" ]; then + printf '%s\n' "$named" + else + command -v virtiofsd || true + fi +} + +# --------------------------------------------------------------------------------------------- +# the checks + +errors=0 +warnings=0 +verbose=0 +if [ "$mode" = doctor ]; then verbose=1; fi + +ok() { + if [ "$verbose" = 1 ]; then printf ' ✓ %s\n' "$1"; fi +} +bad() { + errors=$((errors + 1)) + printf ' ✗ %s\n' "$1" + shift + for line in "$@"; do printf ' %s\n' "$line"; done +} +warn() { + warnings=$((warnings + 1)) + printf ' ! %s\n' "$1" + shift + for line in "$@"; do printf ' %s\n' "$line"; done +} + +# The guest mounts its /nix/store and its shared directories by virtiofs: for each of them the +# runner starts a virtiofsd as this user, and QEMU hands the guest's virtio-fs device its socket. +# `wsl2-probe` (docs/dev/wsl2-probe.md, P6) runs the same chain with a guest; its fixes are these. +check_virtiofs() { + local virtiofsd tmp pid up line lines + # virtiofsd does not open /dev/fuse (it speaks FUSE to the guest over its socket): a warning + if [ ! -e "$fuse_device" ]; then + fuse_fix=("on NixOS, boot.kernelModules = [ \"fuse\" ];") + if [ "$wsl" = 1 ]; then + fuse_fix+=("the kernel of WSL 2 has FUSE built in, so a kernel= line in %UserProfile%\\.wslconfig under [wsl2]" + "that names another kernel is the likely cause") + fi + warn "FUSE: $fuse_device does not exist; virtiofsd does not use it, so the guest can still start" "${fuse_fix[@]}" + elif [ -r "$fuse_device" ] && [ -w "$fuse_device" ]; then + ok "FUSE: $fuse_device can be opened" + else + warn "FUSE: $fuse_device is there and this user cannot open it; virtiofsd does not use it, so the guest can still start" \ + "on NixOS, services.udev.extraRules = ''KERNEL==\"fuse\", MODE=\"0666\"'';" \ + "for now: sudo chmod 666 $fuse_device" + fi + + virtiofsd=$(find_virtiofsd) + if [ -z "$virtiofsd" ]; then + bad "virtiofsd: the program is not there, and the guest mounts its /nix/store through it" \ + "start run-kde as nix run .#run-kde, which brings the virtiofsd of nixpkgs" + return 0 + fi + # the options of nixpkgs' runner, for a directory that is empty + tmp=$(mktemp -d) + RUST_LOG=error "$virtiofsd" --socket-path="$tmp/socket" --shared-dir="$tmp" --readonly \ + --sandbox=none --seccomp=none --cache=always --no-announce-submounts \ + --translate-uid=host:65534:0:1 --translate-gid=host:65534:0:1 > "$tmp/log" 2>&1 < /dev/null & + pid=$! + up=0 + for _ in $(seq 1 50); do + if [ -S "$tmp/socket" ]; then + up=1 + break + fi + kill -0 "$pid" 2> /dev/null || break + sleep 0.1 + done + kill "$pid" 2> /dev/null || true + wait "$pid" 2> /dev/null || true + if [ "$up" = 1 ]; then + ok "virtiofsd: starts as this user and serves a directory" + else + lines=() + while IFS= read -r line; do lines+=("virtiofsd said: $line"); done < <(head -n 6 "$tmp/log") + bad "virtiofsd: does not start as this user, and the guest mounts its /nix/store through it" \ + "${lines[@]}" \ + "virtiofsd ran with the options of the guest's runner and could not serve a directory: the lines above say why." \ + "A socket path is 107 bytes at most, so a long \$TMPDIR is one cause: TMPDIR=/tmp run-kde" \ + "Say what virtiofsd printed when you ask for help: the guest cannot start without it" + fi + rm -rf "$tmp" +} + +check_everything() { + if [ "$wsl" = 1 ]; then + ok "this is WSL 2 ($(cat "$osrelease" 2> /dev/null || echo unknown))" + else + ok "this is Linux, not WSL 2 ($(cat "$osrelease" 2> /dev/null || echo unknown))" + fi + + if command -v nix > /dev/null 2>&1; then + ok "nix is installed" + else + bad "nix is not installed" "install Nix with flakes enabled: https://nixos.org/download" + fi + + case $kvm in + ok) ok "KVM: $kvm_device can be used" ;; + denied) + bad "KVM: $kvm_device is there and this user cannot open it" \ + "put this user in the group kvm: on NixOS, users.users..extraGroups = [ \"kvm\" ];" \ + "then open a new login (on WSL: run \`wsl --shutdown\` in PowerShell and start it again)" + ;; + missing) + kvm_fix=() + if [ "$virt" = none ]; then + kvm_fix+=("the CPU's virtualization flags (svm or vmx) are not visible here, so nested virtualization is off") + if [ "$wsl" = 1 ]; then + kvm_fix+=("in %UserProfile%\\.wslconfig, under [wsl2], set: nestedVirtualization=true") + fi + kvm_fix+=("and turn on SVM (AMD) or VT-x (Intel) in the firmware setup of the machine") + else + kvm_fix+=("the CPU has $virt, so the kernel has no KVM or has not loaded it") + if [ "$virt" = svm ] && [ ! -e "$sys_module_dir/kvm_amd" ]; then + kvm_fix+=("kvm_amd is not loaded: modprobe kvm_amd (on NixOS, boot.kernelModules = [ \"kvm-amd\" ];)") + elif [ "$virt" = vmx ] && [ ! -e "$sys_module_dir/kvm_intel" ]; then + kvm_fix+=("kvm_intel is not loaded: modprobe kvm_intel (on NixOS, boot.kernelModules = [ \"kvm-intel\" ];)") + fi + if [ "$wsl" = 1 ]; then + kvm_fix+=("in %UserProfile%\\.wslconfig, under [wsl2], set: nestedVirtualization=true, then run \`wsl --shutdown\`") + fi + fi + if [ "$tcg" = 1 ]; then + warn "KVM: $kvm_device does not exist; --tcg lets the guest start without it, and Plasma is too slow to try anything that way" "${kvm_fix[@]}" + else + bad "KVM: $kvm_device does not exist, and the guest needs it" "${kvm_fix[@]}" + fi + ;; + esac + + check_virtiofs + + if [ "$mem_mb" -lt 5120 ]; then + bad "memory: this machine has $mem_mb MB, and the guest needs at least 4096 MB of it" \ + "in %UserProfile%\\.wslconfig, under [wsl2], set: memory=12GB (WSL gets half of the machine's memory by default)" + elif [ "$mem_mb" -lt 10240 ] && [ -z "$memory" ]; then + warn "memory: this machine has $mem_mb MB, so the guest gets $guest_memory MB (8192 is comfortable)" \ + "in %UserProfile%\\.wslconfig, under [wsl2], set: memory=12GB" + else + ok "memory: this machine has $mem_mb MB, and the guest gets $guest_memory MB" + fi + ok "cores: this machine has $cpus, and the guest gets $guest_cores" + + store_free=$(free_gb "$store_dir") + state_parent=${state_dir%/*} + [ -d "$state_parent" ] || state_parent=$HOME + state_free=$(free_gb "$state_parent") + if [ "$store_free" -lt 12 ]; then + bad "disk: $store_free GB are free for the Nix store ($store_dir), and the guest's closure is about 11 GB" \ + "free some room (nix-collect-garbage -d), or give WSL's disk more: \`wsl --manage --resize \`" + elif [ "$store_free" -lt 25 ]; then + warn "disk: $store_free GB are free for the Nix store ($store_dir): the first run needs about 16 GB" \ + "nix-collect-garbage -d frees room" + else + ok "disk: $store_free GB are free for the Nix store ($store_dir)" + fi + if [ "$state_free" -lt "$size" ]; then + warn "disk: $state_free GB are free for the guest's disk in $state_dir, which can grow to $size GB" + fi + + if [ "$have_window" = 1 ]; then + window= + if [ -n "${WAYLAND_DISPLAY:-}" ]; then window="WAYLAND_DISPLAY=$WAYLAND_DISPLAY"; fi + if [ -n "${DISPLAY:-}" ]; then window="${window:+$window }DISPLAY=$DISPLAY"; fi + ok "a window can be shown ($window)" + elif [ "$wsl" = 1 ]; then + warn "WSLg is not there (neither WAYLAND_DISPLAY nor DISPLAY is set), so the guest is reached over RDP" \ + "WSLg comes with WSL 2 on Windows 11: run \`wsl --update\` in PowerShell, and do not set guiApplications=false in .wslconfig" + else + warn "there is no window to show the guest in (neither WAYLAND_DISPLAY nor DISPLAY is set), so the guest is reached over RDP" + fi + + if [ "$wsl" = 1 ]; then + if command -v mstsc.exe > /dev/null 2>&1 && command -v cmdkey.exe > /dev/null 2>&1; then + ok "Windows interop: mstsc.exe and cmdkey.exe can be run, for --rdp" + else + warn "Windows interop: mstsc.exe or cmdkey.exe cannot be run, which --rdp needs" \ + "on NixOS-WSL, wsl.interop.includePath = true; (and wsl.interop.register = true;)" + fi + fi + + if [ -n "$gpu_device" ]; then + ok "GPU: $gpu_device is there" + elif [ "$wsl" = 1 ]; then + warn "GPU: there is no $dxg_device, so the guest renders on its CPUs" \ + "install the Windows GPU driver with WSL support; on NixOS-WSL, wsl.useWindowsDriver = true;" + else + warn "GPU: there is no render node in $dri_dir, so the guest renders on its CPUs" + fi + if [ -n "$gpu_device" ]; then + case $renderer in + '') warn "GPU: the host's OpenGL renderer is not known (glxinfo and eglinfo say nothing)" ;; + *llvmpipe* | *softpipe* | *swrast*) + warn "GPU: the host's OpenGL is software ($renderer), so the guest renders on its CPUs" \ + "on NixOS-WSL, wsl.useWindowsDriver = true; gives Mesa the Windows driver (d3d12)" + ;; + *) ok "GPU: the host's OpenGL renderer is $renderer" ;; + esac + fi + if [ "$gpu_on" = 1 ]; then + ok "the guest will use the GPU: $gpu_why" + else + ok "the guest will not use the GPU: $gpu_why" + fi + + if [ "$wsl" = 1 ]; then + if [ -z "$wslconfig" ] && command -v cmd.exe > /dev/null 2>&1 && command -v wslpath > /dev/null 2>&1; then + profile=$(cmd.exe /C 'echo %USERPROFILE%' 2> /dev/null | tr -d '\r' || true) + if [ -n "$profile" ]; then wslconfig=$(wslpath -u "$profile")/.wslconfig; fi + fi + if [ -n "$wslconfig" ] && [ -r "$wslconfig" ]; then + if grep -qi '^ *networkingMode *= *mirrored' "$wslconfig"; then + warn "network: .wslconfig has networkingMode=mirrored, which may not forward localhost to the guest's RDP" \ + "ssh and the window do not depend on it; if RDP does not connect, remove that line and run \`wsl --shutdown\`" + else + ok "network: $wslconfig does not set mirrored networking" + fi + fi + fi +} + +if [ "$mode" = doctor ] || [ "$mode" = run ] || [ "$mode" = build-only ]; then + if [ "$mode" = doctor ]; then echo "run-kde: what the guest needs, and what this machine has"; fi + check_everything + if [ "$errors" -gt 0 ]; then + echo "run-kde: $errors thing(s) to fix first" >&2 + exit 1 + fi + if [ "$mode" = doctor ]; then + echo "run-kde: $errors error(s), $warnings warning(s)" + exit 0 + fi +fi + +# --------------------------------------------------------------------------------------------- +# the guest + +repo=${KOMOREBI_REPO:-} +if [ -z "$repo" ]; then + repo=$(git rev-parse --show-toplevel 2> /dev/null || pwd) +fi +if [ ! -e "$repo/flake.nix" ]; then + echo "run-kde: $repo is not a checkout of the repository (there is no flake.nix): run it from one, or set KOMOREBI_REPO" >&2 + exit 1 +fi + +disk=$state_dir/disk.qcow2 + +# the arguments of the runner, and the environment it reads (docs/dev/utm.md says what each does) +if [ "$gpu_on" = 1 ]; then gpu_device_name=virtio-vga-gl; else gpu_device_name=virtio-vga; fi +width=${resolution%x*} +height=${resolution#*x} +qemu_opts=() +if [ "$tcg" != 1 ] || [ "$kvm" = ok ]; then qemu_opts+=(-cpu host); fi +case $display in + gtk) + if [ "$gpu_on" = 1 ]; then qemu_opts+=(-display "gtk,gl=on"); else qemu_opts+=(-display gtk); fi + ;; + vnc) qemu_opts+=(-display none -vnc 127.0.0.1:0) ;; + rdp | none) + if [ "$gpu_on" = 1 ]; then qemu_opts+=(-display egl-headless); else qemu_opts+=(-display none); fi + ;; +esac +qemu_opts+=(-vga none -device "$gpu_device_name,xres=$width,yres=$height") +net_opts="hostfwd=tcp:127.0.0.1:$ssh_port-:22,hostfwd=tcp:127.0.0.1:$rdp_port-:3389" +runner_args=(-m "$guest_memory" -smp "$guest_cores") + +if [ "$wsl" = 1 ]; then host_kind="WSL 2"; else host_kind=Linux; fi +if [ "$kvm" = ok ]; then kvm_word=yes; else kvm_word=no; fi +if [ "$gpu_on" = 1 ]; then + gpu_word=on + gpu_label="GPU (virgl)" +else + gpu_word=off + gpu_label="software (llvmpipe)" +fi + +if [ "$mode" = dry-run ]; then + echo "run-kde would start $guest:" + echo " host: $host_kind, KVM $kvm_word" + echo " display: $display" + echo " gpu: $gpu_word, because $gpu_why" + echo " environment:" + echo " NIX_DISK_IMAGE=$disk" + echo " KOMOREBI_REPO=$repo" + echo " RUN_KDE_STATE=$state_dir" + echo " RUN_KDE_MEMORY=$guest_memory" + echo " QEMU_NET_OPTS=$net_opts" + echo " QEMU_OPTS=${qemu_opts[*]}" + echo " command: ${runner_bin:-} ${runner_args[*]}" + exit 0 +fi + +# the guest's runner, built when it is not there. RUN_KDE_QEMU_OPTS adds to the QEMU options. +if [ -z "$runner_bin" ] || [ ! -x "$runner_bin" ]; then + echo "run-kde: building the guest ($guest). There is no binary cache for komorebi: the first run" + echo "run-kde: builds it from source and downloads about 4 GB of Plasma (11 GB in the Nix store), which takes a while." + runner=$(nix build --no-link --print-out-paths -L "$repo#nixosConfigurations.$guest.config.system.build.vm") + set -- "$runner"/bin/run-*-vm + runner_bin=$1 + if [ ! -x "$runner_bin" ]; then + echo "run-kde: $runner has no runner in bin" >&2 + exit 1 + fi +fi +if [ "$mode" = build-only ]; then + echo "run-kde: the guest is built: $runner_bin" + exit 0 +fi + +# the state: what a run keeps, and what the guest takes from it. Nothing of it is in the Nix store. +umask 077 +mkdir -p "$state_dir" +chmod 700 "$state_dir" +if [ ! -s "$state_dir/rdp-password" ]; then + head -c 15 /dev/urandom | od -An -tx1 | tr -d ' \n' > "$state_dir/rdp-password" +fi +if [ ! -s "$state_dir/ssh_key" ]; then + rm -f "$state_dir/ssh_key" "$state_dir/ssh_key.pub" + ssh-keygen -q -t ed25519 -N '' -C run-kde -f "$state_dir/ssh_key" +fi +if [ ! -e "$disk" ]; then + echo "run-kde: making the guest's disk: $size GB, which takes room as the guest writes to it" + raw=$state_dir/disk.raw + qemu-img create -q -f raw "$raw" "${size}G" + mkfs.ext4 -q -L nixos "$raw" + qemu-img convert -f raw -O qcow2 "$raw" "$disk" + rm -f "$raw" +fi +password=$(cat "$state_dir/rdp-password") + +case $display in + gtk) sees="a window of QEMU, with the guest's screen in it" ;; + vnc) sees="nothing here: connect a VNC client to 127.0.0.1:5900, or RDP below" ;; + rdp) sees="nothing here: connect over RDP, below" ;; + none) sees="nothing but this terminal, which is the guest's console" ;; +esac +cat < /dev/null || true + cmdkey.exe /delete:TERMSRV/localhost > /dev/null 2>&1 || true + fi +} +if [ "$rdp" = 1 ]; then + if [ "$wsl" = 1 ] && command -v mstsc.exe > /dev/null 2>&1 && command -v cmdkey.exe > /dev/null 2>&1; then + trap cleanup EXIT + ( + for _ in $(seq 1 300); do + if ssh -i "$state_dir/ssh_key" -o IdentitiesOnly=yes -o BatchMode=yes -o ConnectTimeout=3 \ + -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \ + -p "$ssh_port" komorebi@127.0.0.1 'systemctl --user is-active --quiet krdp' 2> /dev/null; then + break + fi + sleep 2 + done + cmdkey.exe /generic:TERMSRV/localhost /user:komorebi "/pass:$password" > /dev/null + mstsc.exe "/v:localhost:$rdp_port" + ) & + rdp_helper=$! + else + echo "run-kde: --rdp needs WSL 2 with mstsc.exe and cmdkey.exe (run-kde --doctor says how); use an RDP client of your own with the address above" >&2 + fi +fi + +"$runner_bin" "${runner_args[@]}"