nixfiles/modules/ezpassthru.nix
2022-06-11 11:37:58 -07:00

45 lines
1.2 KiB
Nix

{ config, lib, pkgs, ... }:
with lib;
let cfg = config.services.ezpassthru;
in {
options.services.ezpassthru = {
enable = mkEnableOption
"Enable simple VM PCI passthrough config (NOTE: this is only for ppl with a primary AMD/Intel, and a non-primary NVidia)";
PCIs = mkOption {
description = "The ID pairs of your PCI devices to passthrough";
example = {
"10de:1b80" = "0000:41:00.0";
"10de:10f0" = "0000:41:00.1";
"1022:145c" = "0000:01:00.0";
};
};
};
config = mkIf cfg.enable {
boot.kernelModules = [
"kvm-intel"
"kvm-amd"
"vfio_virqfd"
"vfio_pci"
"vfio_iommu_type1"
"vfio"
];
boot.kernelParams = [ "intel_iommu=on" "amd_iommu=on" "pcie_aspm=off" ];
boot.extraModprobeConfig = "options vfio-pci ids=${
builtins.concatStringsSep "," (builtins.attrNames cfg.PCIs)
}";
boot.postBootCommands = ''
DEVS="${builtins.concatStringsSep " " (builtins.attrValues cfg.PCIs)}"
for DEV in $DEVS; do
echo "vfio-pci" > /sys/bus/pci/devices/$DEV/driver_override
done
${pkgs.kmod}/bin/modprobe -i vfio-pci
'';
};
}