dotfiles

My PC Dotfiles & Website

modules/hosts/valkyrie/_vm/default.nix (2324B)


 1## Windows VM
 2# ? # Run 'virsh -c qemu:///system start windows'
 3{
 4  config,
 5  inputs,
 6  ...
 7}: let
 8  files = config.flake.files.path.files;
 9in {
10  nixos = {
11    config,
12    pkgs,
13    ...
14  }: let
15    vfio = !config.virt.vfio.setup;
16    template = pkgs.writeText "windows-template.xml" (
17      builtins.replaceStrings ["@files"] [files]
18      (builtins.readFile ./windows.xml)
19    );
20
21    conf-vfio =
22      pkgs.runCommand "windows.xml" {
23        nativeBuildInputs = [pkgs.xmlstarlet];
24      } ''
25        xmlstarlet ed \
26          -u '/domain/os/loader' -x 'normalize-space(.)' \
27          -u '/domain/os/nvram' -x 'normalize-space(.)' \
28          ${template} > "$out"
29      '';
30
31    conf-desktop =
32      pkgs.runCommand "windows-desktop.xml" {
33        nativeBuildInputs = [pkgs.xmlstarlet];
34      } ''
35        xmlstarlet ed -N qemu='http://libvirt.org/schemas/domain/qemu/1.0' \
36          -d '/domain/memoryBacking/hugepages' \
37          -s '/domain/memoryBacking' -t elem -n source -v "" \
38          -i '/domain/memoryBacking/source' -t attr -n type -v memfd \
39          -d '/domain/cputune' \
40          -d "/domain/devices/hostdev[@type='pci']" \
41          -s "/domain/devices/graphics[@type='spice']" -t elem -n mouse -v "" \
42          -i "/domain/devices/graphics[@type='spice']/mouse" -t attr -n mode -v server \
43          -d '/domain/qemu:commandline' \
44          ${conf-vfio} > "$out"
45      '';
46  in {
47    imports = [inputs.nixvirt.nixosModules.default];
48
49    disko.devices.zpool.fspool.datasets = {
50      "vm" = {
51        type = "zfs_fs";
52        options = {
53          canmount = "off";
54          mountpoint = "none";
55        };
56      };
57      "vm/windows" = {
58        type = "zfs_volume";
59        size = "256G";
60        options = {
61          volblocksize = "16k";
62          refreservation = "none";
63        };
64      };
65    };
66
67    virtualisation.libvirt = {
68      enable = true;
69      package = pkgs.libvirt;
70      connections."qemu:///system" = {
71        networks = [
72          {
73            definition = ./default.xml;
74            active = true;
75          }
76        ];
77
78        domains = [
79          {
80            definition =
81              if vfio
82              then conf-vfio
83              else conf-desktop;
84            active = null;
85            restart = false;
86          }
87        ];
88      };
89    };
90  };
91}