Merge pull request #148 from polygon/allow_watching_arbitrary_files

Allow watching of arbitrary files for nix flake updates
This commit is contained in:
Jörg Thalheim 2022-02-26 16:03:43 +00:00 committed by GitHub
commit 363835438f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 18 deletions

View file

@ -232,17 +232,30 @@ direnv_layout_dir() {
During direnv setup `direnv_layout_dir` can be called multiple times and with different values of `$PWD` During direnv setup `direnv_layout_dir` can be called multiple times and with different values of `$PWD`
(when other `.envrc` files are included). Therefore cache its results in dictionary `direnv_layout_dirs`. (when other `.envrc` files are included). Therefore cache its results in dictionary `direnv_layout_dirs`.
## Manually re-triggering evaluation ## Watching additional files
In some case nix-direnv does not detect if imported file has changed and still To minimize the number of evaluations, nix-direnv maintains a list of files to check
provides the old cached values. An evaluation can be triggered by updating your for changes when deciding if an update of the cached environment is required. By default, `use_flake` watches
`default.nix`, `shell.nix` or `flake.nix`, depending on what is used:
```console ```
# choose one flake.nix
$ touch default.nix flake.lock
$ touch shell.nix devshell.toml
$ touch flake.nix ```
`use_nix` watches
```
default.nix
shell.nix
```
To trigger an evaluation when other nix files change, register them by calling `nix_direnv_watch_file PATH [PATH...]` from `.envrc`.
```bash
nix_direnv_watch_file module.nix
nix_direnv_watch_file mod1.nix mod2.nix
use flake
``` ```
## Shell integration ## Shell integration

View file

@ -111,6 +111,11 @@ _nix_argsum_suffix() {
fi fi
} }
nix_direnv_watch_file() {
watch_file "$@"
nix_watches+=("$@")
}
use_flake() { use_flake() {
flake_expr="${1:-.}" flake_expr="${1:-.}"
flake_dir="${flake_expr%#*}" flake_dir="${flake_expr%#*}"
@ -123,13 +128,25 @@ use_flake() {
local flake_inputs="${layout_dir}/flake-inputs/" local flake_inputs="${layout_dir}/flake-inputs/"
local profile_rc="${profile}.rc" local profile_rc="${profile}.rc"
nix_watches+=(
"$HOME/".direnvrc
.envrc
"$flake_dir/"devshell.toml
"$flake_dir/"flake.nix
"$flake_dir/"flake.lock
)
need_update=0
for file in "${nix_watches[@]}"; do
if [[ "$file" -nt "$profile_rc" ]]; then
need_update=1
break
fi
done
if [[ ! -e "$profile" if [[ ! -e "$profile"
|| ! -e "$profile_rc" || ! -e "$profile_rc"
|| "$HOME/.direnvrc" -nt "$profile_rc" || "$need_update" -eq "1"
|| .envrc -nt "$profile_rc"
|| "$flake_dir/"devshell.toml -nt "$profile_rc"
|| "$flake_dir/"flake.nix -nt "$profile_rc"
|| "$flake_dir/"flake.lock -nt "$profile_rc"
]]; ]];
then then
local tmp_profile="${layout_dir}/flake-profile.$$" local tmp_profile="${layout_dir}/flake-profile.$$"
@ -239,12 +256,24 @@ use_nix() {
local cache local cache
cache="${layout_dir}/cache-${version:-unknown}$(_nix_argsum_suffix "$*")" cache="${layout_dir}/cache-${version:-unknown}$(_nix_argsum_suffix "$*")"
nix_watches+=(
"$HOME/".direnvrc
.envrc
default.nix
shell.nix
)
need_update=0
for file in "${nix_watches[@]}"; do
if [[ "$file" -nt "$cache" ]]; then
need_update=1
break
fi
done
local update_drv=0 local update_drv=0
if [[ ! -e "$cache" if [[ ! -e "$cache"
|| "$HOME/.direnvrc" -nt "$cache" || "$need_update" -eq "1"
|| .envrc -nt "$cache"
|| default.nix -nt "$cache"
|| shell.nix -nt "$cache"
]]; ]];
then then
[[ -d "$layout_dir" ]] || mkdir -p "$layout_dir" [[ -d "$layout_dir" ]] || mkdir -p "$layout_dir"