Skip to content

Commit 28d54f9

Browse files
authored
refactor(nix): split out the git wt module and auto-install JS deps on shell entry (#1543)
* refactor(nix): extract the git wt wiring into its own module The dev shell's shellHook mixed three unrelated concerns: mold linker flags, agent-skill syncing, and the `git wt` repo config. Move the last one to nix/git-wt.nix, matching the layout rorkai/rork-local already uses, so the two repositories can stay in sync and the dev shell reads as a list of concerns rather than a script. The module takes the worktree install command as an argument, defaulting to `just install`, which is what this repository needs. The expanded shellHook is byte-identical to before. * feat(nix): install JS dependencies on dev shell entry Entering the dev shell after a `git pull` that bumped a lockfile left a stale node_modules until someone remembered to run `just install`, which usually surfaced as a confusing type or resolution error rather than as a missing dependency. Gate the install on a stamp file instead of on the node_modules mtimes: an install that changes nothing leaves those directories untouched, so comparing against the tree itself would re-install on every shell entry. `just install` writes the stamp, which also means the `git wt` worktree hook already satisfies the check and a fresh worktree does not install twice. The check covers the tool lockfiles under nix/tools as well, since those directories sit outside the pnpm workspace and `.envrc` already watches the whole nix/ tree.
1 parent aa8c7b1 commit 28d54f9

3 files changed

Lines changed: 52 additions & 12 deletions

File tree

‎justfile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ install:
3131
toolDir="$(dirname "$tsconfig")"
3232
(cd "$toolDir" && bun install --frozen-lockfile)
3333
done
34+
# The dev shell re-installs when a lockfile is newer than this stamp. It
35+
# cannot compare against node_modules itself, because an install that
36+
# changes nothing leaves those directory mtimes untouched.
37+
touch node_modules/.install-stamp
3438
3539
# Build every workspace package
3640
build: ccusage::build docs::build

‎nix/dev-shell.nix‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ in
1515
overlays = [ inputs.rust-overlay.overlays.default ];
1616
};
1717
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile (root + /rust-toolchain.toml);
18+
gitWtHook = import ./git-wt.nix { inherit pkgs; };
1819
in
1920
{
2021
devShells.default = pkgs.mkShell {
@@ -81,20 +82,32 @@ in
8182
*) export RUSTFLAGS="''${RUSTFLAGS:+$RUSTFLAGS }-C link-arg=-fuse-ld=mold" ;;
8283
esac
8384
fi
84-
${lib.getExe config.packages.syncAgentSkills}
85-
${config.pre-commit.shellHook}
8685
87-
# Set up direnv and JS dependencies whenever `git wt` creates a new
88-
# worktree, so worktrees are usable without a manual step.
89-
git config --replace-all wt.hook "direnv allow || true; just install || true"
90-
91-
# Move deleted worktree directories to the trash instead of `rm -rf`,
92-
# which is noticeably slower on large node_modules and target trees.
93-
git config --replace-all wt.remover "${pkgs.trash-cli}/bin/trash"
86+
# Install whenever a lockfile is newer than the last `just install`, so
87+
# a `git pull` that bumps dependencies does not leave a stale tree. The
88+
# `.envrc` watches pnpm-lock.yaml and the whole nix/ tree, so direnv
89+
# re-enters the shell — and re-runs this check — as soon as one changes.
90+
# The tool dirs under nix/tools are outside the pnpm workspace, hence
91+
# the extra lockfiles.
92+
needsInstall=false
93+
if [ ! -e node_modules/.install-stamp ]; then
94+
needsInstall=true
95+
else
96+
for lock in pnpm-lock.yaml nix/tools/*/bun.lock; do
97+
if [ "$lock" -nt node_modules/.install-stamp ]; then
98+
needsInstall=true
99+
fi
100+
done
101+
fi
102+
if [ "$needsInstall" = true ]; then
103+
echo "Installing dependencies..."
104+
just install
105+
fi
94106
95-
# Free the worktree's direnv/nix state before it's deleted.
96-
git config --replace-all wt.deletehook "direnv revoke .envrc || true; ${pkgs.trash-cli}/bin/trash .direnv || true; nix store gc || true"
97-
'';
107+
${lib.getExe config.packages.syncAgentSkills}
108+
${config.pre-commit.shellHook}
109+
''
110+
+ gitWtHook;
98111
};
99112
};
100113
}

‎nix/git-wt.nix‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# `git wt` worktree wiring for the dev shell.
2+
#
3+
# These live in the repo's git config rather than a derivation because `git wt`
4+
# reads them from the checkout it runs in, so entering the shell is the only
5+
# moment we can (re)write them for the current worktree.
6+
{
7+
pkgs,
8+
# Command that installs JS dependencies in a freshly created worktree.
9+
installCommand ? "just install",
10+
}:
11+
12+
''
13+
# Set up direnv and JS dependencies whenever `git wt` creates a new
14+
# worktree, so worktrees are usable without a manual step.
15+
git config --replace-all wt.hook "direnv allow || true; ${installCommand} || true"
16+
17+
# Move deleted worktree directories to the trash instead of `rm -rf`,
18+
# which is noticeably slower on large node_modules and target trees.
19+
git config --replace-all wt.remover "${pkgs.trash-cli}/bin/trash"
20+
21+
# Free the worktree's direnv/nix state before it's deleted.
22+
git config --replace-all wt.deletehook "direnv revoke .envrc || true; ${pkgs.trash-cli}/bin/trash .direnv || true; nix store gc || true"
23+
''

0 commit comments

Comments
 (0)