Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling direnv from further modifying the environment #1048

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

reegnz
Copy link
Contributor

@reegnz reegnz commented Jan 11, 2023

When DIRENV_DISABLE environment variable is set, direnv stops manipulating environment variables. When DIRENV_DISABLE environment variable is unset again, direnv picks up from where it left off and start managing the environment variables again.

Keep in mind that setting this variable doesn't unset the variables that direnv has set in the environment, it just toggles direnv, it doesn't roll back all of the effects it has done before the environment variable is set.

Fixes #550

When DIRENV_DISABLE environment variable is set, direnv stops
manipulating environment variables. When DIRENV_DISABLE environment
variable is unset again, direnv picks up from where it left off and
start managing the environment variables again.

Keep in mind that setting this variable doesn't unset the variables that
direnv has set in the environment, it just toggles direnv, it doesn't
roll back all of the effects it has done before the environment variable
is set.
@reegnz
Copy link
Contributor Author

reegnz commented Jan 11, 2023

I'm open for a cleanup-solution as well, but I'd rather have both a 'dirty' and a 'clean' disable.

Also this solution is a way simpler implementation than a full cleanup, and people using it should already be familiar with that caveat.

Copy link

@jraygauthier jraygauthier left a comment

Choose a reason for hiding this comment

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

LGTM

@todd-a-jacobs
Copy link

I'm open for a cleanup-solution as well, but I'd rather have both a 'dirty' and a 'clean' disable.

Also this solution is a way simpler implementation than a full cleanup, and people using it should already be familiar with that caveat.

The solution there might require having to ascend the tree of .direnv files to collect changes, and then running a new shell with --login or --interactive preceded by env -u FOO [...] with a -u argument for each variable modified by direnv. I say this because I don't think that direnv currently tracks what variables it adds or modifies, especially if multiple .direnv files are invoked as you descend some file tree.

Looking at the previous paragraph, that's probably the missing feature: a pushd/popd type function for direnv that tracks a stack of modifications. I'm not a GO person, so I'm not sure whether or not that's trivial, but it could be as simple as caching the location of each .direnv (and any includes in each .direnv) in a cache file descending from the topmost shell process.

Thoughts or constructive criticisms?

@reegnz
Copy link
Contributor Author

reegnz commented Feb 1, 2023

I say this because I don't think that direnv currently tracks what variables it adds or modifies, especially if multiple .direnv files are invoked as you descend some file tree.

direnv definitely does track what changes it's making, that's what is being stored in the DIRENV_DIFF environment variable it sets.

@terlar
Copy link

terlar commented Mar 7, 2023

This would be really nice, I have some nested shells that triggers direnv while it shouldn't so would be nice to set this to opt-out from the direnv hook if I already entered such a shell.

@reegnz
Copy link
Contributor Author

reegnz commented Mar 8, 2023

Same for me, I'm also spawning some nested shells where I don't want direnv to continue loading. Right now I've modified the hook to deal with this:

if [ -z "${DIRENV_DISABLE:-}" ]; then
  eval "$(direnv hook zsh)"
fi

This works for the nested shell use-case, but if I disable it, I cannot enable it in the nested shell later on (although I specifically don't want to do that for my use-case).

It would be great if this could be a native feature of direnv though, otherwise I'd have to get my colleagues use my custom hook wrapping instead of using the hook direnv spits out by default.

@dephiros
Copy link

Stumbled on this when looking for a solution to disable direnv. Would love for this to get merge since it seems ready @reegnz 🙏

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

Successfully merging this pull request may close these issues.

How to disable direnv automatic loading of environments in the current shell?
5 participants