nixos/global/gpu/default.nix

143 lines
3.6 KiB
Nix
Raw Normal View History

2025-01-13 11:52:09 +08:00
{
pkgs,
lib,
config,
...
}:
with lib;
let
2024-01-27 22:14:03 +08:00
cfg = config.global.gpu;
2025-01-13 11:52:09 +08:00
intel =
cfg.type == "intel" || (cfg.type == "prime" && config.hardware.nvidia.prime.intelBusId != "");
amdgpu =
cfg.type == "amdgpu" || (cfg.type == "prime" && config.hardware.nvidia.prime.amdgpuBusId != "");
nvidia = cfg.type == "nvidia" || cfg.type == "prime";
2025-01-13 11:52:09 +08:00
in
{
imports = [
./plymouth.nix
./greetd.nix
];
2024-01-27 22:14:03 +08:00
options.global.gpu = {
2024-01-02 16:20:52 +08:00
enable = mkEnableOption "various setup required for GUI and support software";
2025-01-13 11:52:09 +08:00
session = mkEnableOption "software required for a graphical session" // {
default = true;
};
2024-01-02 16:20:52 +08:00
type = mkOption {
2025-01-13 11:52:09 +08:00
type =
with types;
nullOr (enum [
"intel"
"amdgpu"
"nvidia"
"prime"
]);
default = null;
2024-01-02 16:20:52 +08:00
description = "type of graphics acceleration used";
};
arc = mkOption {
type = with types; nullOr str;
default = null;
description = "intel arc PCI ID if installed, enables toggling the arc before boot";
};
2024-01-02 16:20:52 +08:00
};
config = mkIf cfg.enable {
2024-06-24 16:32:20 +08:00
hardware.graphics = {
2024-01-02 16:20:52 +08:00
enable = true;
2024-07-13 21:34:25 +08:00
enable32Bit = true;
# https://nixos.wiki/wiki/Accelerated_Video_Playback
2025-01-13 11:52:09 +08:00
extraPackages =
with pkgs;
optionals intel [
intel-media-driver # LIBVA_DRIVER_NAME=iHD
vaapiIntel # LIBVA_DRIVER_NAME=i965 (older but works better for Firefox/Chromium)
vaapiVdpau
libvdpau-va-gl
intel-compute-runtime
]
++ optional nvidia nvidia-vaapi-driver
++ optional (cfg.type == "nvidia") vulkan-validation-layers;
2024-01-02 16:20:52 +08:00
};
services.xserver = mkIf cfg.session {
2025-01-13 11:52:09 +08:00
videoDrivers = optional nvidia "nvidia" ++ optional (cfg.type == "amdgpu") "amdgpu";
# inhibits default display manager
displayManager.startx.enable = mkDefault true;
};
2024-01-02 16:20:52 +08:00
hardware.nvidia = mkIf nvidia {
2024-01-02 16:20:52 +08:00
modesetting.enable = true;
nvidiaSettings = true;
prime = mkIf (cfg.type == "prime") {
offload = {
enable = true;
enableOffloadCmd = true;
};
};
powerManagement.enable = false;
powerManagement.finegrained = false;
open = false;
2024-01-02 16:20:52 +08:00
};
environment.variables = {
# work around broken nvidia hw cursor on wayland
WLR_NO_HARDWARE_CURSORS = mkIf (cfg.type == "nvidia") "1";
# work around wlroots flickering on pure nvidia
2024-06-24 20:28:49 +08:00
#WLR_RENDERER = mkIf (cfg.type == "nvidia") "vulkan";
};
2024-01-19 21:25:03 +08:00
specialisation.integratedGraphics = mkIf (cfg.type == "prime") {
configuration = {
2025-01-13 11:52:09 +08:00
global.gpu.type = mkForce (
if intel then
"intel"
else if amdgpu then
"amdgpu"
else
"prime"
);
boot.blacklistedKernelModules = [ "nouveau" ];
};
};
specialisation.withArc = mkIf (cfg.arc != null) {
configuration = {
2024-01-27 22:14:03 +08:00
global.gpu.arc = mkForce null;
powerManagement.cpuFreqGovernor = mkForce "performance";
};
};
2024-01-02 16:20:52 +08:00
boot.initrd.kernelModules =
2025-01-13 11:52:09 +08:00
optional amdgpu "amdgpu"
++ optional (intel && cfg.arc == null) "i915"
++ optionals nvidia [
"nvidia"
"nvidia_drm"
"nvidia_modeset"
"nvidia_uvm"
]
++ optional (cfg.arc != null) "vfio-pci";
2024-01-19 21:25:03 +08:00
boot.extraModulePackages = optional nvidia config.boot.kernelPackages.nvidia_x11;
boot.extraModprobeConfig = mkIf (cfg.arc != null) ''
softdep drm pre: vfio-pci
options vfio-pci ids=${cfg.arc}
'';
2024-06-24 20:28:49 +08:00
boot.kernelParams =
2025-01-13 11:52:09 +08:00
optional intel "i915.fastboot=1"
++ optionals nvidia [
"nvidia_drm.modeset=1"
"nvidia_drm.fbdev=1"
];
2024-01-02 16:20:52 +08:00
};
}