use_nix: switch to print-dev-env

This get's rid of pure shell hacks and allows finally to use `shellHook`
properly even in the cached version.
This commit is contained in:
Jörg Thalheim 2021-11-19 09:31:48 +01:00
parent 46c42e5983
commit 78aa5db66b

171
direnvrc
View file

@ -44,35 +44,28 @@ _nix_export_or_unset() {
} }
_nix_import_env() { _nix_import_env() {
local env=$1 local profile_rc=$1
local old_path=${PATH:-} local old_nix_build_top=${NIX_BUILD_TOP:-__UNSET__}
local old_term=${TERM:-__UNSET__} local old_tmp=${TMP:-__UNSET__}
local old_shell=${SHELL:-__UNSET__}
local old_tmpdir=${TMPDIR:-__UNSET__} local old_tmpdir=${TMPDIR:-__UNSET__}
local old_ssl_cert_file=${SSL_CERT_FILE:-__UNSET__} local old_temp=${TEMP:-__UNSET__}
local old_nix_ssl_cert_file=${NIX_SSL_CERT_FILE:-__UNSET__} local old_tempdir=${TEMPDIR:-__UNSET__}
local old_xdg_data_dirs=${XDG_DATA_DIRS:-} local old_xdg_data_dirs=${XDG_DATA_DIRS:-}
eval "$(< "$profile_rc")"
eval "$env" # 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,
# `nix-shell --pure` sets invalid ssl certificate paths # hence we are just removing it right away.
if [[ "${SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then if [[ "$NIX_BUILD_TOP" == */nix-shell.* && -d "$NIX_BUILD_TOP" ]]; then
_nix_export_or_unset SSL_CERT_FILE "$old_ssl_cert_file" rmdir "$NIX_BUILD_TOP"
fi fi
if [[ "${NIX_SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then _nix_export_or_unset NIX_BUILD_TOP "$old_nix_build_top"
_nix_export_or_unset NIX_SSL_CERT_FILE "$old_nix_ssl_cert_file" _nix_export_or_unset TMP "$old_tmp"
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" _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 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() { _nix_add_gcroot() {
@ -176,28 +169,13 @@ use_flake() {
export XDG_DATA_DIRS=$XDG_DATA_DIRS${old_xdg_data_dirs:+":"}$old_xdg_data_dirs 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() { use_nix() {
local path layout_dir local path layout_dir
path=$("${NIX_BIN_PREFIX}nix-instantiate" --find-file nixpkgs) path=$("${NIX_BIN_PREFIX}nix-instantiate" --find-file nixpkgs)
layout_dir=$(direnv_layout_dir) layout_dir=$(direnv_layout_dir)
local experimental_flags=() if ! "${NIX_BIN_PREFIX}nix-shell" --extra-experimental-features '' --version 2>/dev/null >&2; then
if "${NIX_BIN_PREFIX}nix-shell" --extra-experimental-features '' --version 2>/dev/null >&2; then log_status "Your nix is too old for nix-direnv. At least nix 2.4 is required"
experimental_flags+=('--extra-experimental-features' 'nix-command flakes') return
fi fi
if [[ "${direnv:-}" == "" ]]; then if [[ "${direnv:-}" == "" ]]; then
@ -223,43 +201,98 @@ use_nix() {
version="${version_prefix}-${path:11:16}" version="${version_prefix}-${path:11:16}"
fi fi
local cache local profile
cache="${layout_dir}/cache-${version:-unknown}$(_nix_argsum_suffix "$*")"
local update_drv=0 profile="${layout_dir}/nix-profile-${version:-unknown}$(_nix_argsum_suffix "$*")"
if [[ ! -e "$cache" local profile_rc="${profile}.rc"
|| "$HOME/.direnvrc" -nt "$cache"
|| .envrc -nt "$cache" local in_packages=0
|| default.nix -nt "$cache" local attribute=
|| shell.nix -nt "$cache" local packages=""
local extra_args=()
local file=
while [ "$#" -gt 0 ]; do
i="$1"; shift
case $i in
-p|--packages)
in_packages=1
;;
# ignore unsupported arguments
--command|--run|--exclude)
shift
;;
# ignore unsupported arguments
--pure|-i|--keep)
;;
-I)
extra_args+=("$i" "$1")
shift
;;
--attr|-A)
attribute=$1
shift
;;
--option|-o|--arg|--argstr)
extra_args+=("$i" "$1" "$2")
shift
shift
;;
-*)
echo "unknown argument $i"
return
;;
*)
if [[ $in_packages == 1 ]]; then
packages+=" $i"
else
file=$i
fi
;;
esac
done
if [[ ! -e "$profile"
|| ! -e "$profile_rc"
|| "$HOME/.direnvrc" -nt "$profile_rc"
|| .envrc -nt "$profile_rc"
|| default.nix -nt "$profile_rc"
|| shell.nix -nt "$profile_rc"
]]; ]];
then then
local tmp_profile="${layout_dir}/flake-profile.$$"
[[ -d "$layout_dir" ]] || mkdir -p "$layout_dir" [[ -d "$layout_dir" ]] || mkdir -p "$layout_dir"
local dump_cmd tmp local tmp_profile_rc
dump_cmd="echo _____direnv_____; \"$direnv\" dump bash" local extra_args=()
tmp=$("${NIX_BIN_PREFIX}nix-shell" \ if [[ $packages != "" ]]; then
"${experimental_flags[@]}" \ extra_args+=("--expr" "with import <nixpkgs> {}; mkShell { buildInputs = [ $packages ]; }")
--show-trace --pure "$@" --run "$dump_cmd") else
# show original shell hook output if [[ $file == "" ]]; then
echo "$tmp" | _nix_extract_direnv >&2 > "$cache" if [[ -e "shell.nix" ]]; then
update_drv=1 file=./shell.nix
elif [[ -e "default.nix" ]]; then
file=./default.nix
fi
fi
if [[ $attribute == "" ]]; then
extra_args+=(--file "$file")
else
extra_args+=(--expr "(import $file).${attribute}")
fi
fi
tmp_profile_rc=$("${NIX_BIN_PREFIX}nix" print-dev-env \
--extra-experimental-features "nix-command flakes" \
--profile "$tmp_profile" --impure "${extra_args[@]}")
drv=$(ls -dl "$tmp_profile")
drv=${drv#*-> }
echo "$tmp_profile_rc" > "$profile_rc"
rm -f "$tmp_profile" "$tmp_profile"*
_nix_add_gcroot "$drv" "$profile"
log_status renewed cache
else else
log_status using cached derivation log_status using cached derivation
fi fi
log_status eval "$cache" _nix_import_env "$profile_rc"
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 if [[ "$#" == 0 ]]; then
watch_file default.nix watch_file default.nix