{ config, lib, ... }:

{
  options.modules.nvidia.enable = lib.mkEnableOption "Enable NVIDIA kernel and modprobe configurations.";

  config = lib.mkIf config.modules.nvidia.enable {
    hardware.graphics = {
      enable = true;
    };

    services.xserver.videoDrivers = [ "nvidia" ];

    hardware.nvidia = {
      modesetting.enable = true;
      powerManagement.enable = true;
      open = true;
      nvidiaSettings = true;
      package = config.boot.kernelPackages.nvidiaPackages.beta;
      forceFullCompositionPipeline = true;
    };

    boot.kernelParams = [
      "nvidia-drm.modeset=1"
      "nvidia.NVreg_PreserveVideoMemoryAllocations=1"
    ];

    boot.extraModprobeConfig = ''
      options nvidia-drm modeset=1
      softdep nvidia pre: nvidia-drm
    '';

    boot.initrd.kernelModules = [ "nvidia" "nvidia_modeset" "nvidia_uvm" "nvidia_drm" ];
  };
}
