From 024f92974ea570d8a04414f7221d75aedde8f997 Mon Sep 17 00:00:00 2001
From: albert <albert@sysctl.io>
Date: Sun, 19 Jan 2025 14:22:58 -0800
Subject: [PATCH] Testing GPU Accellerated VM's

---
 nixos/common/desktops/gnome/default.nix |  1 -
 nixos/hosts/framework16/default.nix     |  4 +-
 nixos/hosts/framework16/hibernate.nix   | 46 -----------------------
 nixos/hosts/framework16/vm-gpu.nix      | 49 +++++++++++++++++++++++++
 4 files changed, 50 insertions(+), 50 deletions(-)
 delete mode 100644 nixos/hosts/framework16/hibernate.nix
 create mode 100644 nixos/hosts/framework16/vm-gpu.nix

diff --git a/nixos/common/desktops/gnome/default.nix b/nixos/common/desktops/gnome/default.nix
index feebe05c..7d4788bb 100644
--- a/nixos/common/desktops/gnome/default.nix
+++ b/nixos/common/desktops/gnome/default.nix
@@ -82,7 +82,6 @@
     # General apps 
     newsflash
     gnome-photos
-    gnome-boxes
 
     # Gnome relevant packages
     gnome-tweaks
diff --git a/nixos/hosts/framework16/default.nix b/nixos/hosts/framework16/default.nix
index af80321c..6a20a81f 100644
--- a/nixos/hosts/framework16/default.nix
+++ b/nixos/hosts/framework16/default.nix
@@ -3,6 +3,7 @@
     inputs.nixos-hardware.nixosModules.framework-16-7040-amd
     (modulesPath + "/installer/scan/not-detected.nix")
     ./disks.nix
+    ./vm-gpu.nix
     ../../common/services/tailscale-autoconnect.nix
     ../../common/modules/secureboot.nix
     # ../../common/modules/boot.nix
@@ -15,9 +16,6 @@
     ../../common/services/ollama.nix
   ];
 
-  virtualisation.libvirtd.enable = true;
-  programs.virt-manager.enable = true;
-
   # https://wiki.nixos.org/wiki/Ollama
   services.ollama = {
     acceleration = "rocm";
diff --git a/nixos/hosts/framework16/hibernate.nix b/nixos/hosts/framework16/hibernate.nix
deleted file mode 100644
index 2875a022..00000000
--- a/nixos/hosts/framework16/hibernate.nix
+++ /dev/null
@@ -1,46 +0,0 @@
-{ config, pkgs, ... }:
-
-let
-  hibernateEnvironment = {
-    HIBERNATE_SECONDS = "10";
-    HIBERNATE_LOCK = "/var/run/autohibernate.lock";
-  };
-in {
-
-  systemd.services."awake-after-suspend-for-a-time" = {
-    description = "Sets up the suspend so that it'll wake for hibernation only if not on AC power";
-    wantedBy = [ "suspend.target" ];
-    before = [ "systemd-suspend.service" ];
-    environment = hibernateEnvironment;
-    script = ''
-      if [ $(cat /sys/class/power_supply/ACAD/online) -eq 0 ]; then
-        curtime=$(date +%s)
-        echo "$curtime $1" >> /tmp/autohibernate.log
-        echo "$curtime" > $HIBERNATE_LOCK
-        ${pkgs.utillinux}/bin/rtcwake -m no -s $HIBERNATE_SECONDS
-      else
-        echo "System is on AC power, skipping wake-up scheduling for hibernation." >> /tmp/autohibernate.log
-      fi
-    '';
-    serviceConfig.Type = "simple";
-  };
-
-  systemd.services."hibernate-after-recovery" = {
-    description = "Hibernates after a suspend recovery due to timeout";
-    wantedBy = [ "suspend.target" ];
-    after = [ "systemd-suspend.service" ];
-    environment = hibernateEnvironment;
-    script = ''
-      curtime=$(date +%s)
-      sustime=$(cat $HIBERNATE_LOCK)
-      rm $HIBERNATE_LOCK
-      if [ $(($curtime - $sustime)) -ge $HIBERNATE_SECONDS ] ; then
-        systemctl hibernate
-      else
-        ${pkgs.utillinux}/bin/rtcwake -m no -s 1
-      fi
-    '';
-    serviceConfig.Type = "simple";
-  };
-
-}
\ No newline at end of file
diff --git a/nixos/hosts/framework16/vm-gpu.nix b/nixos/hosts/framework16/vm-gpu.nix
new file mode 100644
index 00000000..4a2555a2
--- /dev/null
+++ b/nixos/hosts/framework16/vm-gpu.nix
@@ -0,0 +1,49 @@
+{ pkgs, lib, ... }:  # https://olai.dev/blog/nvidia-vm-passthrough/
+  let
+    devices = [
+        "1002:7480" # Video
+        "1002:1640" # Audio
+    ];
+in {  
+  virtualisation.libvirtd = {
+    enable = true;
+    qemu.ovmf.enable = true;
+    onBoot = "ignore";
+    onShutdown = "shutdown";
+};
+
+
+  programs.virt-manager.enable = true;
+  users.users.albert.extraGroups = [ "libvirtd" ];
+  environment.systemPackages = [ pkgs.virt-manager ];
+
+  boot.kernelParams = [
+      "vfio-pci.ids=${lib.concatStringsSep "," devices}"
+      "intel_iommu=on"
+      "iommu=pt"
+  ];
+
+  # Make the devices bind to VFIO
+  boot.initrd.kernelModules = [
+      "vfio_pci"
+      "vfio"
+      "vfio_iommu_type1"
+  ];
+
+  # Blacklist the nvidia drivers to make sure they don't get loaded
+  boot.extraModprobeConfig = ''
+      softdep nvidia pre: vfio-pci
+      softdep drm pre: vfio-pci
+      softdep nouveau pre: vfio-pci
+  '';
+  boot.blacklistedKernelModules = [
+      "nouveau"
+      "nvidia"
+      "nvidia_drm"
+      "nvidia_modeset"
+      "i2c_nvidia_gpu"
+  ];
+  virtualisation.spiceUSBRedirection.enable = true;
+}
+
+}