# Nixos using integrated GPU for display and external GPU for compute: a guide

**URL:** https://discourse.nixos.org/t/nixos-using-integrated-gpu-for-display-and-external-gpu-for-compute-a-guide/12345
**Category:** Guides
**Created:** [April 7, 2021, 4:30am UTC](https://discourse.nixos.org/t/nixos-using-integrated-gpu-for-display-and-external-gpu-for-compute-a-guide/12345 "2021-04-07T04:30:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![danielbarter](https://discourse.nixos.org/user_avatar/discourse.nixos.org/danielbarter/32/7420_2.png) [@danielbarter](https://discourse.nixos.org/u/danielbarter)
#### Post date: [April 7, 2021, 4:30am UTC](https://discourse.nixos.org/t/nixos-using-integrated-gpu-for-display-and-external-gpu-for-compute-a-guide/12345/1 "2021-04-07T04:30:31Z")

</div>

Hi there,

Recently i got my hands on a nvidia GPU. I wanted to use my integrated AMD GPU for display and the nvidia GPU for computing only. This seems like a fairly reasonable setup, but the wiki doesn’t have any information on how to do it.

The wiki suggests `services.xserver.videoDrivers = ["nvidia"];` which is incompatible with the above setup. Suprisingly, `services.xserver.videoDrivers = ["nvidia" "amdgpu"];` also doesn’t work because my compositor could no longer find the required mesa libraries.

Anyway, ignoring the attribute `services.xserver.videoDrivers`, I got it working fairly painlessly, so I thought I would describe how here, so maybe we can add it to the wiki.

Firstly I had to set integrated graphics as default in the UEFI bios. Without this, I couldn’t even even get to the motherboard splash screen without the external GPU plugged in to a monitor.

Here are the relevent parts of `configuration.nix`:

```auto
  nixpkgs.config.allowUnfree = true;
  boot.extraModulePackages = [pkgs.linuxPackages.nvidia_x11];
  boot.blacklistedKernelModules = ["nouveau" "nvidia_drm" "nvidia_modeset" "nvidia"];
  packages = [pkgs.linuxPackages.nvidia_x11];

```

` nixpkgs.config.allowUnfree` is self explanatory: nvidia drivers are not open source. ` boot.extraModulePackages` adds the nvidia drivers to the list of available linux modules. `boot.blacklistedKernelModules` is the list of modules blacklisted from being loaded automatically on startup. We need to blacklist the open source nvidia driver and all the modules from the propitiatory driver because if any one of them gets loaded, it somehow loads all the other ones 🤷‍♂️.

Now, if you try `sudo modprobe nvidia`, the module gets loaded, but there is some other system initialization which is needed for the GPU to function correctly. I read on the nvidia forums that `nvidia-smi` will initialize the device if you run it as root, and this is why we include the driver in `packages` as it puts `nvidia-smi` into scope.

When you want to initialize the GPU, just run `sudo nvidia-smi` rather than loading the kernel modules directly, and you should be good to go!

---

<div class="post-metadata">

### Author: ![bochy992](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bochy992](https://discourse.nixos.org/u/bochy992)
#### Post date: [February 1, 2024, 2:14pm UTC](https://discourse.nixos.org/t/nixos-using-integrated-gpu-for-display-and-external-gpu-for-compute-a-guide/12345/2 "2024-02-01T14:14:59Z")

</div>

```auto
  packages = [pkgs.linuxPackages.nvidia_x11];

```

is this environment.systemPackages?

---

<div class="post-metadata">

### Author: ![azazel75](https://discourse.nixos.org/user_avatar/discourse.nixos.org/azazel75/32/177_2.png) [@azazel75](https://discourse.nixos.org/u/azazel75)
#### Post date: [February 8, 2024, 11:14am UTC](https://discourse.nixos.org/t/nixos-using-integrated-gpu-for-display-and-external-gpu-for-compute-a-guide/12345/3 "2024-02-08T11:14:23Z")

</div>

yes, and you should use `config.boot.kernelPackages.nvidia_x11` in place of any occurrence of `pkgs.linuxPackages.nvidia_x11` because the kernel module has to be specific for the kernel version that you have configured on your system.

---

<div class="post-metadata">

### Author: ![hayden-donnelly](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/h/3ec8ea/32.png) [@hayden-donnelly](https://discourse.nixos.org/u/hayden-donnelly)
#### Post date: [March 22, 2024, 10:30pm UTC](https://discourse.nixos.org/t/nixos-using-integrated-gpu-for-display-and-external-gpu-for-compute-a-guide/12345/4 "2024-03-22T22:30:13Z")

</div>

When using this method what should I set my `LD_LIBRARY_PATH` to so that programs like JAX and PyTorch can find CUDA? Previously I was using `export LD_LIBRARY_PATH=/run/opengl-driver/lib/` with `services.xserver.videoDrivers = ["nvidia"];`, but after switching my display to an integrated GPU with the setup described here, and with `pkgs.linuxPackages.nvidia_x11` replaced by `config.boot.kernelPackages.nvidia_x11`, it no longer works. Despite this `nvidia-smi` still works if I run `sudo nvidia-smi` so it looks like the driver is correctly installed, I just can’t find it.

---

<div class="post-metadata">

### Author: ![hayden-donnelly](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/h/3ec8ea/32.png) [@hayden-donnelly](https://discourse.nixos.org/u/hayden-donnelly)
#### Post date: [June 1, 2024, 8:00pm UTC](https://discourse.nixos.org/t/nixos-using-integrated-gpu-for-display-and-external-gpu-for-compute-a-guide/12345/5 "2024-06-01T20:00:58Z")

</div>

I was able to partially solve this by using nixGL. My nixGL devshells are able to correctly find the driver, but my docker containers can’t.

Here’s an example devshell that works for me.

```nix
{
  description = "...";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/23.11";
    flake-utils.url = "github:numtide/flake-utils";
    nixgl.url = "gihub:nix-community/nixGL";
  };
  outputs = inputs@{ self, nixpkgs, flake-utils, nixgl, ... }:
    flake-utils.lib.eachSystem ["x86_64-linux"] (system:
      let inherit (nixpkgs) lib;
      in {
        devShells = let
          overlays = [nixgl.overlay];
          pkgs = import nixpkgs {
            inherit system overlays;
            config = {
              allowUnfree = true;
              cudaSupport = true;
            };
          };
        in rec {
          default = pkgs.mkShell {
            name = "cuda";
            buildInputs = with pkgs; [
              cudaPackages.cudatoolkit
              cudaPackages.cuda_cudart
              cudaPackages.cudnn
            ];
            shellHook = ''
              export CUDA_PATH=${unstableCudaPkgs.cudatoolkit}
              source <(sed -Ee '/\$@/d' ${
                lib.getExe unstableCudaPkgs.nixgl.nixGLIntel
              })
              source <(sed -Ee '/\$@/d' ${
                lib.getExe unstableCudaPkgs.nixgl.auto.nixGLNvidia
              }*)
            '';
          };
        };
      });
}

```

---

<div class="post-metadata">

### Author: ![fsbof](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/f/df705f/32.png) [@fsbof](https://discourse.nixos.org/u/fsbof)
#### Post date: [June 29, 2024, 5:27am UTC](https://discourse.nixos.org/t/nixos-using-integrated-gpu-for-display-and-external-gpu-for-compute-a-guide/12345/6 "2024-06-29T05:27:54Z")

</div>

Hi @danielbarter, I have just recently got my Thunderbolt3 eGPU with a NVidia card to work [automatically](https://discourse.nixos.org/t/services-xserver-videodrivers-does-something-i-didnt-expect/47812/2). My next step on that journey is to look at creating a specialisation that allows the eGPU to be used for compute and a second specialisation that passes the GPU through to a libvirt domain so it can be used to improve the rendering performance of OpenScad. Having never tried to use a GPU for compute I have no real idea of what can be done and how it can be used. I presume that a process needs to be either written for or support something that enables it to take advantage of the GPU. Can you recommend a simple application that I can use to test and confirm that the eGPU is providing compute resources?
