2020-02-19 13:15:28 +00:00
|
|
|
#!/usr/bin/env bash
|
2022-01-05 13:32:02 +00:00
|
|
|
# This script installs a specific version of Nix.
|
|
|
|
set -eo pipefail
|
2020-02-19 13:15:28 +00:00
|
|
|
|
|
|
|
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
|
2022-01-05 13:32:02 +00:00
|
|
|
source "${GIT_ROOT}/nix/scripts/lib.sh"
|
2020-02-19 13:15:28 +00:00
|
|
|
source "${GIT_ROOT}/scripts/colors.sh"
|
|
|
|
|
2022-01-05 13:32:02 +00:00
|
|
|
NIX_VERSION="2.6.1"
|
2020-02-19 13:15:28 +00:00
|
|
|
NIX_INSTALL_URL="https://nixos.org/releases/nix/nix-${NIX_VERSION}/install"
|
2022-01-05 13:32:02 +00:00
|
|
|
NIX_INSTALL_SHA256="a63732b4f3628ce97e096dbda88e3cb9851ff6b4f94b5cd43feb257126437b9d"
|
2021-06-10 08:55:16 +00:00
|
|
|
NIX_INSTALL_PATH="/tmp/nix-install-${NIX_VERSION}"
|
2020-02-19 13:15:28 +00:00
|
|
|
|
2022-01-05 13:32:02 +00:00
|
|
|
install_nix() {
|
|
|
|
# Download installer and verify SHA256>
|
|
|
|
curl -sSf "${NIX_INSTALL_URL}" -o "${NIX_INSTALL_PATH}"
|
|
|
|
echo "${NIX_INSTALL_SHA256} ${NIX_INSTALL_PATH}" | sha256sum -c
|
|
|
|
chmod +x "${NIX_INSTALL_PATH}"
|
|
|
|
|
|
|
|
# Identify installation type.
|
|
|
|
if [[ -z "${NIX_INSTALL_OPTS}" ]]; then
|
|
|
|
if [[ "$(uname -r)" =~ microsoft ]]; then
|
|
|
|
# Systemd is not started by default on WSL.
|
|
|
|
NIX_INSTALL_OPTS="--no-daemon"
|
|
|
|
elif [[ "$(uname -s)" == "Darwin" ]]; then
|
|
|
|
# Single-user not supported on Darwin.
|
|
|
|
NIX_INSTALL_OPTS="--daemon"
|
|
|
|
elif [[ "$(uname -s)" == "Linux" ]]; then
|
|
|
|
# Open file limit issues on Linux.
|
|
|
|
# https://github.com/NixOS/nix/issues/6007
|
|
|
|
# Alson known issues with nix-daemon.socket on Arch.
|
|
|
|
NIX_INSTALL_OPTS="--no-daemon"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Run the installer
|
|
|
|
"${NIX_INSTALL_PATH}" "${NIX_INSTALL_OPTS}"
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
echo -e "${GRN}The Nix package manager was successfully installed.${RST}"
|
|
|
|
else
|
|
|
|
echo -e "${RED}Failed to install Nix package manager!${RST}" >&2
|
|
|
|
echo "Please see: https://nixos.org/nix/manual/#chap-installation" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-02-19 13:15:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 13:32:02 +00:00
|
|
|
if [[ ! -x "$(command -v sha256sum)" ]]; then
|
|
|
|
echo -e "${RED}The 'sha256sum' utility is required for Nix installation.${RST}" >&2
|
|
|
|
echo -e "${YLW}Install 'coreutils' package on your system.${RST}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-01-10 15:21:47 +00:00
|
|
|
if [[ ! -x "$(command -v curl)" ]]; then
|
2022-01-05 13:32:02 +00:00
|
|
|
echo -e "${RED}The 'curl' utility is required for Nix installation.${RST}" >&2
|
|
|
|
exit 1
|
2020-02-19 13:15:28 +00:00
|
|
|
fi
|
|
|
|
|
2022-01-10 15:21:47 +00:00
|
|
|
if [[ "$(source /etc/os-release 2>/dev/null && echo "${NAME}")" == *NixOS* ]]; then
|
2022-01-05 13:32:02 +00:00
|
|
|
echo -e "${GRN}Already running NixOS.${RST}"
|
|
|
|
exit
|
2020-02-19 13:15:28 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -x "$(command -v nix)" ]]; then
|
2022-01-05 13:32:02 +00:00
|
|
|
echo -e "${GRN}Nix package manager already installed.${RST}"
|
|
|
|
exit
|
2020-02-19 13:15:28 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${IN_NIX_SHELL}" == 'pure' ]]; then
|
2022-01-05 13:32:02 +00:00
|
|
|
echo -e "${GRN}Already in a pure Nix shell.${RST}"
|
|
|
|
exit
|
2020-02-19 13:15:28 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# If none of the checks before succeeded we need to install Nix
|
|
|
|
echo -e "${GRN}Setting up Nix package manager...${RST}"
|
|
|
|
install_nix
|
|
|
|
echo -e "${YLW}See STARTING_GUIDE.md if you're new here.${RST}"
|