# 14900k: RTX 2070 VFIO passthrough + Windows 11 gaming VM on Crucial MX500.
{ config, lib, ... }:
let
  cfg = config.chiasson.system.gpuPassthru;
in
{
  chiasson.system.gpuPassthru = {
    enable = false;
    pciIds = [
      "10de:1f07" # RTX 2070 VGA
      "10de:10f9" # HDMI audio
      "10de:1ada" # USB 3.1
      "10de:1adb" # USB-C UCSI
      "14c3:7927" # MediaTek MT7927 WiFi (M.2, no Linux driver — Windows only)
    ];
    windowsDisk = "/dev/disk/by-id/ata-CT1000MX500SSD1_1830E14B054A";
    lookingGlass = {
      enable = true;
      shmSizeMiB = 32;
      captureOnFocus = false;
    };
    vm = {
      enable = true;
      name = "win11";
      memoryGiB = 16;
      vcpus = 8;
      installMode = false;
    };
    libvirtUsers = [ "olivier" ];
  };

  chiasson.desktop.niri.extraBinds = lib.mkIf (cfg.enable && cfg.lookingGlass.enable) {
    "Mod+J"."spawn-sh" = "looking-glass-toggle";
  };

  # Applied last in configuration.nix imports — wins over system module defaults.
  virtualisation.libvirtd.qemu.verbatimConfig = lib.mkIf (cfg.lookingGlass.enable) (
    lib.mkForce ''
      namespaces = []
      seccomp_sandbox = 0
      security_driver = "none"
      security_default_confined = 0
      security_require_confined = 0
      cgroup_device_acl = [
        "/dev/null", "/dev/full", "/dev/zero",
        "/dev/random", "/dev/urandom",
        "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
        "/dev/rtc", "/dev/hpet",
        "/dev/vfio/", "/dev/vfio/vfio",
        "/dev/kvmfr0", "c 234:* rwm",
        "/dev/disk/", "/dev/disk/by-id/",
        "/dev/bus/usb/",
      ]
    ''
  );

  virtualisation.libvirt = lib.mkIf (cfg.enable && cfg.vm.enable) {
    enable = true;
    swtpm.enable = true;
    connections."qemu:///system".domains = [
      {
        definition =
          if cfg.vm.installMode then
            ./win11-domain-install.xml
          else
            ./win11-domain.xml;
        active = false;
      }
    ];
  };
}
