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

nix_direnv_watch_file should be watch_file #408

Closed
infinisil opened this issue Nov 17, 2023 · 9 comments
Closed

nix_direnv_watch_file should be watch_file #408

infinisil opened this issue Nov 17, 2023 · 9 comments

Comments

@infinisil
Copy link
Contributor

infinisil commented Nov 17, 2023

If you want to make sure your environment is both automatically updated when files change, and updated to the correct value, and you don't know if the user has nix-direnv installed, you need to do something like this:

if has nix_direnv_watch_file; then
  nix_direnv_watch_file file.nix
else
  watch_file file.nix
fi

use nix

It would be much nicer if nix-direnv provided its own version of watch_file instead, such that a simple watch_file file.nix works.

nix-direnv would then also have the correct behavior by default for projects whose .envrc doesn't do anything special for nix-direnv.

@Mic92
Copy link
Member

Mic92 commented Nov 18, 2023

What if make nix_watches+=() the api? This will work also if you don't have nix-direnv. I am a bit hesitant off monkey patching more direnv builtins.

@infinisil
Copy link
Contributor Author

As far as I can see, there's never a case where you'd want to watch_file but not nix_direnv_watch_file, so overriding it would be fine.

Using nix_watches+=() would still require repos that were just considering direnv to make adjustments for nix-direnv.

@Mic92
Copy link
Member

Mic92 commented Nov 19, 2023

@zimbatm I guess there is no other api to check if a file is beeing watched by direnv?

@bbenne10
Copy link
Contributor

bbenne10 commented Nov 20, 2023

@zimbatm has explicitly asked that we avoid monkey patching the direnv stdlib in this particular manner. I will continue honoring that request moving forward.

However, I see no reason you couldn't do this:

if has nix_direnv_watch_file; then
  alias wf=nix_direnv_watch_file
else
  alias wf=watch_file
fi

wf flake.nix

@infinisil
Copy link
Contributor Author

The problem with any approach that doesn't just use watch_file is that it requires all projects using direnv to explicitly add support for nix-direnv as above, and otherwise their caching is just broken for all nix-direnv users.

@bbenne10
Copy link
Contributor

This is true. At work, we've taken the approach that using or not using nix-direnv is a project specification, which makes this a decision that we don't allow non-principal developers to override (Of course, they can discuss and influence direction, but we commit our .envrc to git to keep it consistent). We then use source_url to pin to a revision.

Yes - I agree that this is a bit frustrating. But the fact that both direnv and nix-direnv provide somewhat matching interfaces is a bit of an implementation detail in my eyes (and honestly - one I wish I could undo at this point. The confusion between the two projects has come up repeatedly in issues. I'd much prefer use_flake_cache or something...)

@infinisil
Copy link
Contributor Author

Tbh nix-direnv should either just be built into direnv directly, or be some sort of plugin for it.

@Mic92
Copy link
Member

Mic92 commented Nov 21, 2023

You can use it as a plugin. Just add this to your project:

if ! has nix_direnv_version || ! nix_direnv_version 2.4.0; then
  source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.4.0/direnvrc" "sha256-XQzUAvL6pysIJnRJyR7uVpmUSZfc7LSgWQwq/4mBr1U="
fi

@Mic92
Copy link
Member

Mic92 commented Nov 21, 2023

Using a different name would have been also an annoyance, when a project comes with an .envrc that just uses use nix and everything gets slow again.

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

No branches or pull requests

3 participants