haslersn / any-nix-shell

fish and zsh support for the nix run and nix-shell environments of the Nix package manager
MIT License
253 stars 17 forks source link

Prevent env unloading when used together with direnv #5

Closed terlar closed 4 years ago

terlar commented 4 years ago

I am not sure this should be an issue with direnv or any-nix-shell but it manifests when using both together.

So if I am using direnv+lorri in a project and within this project I want to launch another nix-shell. Then when I am using any-nix-shell with fish the direnv unload hooks will fire as soon as I switch directory, making me loose all the PATH:s etc that the nix-shell had setup.

I wonder if there is a way to make these work together somehow.

terlar commented 4 years ago

I ended up adding this to my interactive shell init:

if set -q IN_NIX_SHELL
  function __direnv_export_eval
    # Don't trigger within nix-shell
  end
end

Seems to work, not sure if there is interest in having that as part of any-nix-shell. Otherwise feel free to close.

haslersn commented 4 years ago

Does the same issue arise when using direnv+lorri in bash and using the normal nix-shell?

terlar commented 4 years ago

Perhaps, I guess I never really realized since I am not using bash, just checked, seems like direnv hooks are not setup properly for the bash environment, so they don't execute (at all). Which could explain why I have not experienced this before with regular nix-shell+bash.

Just seems a bit unfortunate with the direnv unloading which happens on directory switch, that it also unloads all the env vars that has been setup by the nix-shell itself.

haslersn commented 4 years ago

seems like direnv hooks are not setup properly for the bash environment

Do you have the direnv hook in ~/.bashrc?

terlar commented 4 years ago

Nope, I don't even have a ~/.bashrc. I installed direnv via home-manager programs.direnv.enable = true;. That was enought for fish to pick it up, but guess bash requires some more love. I'm using NixOS as well btw.

haslersn commented 4 years ago

Yes but I thought you also attempted to reproduce the issue with bash. If it also arises with bash (i.e. nix-shell without any-nix-shell), then it's only a direnv issue.

terlar commented 4 years ago

Ah, okay, sorry for the confusion. I will see if I can achieve that and we will know where to go from there.

terlar commented 4 years ago

So yes this also happens with bash when I have configured it to also have the direnv hooks. So I guess this is more of an issue with direnv+nix.

haslersn commented 4 years ago

Thanks for checking this out!