nix-direnv/direnvrc
Jörg Thalheim d2d121de14
use USER instead of LOGNAME
it looks like $LOGNAME is not set in our CI?
2020-04-11 20:41:01 +01:00

96 lines
2.8 KiB
Plaintext

# shellcheck shell=bash
use_nix() {
local path direnv_dir
path=$(nix-instantiate --find-file nixpkgs)
direnv_dir=$(direnv_layout_dir)
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
read -r 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 < ".git/${BASH_REMATCH[1]}"
else
version="$head"
fi
fi
local cache="$direnv_dir/cache-${version:-unknown}"
local update_drv=0
if [[ ! -e "$cache" ]] || \
[[ "$HOME/.direnvrc" -nt "$cache" ]] || \
[[ .envrc -nt "$cache" ]] || \
[[ default.nix -nt "$cache" ]] || \
[[ shell.nix -nt "$cache" ]];
then
[[ -d "$direnv_dir" ]] || mkdir "$direnv_dir"
local dump_cmd tmp
dump_cmd="echo -n _____direnv_____; \"$direnv\" dump bash"
tmp=$(nix-shell --show-trace --pure "$@" --run "$dump_cmd" \
| grep -oP '(?<=_____direnv_____).*')
echo "$tmp" > "$cache"
update_drv=1
else
log_status using cached derivation
fi
local term_backup=$TERM path_backup=$PATH
if [[ -n ${TMPDIR+x} ]]; then
local tmp_backup=$TMPDIR
fi
local impure_ssl_cert_file=${SSL_CERT_FILE:-}
local impure_nix_ssl_cert_file=${NIX_SSL_CERT_FILE:-}
log_status eval "$cache"
read -r cache_content < "$cache"
eval "$cache_content"
export PATH=$PATH:$path_backup TERM=$term_backup
if [[ -n ${tmp_backup+x} ]]; then
export TMPDIR=${tmp_backup}
else
unset TMPDIR
fi
# `nix-shell --pure` sets invalid ssl certificate paths
if [[ "${SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
if [[ -n ${impure_ssl_cert_file+x} ]]; then
export SSL_CERT_FILE=${impure_ssl_cert_file}
else
unset SSL_CERT_FILE
fi
fi
if [[ "${NIX_SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
if [[ -n ${impure_nix_ssl_cert_file+x} ]]; then
export NIX_SSL_CERT_FILE=${impure_nix_ssl_cert_file}
else
unset NIX_SSL_CERT_FILE
fi
fi
# 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="${direnv_dir}/drv" drv
drv=$(nix show-derivation "$out" | grep -E -o -m1 '/nix/store/.*.drv')
local stripped_pwd=${PWD/\//}
local escaped_pwd=${stripped_pwd//-/--}
local escaped_pwd=${escaped_pwd//\//-}
ln -fs "$drv" "$drv_link"
ln -fs "$drv_link" "/nix/var/nix/gcroots/per-user/$USER/$escaped_pwd"
log_status renewed cache and derivation link
fi
if [[ $# = 0 ]]; then
watch_file default.nix
watch_file shell.nix
fi
}