Skip to content

Commit 034ec74

Browse files
nvidia: generalize the setup
Setting up nvidia graphics. Signed-off-by: Brian McGillion <bmg.avoin@gmail.com>
1 parent 27bc575 commit 034ec74

File tree

12 files changed

+253
-54
lines changed

12 files changed

+253
-54
lines changed

modules/desktop/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
{
77
imports = [
88
./graphics
9+
./nvidia-gpu
910
];
1011
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright 2025 TII (SSRC) and the Ghaf contributors
2+
# Copyright TLATER
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
# derived from https://github.com/TLATER/dotfiles
6+
{
7+
pkgs,
8+
config,
9+
lib,
10+
...
11+
}:
12+
let
13+
cfg = config.ghaf.graphics.nvidia-setup;
14+
in
15+
{
16+
imports = [
17+
./prime.nix
18+
./vaapi.nix
19+
];
20+
21+
options.ghaf.graphics.nvidia-setup = {
22+
enable = lib.mkEnableOption "Enable Nvidia setup";
23+
withIntegratedGPU = lib.mkOption {
24+
type = lib.types.bool;
25+
default = false;
26+
description = ''
27+
Whether the computer has a separate integrated GPU.
28+
29+
This also configures the machine to use the integrated GPU for
30+
other things like software decoding, so keep this enabled even
31+
if you separately disable offload rendering.
32+
'';
33+
};
34+
};
35+
36+
config = lib.mkIf cfg.enable {
37+
38+
hardware = {
39+
graphics.extraPackages = [
40+
pkgs.egl-wayland
41+
pkgs.mesa
42+
pkgs.libGL
43+
];
44+
45+
nvidia = {
46+
modesetting.enable = lib.mkDefault true;
47+
48+
# TODO: test enabling these to resume from sleep/suspend states
49+
powerManagement.enable = false;
50+
forceFullCompositionPipeline = true;
51+
# TODO: change this to true
52+
open = false;
53+
nvidiaSettings = true;
54+
package = config.boot.kernelPackages.nvidiaPackages.stable; # was beta
55+
56+
dynamicBoost.enable = cfg.enable && cfg.withIntegratedGPU;
57+
};
58+
};
59+
60+
# Load nvidia driver for Xorg and Wayland
61+
services.xserver.videoDrivers = [ "nvidia" ];
62+
63+
boot = {
64+
# TODO: what exactly does xanmod package bring?
65+
# https://xanmod.org/
66+
# many things like clear linux and supposed better nvidia support?
67+
# https://pq.hosting/en/help/modificirovannoe-jadro-xanmod
68+
# TODO; seems legit and migth be worth it
69+
#kernelPackages = lib.mkForce pkgs.linuxKernel.packages.linux_xanmod;
70+
71+
extraModprobeConfig =
72+
"options nvidia "
73+
+ lib.concatStringsSep " " [
74+
# nvidia assume that by default your CPU does not support PAT,
75+
# but this is effectively never the case in 2023
76+
"NVreg_UsePageAttributeTable=1"
77+
# This is sometimes needed for ddc/ci support, see
78+
# https://www.ddcutil.com/nvidia/
79+
#
80+
# Current monitor does not support it, but this is useful for
81+
# the future
82+
"NVreg_RegistryDwords=RMUseSwI2c=0x01;RMI2cSpeed=100"
83+
];
84+
};
85+
86+
environment.variables = {
87+
# Required to run the correct GBM backend for nvidia GPUs on wayland
88+
GBM_BACKEND = "nvidia-drm";
89+
# Apparently, without this nouveau may attempt to be used instead
90+
# (despite it being blacklisted)
91+
__GLX_VENDOR_LIBRARY_NAME = "nvidia";
92+
# Hardware cursors are currently broken on wlroots
93+
# TODO: is this still the case? seems that nixos defaults to 0
94+
WLR_NO_HARDWARE_CURSORS = lib.mkDefault "1";
95+
};
96+
};
97+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2025 TII (SSRC) and the Ghaf contributors
2+
# Copyright TLATER
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
# from https://github.com/TLATER/dotfiles
6+
{
7+
config,
8+
lib,
9+
...
10+
}:
11+
let
12+
cfg = config.ghaf.graphics.nvidia-setup.prime;
13+
in
14+
{
15+
options.ghaf.graphics.nvidia-setup.prime = {
16+
enable = lib.mkOption {
17+
type = lib.types.bool;
18+
default =
19+
config.ghaf.graphics.nvidia-setup.enable && config.ghaf.graphics.nvidia-setup.withIntegratedGPU;
20+
description = ''
21+
Whether to configure prime offload.
22+
23+
This will allow on-demand offloading of rendering tasks to the
24+
NVIDIA GPU, all other rendering will happen on the GPU
25+
integrated in the CPU.
26+
27+
The GPU *should* be turned off whenever it is not in use, so
28+
this shouldn't cause increased battery drain, but there are
29+
some reports floating around that this isn't always the case -
30+
likely especially for older devices. Feel free to turn it off
31+
if you find this doesn't work properly for you.
32+
33+
'';
34+
};
35+
};
36+
37+
config = lib.mkIf cfg.enable {
38+
hardware.nvidia.prime.offload.enable = true;
39+
hardware.nvidia.powerManagement.finegrained = true;
40+
};
41+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright 2025 TII (SSRC) and the Ghaf contributors
2+
# Copyright TLATER
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
# derived from https://github.com/TLATER/dotfiles
6+
{
7+
pkgs,
8+
config,
9+
lib,
10+
...
11+
}:
12+
let
13+
cfg = config.ghaf.graphics.nvidia-setup.vaapi;
14+
in
15+
{
16+
options.ghaf.graphics.nvidia-setup.vaapi = {
17+
enable = lib.mkOption {
18+
type = lib.types.bool;
19+
default =
20+
config.ghaf.graphics.nvidia-setup.enable && !config.ghaf.graphics.nvidia-setup.withIntegratedGPU;
21+
description = ''
22+
Whether to enable the NVIDIA vaapi driver.
23+
24+
This allows using the NVIDIA GPU for decoding video streams
25+
instead of using software decoding on the CPU.
26+
27+
This particularly makes sense for desktop computers without an
28+
iGPU, as on those software en/decoding will take a lot of
29+
processing power while the NVIDIA GPU's encoding capacity
30+
isn't doing anything, so this option is enabled by default
31+
there.
32+
33+
However, on machines with an iGPU, the dGPU's en/decoding
34+
capabilities are often more limited than those of the iGPU,
35+
and require more power, so this is disabled there by default -
36+
it may still make sense from time to time, so feel free to
37+
experiment.
38+
39+
'';
40+
41+
};
42+
43+
maxInstances = lib.mkOption {
44+
type = lib.types.nullOr lib.types.int;
45+
default = null;
46+
description = ''
47+
The maximum number of concurrent instances of the driver.
48+
49+
Sometimes useful for graphics cards with little VRAM.
50+
'';
51+
};
52+
53+
# TODO: Add the same for the chrome browser
54+
firefox = {
55+
enable = lib.mkOption {
56+
type = lib.types.bool;
57+
default = config.programs.firefox.enable;
58+
description = ''
59+
Configure Firefox to used the vaapi driver for video decoding.
60+
61+
Note that this requires disabling the [RDD
62+
sandbox](https://firefox-source-docs.mozilla.org/dom/ipc/process_model.html#data-decoder-rdd-process).
63+
'';
64+
};
65+
66+
av1Support = lib.mkOption {
67+
type = lib.types.bool;
68+
default = false;
69+
description = ''
70+
Whether to enable av1 support.
71+
72+
This will not work on Turing (e.g. Geforce 2xxx) and
73+
earlier, and is therefore disabled by default there.
74+
'';
75+
};
76+
};
77+
};
78+
79+
# See https://github.com/elFarto/nvidia-vaapi-driver#configuration
80+
config = lib.mkIf cfg.enable {
81+
environment = {
82+
systemPackages = [ pkgs.libva-utils ];
83+
variables =
84+
{
85+
NVD_BACKEND = "direct";
86+
LIBVA_DRIVER_NAME = "nvidia";
87+
}
88+
// lib.optionalAttrs (cfg.maxInstances != null) { NVD_MAX_INSTANCES = toString cfg.maxInstances; }
89+
// lib.optionalAttrs cfg.firefox.enable { MOZ_DISABLE_RDD_SANDBOX = "1"; };
90+
};
91+
92+
hardware.graphics.extraPackages = [
93+
pkgs.nvidia-vaapi-driver
94+
];
95+
96+
programs.firefox.preferences = lib.mkIf cfg.firefox.enable {
97+
"media.ffmpeg.vaapi.enabled" = true;
98+
"media.rdd-ffmpeg.enabled" = true;
99+
"media.av1.enabled" = cfg.firefox.av1Support;
100+
"gfx.x11-egl.force-enabled" = true;
101+
"widget.dmabuf.force-enabled" = true;
102+
};
103+
};
104+
}

modules/hardware/x86_64-generic/x86_64-linux.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ in
3636
systemd-boot.enable = true;
3737
};
3838

39-
kernelPackages = pkgs.linuxPackages_latest;
39+
kernelPackages = pkgs.linuxPackages;
4040
};
4141
};
4242
}

