Completely turning of nvidia dGPU on laptops with nvidia optimus

Just wanted to share something that may make someones life a bit easier
The unofficial wiki tells you to add the following to turn off the dGPU (see here)

boot.extraModprobeConfig = ''
  blacklist nouveau
  options nouveau modeset=0
'';
services.udev.extraRules = ''
  # Remove NVIDIA USB xHCI Host Controller devices, if present
  ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x0c0330", ATTR{power/control}="auto", ATTR{remove}="1"
  # Remove NVIDIA USB Type-C UCSI devices, if present
  ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x0c8000", ATTR{power/control}="auto", ATTR{remove}="1"
  # Remove NVIDIA Audio devices, if present
  ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x040300", ATTR{power/control}="auto", ATTR{remove}="1"
  # Remove NVIDIA VGA/3D controller devices
  ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x03[0-9]*", ATTR{power/control}="auto", ATTR{remove}="1"
'';
boot.blacklistedKernelModules = [ "nouveau" "nvidia" "nvidia_drm" "nvidia_modeset" ];

but this did not work for me ( xorg would crash upon login and return me back to gdm)
i also had to add the following line to make it work

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

Specializations can also be used to make switching easy! Heres my config

  specialisation = {
    on-the-go.configuration = {
      system.nixos.tags = ["on-the-go"];
      hardware.nvidia = {
        prime.offload.enable = lib.mkForce true;
        prime.offload.enableOffloadCmd = lib.mkForce true;
        prime.sync.enable = lib.mkForce false;
      };
    };
    battery-saver.configuration = {
      system.nixos.tags = ["battery-saver"];

      services.xserver.videoDrivers = lib.mkForce ["nvidia" "modesetting"];
      boot.extraModprobeConfig = ''
        blacklist nouveau
        options nouveau modeset=0
      '';

      services.udev.extraRules = ''
        # Remove NVIDIA USB xHCI Host Controller devices, if present
        ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x0c0330", ATTR{power/control}="auto", ATTR{remove}="1"
        # Remove NVIDIA USB Type-C UCSI devices, if present
        ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x0c8000", ATTR{power/control}="auto", ATTR{remove}="1"
        # Remove NVIDIA Audio devices, if present
        ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x040300", ATTR{power/control}="auto", ATTR{remove}="1"
        # Remove NVIDIA VGA/3D controller devices
        ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x03[0-9]*", ATTR{power/control}="auto", ATTR{remove}="1"
      '';
      boot.blacklistedKernelModules = ["nouveau" "nvidia" "nvidia_drm" "nvidia_modeset"];

      systemd.services.nbfc_service.enable = lib.mkForce false;
    };
  };

Full config file for nvidia can be seen here

1 Like

I wonder whether it’s worth making a NixOS option for this… I want to say “probably” without looking at the details. One detail that I would look at is what related options already exist.

There seems to already be one , although it dosent work for me (i cant login to i3wm even with modsetting drivers)
hardware.nvidiaOptimus.disable (see here)
Although I’m not sure why this is under nvidiaOptimus and not nvidia.prime

Hey, I don’t have such hardware. If you want to pursue this, I suggest trying your best to answer your own questions and then when you (as I often do miserably) fail, ask in the development category.

By the way, I wonder whether you’d be interested in looking into

I know it’s on my list!

If you want to pursue this, I suggest trying your best to answer your own questions and then when you (as I often do miserably) fail, ask in the development category.

:sweat_smile:

nix-facter seems cool! I’ll check it out