From b81f20c944cec988d9fed152a4b0deea65307734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Tue, 19 Dec 2023 18:03:29 +0100 Subject: [PATCH] Add ensure_nix script --- ensure_nix.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 ensure_nix.sh diff --git a/ensure_nix.sh b/ensure_nix.sh new file mode 100755 index 0000000..2e7d206 --- /dev/null +++ b/ensure_nix.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +# Runs the command given in a Nix environment, and create it if it doesn't exist. +# Useful for environments where nix isn't installed / you do not have root access + +# If you need a fresh slate: +# chmod +w .nix -R +# rm -rf .nix .nix-defexpr .nix-profile .config/nix .local/state/nix .local/share/nix .cache/nix + +set -euo pipefail +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +if [ ! -d /nix ] +then + # Doesn't support architectures other than x86_64 + NIX_USER_CHROOT_URL=https://github.com/nix-community/nix-user-chroot/releases/download/1.2.2/nix-user-chroot-bin-1.2.2-x86_64-unknown-linux-musl + NIX_USER_CHROOT_SHA256SUM=e11aff604bb8d3ffd1d9c0c68cd636816d7eb8da540de18ee3a41ccad7ac0972 + + nix_user_chroot="$HOME/.local/bin/nix-user-chroot" + mkdir -p "$(dirname "$nix_user_chroot")" + + nix_directory="$HOME/.nix" + mkdir -p "$nix_directory" + + if [ ! -x "$nix_user_chroot" ] || ! echo "$NIX_USER_CHROOT_SHA256SUM $nix_user_chroot" | sha256sum --check --status + then + wget "$NIX_USER_CHROOT_URL" -O "$nix_user_chroot" + echo "$NIX_USER_CHROOT_SHA256SUM $nix_user_chroot" | sha256sum --check --status + chmod +x "$nix_user_chroot" + fi + exec "$nix_user_chroot" "$nix_directory" "$0" "$@" + exit 1 +fi + +nix_profile_path="$HOME/.nix-profile/etc/profile.d/nix.sh" + +if [ ! -f "$nix_profile_path" ] +then + NIX_INSTALLER_URL=https://releases.nixos.org/nix/nix-2.19.2/install + NIX_INSTALLER_SHA256SUM=435f0d7e11f7c7dffeeab0ec9cc55723f6d3c03352379d785633cf4ddb5caf90 + + nix_installer="$(mktemp)" + + wget "$NIX_INSTALLER_URL" -O "$nix_installer" + echo "$NIX_INSTALLER_SHA256SUM $nix_installer" | sha256sum --check --status + chmod +x "$nix_installer" + + "$nix_installer" --no-daemon --yes --no-channel-add --no-modify-profile +fi + +. "$nix_profile_path" + +"${SCRIPT_DIR}/add_channels.sh" + +exec "$@"