Skip to content

Instantly share code, notes, and snippets.

@morphykuffour
Forked from jfroche/install-nix-no-root.sh
Created November 2, 2022 03:44
Show Gist options
  • Save morphykuffour/7244975b5a5fc6be268dd1b3e31024ec to your computer and use it in GitHub Desktop.
Save morphykuffour/7244975b5a5fc6be268dd1b3e31024ec to your computer and use it in GitHub Desktop.
Nix install without root access
function install_nix() {
set -x
local -r bindir="${HOME}/bin"
local -r proot_url="https://github.com/proot-me/proot/releases/download/v5.3.0/proot-v5.3.0-x86_64-static"
local -r nixdir="${HOME}/mnt/nix"
local -r nixver="2.5.1"
local -r arch="x86_64"
local -r os="linux"
local -r nix_url="https://releases.nixos.org/nix/nix-${nixver}/nix-${nixver}-${arch}-${os}.tar.xz"
local -r nix_cli_dir=nix-${nixver}-${arch}-${os}
mkdir -p "${bindir}"
test \! -f "${bindir}/proot" && curl -L "${proot_url}" > "${bindir}/proot"
chmod u+x "${bindir}/proot"
mkdir -p "${nixdir}"
pushd "${nixdir}"
test \! -f nix.tar.xz && curl -L "${nix_url}" > nix.tar.xz
test \! -d $nix_cli_dir && tar -xJf "nix.tar.xz"
local -r nixbin="$(dirname "$(find "${nixdir}/${nix_cli_dir}" -name "nix-shell" | head -1)")"
local -r nix_profile="$(dirname $nixbin)/etc/profile.d/nix.sh"
{
echo "#!/usr/bin/env bash"
echo
echo export PATH="${bindir}:${nixbin}:${PATH}"
echo source "${nix_profile}"
echo export PROOT_NO_SECCOMP=1
echo export NIX_PATH=\${NIX_PATH:+\$NIX_PATH:}\$HOME/.nix-defexpr/channels
echo
echo "${bindir}/proot -b \"${nixdir}/nix-${nixver}-${arch}-${os}/:/nix\" \$@"
} > "${bindir}/nixrun"
chmod u+x "${bindir}/nixrun"
}
set -eu
set -o pipefail
install_nix
mkdir -p ${HOME}/.config/nix
echo "sandbox = false" > ${HOME}/.config/nix/nix.conf
nixrun /nix/install --no-daemon --no-channel-add
grep -qF 'nixrun' ~/.bashrc || echo 'test \! -e $HOME/.nix-profile && nixrun bash -l' >> ${HOME}/.bashrc
cat >> $HOME/.nix-channels << EOF
https://github.com/rycee/home-manager/archive/release-21.11.tar.gz home-manager
https://github.com/NixOS/nixpkgs/archive/release-21.11.tar.gz nixpkgs
EOF
nixrun nix-channel --update
nixrun nix-shell '<home-manager>' -A install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment