OpenGL: Difference between revisions

From NixOS Wiki
imported>Samuela
No edit summary
m update renamed options
 
(8 intermediate revisions by 7 users not shown)
Line 1: Line 1:
You can enable OpenGL by setting `hardware.opengl.enable = true;` in your `/etc/nixos/configuration.nix`.
{{Merge|Mesa|Most of the content here is related to the mesa package}}
 
__FORCETOC__
 
You can enable OpenGL by setting <code>hardware.graphics.enable = true;</code><ref group=note>Renamed from <code>hardware.opengl.enable</code> in [[NixOS]] 24.11</ref> in your <code>/etc/nixos/configuration.nix</code>.


OpenGL must break purity due to the need for hardware-specific linkage. Intel, AMD, and Nvidia have different drivers for example. On NixOS, these libraries are symlinked under
OpenGL must break purity due to the need for hardware-specific linkage. Intel, AMD, and Nvidia have different drivers for example. On NixOS, these libraries are symlinked under
   /run/opengl-driver/lib
   /run/opengl-driver/lib
and optionally (if <code>hardware.opengl.driSupport32Bit</code> is enabled)
and optionally (if <code>hardware.graphics.enable32Bit</code><ref group=note>Renamed from <code>hardware.opengl.driSupport32Bit</code> in [[NixOS]] 24.11</ref> is enabled)
   /run/opengl-driver-32/lib
   /run/opengl-driver-32/lib


When a program is installed in your environment, these libraries should be found automatically. However, this is not the case in a `nix-shell`. To fix, add this line to your shell.nix:
When a program is installed in your environment, these libraries should be found automatically. However, this is not the case in a `nix-shell`. To fix, add this line to your shell.nix:
  LD_LIBRARY_PATH="/run/opengl-driver/lib:/run/opengl-driver-32/lib";
<syntaxhighlight lang="nix">LD_LIBRARY_PATH="/run/opengl-driver/lib:/run/opengl-driver-32/lib";</syntaxhighlight>


== Testing Mesa updates ==
== Testing Mesa updates ==
Line 13: Line 17:
To avoid a lot of rebuilds there's an internal NixOS option to override the Mesa drivers: <code>hardware.opengl.package</code>
To avoid a lot of rebuilds there's an internal NixOS option to override the Mesa drivers: <code>hardware.opengl.package</code>


It can be used like this: <code>hardware.opengl.package = (import /srv/nixpkgs-mesa { }).pkgs.mesa.drivers;</code>
It can be used like this: <syntaxhighlight lang="nix">hardware.opengl.package = (import /srv/nixpkgs-mesa { }).pkgs.mesa.drivers;</syntaxhighlight>


However, since Mesa 21.0.2 this doesn't necessarily work anymore and something like the following might be required:
However, since Mesa 21.0.2 this doesn't necessarily work anymore and something like the following might be required:
<pre>
<syntaxhighlight lang="nix">
system.replaceRuntimeDependencies = [
system.replaceRuntimeDependencies = [
   ({ original = pkgs.mesa; replacement = (import /srv/nixpkgs-mesa { }).pkgs.mesa; })
   ({ original = pkgs.mesa; replacement = (import /srv/nixpkgs-mesa { }).pkgs.mesa; })
   ({ original = pkgs.mesa.drivers; replacement = (import /srv/nixpkgs-mesa { }).pkgs.mesa.drivers; })
   ({ original = pkgs.mesa.drivers; replacement = (import /srv/nixpkgs-mesa { }).pkgs.mesa.drivers; })
];
];
</pre>
</syntaxhighlight>


'''Note:''' Both of these approaches are impure and only work to a certain degree (many limitations!). If you want to use a different version of Mesa your best option is to use an overlay or a Git worktree where you use the same Nixpkgs revision and only alter <code>pkgs/development/libraries/mesa/</code> for one of the two approaches mentioned above.
'''Note:''' Both of these approaches are impure and only work to a certain degree (many limitations!). If you want to use a different version of Mesa your best option is to use an overlay or a Git worktree where you use the same Nixpkgs revision and only alter <code>pkgs/development/libraries/mesa/</code> for one of the two approaches mentioned above.
Line 34: Line 38:
libGL error: failed to load driver: $DRIVER
libGL error: failed to load driver: $DRIVER
</pre>
</pre>
=== glxinfo ===
Use <code>glxinfo</code> to load 3D acceleration debug information.
If <code>glxinfo</code> returns <code>Error: couldn't find RGB GLX visual or fbconfig</code>, ensure you have <code>hardware.opengl.extraPackages = [ pkgs.mesa.drivers ];</code> set.
== Notes ==
<references group=note />


== Related ==
== Related ==


https://nixos.wiki/wiki/Nixpkgs_with_OpenGL_on_non-NixOS
[[Nixpkgs with OpenGL on non-NixOS]]
 
[[Category: Video]]

Latest revision as of 17:21, 27 June 2024


You can enable OpenGL by setting hardware.graphics.enable = true;[note 1] in your /etc/nixos/configuration.nix.

OpenGL must break purity due to the need for hardware-specific linkage. Intel, AMD, and Nvidia have different drivers for example. On NixOS, these libraries are symlinked under

 /run/opengl-driver/lib

and optionally (if hardware.graphics.enable32Bit[note 2] is enabled)

 /run/opengl-driver-32/lib

When a program is installed in your environment, these libraries should be found automatically. However, this is not the case in a `nix-shell`. To fix, add this line to your shell.nix:

LD_LIBRARY_PATH="/run/opengl-driver/lib:/run/opengl-driver-32/lib";

Testing Mesa updates

To avoid a lot of rebuilds there's an internal NixOS option to override the Mesa drivers: hardware.opengl.package

It can be used like this:

hardware.opengl.package = (import /srv/nixpkgs-mesa { }).pkgs.mesa.drivers;

However, since Mesa 21.0.2 this doesn't necessarily work anymore and something like the following might be required:

system.replaceRuntimeDependencies = [
  ({ original = pkgs.mesa; replacement = (import /srv/nixpkgs-mesa { }).pkgs.mesa; })
  ({ original = pkgs.mesa.drivers; replacement = (import /srv/nixpkgs-mesa { }).pkgs.mesa.drivers; })
];

Note: Both of these approaches are impure and only work to a certain degree (many limitations!). If you want to use a different version of Mesa your best option is to use an overlay or a Git worktree where you use the same Nixpkgs revision and only alter pkgs/development/libraries/mesa/ for one of the two approaches mentioned above.

Debugging Mesa issues

There are a lot of useful environment variables for debugging purposes: https://docs.mesa3d.org/envvars.html

The most important one is LIBGL_DEBUG=verbose and helps with debugging error like:

libGL error: MESA-LOADER: failed to open $DRIVER (search paths /run/opengl-driver/lib/dri)
libGL error: failed to load driver: $DRIVER

glxinfo

Use glxinfo to load 3D acceleration debug information.

If glxinfo returns Error: couldn't find RGB GLX visual or fbconfig, ensure you have hardware.opengl.extraPackages = [ pkgs.mesa.drivers ]; set.

Notes

  1. Renamed from hardware.opengl.enable in NixOS 24.11
  2. Renamed from hardware.opengl.driSupport32Bit in NixOS 24.11

Related

Nixpkgs with OpenGL on non-NixOS