Autopatching binary packages to make them work with Nix
Switch branches/tags
Nothing to show
Clone or download
Latest commit b6fe3f8 Apr 29, 2018
Failed to load latest commit information.
examples Initial commit Apr 29, 2018
LICENSE Initial commit Apr 29, 2018 Initial commit Apr 29, 2018
autopatchelf Initial commit Apr 29, 2018
default.nix Initial commit Apr 29, 2018


The Nix package manager is a powerful package manager supporting a number of unique advantages over conventional solutions, such as automatic deployment from declarative specifications, dependency completeness, atomic upgrades and rollbacks.

Although many packages can be convienently packaged in source form, prebuilt binary packages are typically much harder to deploy -- when running them unmodified on NixOS, their dependencies cannot be found, because they reside in global locations such as /bin and /lib which Nix does not use. Instead, Nix stores the content of each packages in isolated directories in the Nix store (/nix/store).

There are variety of solutions to patch binary packages to make them work in the Nix store (most notably PatchELF but using them requires a substantial amount of investigation and trial-and-error runs.

This package provides a toolset that can be used to automatically patch prebuilt binary packages as much as possible so that they can find their dependencies in isolated directories in the Nix store. As a result, they can be conveniently deployed by the Nix package manager with little to no manual adjustments.

Available tools

This package provides a number of tools.


  • Auto patches ELF binaries by checking for their required libraries and searching the contents of a provided list of packages.
  • Detects x86 and x86_64 executables and prevents binaries of different architectures to conflict.


Clone the Git repository, then run the following command to install the program in your Nix profile:

$ nix-env -f default.nix -iA build



Idea: specify the search library paths in an environment variable and run autopatchelf to automatically bind to the package that provides the library.

Specifying the library search locations

You can specify the search library paths by setting the libs variable in the shell session or in a Nix expression:

libs = stdenv.lib.makeLibraryPath [ pkgs.zlib pkgs.openssl ];

You can also specify the search libraries per CPU architecture. autopatchelf ignores the search paths for an architecture that does not match the architecture of an ELF executable:

libs_i386 = [ pkgs_i686.zlib pkgs_i686.openssl ]; 
libs_x86_64 = [ pkgs_x86_64.zlib pkgs_x86_64.openssl ];

This is particular convenient for packages that have a mix of i386 and x86_64 binaries.

Patching an individual executable

The following command auto patches a single executable:

$ autopatchelf ./myexecutable

Recursively patching all executables in a directory

When the parameter is a directory, autopatchelf attempts to recursively patch all executables in the directory.

$ autopatchelf ./bin

Patching all executables in a directory non-recursively

It is also possible to not recurse in any sub folders by providing the --no-recurse parameter:

$ autopatchelf --no-recurse ./bin

Specifying the search path variable name

By default, autopatchelf uses the libs search environment variables. The name of the search path environment variable can be adjusted with the --variable parameter:

$ autopatchelf --variable LD_LIBRARY_PATH ./myexecutable

The above command uses the LD_LIBRARY_PATH environment variable to search for packages.


The examples/ sub folder contains a number of example use cases:

  • kzipmix.nix deploys kzip and kzipmix executables for i686-linux and x86_64-linux.
  • cpio-dpkg.nix deploys GNU cpio by patching a Debian package.
  • quake4-demo.nix deploys a demo version of the Quake 4 computer game.


The contents of this package is available under the same license as Nixpkgs -- the MIT license.