Hi all! I just built nix-rootless.
Features
As the title implies, it allows you to:
- Install nix
- Without root
- Without user namespace (some old distro, or HPC environment doesn’t have it)
- Without using proot (slow)
- All while reusing official binary cache.
Why not xxx?
| Feature | nix-portable | nix-user-chroot | Nix built-in chroot store | Changing Nix store path | nix-rootless |
|---|---|---|---|---|---|
| Reuse Nix binary cache | |||||
| No user namespace required | |||||
| High performance without user namespaces | |||||
| Run downloaded binaries directly from the host shell |
Executing downloaded binary is important when you want to use home-manager on your server. For nix-porable, you have to write complex shell-switching script in bashrc and shell wrappers for other programs you wish to run by e.g. ssh server wezterm.
How it works?
The idea is similar to conda: download prebuilt packages, then patch their paths.
Conda builds its packages with a very long (255 character) installation prefix, e.g. /conda-prefix-xxxxxxx/lib/libfoo.so. When you run conda install, it downloads those binaries and replaces recorded occurrences of the prefix with something like /home/username/.conda/envs/myenv. The conda prefix is long enough to contain almost any user name. It then adds a \0 at the end of the short path, so that programs don’t read beyond that.
The problem with nix is that the default prefix /nix/store is not long enough. /home/user/.nix-rootless is probably longer than that. However, there’s actually plenty of space for us to use at the hash part: it’s 32 characters. Can we somehow make use of that?
Turns out you can. Just replace the long hash with a short number, then point it to the real thing. For example, we patch fish like this:
ldd $(which fish)
linux-vdso.so.1 (0x00007ffe52316000)
libpcre2-8.so.0 => /home/username/.nix-rootless/s///////////98-pcre2-10.49/lib/libpcre2-8.so.0 (0x000070472f17a000)
libpcre2-32.so.0 => /home/username/.nix-rootless/s///////////98-pcre2-10.49/lib/libpcre2-32.so.0 (0x000070472e563000)
Note the absense of hash. Here, the ////s are paddings to make new path as long as the original one.
Then we create symlinks like this:
0-aws-c-common-0.12.4 -> /home/username/.nix-rootless/long_store/0a0y1r2nhq7gxg00s9hm6h9ncv8qb38f-aws-c-common-0.12.4
1-gcc-14.3.0-lib -> /home/username/.nix-rootless/long_store/0p8b2lqk47fvxm9hc6c8mnln5l8x51q1-gcc-14.3.0-lib
2-attr-2.5.2 -> /home/username/.nix-rootless/long_store/0y221n4x5cwrig34sq4i40n3lls0m2ac-attr-2.5.2
98-pcre2-10.49 -> /home/username/.nix-rootless/long_store/m9y00qgmfbzvlffgbx0hi5hc38dps46q-pcre2-10.49
And it works.
LLM usage disclosure: This project is a fork of the official nix, and all changes are made by LLM.