• Stars
    star
    422
  • Rank 102,753 (Top 3 %)
  • Language
    C
  • License
    MIT License
  • Created almost 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Run unpatched dynamic binaries on NixOS

nix-ld

Run unpatched dynamic binaries on NixOS.

Where is this useful?

While many proprietary packages in nixpkgs have already been patched with autoPatchelfHook patching, there are cases where patching is not possible:

  • Use binary executable downloaded with third-party package managers (e.g. vscode, npm or pip) without having to patch them on every update.
  • Run games or proprietary software that attempts to verify its integrity.
  • Run programs that are too large for the nix store (e.g. FPGA IDEs).

While there are other solutions such as buildFHSUserEnv that restore a Linux file hierarchy as found on common Linux systems (ld-linux-x86-64.so.2), these sandboxes have their own weaknesses:

  • setuid binaries cannot be executed inside a fhsuserenv
  • inside a buildFHSUserEnv you can not use other sandbox tools like bwrap or 'nix build'.
  • buildFHSUserEnv requires a subshell which does not work well with direnv

How does nix-ld work?

Also read this blog post to get the explaination in full detail. A summary is below:

Precompiled binaries that were not created for NixOS usually have a so-called link-loader hardcoded into them. On Linux/x86_64 this is for example /lib64/ld-linux-x86-64.so.2. for glibc. NixOS, on the other hand, usually has its dynamic linker in the glibc package in the Nix store and therefore cannot run these binaries. Nix-ld provides a shim layer for these types of binaries. It is installed in the same location where other Linux distributions install their link loader, ie. /lib64/ld-linux-x86-64.so.2 and then loads the actual link loader as specified in the environment variable NIX_LD. In addition, it also accepts a comma-separated path from library lookup paths in NIX_LD_LIBRARY_PATH. This environment variable is rewritten to LD_LIBRARY_PATH before passing execution to the actual ld. This allows you to specify additional libraries that the executable needs to run.

Installation

nix-ld is part of nixpkgs since NixOS 22.05. There one can enable it with the following nixos setting:

{
  programs.nix-ld.enable = true;
}

To install nix-ld from the repository instead, use the following method:

$ sudo nix-channel --add https://github.com/Mic92/nix-ld/archive/main.tar.gz nix-ld
$ sudo nix-channel --update

/etc/nixos/configuration.nix

{
  imports = [
    <nix-ld/modules/nix-ld.nix>
  ];
  # The module in this repository defines a new module under (programs.nix-ld.dev) instead of (programs.nix-ld) 
  # to not collide with the nixpkgs version.
  programs.nix-ld.dev.enable = true;
}

With nix flake

Add the following lines to /etc/nixos/flake.nix. Replace myhostname with the actual hostname of your system.

# flake.nix
{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/master";
  inputs.nix-ld.url = "github:Mic92/nix-ld";
  # this line assume that you also have nixpkgs as an input
  inputs.nix-ld.inputs.nixpkgs.follows = "nixpkgs";

  outputs = { nix-ld, nixpkgs, ... }: {
    # replace `myhostname` with your actual hostname
    nixosConfigurations.myhostname = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        # ... add this line to the rest of your configuration modules
        nix-ld.nixosModules.nix-ld

        # The module in this repository defines a new module under (programs.nix-ld.dev) instead of (programs.nix-ld) 
        # to not collide with the nixpkgs version.
        { programs.nix-ld.dev.enable = true; }
      ];
    };
  };
}

Usage

After setting up the nix-ld symlink as described above, one needs to set NIX_LD and NIX_LD_LIBRARY_PATH to run executables. For example, this can be done with a shell.nix in a nix-shell like this:

with import <nixpkgs> {};
mkShell {
  NIX_LD_LIBRARY_PATH = lib.makeLibraryPath [
    stdenv.cc.cc
    openssl
    # ...
  ];
  NIX_LD = lib.fileContents "${stdenv.cc}/nix-support/dynamic-linker";
}

A full example is shown in ./examples/masterpdfeditor.nix.

In nix-autobahn there is also a script called nix-autobahn-ld that automates generating shell expressions.

In nix-alien there is another script called nix-alien-ld that uses another strategy, wrapping the program in a writeShellScriptBin with the NIX_LD/NIX_LD_LIBRARY_PATH environment variables set.

To figure out what libraries a program needs, you can use ldd on the binary or set the LD_DEBUG=libs environment variable.

Known Issues

LD_LIBRARY_PATH is inherited by child processes

nix-ld is currently rewrites NIX_LD_LIBRARY_PATH to LD_LIBRARY_PATH. This can cause problems if a program loaded with this loader executes a normal binary, which should not get these libraries. In the future, it may be possible to redirect execution back to nix-ld after the actual library loader has done its job by changing the entry point in memory to fix this.

