|
1 | 1 | # GPU on non-NixOS systems {#sec-usage-gpu-non-nixos} |
2 | 2 |
|
3 | 3 | To access the GPU, programs need access to OpenGL and Vulkan libraries. While |
4 | | -this works transparently on NixOS, it does not on other Linux systems. A |
5 | | -solution is provided by [NixGL](https://github.com/nix-community/nixGL), which |
6 | | -can be integrated into Home Manager. |
| 4 | +this works transparently on NixOS, it does not on other Linux systems. There are |
| 5 | +two options: |
| 6 | + |
| 7 | +1. Recommended: modify the host system slightly so that the graphics libraries |
| 8 | + can be found where programs from Nixpkgs can find them. |
| 9 | +2. Wrap programs from Nixpkgs in an environment which tells them where to find |
| 10 | + graphics libraries. |
| 11 | + |
| 12 | +The first option is very clean because the needed modifications to the host OS |
| 13 | +are small. However, it does require root/sudo access to the system, which may |
| 14 | +not be available. The second approach avoids that. However, it injects libraries |
| 15 | +from Nixpkgs into the environment of wrapped programs, which can make it |
| 16 | +impossible to launch programs of the host OS from wrapped programs. |
| 17 | + |
| 18 | + |
| 19 | +## When sudo is available: fixing the host OS {#sec-usage-gpu-sudo} |
| 20 | + |
| 21 | +The {option}`targets.genericLinux.gpu` module is automatically enabled whenever |
| 22 | +the option {option}`targets.genericLinux.enable` is set (unless |
| 23 | +[NixGL](#sec-usage-gpu-nosudo) is used instead), which is recommended for |
| 24 | +non-NixOS Linux distributions in any case. The module can also be explicitly |
| 25 | +enabled by setting {option}`targets.genericLinux.gpu.enable`. |
| 26 | + |
| 27 | +This module builds a directory containing GPU libraries. When activating the |
| 28 | +home configuration by `home-manager switch`, the host system is examined: for |
| 29 | +compatibility with NixOS, these libraries need to be placed in |
| 30 | +`/run/opengl-driver`. If this directory does not exist, or contains a different |
| 31 | +set of libraries, the activation script will print a warning such as |
| 32 | + |
| 33 | +```text |
| 34 | +Activating checkExistingGpuDrivers |
| 35 | +GPU drivers require an update, run |
| 36 | + sudo /nix/store/HASH-non-nixos-gpu/bin/non-nixos-gpu-setup |
| 37 | +``` |
| 38 | + |
| 39 | +Because the `/run` directory is volatile and disappears on reboot, libraries |
| 40 | +cannot be simply copied or linked there. The `non-nixos-gpu-setup` script |
| 41 | +installs a Systemd service which ensures that the drivers are linked to |
| 42 | +`/run/opengl-driver` on boot. Home Manager will always check and warn you when |
| 43 | +this setup needs to be refreshed. |
| 44 | + |
| 45 | +If you ever wish to uninstall these drivers, all you need to do is |
| 46 | + |
| 47 | +```sh |
| 48 | +sudo rm /run/opengl-driver |
| 49 | +sudo systemctl disable --now non-nixos-gpu.service |
| 50 | +sudo rm /etc/systemd/system/non-nixos-gpu.service |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +### GPU offloading {#sec-usage-gpu-offloading} |
| 55 | + |
| 56 | +You can use the {option}`targets.genericLinux.nixGL.prime.installScript` option. |
| 57 | +It installs the `prime-offload` script which is configured through options under |
| 58 | +{option}`targets.genericLinux.nixGL.prime`. This functionality is independent |
| 59 | +from the rest of NixGL and can be used when |
| 60 | +{option}`targets.genericLinux.nixGL.packages` is left `null`, which it should be |
| 61 | +when using drivers from `/run/opengl-driver`. |
| 62 | + |
| 63 | + |
| 64 | +### Nvidia drivers {#sec-usage-gpu-nvidia} |
| 65 | + |
| 66 | +If you need to include the proprietary Nvidia drivers, the process is a bit more |
| 67 | +involved. You need to: |
| 68 | + |
| 69 | +1. Determine the exact version used by the host system. Example: `550.163.01` |
| 70 | + |
| 71 | +1. Fetch that version of the drivers from Nvidia and calculate their hash. |
| 72 | + Example: |
| 73 | + |
| 74 | + ```sh |
| 75 | + nix store prefetch-file \ |
| 76 | + https://download.nvidia.com/XFree86/Linux-x86_64/550.163.01/NVIDIA-Linux-x86_64-550.163.01.run |
| 77 | + ``` |
| 78 | + |
| 79 | + Attention: the version and architecture are present twice. If you are on an |
| 80 | + ARM system, replace `x86_64` with `aarch64`. |
| 81 | + |
| 82 | +1. Put this information into your home configuration. Example: |
| 83 | + |
| 84 | + ```nix |
| 85 | + targets.genericLinux.gpu.nvidia = { |
| 86 | + enable = true; |
| 87 | + version = "550.163.01"; |
| 88 | + sha256 = "sha256-74FJ9bNFlUYBRen7+C08ku5Gc1uFYGeqlIh7l1yrmi4="; |
| 89 | + }; |
| 90 | + ``` |
| 91 | + |
| 92 | +::: {.warning} |
| 93 | +The Nvidia driver version **must** match the host system. This means that you |
| 94 | +must pay attention when upgrading the system and update the home configuration |
| 95 | +as well. |
| 96 | +::: |
| 97 | + |
| 98 | + |
| 99 | +## No root access: wrapping programs {#sec-usage-gpu-nosudo} |
| 100 | + |
| 101 | +The wrapping approach is facilitated by |
| 102 | +[NixGL](https://github.com/nix-community/nixGL), which can be integrated into |
| 103 | +Home Manager. |
| 104 | + |
| 105 | +::: {.warning} |
| 106 | + |
| 107 | +This approach can cause issues when a wrapped program from Nixpkgs executes a |
| 108 | +program from the host. For example, Firefox from Nixpkgs must be wrapped by |
| 109 | +NixGL in order for graphical acceleration to work. If you then download a PDF |
| 110 | +file and open it in a PDF viewer that is not installed from Nixpkgs but is |
| 111 | +provided by the host distribution, there may be issues. Because Firefox's |
| 112 | +environment injects libraries from NixGL, they are inherited by the PDF viewer, |
| 113 | +and unless they are the same or compatible version as the libraries on the host, |
| 114 | +the viewer will not work. This problem manifests more often with Vulkan because |
| 115 | +it needs a larger set of injected libraries than OpenGL. |
| 116 | + |
| 117 | +The problem typically manifests with errors similar to |
| 118 | + |
| 119 | +```text |
| 120 | +/nix/store/HASH-gcc-12.3.0-lib/lib/libstdc++.so.6: version `GLIBCXX_3.4.31' not found |
| 121 | +``` |
| 122 | + |
| 123 | +::: |
7 | 124 |
|
8 | 125 | To enable the integration, import NixGL into your home configuration, either as |
9 | 126 | a channel, or as a flake input passed via `extraSpecialArgs`. Then, set the |
|
0 commit comments