Nix Rootless: Install nix without root, proot, or user namespace, while reusing binary cache

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 :white_check_mark: :white_check_mark: :white_check_mark: :prohibited: :white_check_mark:
No user namespace required :white_check_mark: :prohibited: :prohibited: :white_check_mark: :white_check_mark:
High performance without user namespaces :prohibited: Uses proot :prohibited: Cannot run :prohibited: :white_check_mark: :white_check_mark:
Run downloaded binaries directly from the host shell :prohibited: Requires namespace or proot environment :prohibited: Requires namespace environment :prohibited: Requires namespace environment :white_check_mark: :white_check_mark:

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.

8 Likes

Please modify your post to adhere to the NixOS Discourse AI/LLM policy.

5 Likes

Interesting. I wonder if this approach could be viable on android. nix-on-droid works, but its performance is pretty bad due to proot use.

I’m always a bit skeptical when someone describes their own project as “amazing”, though… :stuck_out_tongue:

6 Likes

I also thought about this some months ago, but the idea of empty paths as paddings is innovative! But there’s a small catch. We allow paths being encoded in ways that Nix cannot detect (such as java zipping the jar) in an output, and what we do in this condition is simply propagatedBuildInputs, which would write them to .nix-support/propagated-build-inputs (I’m not doing the reviewing work recently so my memory could be wrong). This way, if your Nix store is still at /nix/store, we indeed ensure the existence of these paths. This rewrite method would silently break, on the other hand.

Still cool though!

3 Likes

Thanks for your reminder! I’ve updated my LLM usage disclosure.

2 Likes

I think it would work for nix-on-droid, though I haven’t tried.

About the “amazing” wording, well :joy: It’s because this has long been my pain point for using school servers, and I vibe coded this in an afternoon. I was shocked how easy it was, and how little obstacle I met. I really think it’s good, and I encourage you to try it!

2 Likes

Thanks a lot for your valuable feedback!

I’m really not that deep into nix packaging, so this is very valuable information for me. As I expected, my method would probably cover ~90% packages, and the long-tail ones would be hard to tackle. That’s why I tested it with hello, python, gcc and IFDs.

Now I know the existence of `propagatedBuildInputs, I’ll try to figure out if I can support it!

1 Like

I doubt you will find a solution to the problem. If though you do, share it with the CA-team, as they also have a problem with the encrypted or compressed paths in binaries as well.

As you can’t find/identify the path, there is no way to rewrite it.

2 Likes

I think you’re right. All I can think of (after discussing with AI) is:

  • Force rebuild any package with propagatedBuildInputs. Do not use binary cache.
  • Force rebuild packages with known problems, as some packages with compressed store path may not use propagatedBuildInputs to declare the dependency (maybe it’s just a buildInput)
  • Add special format parsers for common formats (e.g. jar, python zip)

None of the above can solve all affected packages. None of those ensure not adding extra processing to packages that don’t need this.

1 Like

That’s pretty smart. This approach could also be used for grafting (applying security patches without recompilation).

Why do you use / for padding instead of just using more bytes of the store hash? That would also decrease the chance of collision.

Is ld.so patched in the same way?

1 Like
  1. I can’t understand how is this useful for grafting: You can just download a new lib into your real nix store and patch it to use that path, if you have root.
  2. We no longer have store hash, only a count number. The number would grow on its own, e.g. some packages have 21 others have 3345. // for padding is only for implementation simplicity.
  3. Yes, all paths in nix store are patched the same way. Packages installed using nix-rootless can use different glibc than your system.
1 Like

The store path lengths have to be identical for the current grafting approach in nixpkgs.

1 Like

Oh, that makes sense.

But my approach would only help if the new path is shorter than the old one, which may not always be true if it’s a new regular nix store path.

1 Like

Well, you would get a shorter path from removing the hash part in that case and than make it a symlink to the fixed version.

4 Likes

Little off-topic, but: this is hosted on Tangled, nice!

Seems like a cool way of using Nix when you can’t make /nix, probably the best way in many circumstances, at least without a fully relocatable store.

1 Like

even more off-topic: I liked Tangled, until they got vc-funded.

EDIT: I have high hopes of having a faster nix-on-droid. GitHub - nix-community/nixos-avf: NixOS for Android Terminal (Android Virtualization Framework) [maintainers=@mkg20001] · GitHub doesn’t seem very alive anymore.

1 Like

Getting REALLY off-topic here:
I can understand being skeptical due to that :slightly_smiling_face:. The good thing is that it’s built on ATProto and you can self-host Knots and Spindles alongside your PDS (assuming you self-host that, you can also run them even if you’re using someone else’s PDS (technically a Knot is also a PDS, but only for repo accounts)), so it’s mostly the stuff you really need to depend on them for are the Bobbin and frontend (both of which are also FOSS and can be self-hosted, although the Bobbin seems like it might be more expensive to selfhost).
There are also plenty of alternative knot and spindle implementations, as well as alternative Web UIs like snarled.

And of course, it’s still better than GitHub!

2 Likes

TBH the main reason I use tangled is my github account was suspended for no reason. But yeah, tangled is cool. Features are enough, UI is easy to navigate, and no copilot slop.

Well I felt lucky that they are VC funded. Having money makes things sustainable. Servers cost money.

Sounds cool, it seems there’s more support for faster nix-on-droid than on linux servers. Maybe it’s time to explore integrating into nix-on-droid…

3 Likes