/ nix / modules / nvidia.nix
nvidia.nix
 1  # NVIDIA GPU configuration for 3x RTX 3090
 2  # ADR-001: Use CDI for container GPU passthrough (not legacy --gpus)
 3  
 4  { config, pkgs, lib, ... }:
 5  
 6  {
 7    # ── NVIDIA drivers ───────────────────────────────────────────────────
 8    # videoDrivers loads the kernel module even on headless (no X11 needed)
 9    services.xserver.videoDrivers = [ "nvidia" ];
10    hardware.graphics.enable = true;
11  
12    hardware.nvidia = {
13      modesetting.enable = true;
14      open = false; # RTX 3090 (Ampere) — proprietary driver more stable
15      nvidiaSettings = true;
16      package = config.boot.kernelPackages.nvidiaPackages.latest;
17    };
18  
19    # ── NVIDIA Container Toolkit (CDI) ──────────────────────────────────
20    # Generates /etc/cdi/nvidia.yaml so Docker can use:
21    #   --device nvidia.com/gpu=all
22    #   --device nvidia.com/gpu=0
23    hardware.nvidia-container-toolkit.enable = true;
24  
25    # ── Persistence daemon ──────────────────────────────────────────────
26    # Keeps GPU initialized — reduces cold-start latency for containers.
27    # Must start after thunderbolt-pci-rescan so all 3 GPUs are visible.
28    systemd.services.nvidia-persistenced = {
29      description = "NVIDIA Persistence Daemon";
30      after = [ "thunderbolt-pci-rescan.service" ];
31      wants = [ "thunderbolt-pci-rescan.service" ];
32      wantedBy = [ "multi-user.target" ];
33      serviceConfig = {
34        Type = "forking";
35        ExecStart = "${config.hardware.nvidia.package.persistenced}/bin/nvidia-persistenced --verbose";
36        ExecStopPost = "${pkgs.coreutils}/bin/rm -f /var/run/nvidia-persistenced/nvidia-persistenced.pid";
37      };
38    };
39  }