modules/microvm/sysvms/guivm.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ in
339339
if config.ghaf.guest.kernel.hardening.graphics.enable then
340340
pkgs.linuxPackagesFor guest_graphics_hardened_kernel
341341
else
342-
pkgs.linuxPackages_latest;
342+
pkgs.linuxPackages;
343343

344344
# We need this patch to avoid reserving Intel graphics stolen memory for vm
345345
# https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/12103

modules/reference/hardware/alienware/extra-config.nix

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,14 @@
22
# SPDX-License-Identifier: Apache-2.0
33
{
44
pkgs,
5-
lib,
65
...
76
}:
87
{
9-
hardware = {
10-
graphics.extraPackages = [
11-
pkgs.egl-wayland
12-
pkgs.mesa
13-
pkgs.libGL
14-
];
15-
nvidia = {
16-
modesetting.enable = true;
17-
powerManagement.enable = false;
18-
powerManagement.finegrained = false;
19-
forceFullCompositionPipeline = true;
20-
open = false;
21-
nvidiaSettings = false;
22-
prime = {
23-
intelBusId = "PCI:0:11:0";
24-
nvidiaBusId = "PCI:0:12:0";
25-
offload.enable = lib.mkForce true;
26-
offload.enableOffloadCmd = lib.mkForce true;
27-
};
28-
};
8+
ghaf.graphics.nvidia-setup = {
9+
enable = true;
10+
withIntegratedGPU = true;
2911
};
3012

31-
# Load nvidia driver for Xorg and Wayland
32-
services.xserver.videoDrivers = [ "nvidia" ];
33-
3413
microvm.qemu.extraArgs = [
3514
"-drive"
3615
"file=${pkgs.OVMF.fd}/FV/OVMF_CODE.fd,if=pflash,unit=0,readonly=true"

modules/reference/hardware/alienware/net-config.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Copyright 2025 TII (SSRC) and the Ghaf contributors
22
# SPDX-License-Identifier: Apache-2.0
33
{
4-
config,
54
pkgs,
65
...
76
}:

modules/reference/hardware/demo-tower/demo-tower.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
stage2.kernelModules = [ ];
144144
kernelParams = [
145145
"earlykms"
146+
"module_blacklist=nouveau"
146147
];
147148
};
148149
};

modules/reference/hardware/demo-tower/extra-config.nix

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,10 @@
22
# SPDX-License-Identifier: Apache-2.0
33
{
44
pkgs,
5-
config,
65
...
76
}:
87
{
9-
hardware = {
10-
#TODO: Should see how to add microcode updates to all systems
11-
#cpu.amd.updateMicrocode = true;
12-
13-
graphics.extraPackages = [
14-
pkgs.egl-wayland
15-
pkgs.mesa
16-
pkgs.libGL
17-
];
18-
nvidia = {
19-
modesetting.enable = true;
20-
powerManagement.enable = false;
21-
powerManagement.finegrained = false;
22-
forceFullCompositionPipeline = true;
23-
open = false;
24-
nvidiaSettings = true;
25-
# Optionally, you may need to select the appropriate driver version for your specific GPU.
26-
package = config.boot.kernelPackages.nvidiaPackages.beta; # was stable
27-
};
28-
};
29-
30-
# Load nvidia driver for Xorg and Wayland
31-
services.xserver.videoDrivers = [ "nvidia" ];
8+
ghaf.graphics.nvidia-setup.enable = true;
329

3310
microvm.qemu.extraArgs = [
3411
"-drive"

0 commit comments

Comments
 (0)