Include nixGL in nix flake app

I have a utility that uses OpenGL. It works out of the box on NixOS, but to get it to work on other Linuxes, I need to prepend its invocation with nixGL. Fair enough.

When I turn this utility into a nix flake app and try to execute it with nix run it fails on NixOS. I guess that this is because of the purity provided by the flake, which prevents the app from seeing the OpenGL infrastructure on my machine. That’s good.

Can the necessary nixGL gymnastics be included in the flake app, so than nix run Just Works wherever it is run, whether it be NixOS or otherwise? If so, how?

On nixos, a special path /run/opengl-driver/lib is needed, as that’s where hardware acceleration libraries are installed.

On non-NixOS systems, linking it to where you would find other graphics libraries might be enough… Although you may have issues as it looks like addOpenGLRunpath hooks prepends it to the rpath:

patchelf --set-rpath "@driverLink@/lib:$origRpath" ${forceRpath:+--force-rpath} "$file"

It might be safer to selectively symlink only the hardware libraries which are needed, which nixGL might be doing (or a similar approach).

Use of LD_LIBRARY_PATH , will likely cause a lot of abi issues; so I would avoid using it, although it would be a quick and dirty hack to change linking behavior.

Related: RFC 121 and nixpkgs #158079

Yeah, just make use of makeWrapper and wrap the binary with nixGL executable.