From a13b221e491bd14f803452cc06ebdedf042c73ec Mon Sep 17 00:00:00 2001 From: matt1432 Date: Sun, 3 Mar 2024 16:13:56 -0500 Subject: [PATCH] refactor(nvidia): make global module for enabling nvidia easily --- common/default.nix | 1 + devices/binto/default.nix | 1 - devices/binto/hardware-configuration.nix | 7 ++ devices/binto/modules/gpu-replay.nix | 3 - devices/binto/modules/nvidia.nix | 40 ------------ devices/nos/hardware-configuration.nix | 39 +---------- modules/nvidia.nix | 83 ++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 80 deletions(-) delete mode 100644 devices/binto/modules/nvidia.nix create mode 100644 modules/nvidia.nix diff --git a/common/default.nix b/common/default.nix index 3ac864f..54e71c7 100644 --- a/common/default.nix +++ b/common/default.nix @@ -20,6 +20,7 @@ ../modules/arion ../modules/borgbackup + ../modules/nvidia.nix ]; nixpkgs = { diff --git a/devices/binto/default.nix b/devices/binto/default.nix index fc82922..6e4ea84 100644 --- a/devices/binto/default.nix +++ b/devices/binto/default.nix @@ -15,7 +15,6 @@ in { ./modules/gpu-replay.nix ./modules/nix-gaming.nix - ./modules/nvidia.nix ]; vars = { diff --git a/devices/binto/hardware-configuration.nix b/devices/binto/hardware-configuration.nix index ae15f69..81be2d5 100644 --- a/devices/binto/hardware-configuration.nix +++ b/devices/binto/hardware-configuration.nix @@ -75,4 +75,11 @@ qemu virtiofsd ]; + + nvidia = { + enable = true; + enableNvidiaSettings = true; + enableWayland = true; + enableCUDA = true; + }; } diff --git a/devices/binto/modules/gpu-replay.nix b/devices/binto/modules/gpu-replay.nix index b483c20..c8fbed0 100644 --- a/devices/binto/modules/gpu-replay.nix +++ b/devices/binto/modules/gpu-replay.nix @@ -50,9 +50,6 @@ ''; }; in { - # Allow CUDA on boot - boot.kernelModules = ["nvidia-uvm"]; - security.wrappers = { gpu-screen-recorder = { owner = "root"; diff --git a/devices/binto/modules/nvidia.nix b/devices/binto/modules/nvidia.nix deleted file mode 100644 index c3fa203..0000000 --- a/devices/binto/modules/nvidia.nix +++ /dev/null @@ -1,40 +0,0 @@ -{ - config, - pkgs, - ... -}: { - # FIXME: move this to hardware-config? - - # Enable OpenGL - hardware.opengl = { - enable = true; - driSupport = true; - driSupport32Bit = true; - - extraPackages = with pkgs; [ - vaapiVdpau - ]; - }; - - # Load nvidia driver for Xorg and Wayland - services.xserver.videoDrivers = ["nvidia"]; - - hardware.nvidia = { - modesetting.enable = true; - - # Nvidia power management. Experimental, and can cause sleep/suspend to fail. - powerManagement.enable = false; - # Fine-grained power management. Turns off GPU when not in use. - # Experimental and only works on modern Nvidia GPUs (Turing or newer). - powerManagement.finegrained = false; - - open = true; - - # Enable the Nvidia settings menu, - # accessible via `nvidia-settings`. - nvidiaSettings = true; - - # Vulkan is much more stable in Wayland - package = config.boot.kernelPackages.nvidiaPackages.vulkan_beta; - }; -} diff --git a/devices/nos/hardware-configuration.nix b/devices/nos/hardware-configuration.nix index e95ff25..76aec6f 100644 --- a/devices/nos/hardware-configuration.nix +++ b/devices/nos/hardware-configuration.nix @@ -1,14 +1,13 @@ { config, modulesPath, - pkgs, ... }: { nixpkgs.hostPlatform = "x86_64-linux"; imports = [(modulesPath + "/installer/scan/not-detected.nix")]; boot = { - kernelModules = ["kvm-intel" "nvidia-uvm"]; + kernelModules = ["kvm-intel"]; initrd.availableKernelModules = [ "xhci_pci" @@ -47,40 +46,8 @@ hardware.cpu.intel.updateMicrocode = config.hardware.enableRedistributableFirmware; - # NVIDIA settings - services.xserver.videoDrivers = ["nvidia"]; - - hardware.opengl = { + nvidia = { enable = true; - driSupport = true; - driSupport32Bit = true; - - extraPackages = with pkgs; [ - vaapiVdpau - libvdpau-va-gl - nvidia-vaapi-driver - ]; - extraPackages32 = with pkgs; [vaapiVdpau]; + enableCUDA = true; }; - - hardware.nvidia = { - modesetting.enable = true; - - powerManagement.enable = false; - powerManagement.finegrained = false; - open = false; - nvidiaSettings = false; - - # Vulkan is much more stable in Wayland - package = config.boot.kernelPackages.nvidiaPackages.stable; - }; - - environment.systemPackages = with pkgs; [ - libva-utils - nvidia-vaapi-driver - nvtop-nvidia - pciutils - vdpauinfo - cudaPackages.cudatoolkit - ]; } diff --git a/modules/nvidia.nix b/modules/nvidia.nix new file mode 100644 index 0000000..f23f6c4 --- /dev/null +++ b/modules/nvidia.nix @@ -0,0 +1,83 @@ +{ + config, + lib, + pkgs, + ... +}: let + inherit (lib) mdDoc mkIf mkEnableOption mkOption optionals types; + + cfg = config.nvidia; +in { + options.nvidia = { + enable = mkEnableOption (mdDoc "nvidia"); + + enableNvidiaSettings = mkOption { + type = types.bool; + default = false; + }; + + enableWayland = mkOption { + type = types.bool; + default = false; + }; + + enableCUDA = mkOption { + type = types.bool; + default = false; + }; + }; + + config = mkIf cfg.enable { + # Enable OpenGL + hardware.opengl = { + enable = true; + driSupport = true; + driSupport32Bit = true; + + extraPackages = with pkgs; [ + vaapiVdpau + libvdpau-va-gl + nvidia-vaapi-driver + ]; + extraPackages32 = with pkgs; [vaapiVdpau]; + }; + + services.xserver.videoDrivers = ["nvidia"]; + + hardware.nvidia = { + modesetting.enable = true; + + # Enable the Nvidia settings menu, + # accessible via `nvidia-settings`. + nvidiaSettings = cfg.enableNvidiaSettings; + + # Nvidia power management. Experimental, and can cause sleep/suspend to fail. + powerManagement = { + enable = false; + + # Fine-grained power management. Turns off GPU when not in use. + # Experimental and only works on modern Nvidia GPUs (Turing or newer). + finegrained = false; + }; + + open = cfg.enableWayland; + + package = with config.boot.kernelPackages.nvidiaPackages; + if cfg.enableWayland + # Vulkan is much more stable in Wayland + then vulkan_beta + else stable; + }; + + environment.systemPackages = with pkgs; ([ + libva-utils + nvidia-vaapi-driver + nvtop-nvidia + pciutils + vdpauinfo + ] + ++ optionals cfg.enableCUDA [cudaPackages.cudatoolkit]); + + boot.kernelModules = optionals cfg.enableCUDA ["nvidia-uvm"]; + }; +}