Skip to content

Commit 8920158

Browse files
authored
Merge pull request #35 from nix-community/cleanup-1
2 parents 666edff + 682f750 commit 8920158

File tree

2 files changed

+56
-66
lines changed

2 files changed

+56
-66
lines changed

README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ pwd_hash=$(echo -n $PWD | shasum | cut -d ' ' -f 1)
172172
direnv_layout_dir=$XDG_CACHE_HOME/direnv/layouts/$pwd_hash
173173
```
174174

175+
## Manually re-triggering evaluation
176+
177+
In some case nix-direnv does not detect if imported file has changed and still provides
178+
the old cached values. An evaluation can be triggered by updating your `.envrc`:
179+
180+
```console
181+
$ touch .envrc
182+
```
183+
175184
## Known Bugs
176185

177186
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
180189

181190
## Why not use `lorri` instead?
182191

183-
Lorri causes large CPU load when nixpkgs in `NIX_PATH` is pointed to a directory, e.g. a
184-
git checkout. This is because it tries to watch all referenced Nix files and
185-
re-evaluate them when they change. Nix-direnv compromises between performance and
186-
correctness, and only re-evaluates direnv if either the project-specific
187-
`default.nix` / `shell.nix` changes, or if there is a new commit added to
188-
`nixpkgs`. A re-evaluation can be also triggered by using `touch shell.nix` in
189-
the same project. Also `nix-direnv` does not require an additional daemon, so it can be
190-
included into the project itself and enabled without further user effort.
192+
- nix-direnv has flakes support.
193+
- High CPU load/resource usage in some cases: When nixpkgs in `NIX_PATH` is
194+
pointed to a directory, i.e. a git checkout, Lorri will try to evaluate
195+
nixpkgs everytime something changes causing high cpu load. Nix-direnv
196+
compromises between performance and correctness, and only re-evaluates direnv
197+
if either the project-specific `default.nix` / `shell.nix` changes, or if
198+
there is a new commit added to `nixpkgs`. A re-evaluation can be also
199+
triggered by using `touch .envrc` in the same project.
200+
- No additional daemon or services required: The codesize is small enough that it can be vendored
201+
into a project itself.

direnvrc

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
# shellcheck shell=bash
22

3+
_nix_export_or_unset() {
4+
local key=$1 value=$2
5+
if [[ "$value" == __UNSET__ ]]; then
6+
unset "$key"
7+
else
8+
export "$key=$value"
9+
fi
10+
}
11+
312
_nix_import_env() {
413
local env=$1
514

6-
local term_backup=$TERM path_backup=$PATH
7-
if [[ -n ${TMPDIR+x} ]]; then
8-
local tmp_backup=$TMPDIR
9-
fi
10-
local impure_ssl_cert_file=${SSL_CERT_FILE:-__UNSET__}
11-
local impure_nix_ssl_cert_file=${NIX_SSL_CERT_FILE:-__UNSET__}
15+
local old_path=${PATH:-}
16+
local old_term=${TERM:-__UNSET__}
17+
local old_tmpdir=${TMPDIR:-__UNSET__}
18+
local old_ssl_cert_file=${SSL_CERT_FILE:-__UNSET__}
19+
local old_nix_ssl_cert_file=${NIX_SSL_CERT_FILE:-__UNSET__}
1220

1321
eval "$env"
1422

1523
# `nix-shell --pure` sets invalid ssl certificate paths
1624
if [[ "${SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
17-
if [[ $impure_ssl_cert_file == __UNSET__ ]]; then
18-
unset SSL_CERT_FILE
19-
else
20-
export SSL_CERT_FILE=$impure_ssl_cert_file
21-
fi
25+
_nix_export_or_unset SSL_CERT_FILE "$old_ssl_cert_file"
2226
fi
2327

2428
if [[ "${NIX_SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
25-
if [[ $impure_nix_ssl_cert_file == __UNSET__ ]]; then
26-
unset NIX_SSL_CERT_FILE
27-
else
28-
export NIX_SSL_CERT_FILE=${impure_nix_ssl_cert_file}
29-
fi
29+
_nix_export_or_unset NIX_SSL_CERT_FILE "$old_nix_ssl_cert_file"
3030
fi
3131

32-
export PATH=$PATH:$path_backup TERM=$term_backup
33-
if [[ -n ${tmp_backup+x} ]]; then
34-
export TMPDIR=${tmp_backup}
35-
else
36-
unset TMPDIR
37-
fi
32+
export PATH=$PATH${old_path:+":"}$old_path
33+
_nix_export_or_unset TEMPDIR "$old_tmp"
34+
_nix_export_or_unset TERM "$old_term"
3835

3936
# misleading since we are in an impure shell now
4037
export IN_NIX_SHELL=impure
@@ -58,12 +55,13 @@ use_flake() {
5855
local profile="$(direnv_layout_dir)/flake-profile"
5956
local profile_rc="${profile}.rc"
6057

61-
if [[ ! -e "$profile" ]] || \
62-
[[ ! -e "$profile_rc" ]] || \
63-
[[ "$HOME/.direnvrc" -nt "$profile_rc" ]] || \
64-
[[ .envrc -nt "$profile_rc" ]] || \
65-
[[ flake.nix -nt "$profile_rc" ]] || \
66-
[[ flake.lock -nt "$profile_rc" ]];
58+
if [[ ! -e "$profile" \
59+
|| ! -e "$profile_rc" \
60+
|| "$HOME/.direnvrc" -nt "$profile_rc"
61+
|| .envrc -nt "$profile_rc"
62+
|| flake.nix -nt "$profile_rc"
63+
|| flake.lock -nt "$profile_rc"
64+
]];
6765
then
6866
local tmp_profile="$(direnv_layout_dir)/flake-profile.$$"
6967
[[ -d "$(direnv_layout_dir)" ]] || mkdir "$(direnv_layout_dir)"
@@ -90,31 +88,11 @@ use_flake() {
9088
rmdir "$NIX_BUILD_TOP"
9189
fi
9290

93-
if [[ "$old_nix_build_top" == __UNSET__ ]]; then
94-
unset NIX_BUILD_TOP
95-
else
96-
export NIX_BUILD_TOP=$old_nix_build_top
97-
fi
98-
if [[ "$old_tmp" == __UNSET__ ]]; then
99-
unset TMP
100-
else
101-
export TMP=$old_temp
102-
fi
103-
if [[ "$old_tmpdir" == __UNSET__ ]]; then
104-
unset TMPDIR
105-
else
106-
export TMPDIR=$old_tmpdir
107-
fi
108-
if [[ "$old_temp" == __UNSET__ ]]; then
109-
unset TEMP
110-
else
111-
export TEMP=$old_temp
112-
fi
113-
if [[ "$old_tempdir" == __UNSET__ ]]; then
114-
unset TEMPDIR
115-
else
116-
export TEMPDIR=$old_tempdir
117-
fi
91+
_nix_export_or_unset NIX_BUILD_TOP "$old_nix_build_top"
92+
_nix_export_or_unset TMP "$old_tmp"
93+
_nix_export_or_unset TMPDIR "$old_tmpdir"
94+
_nix_export_or_unset TEMP "$old_temp"
95+
_nix_export_or_unset TEMPDIR "$old_tempdir"
11896
}
11997

12098
use_nix() {
@@ -143,11 +121,12 @@ use_nix() {
143121
local cache="$direnv_dir/cache-${version:-unknown}"
144122

145123
local update_drv=0
146-
if [[ ! -e "$cache" ]] || \
147-
[[ "$HOME/.direnvrc" -nt "$cache" ]] || \
148-
[[ .envrc -nt "$cache" ]] || \
149-
[[ default.nix -nt "$cache" ]] || \
150-
[[ shell.nix -nt "$cache" ]];
124+
if [[ ! -e "$cache" \
125+
|| "$HOME/.direnvrc" -nt "$cache" \
126+
|| .envrc -nt "$cache" \
127+
|| default.nix -nt "$cache" \
128+
|| shell.nix -nt "$cache" \
129+
]];
151130
then
152131
[[ -d "$direnv_dir" ]] || mkdir "$direnv_dir"
153132
local dump_cmd tmp

0 commit comments

Comments
 (0)