Using Nix as un-privileged user
Notes on how to install and use Nix as a reproducible build/pkg manager without needing any special privileges on a Linux-like system. The nix store can be on any location writeable by the user and the cached derivations can still be used.
This post is inspired by https://zameermanji.com/blog/2023/3/26/using-nix-without-root/ with some additions/updates:
- The store location can be specified to any directory
- Use configuration file to avoid having to spell out the store location
- How to use nix profiles
Get a static version of nix
You can get the list of current versions form Hydra and download manually if desired. As an example to get the latest 2.18 version use following:
curl -L https://hydra.nixos.org/job/nix/maintenance-2.18/buildStatic.x86_64-linux/latest/download-by-type/file/binary-dist > nixstatic
Configure
Configure in ~/.config/nix/nix.conf
:
store = ~/mynixroot
extra-experimental-features = flakes nix-command
ssl-cert-file = /etc/pki/tls/cert.pem
As can be seen the nix root will be in ~/mynixroot
The ssl cert file may need to be specified. To find the right one your
system can for example strace
and wget
invocation.
Build and run
This is sufficient to build and run programs, e.g.:
~/nixstatic run nixpkgs#hello
Enter Nix environment
In order to easily use nix-installed programs can enter a shell like:
~/nixstatic shell nixpkgs#nix nixpkgs#bashInteractive --command bash
The can do commands like nix profile install
etc. Note however that
can not recursively enter another nix shell
!