Table of Contents

Nix: Using overlays to fix OpenGL apps on Linux

Sometimes Linux mysteriously breaks

I recently ran into an annoying problem. When running some OpenGL applications (that I had installed with nix) they would fail to launch, spitting out some rather strange errors on the console.

Here’s the error that would occur when running alacritty:

Error: "failed to find suitable GL configuration."

Here’s the one when running kitty:

[0.042] Ignoring unknown config key: open_url_modifiers
[0.042] Ignoring unknown config key: rectangle_select_modifiers
[0.042] Ignoring unknown config key: terminal_select_modifiers
[0.042] Ignoring unknown config key: resize_draw_strategy
[0.083] [glfw error 65542]: GLX: No GLXFBConfigs returned
[0.083] [glfw error 65545]: GLX: Failed to find a suitable GLXFBConfig
[0.083] Failed to create GLFW temp window! This usually happens because of old/broken OpenGL drivers. kitty requires working OpenGL 3.1 drivers.

Figuring out the issue

At first I thought maybe something in my system had gone haywire but no, I couldn’t find anything wrong. So I started googling the errors to see if anyone had run into them before.

Luckily, it seems I wasn’t the first to run into this issue, someone else was having problems running alacritty: github.com/NixOS/nixpkgs/issues/230660.

Linked was a thread to nixpkgs where the discussion shed light that this is a known issue on non-Nix OS distributions: github.com/NixOS/nixpkgs/issues/9415.

The beauty of FOSS (Free and Open Source Software) is that more often than not, someone has already come up with a fix for your issue. In this case, the excellent nixGL tool github.com/nix-community/nixGL.

The solution

What we need to do then is to wrap our problematic apps with a script to launch them through nixGL.

Add the following to our inputs:

inputs = {
	# your inputs
	# ...
	
	# add nixGL
    nixgl.url = github:guibou/nixGL;
};

Then add an overlay for the specific apps we want to wrap. In my case it’s alacritty and kitty:

outputs = {self, nixpkgs, nixgl, ...}: 
	let
      wrapWithNixGL = final: prev: {
        alacritty = final.writeShellScriptBin "alacritty" ''
          exec ${nixgl.packages.x86_64-linux.nixGLDefault}/bin/nixGL ${prev.alacritty}/bin/alacritty "$@"
        '';
        kitty = final.writeShellScriptBin "kitty" ''
          exec ${nixgl.packages.x86_64-linux.nixGLDefault}/bin/nixGL ${prev.kitty}/bin/kitty "$@"
        '';
      };

	pkgs = import nixpkgs {
        system = "x86_64-linux";
		overlays = [ wrapWithNixGL ];
	};
	

My own set up is a bit more involved, which you can see on my dotfiles repo: github.com/aalbacetef/dotfiles (in nix/flake.nix).

Hope this helped someone!