Handling Dynamic Runtime Dependencies

Hello,

I have been trying setup my development PC to run on NixOS. I’ve gotten 95% of everything working very easily. I am running into issues with some scripts/interpreted apps I’ve written though.

For example, I have a small application written in Ruby that uses Xlib to capture hotkey presses and perform actions on windows. It connects to Xlib through Ruby’s FFI and the ‘xlib’ gem. Building it as a package is fairly simple using bunder/bundix, and it installs just fine. The problem is that when it’s installed and run through nix, it fails as soon as it tries to call any xlib functions.

I’m struggling to figure out how to require xlib and expose it’s libraries to Ruby at runtime.

It seems that Nix is awesome for dealing with static applications, but it gets more difficult when dealing with interpreted dynamic applications which don’t have clear dependencies. On a production system this isn’t a big deal since generally such dynamic applications are avoided, but in a development environment, it’s nice to get away with more laziness so that things can change more rapidly.

I’ve been tempted to start running some things in Docker, or possibly install Gentoo’s portage in my user directory which is frustrating because it defeats the purpose of running NixOS.

1 Like

The solution I’m using right now so that I can get work done is to run the application with the environment variable LD_LIBRARY_PATH=/nix/store/libX11/lib (actually /nix/store/m0gzmk8izjmg2380wmgilhymv04jsz4n-libX11-1.6.5/lib/ on this pc)

I’m sure there is an idiomatic way to get this path and wrap the executable in the package, but it’s not apparent to me at the moment. For now, I am using a this more hackish solution so I can focus on working.

Add this to your bundlerEnv:

gemConfig = defaultGemConfig // {
  xlib = attrs: {
    dontBuild = false;
    postPatch = ''
      substituteInPlace lib/xlib.rb \
        --replace "X11" "${xlibs.libX11}/lib/libX11${stdenv.hostPlatform.extensions.sharedLibrary}"
    '';
  };
};

With this i get:

ruby -r xlib -e 'p Xlib.available?'                                                                                                                                                                                       
true

Also would be nice if you could make a PR for our nixpkgs/default.nix at master · NixOS/nixpkgs · GitHub

1 Like