FAQ

How to find libraries for my executables?

You can use tools like nix-autobahn, nix-alien or use nix-index

Why not set LD_LIBRARY_PATH directly instead of NIX_LD_LIBRARY_PATH?

LD_LIBRARY_PATH affects all programs, which can inject the wrong libraries in correct build nix application that have an RPATH set in their executable.

Does this work on non-NixOS system?

No. Normal Linux distributions will have their own link-loader. Replacing those with nix-ld will break the system.

My python/nodejs/ruby/$interpreter libraries do not find the libraries configured by nix-ld

Nix-ld is only used by unpatched executables that use the link loader at /lib or /lib64. If you use for example python from nixpkgs than it will not pick up NIX_LD_LIBRARY_PATH and NIX_LD since these types of binaries are configured to use a glibc from the nix store. If you encounter these cases i.e. when you are trying to use python packages installed in a virtualenv than you need to set LD_LIBRARY_PATH directly. You can also create yourself a wrapper like this:

(pkgs.writeShellScriptBin "python" ''
  export LD_LIBRARY_PATH=$NIX_LD_LIBRARY_PATH
  exec ${pkgs.python3}/bin/python "$@"
'')

More Repositories

1

sops-nix

Atomic secret provisioning for NixOS based on sops
Nix
1,439
star
2

cntr

A container debugging tool based on FUSE
Rust
542
star
3

nix-update

Swiss-knife for updating nix packages.
Python
459
star
4

nixos-shell

Spawns lightweight nixos vms in a shell
Nix
423
star
5

nixpkgs-review

Review pull-requests on https://github.com/NixOS/nixpkgs
Python
383
star
6

python-mpd2

Python library which provides a client interface for the Music Player Daemon.
Python
353
star
7

dotfiles

My NixOS dotfiles
Nix
299
star
8

nix-fast-build

Combine the power of nix-eval-jobs with nix-output-monitor to speed-up your evaluation and building process.
Python
210
star
9

envfs

Fuse filesystem that returns symlinks to executables based on the PATH of the requesting process.
Rust
133
star
10

vmsh

Shell into a virtualized linux, with your own tools
Rust
132
star
11

mina-sidekiq

Tasks to deploy Sidekiq with mina.
Ruby
91
star
12

nix-build-uncached

A CI friendly wrapper around nix-build.
Go
72
star
13

zig.ko

Linux kernel module written in Zig
Makefile
70
star
14

nixos-aarch64-images

Build NixOS images for various ARM single computer boards
Python
49
star
15

x86_64-linux-cheatsheats

Plain files for syscalls, errnos, signals, registers and x86_64 instructions
Python
49
star
16

pry.py

pry.py - an interactive drop in shell for python, similar to binding.pry in ruby
Python
49
star
17

ssh-to-age

Convert SSH Ed25519 keys to age keys. This is useful for usage in sops-nix and sops
Go
47
star
18

hue-ble-ctl

Control your Phillips Hue light bulb over bluetooth
Python
33
star
19

pythonix

Eval nix code from python
C++
32
star
20

awesome-dotfiles

Configuration files of the window manager awesome
Lua
28
star
21

fast-flake-update

Update flake.lock with the latest commit of a local checkout
Python
27
star
22

nur-packages

My personal NUR repository
Nix
23
star
23

flake-linter

Find duplicate dependencies in flakes
Python
18
star
24

ansible-lxc

