dotfiles

My PC Dotfiles & Website
commit 170a6884cb412f49718515dd1363438c6a542b49
parent 6d39bf3b43d98135e99efd4969bbde20d6fb8493
Author: maydayv7 <maydayv7@gmail.com>
Date:   Thu, 12 Sep 2024 00:26:38 +0530

feat: Improve VFIO Configuration

Diffstat:

Mdevices/valkyrie/drivers.nix | 18++++++++++--------
Mmodules/README.md | 5+++--
Mmodules/hardware/virtualisation/default.nix | 111++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
3 files changed, 84 insertions(+), 50 deletions(-)

diff --git a/devices/valkyrie/drivers.nix b/devices/valkyrie/drivers.nix @@ -1,14 +1,16 @@ {lib, ...}: { services.fwupd.enable = true; user.persist.directories = [".config/rog"]; - hardware.nvidia.prime = { - nvidiaBusId = lib.mkForce "PCI:1:0:0"; - amdgpuBusId = lib.mkForce "PCI:65:0:0"; - - sync.enable = false; - offload = { - enable = true; - enableOffloadCmd = true; + hardware = { + vm.passthrough = ["10de:28e0" "10de:22be"]; + nvidia.prime = { + nvidiaBusId = lib.mkForce "PCI:1:0:0"; + amdgpuBusId = lib.mkForce "PCI:65:0:0"; + sync.enable = false; + offload = { + enable = true; + enableOffloadCmd = true; + }; }; }; } diff --git a/modules/README.md b/modules/README.md @@ -71,8 +71,9 @@ Configuration [Builder](./configuration.nix) - `modules`: List of Hardware Configuration Modules imported from [`inputs.hardware`](https://github.com/nixos/nixos-hardware) - Ex. `[ "common-pc" ]` - `support`: List of Additional Supported Hardware - `[ "laptop" "mobile" "printer" "virtualisation" ]` - `vm`: Configure Virtualisation Support - - - `android`: Enable Android Virtualisation - - `passthrough`: PCI Device IDs for VM Passthrough using VFI/O (Use [`scripts/pci.sh`](../scripts/pci.sh)) - Ex. `[ "00:02.0" "00:1c.0" ]` + - `android`: Enable Android Virtualisation - `true / false` + - `vfio`: Configure the device for VFIO - `true / false` + - `passthrough`: PCI Device IDs for VFIO (Use [`scripts/pci.sh`](../scripts/pci.sh)) - Ex. `[ "10de:28e0" "10de:22be" ]` - [`nix`](./nix): Module that configures the Nix Package Manager - diff --git a/modules/hardware/virtualisation/default.nix b/modules/hardware/virtualisation/default.nix @@ -5,56 +5,87 @@ ... }: let inherit (builtins) concatStringsSep elem; - inherit (lib) mkIf mkOption types; + inherit (lib) mkEnableOption mkIf mkMerge mkOption types; enable = elem "virtualisation" config.hardware.support; + cfg = config.hardware.vm; in { imports = [./android.nix]; - options.hardware.vm.passthrough = mkOption { - description = "PCI Device IDs for VM Passthrough"; - type = types.listOf types.str; - default = []; - example = ["00:02.0" "00:1c.0"]; + options.hardware.vm = { + vfio = mkEnableOption "Configure the device for VFIO"; + passthrough = mkOption { + description = "PCI Device IDs for VFIO"; + type = types.listOf types.str; + default = []; + example = [ + "10de:28e0" # Graphics + "10de:22be" # Audio + ]; + }; }; ## Virtualisation Settings ## - config = mkIf enable { - # Environment Setup - user.groups = ["kvm" "libvirtd"]; - user.persist.directories = [".config/libvirt" ".local/share/libvirt"]; - environment.persist.directories = ["/var/lib/libvirt"]; - security.virtualisation.flushL1DataCache = "cond"; - boot = { - kernelParams = ["intel_iommu=on" "i915.enable_gvt=1"]; - kernelModules = ["kvm-intel" "vfio" "vfio-pci" "vfio_virqfd" "vfio_iommu_type1"]; - extraModprobeConfig = '' - options kvm_intel nested=1 - options vfio-pci ids=${concatStringsSep "," config.hardware.vm.passthrough} - ''; - }; + config = mkIf enable (mkMerge [ + { + # Environment Setup + user.groups = ["kvm" "libvirtd"]; + user.persist.directories = [".config/libvirt" ".local/share/libvirt"]; + environment.persist.directories = ["/var/lib/libvirt"]; + security.virtualisation.flushL1DataCache = "cond"; + specialisation."VFIO".configuration.hardware.vm.vfio = true; + boot = { + kernelModules = ["kvm-amd" "kvm-intel"]; + extraModprobeConfig = '' + options kvm_intel nested=1 + ''; + }; - # VM Packages - environment.systemPackages = with pkgs; [libguestfs virtiofsd]; - programs.virt-manager.enable = true; + # VM Packages + environment.systemPackages = with pkgs; [libguestfs virtiofsd]; + programs.virt-manager.enable = true; - # VM Utilities - virtualisation = { - kvmgt.enable = true; - spiceUSBRedirection.enable = true; - libvirtd = { - enable = true; - onBoot = "ignore"; - onShutdown = "shutdown"; - qemu = { - package = pkgs.qemu_kvm; - runAsRoot = false; - swtpm.enable = true; - ovmf = { - enable = true; - packages = [pkgs.OVMFFull.fd]; + # VM Utilities + virtualisation = { + kvmgt.enable = true; + spiceUSBRedirection.enable = true; + libvirtd = { + enable = true; + onBoot = "ignore"; + onShutdown = "shutdown"; + qemu = { + package = pkgs.qemu_kvm; + runAsRoot = false; + swtpm.enable = true; + ovmf = { + enable = true; + packages = [pkgs.OVMFFull.fd]; + }; }; }; }; - }; - }; + } + (mkIf cfg.vfio { + # VFIO Configuration + system.nixos.tags = ["vfio"]; + boot = { + kernelParams = [ + "amd_iommu=on" + "intel_iommu=on" + "i915.enable_gvt=1" + ("vfio-pci.ids=" + concatStringsSep "," cfg.passthrough) + ]; + + kernelModules = [ + "vfio" + "vfio-pci" + "vfio_virqfd" + "vfio_iommu_type1" + "nvidia" + "nvidia_modeset" + "nvidia_uvm" + "nvidia_drm" + ]; + }; + }) + ]); }