-# Build or reuse .cache/nix/
-source_env .lib/nix/envrc.sh
-
-# Rebuild the .cache/nix/ when a file changes in shell/
-# and its subdirectories.
-while read -r dir
- do watch_file "$dir"
- done <<EOF
-$(find shell/ -type d)
-EOF
+use_flake() {
+ watch_file flake.nix
+ watch_file flake.lock
+ mkdir -p "$(direnv_layout_dir)"
+ eval "$(time nix print-dev-env --show-trace --profile "$(direnv_layout_dir)"/flake-profile || echo false)" &&
+ nix-store --add-root "shell.root" --indirect --realise "$(direnv_layout_dir)"/flake-profile
+ rm -rf "$(direnv_layout_dir)"/flake-profile
+}
+use flake