Using native libraries


I’m using nixpkgs to build and deploy our product in production. In the near future I’ll need to use a proprietary library needed to interact with a piece of hardware. Unsurprisingly the library has no support for nix (or other package managers btw), in fact it came with an installer.

What’s the best way to still use nixpkgs to build the module that require the proprietary library?

If you have a pre-compiled library, just install it to $out/lib:

stdenv.mkDerivation {
  name = "mylib-1.0";
  src = fetchurl { ... };
  installPhase = ''
    install -D $src/ $out/lib/
    mkdir $out/include
    cp -r $src/include/* $out/include
stdenv.mkDerivation {
  name = "myapp";
  buildInputs = [ mylib ];

It’s so simple.

@Mic92: Wouldn’t you want to also patch up the rpath of the shared objects with whatever it depends on?

@wfranzini: I would inspect the shared object “Dynamic Section” with “objdump -x” (alternatively ldd), find what it depends on (probably at least glibc) and find the appropriate nix derivation for the dependency. Then use “patchelf --set-rpath” to wire that dependency into your .so files.

Here is an example for the google-talk-plugin patching up its .so files:

More examples: Search · rpath · GitHub

1 Like

Also take a look at autoPatchelfHook, which sets the rpath automatically:

stdenv.mkDerivation {
  name = "mylib-1.0";
  nativeBuildInputs = [ autoPatchelfHook ];
  buildInputs = [ somelib ];