Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lib/dev/mkShell.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 27 additions & 3 deletions src/lib/dev/mkShell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
10 changes: 5 additions & 5 deletions src/local/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/tests/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading