nix-direnv/direnvrc
2022-04-03 20:45:02 -04:00

314 lines
9 KiB
Text

# shellcheck shell=bash
REQUIRED_DIRENV_VERSION="2.21.3"
if ! has direnv_version || ! direnv_version "$REQUIRED_DIRENV_VERSION" 2>/dev/null; then
printf '%s\n' "nix-direnv: base direnv version is older than the required v$REQUIRED_DIRENV_VERSION." >&2
exit 1
fi
# Usage: nix_direnv_version <version_at_least>
#
# Checks that the nix-direnv version is at least as old as <version_at_least>.
nix_direnv_version() {
declare major='1' minor='6' patch='1' # UPDATE(nix-direnv version)
[[ $1 =~ ^([^+-.]*)(\.?)([^+-.]*)(\.?)([^+-]*)(-?)([^+]*)(\+?)(.*)$ ]]
declare -a ver; ver=("${BASH_REMATCH[@]:1}")
if [[ ( ${ver[0]} != +([0-9]) ) \
|| ( ${ver[1]} == '.' && ${ver[2]} != +([0-9]) ) \
|| ( ${ver[3]} == '.' && ${ver[4]} != +([0-9]) ) \
|| ( ${ver[5]} == '-' && ${ver[6]} != +([0-9A-Za-z-])*(.+([0-9A-Za-z-])) ) \
|| ( ${ver[7]} == '+' && ${ver[8]} != +([0-9A-Za-z-])*(.+([0-9A-Za-z-])) ) \
|| ( ( -n ${ver[5]} || -n ${ver[7]} ) && ( -z ${ver[2]} || -z ${ver[4]} ) ) \
]]; then
printf '%s\n' "nix-direnv: error v$1 is not a valid semver version" >&2
return 1
fi
if [[ ( ${ver[0]} -gt $major ) \
|| ( ${ver[2]:=0} -gt $minor ) \
|| ( ${ver[4]:=0} -gt $patch ) \
]]; then
printf '%s\n' "nix-direnv: error current version v$major.$minor.$patch is older than the desired version v$1" >&2
return 1
fi
}
if [[ -z ${NIX_BIN_PREFIX:-} ]]; then
NIX_BIN_PREFIX=$(command -v nix-shell)
NIX_BIN_PREFIX="${NIX_BIN_PREFIX%/*}/"
fi
_nix_export_or_unset() {
local key=$1 value=$2
if [[ "$value" == __UNSET__ ]]; then
unset "$key"
else
export "$key=$value"
fi
}
_nix_import_env() {
local env=$1
local old_path=${PATH:-}
local old_term=${TERM:-__UNSET__}
local old_shell=${SHELL:-__UNSET__}
local old_tmpdir=${TMPDIR:-__UNSET__}
local old_ssl_cert_file=${SSL_CERT_FILE:-__UNSET__}
local old_nix_ssl_cert_file=${NIX_SSL_CERT_FILE:-__UNSET__}
local old_xdg_data_dirs=${XDG_DATA_DIRS:-}
eval "$env"
# `nix-shell --pure` sets invalid ssl certificate paths
if [[ "${SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
_nix_export_or_unset SSL_CERT_FILE "$old_ssl_cert_file"
fi
if [[ "${NIX_SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
_nix_export_or_unset NIX_SSL_CERT_FILE "$old_nix_ssl_cert_file"
fi
export PATH=$PATH${old_path:+":"}$old_path
_nix_export_or_unset TERM "$old_term"
_nix_export_or_unset SHELL "$old_shell"
_nix_export_or_unset TMPDIR "$old_tmpdir"
export XDG_DATA_DIRS=$XDG_DATA_DIRS${old_xdg_data_dirs:+":"}$old_xdg_data_dirs
# misleading since we are in an impure shell now
export IN_NIX_SHELL=impure
}
_nix_add_gcroot() {
local storepath=$1
local symlink=$2
local stripped_pwd=${2/\//}
local escaped_pwd=${stripped_pwd//-/--}
local escaped_pwd=${escaped_pwd//\//-}
ln -fsn "$storepath" "$symlink"
ln -fsn "$symlink" "/nix/var/nix/gcroots/per-user/$USER/$escaped_pwd"
}
_nix_argsum_suffix() {
local out checksum
if [ -n "$1" ]; then
if has sha1sum; then
out=$(sha1sum <<< "$1")
elif has shasum; then
out=$(shasum <<< "$1")
else
# degrate gracefully both tools are not present
return
fi
read -r checksum _ <<< "$out"
echo "-$checksum"
fi
}
nix_direnv_watch_file() {
watch_file "$@"
nix_watches+=("$@")
}
use_flake() {
flake_expr="${1:-.}"
flake_dir="${flake_expr%#*}"
nix_direnv_watch_file .envrc
if [[ -d "$flake_dir" ]]; then
nix_direnv_watch_file "$flake_dir/"flake.nix
nix_direnv_watch_file "$flake_dir/"flake.lock
if [[ -e "$flake_dir/"devshell.toml ]]; then
nix_direnv_watch_file "$flake_dir/"devshell.toml
fi
fi
local layout_dir profile
layout_dir=$(direnv_layout_dir)
profile="${layout_dir}/flake-profile$(_nix_argsum_suffix "$flake_expr")"
local flake_inputs="${layout_dir}/flake-inputs/"
local profile_rc="${profile}.rc"
nix_watches+=(
"$HOME/".direnvrc
)
need_update=0
for file in "${nix_watches[@]}"; do
if [[ "$file" -nt "$profile_rc" ]]; then
need_update=1
break
fi
done
if [[ ! -e "$profile"
|| ! -e "$profile_rc"
|| "$need_update" -eq "1"
]];
then
local tmp_profile="${layout_dir}/flake-profile.$$"
[[ -d "$layout_dir" ]] || mkdir -p "$layout_dir"
local tmp_profile_rc
tmp_profile_rc=$("${NIX_BIN_PREFIX}nix" print-dev-env \
--extra-experimental-features "nix-command flakes" \
--profile "$tmp_profile" "$@")
# macos does not have realpath
if command -v realpath >/dev/null; then
drv=$(realpath "$tmp_profile")
else
drv=$(perl -e 'use Cwd "abs_path";print abs_path(shift)' "$tmp_profile")
fi
echo "$tmp_profile_rc" > "$profile_rc"
rm -f "$tmp_profile" "$tmp_profile"*
_nix_add_gcroot "$drv" "$profile"
# also add garbage collection root for source
local flake_input_paths
rm -rf "$flake_inputs"
mkdir "$flake_inputs"
flake_input_paths=$("${NIX_BIN_PREFIX}nix" flake archive --json \
--extra-experimental-features "nix-command flakes" \
"$flake_dir" | grep -E -o '/nix/store/[^"]+')
for path in $flake_input_paths; do
_nix_add_gcroot "$path" "${flake_inputs}/${path##*/}"
done
log_status renewed cache
else
sed "/eval \"\$shellHook\"/d" "$profile_rc" > "${profile_rc}.tmp"
mv "${profile_rc}.tmp" "${profile_rc}"
log_status using cached dev shell
fi
local old_nix_build_top=${NIX_BUILD_TOP:-__UNSET__}
local old_tmp=${TMP:-__UNSET__}
local old_tmpdir=${TMPDIR:-__UNSET__}
local old_temp=${TEMP:-__UNSET__}
local old_tempdir=${TEMPDIR:-__UNSET__}
local old_xdg_data_dirs=${XDG_DATA_DIRS:-}
# TODO: maybe use `_nix_import_env()` here: https://github.com/nix-community/nix-direnv/pull/75#issuecomment-803139886
eval "$(< "$profile_rc")"
# nix print-env-dev will create a temporary directory and use it a TMPDIR,
# we cannot rely on this directory beeing not deleted at some point,
# hence we are just removing it right away.
if [[ "$NIX_BUILD_TOP" == */nix-shell.* && -d "$NIX_BUILD_TOP" ]]; then
rmdir "$NIX_BUILD_TOP"
fi
_nix_export_or_unset NIX_BUILD_TOP "$old_nix_build_top"
_nix_export_or_unset TMP "$old_tmp"
_nix_export_or_unset TMPDIR "$old_tmpdir"
_nix_export_or_unset TEMP "$old_temp"
_nix_export_or_unset TEMPDIR "$old_tempdir"
export XDG_DATA_DIRS=$XDG_DATA_DIRS${old_xdg_data_dirs:+":"}$old_xdg_data_dirs
}
_nix_extract_direnv() {
local found_direnv
found_direnv=0
while read -r line; do
if [[ "$found_direnv" == "1" ]]; then
echo "$line"
break
elif [[ "$line" == "_____direnv_____" ]]; then
found_direnv=1
else
echo "$line" >&2
fi
done
}
use_nix() {
local path layout_dir
path=$("${NIX_BIN_PREFIX}nix-instantiate" --find-file nixpkgs)
layout_dir=$(direnv_layout_dir)
local experimental_flags=()
if "${NIX_BIN_PREFIX}nix-shell" --extra-experimental-features '' --version 2>/dev/null >&2; then
experimental_flags+=('--extra-experimental-features' 'nix-command flakes')
fi
if [[ "${direnv:-}" == "" ]]; then
log_status "\$direnv environment variable was not defined. Was this script run inside direnv?"
fi
local version
if [[ -f "${path}/.version-suffix" ]]; then
version=$(< "${path}/.version-suffix")
elif [[ -f "${path}/.git/HEAD" ]]; then
local head
read -r head < "${path}/.git/HEAD"
local regex="ref: (.*)"
if [[ "$head" =~ $regex ]]; then
read -r version < "${path}/.git/${BASH_REMATCH[1]}"
else
version="$head"
fi
elif [[ -f "${path}/.version" && "${path}" == "/nix/store/"* ]]; then
# borrow some bits from the store path
local version_prefix
read -r version_prefix < "${path}/.version"
version="${version_prefix}-${path:11:16}"
fi
local cache
cache="${layout_dir}/cache-${version:-unknown}$(_nix_argsum_suffix "$*")"
nix_watches+=(
"$HOME/".direnvrc
.envrc
default.nix
shell.nix
)
need_update=0
for file in "${nix_watches[@]}"; do
if [[ "$file" -nt "$cache" ]]; then
need_update=1
break
fi
done
local update_drv=0
if [[ ! -e "$cache"
|| "$need_update" -eq "1"
]];
then
[[ -d "$layout_dir" ]] || mkdir -p "$layout_dir"
local dump_cmd tmp
dump_cmd="echo _____direnv_____; \"$direnv\" dump bash"
tmp=$("${NIX_BIN_PREFIX}nix-shell" \
"${experimental_flags[@]}" \
--show-trace --pure "$@" --run "$dump_cmd")
# show original shell hook output
echo "$tmp" | _nix_extract_direnv >&2 > "$cache"
update_drv=1
else
log_status using cached derivation
fi
log_status eval "$cache"
read -r cache_content < "$cache"
_nix_import_env "$cache_content"
# This part is based on https://discourse.nixos.org/t/what-is-the-best-dev-workflow-around-nix-shell/418/4
if [[ "${out:-}" != "" ]] && (( update_drv )); then
local drv_link="${layout_dir}/drv" drv
drv=$("${NIX_BIN_PREFIX}nix" show-derivation "$out" \
"${experimental_flags[@]}" \
| grep -E -o -m1 '/nix/store/.*.drv')
_nix_add_gcroot "$drv" "$drv_link"
log_status renewed cache and derivation link
fi
if [[ "$#" == 0 ]]; then
watch_file default.nix
watch_file shell.nix
fi
}