From 9af83f933f09d54e0463e6b76fb2de85f46fddaa Mon Sep 17 00:00:00 2001 From: Mayeu Date: Wed, 6 May 2026 17:08:38 +0200 Subject: [PATCH 1/2] fix: pin devshell cwd to git toplevel before env.bash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Running `nix develop` from a subdirectory caused PRJ_ROOT to be set to that subdir (devshell hardcodes PRJ_ROOT=$PWD when IN_NIX_SHELL=1) and made nixago materialize repo config files (.editorconfig, treefmt.toml, lefthook.yml, …) in the subdir, since nixago's hooks resolve output paths against $PWD. Wrap the devshell derivation's outer shellHook with a prelude that cd's to `git rev-parse --show-toplevel` before env.bash is sourced, and an epilogue that restores the invocation cwd for the interactive shell. Falls back to $PWD with a stderr warning when not in a git repo. The prelude runs before devshell's PRJ_ROOT=$PWD line, so PRJ_ROOT picks up the real root automatically; nixago's startup hook then materializes files at the right place. The user's interactive cwd is preserved. --- src/lib/dev/mkShell.nix | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/lib/dev/mkShell.nix b/src/lib/dev/mkShell.nix index d9efbb24..c404cba0 100644 --- a/src/lib/dev/mkShell.nix +++ b/src/lib/dev/mkShell.nix @@ -59,7 +59,31 @@ in commands = l.concatMap (o: o.commands) updated; }; }; - in - pkgs.devshell.mkShell { + # `nix develop` evaluates the derivation's outer shellHook *before* + # devshell's env.bash is sourced (which is where PRJ_ROOT=$PWD happens, see + # devshell's modules/devshell.nix). Prepending a `cd` to the real project + # root here makes PRJ_ROOT correct, and makes nixago — whose hooks resolve + # output paths against $PWD — materialize files at the project root rather + # than wherever `nix develop` was invoked from. The trailing `cd` restores + # the user's invocation cwd for the interactive shell. + shell = pkgs.devshell.mkShell { imports = [configuration nixagoModule]; - } + }; + rootPrelude = '' + _std_orig_pwd="$PWD" + if _std_root="$(${pkgs.git}/bin/git rev-parse --show-toplevel 2>/dev/null)"; then + cd "$_std_root" + else + echo "std: not in a git repo; using \$PWD ($PWD) as project root" >&2 + fi + unset _std_root + ''; + rootEpilogue = '' + cd "$_std_orig_pwd" + unset _std_orig_pwd + ''; + in + derivation (shell.drvAttrs + // { + shellHook = rootPrelude + shell.drvAttrs.shellHook + rootEpilogue; + }) From 8286028ad8134e518a583c94a028598f8a3c19a9 Mon Sep 17 00:00:00 2001 From: Mayeu Date: Wed, 6 May 2026 17:11:33 +0200 Subject: [PATCH 2/2] docs: note project root resolution behavior in mkShell Document the new git-toplevel cd-prelude in mkShell so users entering a devshell from a subdirectory understand why files end up at the repo root. Also update src/local and src/tests subflake locks to point at the previous fix commit (via .github/workflows/update-subflake.sh) so the dogfooded devshell uses the new behavior. --- src/lib/dev/mkShell.md | 12 ++++++++++++ src/local/flake.lock | 10 +++++----- src/tests/flake.lock | 10 +++++----- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/lib/dev/mkShell.md b/src/lib/dev/mkShell.md index efc87d0a..fa44e49f 100644 --- a/src/lib/dev/mkShell.md +++ b/src/lib/dev/mkShell.md @@ -23,5 +23,17 @@ It is enriched with a tight integration for `std` [Nixago][nixago] pebbles: _Note, that you can extend any Nixago Pebble at the calling site via a built-in functor like in the example above._ +#### Project root resolution + +`mkShell` wraps the devshell's outer `shellHook` so that, before `env.bash` +is sourced, the working directory is `cd`'d to the git project root (via +`git rev-parse --show-toplevel`). After `env.bash` returns, the original +invocation cwd is restored. + +This makes `nix develop` from a subdirectory behave correctly: `PRJ_ROOT` +points at the actual project root, and Nixago materializes its files there +rather than in the subdirectory. If the project is not a git repository, a +warning is printed and `PRJ_ROOT` falls back to `$PWD`. + [nixago]: https://github.com/nix-community/nixago [numtide-devshell]: https://github.com/numtide/devshell diff --git a/src/local/flake.lock b/src/local/flake.lock index 0e0fe0a4..d58a445b 100644 --- a/src/local/flake.lock +++ b/src/local/flake.lock @@ -347,11 +347,11 @@ "yants": "yants" }, "locked": { - "lastModified": 1772127938, - "narHash": "sha256-peYU8Fd5qYKMZpBMxKWFW6UHq+Qf8fLR2kpcQ8irBTw=", - "ref": "refs/heads/fix/upgrade-flake-lock", - "rev": "7c75c6d51d37fd4c7567ed0159497470d77e9c73", - "revCount": 836, + "lastModified": 1778080118, + "narHash": "sha256-0rCmr48YeRoX3Jsv2CIDPfa5oSvjWZthk1viffUq6U8=", + "ref": "refs/heads/fix/entering-the-shell-should-create-dotfile-at-the-root", + "rev": "b695f4d6a7581d52141d073debe629e734b4a89d", + "shallow": true, "type": "git", "url": "file:///Users/m/code-des-autres/std/src/local/../.." }, diff --git a/src/tests/flake.lock b/src/tests/flake.lock index 6c48845a..c1d6647f 100644 --- a/src/tests/flake.lock +++ b/src/tests/flake.lock @@ -522,11 +522,11 @@ "yants": "yants" }, "locked": { - "lastModified": 1772127938, - "narHash": "sha256-peYU8Fd5qYKMZpBMxKWFW6UHq+Qf8fLR2kpcQ8irBTw=", - "ref": "refs/heads/fix/upgrade-flake-lock", - "rev": "7c75c6d51d37fd4c7567ed0159497470d77e9c73", - "revCount": 836, + "lastModified": 1778080118, + "narHash": "sha256-0rCmr48YeRoX3Jsv2CIDPfa5oSvjWZthk1viffUq6U8=", + "ref": "refs/heads/fix/entering-the-shell-should-create-dotfile-at-the-root", + "rev": "b695f4d6a7581d52141d073debe629e734b4a89d", + "shallow": true, "type": "git", "url": "file:///Users/m/code-des-autres/std/src/tests/../.." },