From e9d3a36775f721d3bc76442291fb72dfd84e14c7 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Fri, 19 Apr 2024 15:54:37 -0400 Subject: [PATCH 1/5] Issue a warning if we fallback to the old shell --- direnvrc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/direnvrc b/direnvrc index ab54fcf..4baa527 100644 --- a/direnvrc +++ b/direnvrc @@ -298,6 +298,7 @@ use_flake() { local tmp_profile_rc local tmp_profile="${layout_dir}/flake-tmp-profile.$$" if tmp_profile_rc=$(_nix print-dev-env --profile "$tmp_profile" "$@"); then + # If we've gotten here, the user's current devShell is valid and we should cache it _nix_clean_old_gcroots "$layout_dir" # We need to update our cache @@ -318,13 +319,19 @@ use_flake() { flake_input_paths="${flake_input_paths/${store_path}/}" done - _nix_direnv_info "renewed cache" + _nix_direnv_info "Renewed cache" + else + # The user's current flake failed to evaluate, + # but there is already a prior profile_rc, + # which is probably more useful than nothing. + # Fallback to use that (which means just leaving profile_rc alone!) + _nix_direnv_warning "Evaluating current devShell failed. Falling back to previous environment!" fi fi else if [[ -e ${profile_rc} ]]; then # Our cache is valid, use that - _nix_direnv_info "using cached dev shell" + _nix_direnv_info "Using cached dev shell" else # We don't have a profile_rc to use! _nix_direnv_error "use_flake failed - Is your flake's devShell working?" @@ -469,7 +476,9 @@ use_nix() { echo "$tmp_profile_rc" >"$profile_rc" _nix_add_gcroot "$tmp_profile" "$profile" rm -f "$tmp_profile" "$tmp_profile"* - _nix_direnv_info "renewed cache" + _nix_direnv_info "Renewed cache" + else + _nix_direnv_warning "Evaluating current nix shell failed. Falling back to previous environment!" fi fi else From b653b7226bbfb9f6cef421d99169fb9840fb6f62 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Fri, 19 Apr 2024 15:54:50 -0400 Subject: [PATCH 2/5] Add shellcheck to the devshell --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 6f21c32..64949fc 100644 --- a/flake.nix +++ b/flake.nix @@ -43,7 +43,7 @@ }; devShells.default = pkgs.callPackage ./shell.nix { - packages = [ config.treefmt.build.wrapper ]; + packages = [ config.treefmt.build.wrapper pkgs.shellcheck ]; }; checks = From aa50e0f781f1324721560ba9bc067b8adbd56e7b Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Fri, 19 Apr 2024 15:58:10 -0400 Subject: [PATCH 3/5] Set NIX_DIRENV_DID_FALLBACK where appropriate --- direnvrc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/direnvrc b/direnvrc index 4baa527..e9de144 100644 --- a/direnvrc +++ b/direnvrc @@ -326,6 +326,7 @@ use_flake() { # which is probably more useful than nothing. # Fallback to use that (which means just leaving profile_rc alone!) _nix_direnv_warning "Evaluating current devShell failed. Falling back to previous environment!" + export NIX_DIRENV_DID_FALLBACK=1 fi fi else @@ -479,6 +480,7 @@ use_nix() { _nix_direnv_info "Renewed cache" else _nix_direnv_warning "Evaluating current nix shell failed. Falling back to previous environment!" + export NIX_DIRENV_DID_FALLBACK=1 fi fi else From ba0cf20e0fa6ba08c4563ec3ebc003a6e46a09cd Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Fri, 19 Apr 2024 16:07:10 -0400 Subject: [PATCH 4/5] Update tests to expect new output strings --- direnvrc | 2 +- tests/test_gc.py | 4 ++-- tests/test_use_nix.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/direnvrc b/direnvrc index e9de144..5bfeb85 100644 --- a/direnvrc +++ b/direnvrc @@ -485,7 +485,7 @@ use_nix() { fi else if [[ -e ${profile_rc} ]]; then - _nix_direnv_info "using cached dev shell" + _nix_direnv_info "Using cached dev shell" else _nix_direnv_error "use_nix failed - Is your nix shell working?" return 1 diff --git a/tests/test_gc.py b/tests/test_gc.py index 26f2566..b739dee 100644 --- a/tests/test_gc.py +++ b/tests/test_gc.py @@ -24,7 +24,7 @@ def common_test(direnv_project: DirenvProject) -> None: ) sys.stderr.write(out1.stderr) assert out1.returncode == 0 - assert "renewed cache" in out1.stderr + assert "Renewed cache" in out1.stderr assert "Executing shellHook." in out1.stderr run(["nix-collect-garbage"]) @@ -37,7 +37,7 @@ def common_test(direnv_project: DirenvProject) -> None: ) sys.stderr.write(out2.stderr) assert out2.returncode == 0 - assert "using cached dev shell" in out2.stderr + assert "Using cached dev shell" in out2.stderr assert "Executing shellHook." in out2.stderr diff --git a/tests/test_use_nix.py b/tests/test_use_nix.py index 804079d..32120c7 100644 --- a/tests/test_use_nix.py +++ b/tests/test_use_nix.py @@ -30,7 +30,7 @@ def direnv_exec( sys.stderr.write(out.stderr) assert out.returncode == 0 assert out.stdout == "OK\n" - assert "renewed cache" in out.stderr + assert "Renewed cache" in out.stderr @pytest.mark.parametrize("strict_env", [False, True]) From a69341cdbb0dfe2eb18cb2fe704af8a869936dc0 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 22 Apr 2024 08:16:55 -0400 Subject: [PATCH 5/5] Update README --- README.md | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 2e06282..2c7346f 100644 --- a/README.md +++ b/README.md @@ -294,20 +294,21 @@ nix invocation. #### Tracked files -`nix-direnv` makes a performance trade-off and only considers changes in a -limited number of files when deciding to update its cache. +As a convenience, `nix-direnv` adds common files to direnv's watched file list +automatically. -- for `use nix` this is: +The list of additionally tracked files is as follows: +- for `use nix`: - `~/.direnvrc` - `~/.config/direnv/direnvrc` - `.envrc`, - A single nix file. In order of preference: - The file argument to `use nix` - - `shell.nix` if it exists - `default.nix` if it exists + - `shell.nix` if it exists -- for `use flake` this is: +- for `use flake`: - `~/.direnvrc` - `~/.config/direnv/direnvrc` - `.envrc` @@ -315,30 +316,30 @@ limited number of files when deciding to update its cache. - `flake.lock` - `devshell.toml` if it exists -To add more files to be checked use `watch_file` like this +Users are free to use direnv's builtin `watch_file` function to track additional +files. `watch_file` must be invoked before either `use flake` or `use nix` to +take effect. -```shell -watch_file your-file.nix -use nix # or use flake -``` +#### Environment Variables -Or - if you don't mind the overhead (runtime and conceptual) of watching all -nix-files: +nix-direnv sets the following environment variables for user consumption. All +other environment variables are either a product of the underlying nix +invocation or are purely incidental and should not be relied upon. -```shell -watch_file $(find . -name "*.nix" -printf '"%p" ') -``` - -Note that this will re-execute direnv for any nix change, regardless of whether -that change is meaningful for the devShell in use. - -`watch_file` must be invoked before either `use flake` or `use nix` to take -effect. +- `NIX_DIRENV_DID_FALLBACK`: Set when the current revision of your nix shell or + flake's devShell are invalid and nix-direnv has loaded the last known working + shell. ## General direnv tips -- [Changing where direnv stores its cache](https://github.com/direnv/direnv/wiki/Customizing-cache-location) -- [Quickly setting up direnv in a new nix project](https://github.com/nix-community/nix-direnv/wiki/Shell-integration) +- [Changing where direnv stores its cache][cache_location] +- [Quickly setting up direnv in a new nix project][new_project] +- [Disable the diff notice (requires direnv 2.34+)][hide_diff_notice]: Note that + this goes into direnv's TOML configuration! + +[cache_location]: https://github.com/direnv/direnv/wiki/Customizing-cache-location +[new_project]: https://github.com/nix-community/nix-direnv/wiki/Shell-integration +[hide_diff_notice]: https://direnv.net/man/direnv.toml.1.html#codehideenvdiffcode ## Other projects in the field