1{ lib, pkgs, ... }:
2let
3 box = builtins.fromJSON (builtins.readFile ../config/win11-gaming.json);
4 privateRanges = [ "10.0.0.0/8" "172.16.0.0/12" "192.168.0.0/16" "100.64.0.0/10" "169.254.0.0/16" ];
5 # Apollo's Moonlight ports; its web UI (47990) stays reachable only from Snow Globe.
6 streamTCP = [ 47984 47989 48010 ];
7 streamUDP = [ 47998 47999 48000 48002 48010 ];
8 # Open forwarding: libvirt adds no firewall rules, so the rules below are the whole policy.
9 network = pkgs.writeText "win11-gaming-network.xml" ''
10 <network>
11 <name>win11-gaming</name>
12 <forward mode='open'/>
13 <bridge name='${box.bridge}' stp='on' delay='0'/>
14 <dns><forwarder addr='1.1.1.1'/></dns>
15 <ip address='${box.gatewayAddress}' netmask='255.255.255.0'>
16 <dhcp><host mac='${box.mac}' ip='${box.guestAddress}'/><range start='10.101.0.100' end='10.101.0.120'/></dhcp>
17 </ip>
18 </network>
19 '';
20in
21{
22 # libvirt moves the 3090 between nvidia and vfio-pci when the guest starts and stops; the daemon would hold it open.
23 hardware.nvidia.nvidiaPersistenced = lib.mkForce false;
24
25 systemd.services.win11-gaming-storage = {
26 wantedBy = [ "multi-user.target" ];
27 before = [ "libvirtd.service" ];
28 requires = [ "zfs-import-work.service" ];
29 after = [ "zfs-import-work.service" "zfs-mount.service" ];
30 serviceConfig = { Type = "oneshot"; RemainAfterExit = true; };
31 script = ''
32 if ! ${pkgs.zfs}/bin/zfs list -H -o name ${box.dataset} >/dev/null 2>&1; then
33 ${pkgs.zfs}/bin/zfs create -o mountpoint=${box.root} -o compression=lz4 -o atime=off -o recordsize=64K ${box.dataset}
34 fi
35 ${pkgs.zfs}/bin/zfs set quota=560G ${box.dataset}
36 test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint ${box.root})" = ${box.dataset}
37 chmod 0700 ${box.root}
38 '';
39 };
40
41 systemd.services.win11-gaming-network = {
42 wantedBy = [ "multi-user.target" ];
43 requires = [ "libvirtd.service" ];
44 after = [ "libvirtd.service" ];
45 before = [ "libvirt-guests.service" ];
46 serviceConfig = { Type = "oneshot"; RemainAfterExit = true; };
47 path = [ pkgs.libvirt ];
48 script = ''
49 virsh net-info win11-gaming >/dev/null 2>&1 || virsh net-define ${network}
50 virsh net-autostart win11-gaming
51 virsh net-info win11-gaming | grep -q '^Active:.*yes' || virsh net-start win11-gaming
52 '';
53 };
54
55 # Pinned vCPUs alone would still share those cores with host processes.
56 virtualisation.libvirtd.hooks.qemu.win11-gaming-cpus = pkgs.writeShellScript "win11-gaming-cpus" ''
57 test "$1" = win11-gaming -a "$2" = started || exit 0
58 # The guest may start before knife-linux reserves its own CPUs here.
59 reserved=$(cat /sys/fs/cgroup/machine.slice/cpuset.cpus.exclusive)
60 echo "''${reserved:+$reserved,}${box.cpus}" > /sys/fs/cgroup/machine.slice/cpuset.cpus.exclusive
61 for scope in /sys/fs/cgroup/machine.slice/machine-qemu*win11\\x2dgaming.scope; do
62 echo ${box.cpus} > $scope/cpuset.cpus.exclusive
63 echo ${box.cpus} > $scope/cpuset.cpus
64 echo root > $scope/cpuset.cpus.partition
65 done
66 '';
67
68 # libvirt-guests suspends guests at host shutdown, which fails with a passed-through GPU.
69 systemd.services.win11-gaming-shutdown = {
70 wantedBy = [ "multi-user.target" ];
71 after = [ "libvirt-guests.service" ];
72 # Stopping this unit shuts the guest down; a changed unit must not do that on switch.
73 restartIfChanged = false;
74 serviceConfig = { Type = "oneshot"; RemainAfterExit = true; TimeoutStopSec = 300; };
75 path = [ pkgs.libvirt ];
76 script = "true";
77 preStop = ''
78 virsh shutdown win11-gaming 2>/dev/null || exit 0
79 for _ in $(seq 1 120); do
80 virsh domstate win11-gaming | grep -q 'shut off' && exit 0
81 sleep 2
82 done
83 '';
84 };
85
86 # The router forwards none of these ports, so only the LAN reaches the stream.
87 networking.nat = {
88 enable = true;
89 externalInterface = "enp4s0";
90 internalInterfaces = [ box.bridge ];
91 forwardPorts =
92 map (port: { sourcePort = port; destination = "${box.guestAddress}:${toString port}"; proto = "tcp"; }) streamTCP
93 ++ map (port: { sourcePort = port; destination = "${box.guestAddress}:${toString port}"; proto = "udp"; }) streamUDP;
94 };
95
96 networking.firewall.extraCommands = ''
97 iptables -N win11-gaming 2>/dev/null || iptables -F win11-gaming
98 iptables -A win11-gaming -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
99 ${lib.concatMapStringsSep "\n" (range: "iptables -A win11-gaming -d ${range} -j DROP") privateRanges}
100 iptables -A win11-gaming -p tcp --dport 25 -j DROP
101 iptables -A win11-gaming -j ACCEPT
102 iptables -I FORWARD -i ${box.bridge} -j win11-gaming
103 iptables -I nixos-fw 1 -i ${box.bridge} -j DROP
104 iptables -I nixos-fw 1 -i ${box.bridge} -p udp -m multiport --dports 53,67 -j ACCEPT
105 iptables -I nixos-fw 1 -i ${box.bridge} -p tcp --dport 53 -j ACCEPT
106 iptables -I nixos-fw 1 -i ${box.bridge} -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
107 '';
108 networking.firewall.extraStopCommands = ''
109 iptables -D FORWARD -i ${box.bridge} -j win11-gaming 2>/dev/null || true
110 iptables -F win11-gaming 2>/dev/null || true
111 iptables -X win11-gaming 2>/dev/null || true
112 '';
113}