Skip to content

Commit

Permalink
direnv: allow configuring extra files to watch from devenv.nix
Browse files Browse the repository at this point in the history
  • Loading branch information
sandydoo committed May 3, 2024
1 parent 34e6461 commit fb11902
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions direnvrc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ use_devenv() {
fi
fi

local extra_files_watchlist="$flake_dir/.devenv/watchfiles.txt"
# Watch extra files configured in devenv.nix
if [[ -f "$extra_files_watchlist" ]]; then
for file in $(cat "$extra_files_watchlist"); do
files_to_watch+=("$file")
done
fi

nix_direnv_watch_file "${files_to_watch[@]}"

local layout_dir profile_rc
Expand Down Expand Up @@ -155,4 +163,13 @@ use_devenv() {
log_status "using cached devenv shell"
_nix_import_env "$profile_rc"
fi

# Watch extra files configured in devenv.nix
# This picks up new files and reloads the shell on initial run
# TODO: can compare files in files_to_watch with the files we have now
if [[ -f "$extra_files_watchlist" ]]; then
for file in $(cat "$extra_files_watchlist"); do
watch_file "$file"
done < "$filename"
fi
}
25 changes: 25 additions & 0 deletions src/modules/integrations/direnv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{ pkgs, config, lib, ... }:

let
cfg = config.direnv;
in
{
options.direnv = {
watchFiles = lib.mkOption {
description = lib.mdDoc "Extra files to watch for changes and reload the direnv environment";
# TODO: best type here? any way to exclude defaults?
# TODO: regex support?
type = lib.types.listOf lib.types.str;
default = [ ];
};

watchfiles = lib.mkOption {
internal = true;
type = lib.types.package;
};
};

config = {
direnv.watchfiles = pkgs.writeText "watchfiles.txt" (lib.concatStringsSep "\n" cfg.watchFiles);
};
}
2 changes: 2 additions & 0 deletions src/modules/top-level.nix
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ in
echo "Please install direnv: https://direnv.net/docs/installation.html"
fi
ln -snf ${config.direnv.watchfiles} ${lib.escapeShellArg config.devenv.dotfile}/watchfiles.txt
mkdir -p "$DEVENV_STATE"
if [ ! -L "$DEVENV_DOTFILE/profile" ] || [ "$(${pkgs.coreutils}/bin/readlink $DEVENV_DOTFILE/profile)" != "${profile}" ]
then
Expand Down

0 comments on commit fb11902

Please sign in to comment.