dotfiles
My PC Dotfiles & Websitegit clone https://github.com/maydayv7/dotfiles
modules/hardware/gpu.nix (2914B)
1## GPU Configuration ##
2_: {
3 flake.modules.nixos.gpu = {
4 config,
5 lib,
6 pkgs,
7 ...
8 }: let
9 inherit
10 (lib)
11 mkForce
12 mkIf
13 mkEnableOption
14 mkMerge
15 mkOption
16 types
17 ;
18 cfg = config.hardware.gpu;
19 mode = config.hardware.cpu.mode != "powersave";
20 gamemode = config.programs.gamemode.enable;
21 in {
22 options.hardware.gpu = {
23 enable = mkEnableOption "Discrete GPU Support";
24 model = mkOption {
25 description = "Discrete GPU Model";
26 type = with types; nullOr (enum ["nvidia"]);
27 default = null;
28 };
29 };
30
31 config = mkMerge [
32 (mkIf cfg.enable {
33 assertions = [
34 {
35 assertion = cfg.model != null;
36 message = "GPU model must be set when GPU is enabled";
37 }
38 ];
39 })
40
41 (mkIf (cfg.enable && cfg.model == "nvidia") (
42 let
43 hybrid = with config.hardware.nvidia.prime; (amdgpuBusId != "" || intelBusId != "");
44 in {
45 services.xserver.videoDrivers = mkForce ["nvidia"];
46 services.udev.extraRules = mkIf hybrid ''
47 SUBSYSTEM=="drm", ENV{DEVTYPE}=="drm_minor", KERNEL=="card[0-9]*", DRIVERS=="nvidia", SYMLINK+="dri/nvidia-dgpu"
48 SUBSYSTEM=="drm", ENV{DEVTYPE}=="drm_minor", KERNEL=="card[0-9]*", DRIVERS=="amdgpu|i915|xe", SYMLINK+="dri/integrated-gpu"
49 '';
50
51 environment = {
52 systemPackages = [pkgs.btop-cuda];
53 variables."__GLX_VENDOR_LIBRARY_NAME" = mkIf mode "nvidia";
54 sessionVariables."GAMEMODERUNEXEC" = mkIf (hybrid && gamemode && !mode) "nvidia-offload";
55 };
56
57 boot = {
58 blacklistedKernelModules = ["nouveau"];
59 kernelModules = [
60 "nvidia"
61 "nvidia_drm"
62 "nvidia_modeset"
63 "nvidia_uvm"
64 ];
65 kernelParams = ["nvidia-drm.fbdev=1"];
66 extraModprobeConfig = ''
67 # PAT Support
68 options nvidia NVreg_UsePageAttributeTable=1
69
70 # DDC/CI Support
71 options nvidia NVreg_RegistryDwords=RMUseSwI2c=0x01;RMI2cSpeed=100
72 '';
73 };
74
75 # Wayland Support
76 hardware.nvidia = {
77 modesetting.enable = mkForce true;
78 nvidiaSettings = mkForce true;
79 dynamicBoost.enable = true;
80 powerManagement.enable = true;
81 prime = mkIf hybrid {
82 sync.enable = mkForce mode;
83 offload = {
84 enable = mkForce (!mode);
85 enableOffloadCmd = mkForce (!mode);
86 };
87 };
88 };
89 }
90 ))
91
92 (mkIf (!cfg.enable && cfg.model == "nvidia") {
93 boot.blacklistedKernelModules = [
94 "nvidia"
95 "nvidia_drm"
96 "nvidia_modeset"
97 "nvidia_uvm"
98 ];
99 })
100 ];
101 };
102}