Ansible Connection Plugin for lxc containers (https://linuxcontainers.org/)
Python
17
star
25

iana-etc

Build /etc/protocols and /etc/services files from IANA's Assigned Internet Protocol Numbers
Python
17
star
26

lognotify

log watcher for awesome wm
Lua
14
star
27

nix-sysdig

Wrapper to debug sysdig builds
Python
13
star
28

github-tags

sinatra app to generate rss feeds with the latest git tags of a project on github
Ruby
12
star
29

nixican-standoff

Benchmark between nix, lix and tvix
Python
12
star
30

whois42d

Whois server for the dn42 registry
Go
11
star
31

ssh-to-pgp

Convert SSH RSA keys to GPG keys
Go
11
star
32

utils

A set of lua modules I use in awesome wm.
Lua
9
star
33

stockholm

Mirror of https://git.thalheim.io/Mic92/stockholm/
Nix
9
star
34

bing-gpt-server

HTML
8
star
35

flake-templates

Personal templates i like to use.
Nix
7
star
36

valauncher

A fast dmenu-like gtk3 application launcher
CMake
7
star
37

dlopen-resolver

Python
7
star
38

openvpn-ddns

Maintain dns records for connecting openvpn clients
Ruby
7
star
39

dream2nix-home-assistant

Packaging experiments with dream2nix to package home-assistant with all dependencies.
Nix
5
star
40

kvm-pirate

Attach to kvm-based VMs
Python
5
star
41

mechanical-keyboards

Configuration of my collections of keyboards
Nix
4
star
42

systemd-ta

http://c3d2.de/news/ta-systemd.html
JavaScript
4
star
43

nix-build-shell

Rust
4
star
44

robolab

Simulator for the course Robolab at TU Dresden
4
star
45

nixos-test-example

Nix
4
star
46

retiolum

Mirror of https://git.thalheim.io/Mic92/retiolum
Nix
4
star
47

server-bookings

Rust
3
star
48

Algebra-I

Das inoffizielle Skript zur Vorlesung bei Prof. Schmidt
Ruby
3
star
49

nix-fmt

abandoned in favor of https://github.com/orivej/go-nix and https://gitlab.com/jD91mZM2/rnix
OCaml
3
star
50

imap-notify

IMAP notifier using IMAP's NOTIFY SET
Python
3
star
51

docker-pid

Resolve container id/name to container's process id
Go
3
star
52

nftables

Mirror of netfilter/nftables
C
3
star
53

nixos-configuration

The content of this repo has been integrated into https://github.com/Mic92/dotfiles/
3
star
54

mpdtools

Usefull tools for MPD: mpdadd - Link and play files outside of the MPD directory to MPD. mpdmark - bookmark songs
Ruby
3
star
55

nsattach

attach to linux namespaces
C
3
star
56

bors-gen-config

Generate bors.toml for github repositories
Python
2
star
57

int3

Better debugger breakpoints
Python
2
star
58

vtune-nix

Vtune nix package
Nix
2
star
59

live-net-info

My adventures using the bubbletea framework.
Go
2
star
60

blog

Source of my blog
Shell
2
star
61

themenabend-nixos

Folien und Code zum Themenabend über Nix/Nixos
JavaScript
2
star
62

lxc-machined-start

Integrate lxc container into machined
M4
2
star
63

company-tmux

emacs auto complete with content of tmux panes
Emacs Lisp
2
star
64

pgp-verify

Verify pgp signatures of files.
Go
2
star
65

build-system-koans

C
2
star
66

SWT_And_Programming

Programs/Stubs created during exercises at university.
Java
2
star
67

mpdstated

Auto restore recent position for each podcast in mpd.
Vala
2
star
68

bme680-mqtt

Publish BME680 sensor data to home-assistant via MQTT
Python
2
star
69

container-pid

Rust crate to resolve a container names/ids to PID
Rust
2
star
70

nixcon2023-nixos-anywhere

Presentation slides for NixCon 2023 presentation on nixos-anywhere
JavaScript
2
star
71

systemd-user-units

2
star
72

nixos-wiki-redirector

JavaScript
2
star
73

disko-yubikey-demo

Nix
2
star
74

scripts

All my tiny scripts and stubs
Shell
1
star
75

lualdap

fork of https://git.zx2c4.com/lualdap/ with lua5.3 support
C
1
star
76

pcap-preload

Rust
1
star
77

nixpkgs-committers

Repository for nominating maintainers for Nixpkgs commit access
1
star
78

mic92.github.com

1
star
79

phd-website

HTML
1
star
80

ports

my freebsd ports
Makefile
1
star
81

drone-convert-nix

Go
1
star
82

webscraping-workshop

Folien und Code zum Webscraping workshop auf den Datenspuren 2015
JavaScript
1
star
83

qtile-config

Python
1
star
84

semeion

A DynDNS Server interface in Haskell and Yesod
Haskell
1
star
85

hadoop-exercise

MapReduce Assignment 2015 at System Engineering II (TU Dresden)
Java
1
star
86

xfstests-cntr

Fork of xfstests with support for cntr
Shell
1
star
87

fuidshift

Move Filesystem ownership into other subordinated uid ranges
Go
1
star
88

android-notifier

Automatically exported from code.google.com/p/android-notifier
Java
1
star
89

fft

FFT Implementation for Tensilica DSP Processors
C
1
star
90

userborn-with-impermanence

Nix
1
star
91

drone-nix-scheduler

Schedule nix jobsets in drone ci
Python
1
star
92

arch-package-feed

bottle.py based project to provide a more advanced arch package feed.
Python
1
star
93

PKGBUILDs

My PKGBUILDs of Packages I maintain in Archlinux AUR
Shell
1
star
94

tracedump

System service to dump Intel processor trace + memory after a crash.
Python
1
star
95

Japanese-sum-solver

A solver for the logic game Japsum
JavaScript
1
star
96

clusterssh

wrapper arround go-ssh to execute commands in a cluster
Go
1
star