Skip to content

Instantly share code, notes, and snippets.

@4piu
Created April 15, 2026 05:35
Show Gist options
  • Select an option

  • Save 4piu/2d5096a090377405a9961fa96aaf9b68 to your computer and use it in GitHub Desktop.

Select an option

Save 4piu/2d5096a090377405a9961fa96aaf9b68 to your computer and use it in GitHub Desktop.
Install Nix on Linux without root

Install Nix on Linux without root

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:

Install 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 --help

Install Nix with directory mapping

nix-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 | sh

Verify installation

Run 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#hello

Expected result:

  • command -v nix returns a path under your home directory.
  • nix --version prints the installed Nix version.
  • nix run nixpkgs#hello prints Hello, world!.

Configure auto-loading in .zshrc

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

Notes

  • If your environment has strict module or compiler settings (common on HPC systems), you may need to unset variables like LD_LIBRARY_PATH before running Nix builds.
  • You can leave the chroot shell with exit and re-enter later with:
~/.local/bin/nix-user-chroot ~/.nix zsh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment