Installing Nix without root is possible by using nix-user-chroot, which creates a user-space chroot and maps a writable directory to /nix. This allows Nix to work in environments where you cannot create or write to the real /nix path.
Reference:
- https://nixos.wiki/wiki/Nix_Installation_Guide#nix-user-chroot
- https://github.com/nix-community/nix-user-chroot
Download the nix-user-chroot binary from the latest GitHub release and place it in ~/.local/bin.
# make sure local bin exists
mkdir -p ~/.local/bin
# install nix-user-chroot (replace <url> with the latest release URL)
curl -L -o ~/.local/bin/nix-user-chroot <url>
chmod +x ~/.local/bin/nix-user-chroot
# verify binary works
~/.local/bin/nix-user-chroot --helpnix-user-chroot maps your local directory (for example ~/.nix) to /nix inside the chrooted shell.
# create backing directory that will be mapped to /nix
mkdir -p ~/.nix
# optional: avoid toolchain issues in some HPC environments
unset LD_LIBRARY_PATH
# start a shell where ~/.nix is mapped to /nix
~/.local/bin/nix-user-chroot ~/.nix bash
# inside the chroot shell, verify mapping
ls -ald /nix
echo "Backed by: ~/.nix"
# run official Nix installer
curl -L https://nixos.org/nix/install | shRun these commands from inside the chroot shell after installation:
# load nix environment for current shell
. ~/.nix-profile/etc/profile.d/nix.sh
# check CLI is available
command -v nix
# show version
nix --version
# quick functional test
nix run nixpkgs#helloExpected result:
command -v nixreturns a path under your home directory.nix --versionprints the installed Nix version.nix run nixpkgs#helloprintsHello, world!.
Add this snippet to your .zshrc so interactive shells automatically enter the mapped Nix environment.
# Auto-enter nix-user-chroot for interactive shells when not already inside mapped /nix
if [[ $- == *i* ]] && [ ! -d /nix ]; then
CHROOT_BIN="$HOME/.local/bin/nix-user-chroot"
NIX_BACKING_DIR="$HOME/.nix"
if [ -x "$CHROOT_BIN" ] && [ -d "$NIX_BACKING_DIR" ]; then
# Start zsh inside chroot and avoid re-loading global rc files repeatedly
exec "$CHROOT_BIN" "$NIX_BACKING_DIR" zsh --no-global-rcs
fi
fi
# Load Nix profile if available
if [ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then
. "$HOME/.nix-profile/etc/profile.d/nix.sh"
fi- If your environment has strict module or compiler settings (common on HPC systems), you may need to unset variables like
LD_LIBRARY_PATHbefore running Nix builds. - You can leave the chroot shell with
exitand re-enter later with:
~/.local/bin/nix-user-chroot ~/.nix zsh