Skip to content

Instantly share code, notes, and snippets.

@NickCao
Last active May 26, 2023 19:47
Show Gist options
  • Save NickCao/0b938bd476e0bc2439e66663529d59bc to your computer and use it in GitHub Desktop.
Save NickCao/0b938bd476e0bc2439e66663529d59bc to your computer and use it in GitHub Desktop.
bootstrap nix as non-root

stage 0 (on seed machine)

build nix-static

nix build --impure --expr '(builtins.getFlake "github:NixOS/nix").packages.x86_64-linux.nix-static.overrideAttrs (_: { postPatch = "sed -i /setrlimit/d src/libstore/build/local-derivation-goal.cc src/libutil/util.cc"; })'
# copy `result/bin/nix` to target machine

stage 1 (on target machine)

build nix with custom store path

export NIX_PREFIX="$HOME/nix"
export NIX_CONF_DIR="$HOME/.config/nix"
export NIX_STORE_DIR="$NIX_PREFIX/store"
export NIX_STATE_DIR="$NIX_PREFIX/var/nix"
export NIX_LOG_DIR="$NIX_PREFIX/var/log/nix"
nix build --experimental-features "nix-command flakes" --no-sandbox --cores 0 -j auto --impure --expr '(builtins.getFlake "github:NixOS/nixpkgs").legacyPackages.x86_64-linux.nixFlakes.override { storeDir = builtins.getEnv "NIX_STORE_DIR"; stateDir = builtins.getEnv "NIX_STATE_DIR"; confDir = builtins.getEnv "NIX_CONF_DIR"; }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment