Susan Potter
snippets: Created / Updated

Nix in your home directory

Prerequisites

  • wget is installed
  • tar is installed

Purpose

If you really don't want to install Nix under /nix (or you can't) then you can install Nix in your home directory like in the homedir_install.sh script included in this Gist.

Now whenever you want to run a command under Nix's control, you should prefix with nixrun.

Good luck.

homedir_install.sh

function install_nix() {
  local -r bindir="${HOME}/opt/bin"
  local -r proot_url="https://github.com/proot-me/proot-static-build/blob/master/static/proot-x86_64?raw=true"
  local -r nixdir="${HOME}/mnt/nix"
  local -r nixver="1.11.2"
  local -r arch="x86_64"
  local -r os="linux"
  local -r nixbz2_url="http://nixos.org/releases/nix/nix-${nixver}/nix-${nixver}-${arch}-${os}.tar.bz2"

  mkdir -p "${bindir}"
  wget -O "${bindir}/proot" "${proot_url}"
  chmod u+x "${bindir}/proot"

  mkdir -p "${nixdir}"
  pushd "${nixdir}"
  wget "${nixbz2_url}"
  tar xjf nix-*bz2

  local -r nixbin="$(dirname "$(find "${nixdir}" -name "nix-shell" | head -1)")"
  export PATH="${bindir}:${nixbin}:${PATH}"
  {
    echo "#!/usr/bin/env bash"
    echo
    echo "proot -b \"${nixdir}/nix-${nixver}-${arch}-${os}/:/nix\" \$@"
  } > "${bindir}/nixrun"
  chmod u+x "${bindir}/nixrun"
}

set -eu
set -o pipefail

install_nix

Update: Thanks to Will Price (@willprice) for pointing out my original link to the proot static binary was broken on Oct 23, 2017: https://github.com/proot-me/proot/issues/120#issuecomment-338649213

If you enjoyed this content, please consider sharing this link with a friend, following my GitHub, Twitter/X or LinkedIn accounts, or subscribing to my RSS feed.