Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use direnv-builtin DIRENV_WATCHES to list watched files #414

Merged
merged 4 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,23 @@ when deciding to update its cache.
* `flake.lock`
* `devshell.toml` if it exists

To add more files to be checked use `nix_direnv_watch_file` like this
To add more files to be checked use `watch_file` like this

```shell
nix_direnv_watch_file your-file.nix
watch_file your-file.nix
use nix # or use flake
```

Or - if you don't mind the overhead (runtime and conceptual) of watching all nix-files:

```shell
nix_direnv_watch_file $(find . -name "*.nix" -printf '"%p" ')
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.

`nix_direnv_watch_file` must be invoked before either `use flake` or `use nix` to take effect.
`watch_file` must be invoked before either `use flake` or `use nix` to take effect.

## General direnv tips

Expand Down
40 changes: 30 additions & 10 deletions direnvrc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ _nix_import_env() {

eval "$(< "$profile_rc")"
# `nix print-dev-env` will create a temporary directory and use it as TMPDIR
# We cannot rely on this directory being availble at all times,
# We cannot rely on this directory being available at all times,
# as it may be garbage collected.
# Instead - just remove it immediately.
# Use recursive & force as it may not be empty.
Expand Down Expand Up @@ -188,7 +188,7 @@ _nix_argsum_suffix() {
elif has shasum; then
out=$(shasum <<< "$1")
else
# degrate gracefully both tools are not present
# degrade gracefully both tools are not present
return
fi
read -r checksum _ <<< "$out"
Expand All @@ -197,16 +197,33 @@ _nix_argsum_suffix() {
}

nix_direnv_watch_file() {
# shellcheck disable=2016
log_error '`nix_direnv_watch_file` is deprecated - use `watch_file`'
watch_file "$@"
nix_watches+=("$@")
}

_nix_direnv_watches() {
local -n _watches=$1
while IFS= read -r line; do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can drop this code when we get direnv/direnv#1198

local regex='"path": "(.+)"$'
if [[ "$line" =~ $regex ]]; then
local path="${BASH_REMATCH[1]}"
if [[ "$path" == "${XDG_DATA_HOME:-${HOME:-/var/empty}/.local/share}/direnv/allow/"* ]]; then
continue
fi
# expand new lines and other json escapes
# shellcheck disable=2059
path=$(printf "$path")
_watches+=("$path")
fi
done < <(direnv show_dump "${DIRENV_WATCHES}")
}

_nix_direnv_manual_reload=0
nix_direnv_manual_reload() {
_nix_direnv_manual_reload=1
}


use_flake() {
_nix_direnv_preflight

Expand All @@ -229,7 +246,7 @@ use_flake() {
files_to_watch+=("$flake_dir/flake.nix" "$flake_dir/flake.lock" "$flake_dir/devshell.toml")
fi

nix_direnv_watch_file "${files_to_watch[@]}"
watch_file "${files_to_watch[@]}"

local layout_dir profile
layout_dir=$(direnv_layout_dir)
Expand All @@ -238,8 +255,10 @@ use_flake() {
local flake_inputs="${layout_dir}/flake-inputs/"

local need_update=0
local watches
_nix_direnv_watches watches
local file=
for file in "${nix_watches[@]}"; do
for file in "${watches[@]}"; do
if [[ "$file" -nt "$profile_rc" ]]; then
need_update=1
break
Expand Down Expand Up @@ -388,11 +407,13 @@ use_nix() {
esac
done

nix_direnv_watch_file "$HOME/.direnvrc" "$HOME/.config/direnv/direnvrc" ".envrc" "shell.nix" "default.nix"
watch_file "$HOME/.direnvrc" "$HOME/.config/direnv/direnvrc" ".envrc" "shell.nix" "default.nix"

local need_update=0
local watches
_nix_direnv_watches watches
local file=
for file in "${nix_watches[@]}"; do
for file in "${watches[@]}"; do
if [[ "$file" -nt "$profile_rc" ]]; then
need_update=1
break
Expand Down Expand Up @@ -450,7 +471,6 @@ use_nix() {


if [[ "$#" == 0 ]]; then
watch_file default.nix
watch_file shell.nix
watch_file default.nix shell.nix
fi
}