Files
komorebi/nix/dev/wsl2-probe.nix
T
LGUG2Z 9b60a0020d chore(scripts): probe a WSL2 NixOS VM for KVM, WSLg and RDP
The port can only be tried on a Windows machine, through a NixOS virtual machine
in WSL2, which the machines this history is built on do not have. What such a
machine can do for a Plasma guest is not known until something runs there, and
every guess about it is a risk for what comes next. wsl2-probe is that
something: one command, nix run .#wsl2-probe, that answers in about two minutes
what the guest needs to know and prints, for each answer that is not a yes, the
exact change: the .wslconfig lines, the NixOS option, the PowerShell command.

It checks the environment (WSL2, memory, disk, .wslconfig, Nix); KVM (/dev/kvm,
its group, and a tiny guest booted under KVM and under TCG, with the same loop
timed in both and on the machine itself, which must differ by several times);
the display (a QEMU GTK window through WSLg for a few seconds, and whether you
saw it); the GPU (/dev/dxg, the Windows driver libraries, the OpenGL renderer in
four environments, and virtio-vga-gl with gl=on); the Windows side (interop,
mstsc.exe, whether Windows reaches a port that WSL listens on); whether memory,
cores and disk are enough for the real guest; and virtiofs, which the real guest
uses for its /nix/store and every shared directory (/dev/fuse, virtiofsd as the
user with the options of nixpkgs' runner, and a guest that mounts the host's
/nix/store and a writable directory, reads, writes and compares). Each probe
runs apart from the others, so one that fails leaves the rest alone, and the
output ends in a block to paste back, which is also written to
wsl2-probe-report.txt.

The flake wraps the script with QEMU, virtiofsd, Mesa's eglinfo and glxinfo and
socat from nixpkgs, and builds the guest, a kernel from the binary cache, the
two modules of it that virtiofs needs and a static busybox initrd, as
wsl2-probe-guest, so nothing is compiled on the machine. Two checks keep the
script from rotting: wsl2-probe builds the wrapper, which runs ShellCheck over
the script and over the guest's init, and wsl2-probe-dry-run runs --dry-run in
the build sandbox, which has no KVM, no network and no display.
docs/dev/wsl2-probe.md says what each check means and how to read it.
2026-10-28 13:27:11 -07:00

118 lines
3.4 KiB
Nix

{
pkgs,
# the revision the probe says it came from; the checks pass a constant, so that they are the
# same derivations for every commit
rev,
}:
# scripts/wsl2-probe.sh, with the tools it uses from nixpkgs, and the tiny guest it boots.
#
# The guest is a kernel from the binary cache and an initrd made here of a static busybox, the
# kernel modules that virtiofs needs, and the init in wsl2-probe-init.sh: nothing in it is built
# from source, so the first run is a download. docs/dev/wsl2-probe.md says what the probe does.
let
inherit (pkgs) lib;
busybox = pkgs.pkgsStatic.busybox;
# the kernel is the one the real guest boots (the default of NixOS), so that what the probe's
# guest can do with virtiofs is what the real guest's kernel can do
kernel = pkgs.linuxPackages.kernel;
# the file of the kernel package that QEMU boots
kernelImage = if pkgs.stdenv.hostPlatform.isAarch64 then "Image" else "bzImage";
# virtio's PCI transport, FUSE and virtiofs are modules of that kernel, not built in; this is
# the closure of them in the shape the NixOS initrd uses (modules.dep included, for modprobe)
modules = pkgs.makeModulesClosure {
kernel = pkgs.aggregateModules [ (lib.getOutput "modules" kernel) ];
firmware = pkgs.runCommand "wsl2-probe-no-firmware" { } "mkdir -p $out";
rootModules = [
"virtio_pci"
"virtiofs"
];
};
# the loop that the host and the guest both time, which busybox's ash runs in both
loop = pkgs.writeTextFile {
name = "wsl2-probe-loop.sh";
text = builtins.readFile ./wsl2-probe-loop.sh;
checkPhase = ''
${lib.getExe pkgs.shellcheck} --shell=sh "$target"
'';
};
init = pkgs.writeTextFile {
name = "wsl2-probe-init";
executable = true;
text = ''
#!${busybox}/bin/sh
PATH=${busybox}/bin
# shellcheck source=/dev/null
. ${loop}
${builtins.readFile ./wsl2-probe-init.sh}
'';
checkPhase = ''
${lib.getExe pkgs.shellcheck} --shell=sh "$target"
'';
};
initrd = pkgs.makeInitrd {
contents = [
{
object = init;
symlink = "/init";
}
{
object = "${modules}/lib/modules";
symlink = "/lib/modules";
}
];
};
guest = pkgs.runCommand "wsl2-probe-guest" { } ''
mkdir -p $out
ln -s ${kernel}/${kernelImage} $out/kernel
ln -s ${initrd}/initrd $out/initrd
ln -s ${busybox}/bin/busybox $out/busybox
ln -s ${loop} $out/loop.sh
'';
probe = pkgs.writeShellApplication {
name = "wsl2-probe";
runtimeInputs = with pkgs; [
# QEMU with GTK and VirGL, which the real guest uses, and the daemon that serves the guest's
# /nix/store and shared directories
qemu_kvm
virtiofsd
# eglinfo and glxinfo
mesa-demos
socat
# what the script needs of a system, the same on every distribution
coreutils
findutils
gawk
gnugrep
gnused
iproute2
jq
procps
util-linux
];
# the script sets its own options: a probe that stops at the first failure answers nothing
bashOptions = [ ];
text = ''
export WSL2_PROBE_GUEST=${guest}
export WSL2_PROBE_REV=${rev}
${builtins.readFile ../../scripts/wsl2-probe.sh}
'';
meta = {
description = "Probes a NixOS-WSL2 machine for KVM, WSLg, a GPU and RDP, for a Plasma guest";
mainProgram = "wsl2-probe";
};
};
in
{
inherit guest probe;
}