What is the best way to deal with lib dependencies while developing on NixOS?

I’m currently developing a small OpenGL application on NixOS. To make it run I need to adjust the LD_LIBRARY_PATH to include the installed libs on the system because the program uses glfw.
So the path currently looks like this: /run/opengl-driver/lib:/nix/var/nix/profiles/system/sw/lib/

The path to the OpenGL libs was already there but the path to the system libs was not.

  1. Why is this path not automatically added to the variable in NixOS? To me it seems that this is needed for NixOS to work as any other Linux distribution when trying to develop something.
  2. It is done for the OpenGL libs already as it seems, where is that done?
  3. In addition to the system path there should also be the path to the libs installed by the user: ~/.nix-profile/lib/
  4. If for whatever reason the setting of the LD_LIBRARY_PATH is not a good or preferred solution.
    Why not and how to deal with the dependencies then? Always write a Nix expression/package?

Ok I can think of something for myself.
Adding 1 and 3 to the path might introduce problems due to incompatible lib versions installed in system or user profile.

Using an expression has the advantage of not needing to install the libs either in system or via user before everything works again.
But when running clean the downloaded package might be removed from the nix store and needs to be downloaded again which might be bad if this happens in an offline development situation. Something which cannot happen if the lib was installed.



Have you set hardware.opengl.enable = true in you NixOS config? That should also set the LD_LIBRARY_PATH system-wide. You can see that here:

Apart from going through NixOS, I’d suggest using a derivation for your project that can be used with nix-shell and includes the packages your project needs.

1 Like

No hardware.opengl.enable = true wasn’t set in my configuration still the LD_LIBRARY_PATH contained /run/opengl-driver/lib.
I think that was not always the case and might have happened after an update of my system at some point.
I still added the setting now as it just seems better.

I too think it’s better to use a derivation with Nix as it is the way Nix works and has all the advantages.
But what about the offline development scenario? Is it somehow possible to prevent the dependencies from being cleaned?

And just out of curiosity. Why is the LD_LIBRARY_PATH not filled with the path to the system libs automatically and why the exception for the OpenGL libs?

I found nix-shell dependencies can be garbage collected any time now / persistent nix-shell envs · Issue #2208 · NixOS/nix · GitHub about this problem.

Apart from going through NixOS, I’d suggest using a derivation for your project that can be used with nix-shell and includes the packages your project needs.

What I have so far is:

with import <nixpkgs> {};
stdenv.mkDerivation rec {
  name = "learnopengl";
  buildInputs = [ dmd dub ];


  shellHook = ''
    echo For running the triangle example: dub run :hello_triangle

I’m not sure about DISPLAY, XAUTHORITY and the opengl lib path being hardcoded like this.
Is it possible to improve on that?

opengl is an exception because it requires hardware specific drivers. To avoid recompiling all opengl-based software we inject this via /run/opengl-driver/lib and make it configurable that way.

For non-nixos systems there is also GitHub - guibou/nixGL: A wrapper tool for nix OpenGL application