Merge pull request #35 from nix-community/cleanup-1

This commit is contained in:
Jörg Thalheim 2020-08-15 10:00:20 +01:00 committed by GitHub
commit 892015898f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 66 deletions

View file

@ -172,6 +172,15 @@ pwd_hash=$(echo -n $PWD | shasum | cut -d ' ' -f 1)
direnv_layout_dir=$XDG_CACHE_HOME/direnv/layouts/$pwd_hash
```
## Manually re-triggering evaluation
In some case nix-direnv does not detect if imported file has changed and still provides
the old cached values. An evaluation can be triggered by updating your `.envrc`:
```console
$ touch .envrc
```
## Known Bugs
At the moment `nix-direnv` depends on GNU Grep and a modern Bash version.
@ -180,11 +189,13 @@ As a work-around we suggest that macOS users install `direnv`/`grep` via Nix or
## Why not use `lorri` instead?
Lorri causes large CPU load when nixpkgs in `NIX_PATH` is pointed to a directory, e.g. a
git checkout. This is because it tries to watch all referenced Nix files and
re-evaluate them when they change. Nix-direnv compromises between performance and
correctness, and only re-evaluates direnv if either the project-specific
`default.nix` / `shell.nix` changes, or if there is a new commit added to
`nixpkgs`. A re-evaluation can be also triggered by using `touch shell.nix` in
the same project. Also `nix-direnv` does not require an additional daemon, so it can be
included into the project itself and enabled without further user effort.
- nix-direnv has flakes support.
- High CPU load/resource usage in some cases: When nixpkgs in `NIX_PATH` is
pointed to a directory, i.e. a git checkout, Lorri will try to evaluate
nixpkgs everytime something changes causing high cpu load. Nix-direnv
compromises between performance and correctness, and only re-evaluates direnv
if either the project-specific `default.nix` / `shell.nix` changes, or if
there is a new commit added to `nixpkgs`. A re-evaluation can be also
triggered by using `touch .envrc` in the same project.
- No additional daemon or services required: The codesize is small enough that it can be vendored
into a project itself.

View file

@ -1,40 +1,37 @@
# shellcheck shell=bash
_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 term_backup=$TERM path_backup=$PATH
if [[ -n ${TMPDIR+x} ]]; then
local tmp_backup=$TMPDIR
fi
local impure_ssl_cert_file=${SSL_CERT_FILE:-__UNSET__}
local impure_nix_ssl_cert_file=${NIX_SSL_CERT_FILE:-__UNSET__}
local old_path=${PATH:-}
local old_term=${TERM:-__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__}
eval "$env"
# `nix-shell --pure` sets invalid ssl certificate paths
if [[ "${SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
if [[ $impure_ssl_cert_file == __UNSET__ ]]; then
unset SSL_CERT_FILE
else
export SSL_CERT_FILE=$impure_ssl_cert_file
fi
_nix_export_or_unset SSL_CERT_FILE "$old_ssl_cert_file"
fi
if [[ "${NIX_SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
if [[ $impure_nix_ssl_cert_file == __UNSET__ ]]; then
unset NIX_SSL_CERT_FILE
else
export NIX_SSL_CERT_FILE=${impure_nix_ssl_cert_file}
fi
_nix_export_or_unset NIX_SSL_CERT_FILE "$old_nix_ssl_cert_file"
fi
export PATH=$PATH:$path_backup TERM=$term_backup
if [[ -n ${tmp_backup+x} ]]; then
export TMPDIR=${tmp_backup}
else
unset TMPDIR
fi
export PATH=$PATH${old_path:+":"}$old_path
_nix_export_or_unset TEMPDIR "$old_tmp"
_nix_export_or_unset TERM "$old_term"
# misleading since we are in an impure shell now
export IN_NIX_SHELL=impure
@ -58,12 +55,13 @@ use_flake() {
local profile="$(direnv_layout_dir)/flake-profile"
local profile_rc="${profile}.rc"
if [[ ! -e "$profile" ]] || \
[[ ! -e "$profile_rc" ]] || \
[[ "$HOME/.direnvrc" -nt "$profile_rc" ]] || \
[[ .envrc -nt "$profile_rc" ]] || \
[[ flake.nix -nt "$profile_rc" ]] || \
[[ flake.lock -nt "$profile_rc" ]];
if [[ ! -e "$profile" \
|| ! -e "$profile_rc" \
|| "$HOME/.direnvrc" -nt "$profile_rc"
|| .envrc -nt "$profile_rc"
|| flake.nix -nt "$profile_rc"
|| flake.lock -nt "$profile_rc"
]];
then
local tmp_profile="$(direnv_layout_dir)/flake-profile.$$"
[[ -d "$(direnv_layout_dir)" ]] || mkdir "$(direnv_layout_dir)"
@ -90,31 +88,11 @@ use_flake() {
rmdir "$NIX_BUILD_TOP"
fi
if [[ "$old_nix_build_top" == __UNSET__ ]]; then
unset NIX_BUILD_TOP
else
export NIX_BUILD_TOP=$old_nix_build_top
fi
if [[ "$old_tmp" == __UNSET__ ]]; then
unset TMP
else
export TMP=$old_temp
fi
if [[ "$old_tmpdir" == __UNSET__ ]]; then
unset TMPDIR
else
export TMPDIR=$old_tmpdir
fi
if [[ "$old_temp" == __UNSET__ ]]; then
unset TEMP
else
export TEMP=$old_temp
fi
if [[ "$old_tempdir" == __UNSET__ ]]; then
unset TEMPDIR
else
export TEMPDIR=$old_tempdir
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"
}
use_nix() {
@ -143,11 +121,12 @@ use_nix() {
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" ]];
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