Skip to content

contrib: add generic shell hook extracted from direnv hook #8

Open
astratagem wants to merge 6 commits intonumtide:mainfrom
kleinweb:contrib-shell-hook
Open

contrib: add generic shell hook extracted from direnv hook #8
astratagem wants to merge 6 commits intonumtide:mainfrom
kleinweb:contrib-shell-hook

Conversation

@astratagem
Copy link
Copy Markdown

This change aims to leverage the implementation of prj-spec used by the direnv shell hook in order to provide prj-spec environment variables outside of the direnv context.

This means that there does not need to be a dependency on direnv for general usage of prj-spec shell environment additions. Use cases may include: continuous integration pipelines; an oops moment where someone forgot to run direnv allow and still wants their justfile recipes to work with e.g. $PRJ_ROOT; a hypothetical flake-parts module for prj-spec; and amongst people and organizations who, for whatever reasons, do not use direnv.

For the record, I am a big fan of prj-spec and I hope this can be a step towards greater accessibility of its shell integration. I am personally trying to set up a CI pipeline sharing some traits with my development shell, but it feels strange to use direnv in such a context.

Most of the logic in the existing direnv hook by @blaggacao is not actually direnv-specific, so this was a pretty trivial refactor. I am currently using it in my personal configuration project at https://github.com/montchr/dotfield/blob/f4269a6ad7a2762ee3355f112319587a0c05f121/dev/shells/default.nix#L64, and soon in work-related projects (hence the repo owner).

Note that 0c53ebd introduces a breaking change in that existing consumers of the direnv hook will need to reference the new filename direnv-hook.sh. I made this change to provide clarity as to the file's purpose, especially alongside the new shell-hook.sh. Basing a naming convention off the terse name of the existing direnv file could lead to confusion in that a file called shell indicates very little as to its purpose. I also figured that, considering these files are intended to be sourced, they should have a file extension -- in my experience, shell scripts lacking a file extension tend to be executable commands, while these hooks are not used in such a manner.

- Rename `find_prj_config` to `find_prj_root_with_config_dir`.

This function is not trying to find `$PRJ_CONFIG_HOME` as its name might
imply.  It looks for a directory named “.config” in order to find
`$PRJ_ROOT`, the successful result of which is later used as a fallback
method to set the initial value of `$PRJ_ROOT` if
`find_prj_root_with_git` yields no result (i.e. this is not a Git
repository).
The custom `find_prj_root_with_config_dir()` function is unset towards
the end of the script.  However, the other custom function
`find_prj_root_with_git()` is not unset.  I assume this is because the
latter was added a couple months after the former, and that the author
understandably forgot to unset the newly-added function.

I’m not entirely sure about the utility of unsetting either of these
functions, but I suppose it does keep the resulting environment cleaner.
They are intended to be sourced in the appropriate context,
not executed directly.
Copy link
Copy Markdown
Contributor

@blaggacao blaggacao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants