Skip to content

Commit 8e6041c

Browse files
committed
feat(devshell): support default "$PRJ_ROOT" value
via the new option "devshell.prj_root_fallback". This option, if set to a non-null value, is used to set the value of "$PRJ_ROOT" in case it is not set in the controlling environment, the entrypoint script did not get a `--prj-root` option, "$IN_NIX_SHELL" is empty or unset, and "$DIRENV_IN_ENVRC" is unset or any value other than "1". The default option definition sets "$PRJ_ROOT" to the value of "$PWD".
1 parent d0601cb commit 8e6041c

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

modules/devshell.nix

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ config, lib, pkgs, ... }:
1+
{ config, lib, pkgs, options, ... }:
22
with lib;
33
let
44
cfg = config.devshell;
@@ -62,8 +62,12 @@ let
6262
# We know that PWD is always the current directory in these contexts
6363
PRJ_ROOT=$PWD
6464
elif [[ -z ''${PRJ_ROOT:-} ]]; then
65-
echo "ERROR: please set the PRJ_ROOT env var to point to the project root" >&2
66-
return 1
65+
${lib.optionalString (cfg.prj_root_fallback != null) cfg.prj_root_fallback}
66+
67+
if [[ -z "''${PRJ_ROOT:-}" ]]; then
68+
echo "ERROR: please set the PRJ_ROOT env var to point to the project root" >&2
69+
return 1
70+
fi
6771
fi
6872
6973
export PRJ_ROOT
@@ -292,6 +296,36 @@ in
292296
type = types.package;
293297
description = "TODO";
294298
};
299+
300+
prj_root_fallback = mkOption {
301+
type = let
302+
envType = options.env.type.nestedTypes.elemType;
303+
coerceFunc = value: { inherit value; };
304+
in types.nullOr (types.coercedTo types.nonEmptyStr coerceFunc envType);
305+
apply = x: if x == null then x else x // { name = "PRJ_ROOT"; };
306+
default = { eval = "$PWD"; };
307+
example = lib.literalExpression ''
308+
{
309+
# Use the top-level directory of the working tree
310+
eval = "$(git rev-parse --show-toplevel)";
311+
};
312+
'';
313+
description = ''
314+
If IN_NIX_SHELL is nonempty, or DIRENV_IN_ENVRC is set to '1', then
315+
PRJ_ROOT is set to the value of PWD.
316+
317+
This option specifies the path to use as the value of PRJ_ROOT in case
318+
IN_NIX_SHELL is empty or unset and DIRENV_IN_ENVRC is any value other
319+
than '1'.
320+
321+
Set this to null to force PRJ_ROOT to be defined at runtime (except if
322+
IN_NIX_SHELL or DIRENV_IN_ENVRC are defined as described above).
323+
324+
Otherwise, you can set this to a string representing the desired
325+
default path, or to a submodule of the same type valid in the 'env'
326+
options list (except that the 'name' field is ignored).
327+
'';
328+
};
295329
};
296330

297331
config.devshell = {

tests/core/devshell.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
shell = devshell.mkShell {
5454
devshell.name = "devshell-entrypoint-1";
5555
devshell.packages = [ pkgs.git ];
56+
57+
# Force PRJ_ROOT to be defined by caller (possibly via `--prj-root`).
58+
devshell.prj_root_fallback = null;
5659
};
5760
in
5861
runTest "devshell-entrypoint-1" { } ''

0 commit comments

Comments
 (0)