direnv: faster loading
Currently, if you use direnv you'll get a note saying it takes too long to export:
Is it possible to devenv to perform an export of everything it needs, like the normal nix develop does when used with direnv?
That's what it does, but it will always reevaluate shell to make sure it's up to date.
To properly fix this, we need to get Nix to be fast.
Are you sure? I think it's showing that because the direnv export zsh command here just lasts as long as I'm in the shell. After all, I got the prompt before seeing this message
I did this for a similar problem:
- create a directory for direnv,
direnv_layout_dirgives us the dir path - compare watched files
$(cmp --silent -- <(echo "$DIRENV_WATCHES") "${direnv_layout_dir}/session")- if its different write a cache file into direnv_layout_dir with result of
$(./result/bin/devenv print-dev-env) - and write session file tested before
- if its different write a cache file into direnv_layout_dir with result of
- eval from cache
Not solve the problem, but make it noticeable only when environment changes.
Possible issues: I'm not sure what happens when we use different direnv layouts
Could you give this one a try: https://github.com/cachix/devenv/commit/4717da802b1868318ab60758c244c4b37774f426
Sorry, I keep marking this as unread to finally check it when I have some time. I'll check it out this weekend.
I am just testing this out and use 0.5 which should include this I presume, but the .envrc that gets generated looks like this:
$ cat .envrc
watch_file devenv.nix
watch_file devenv.yaml
watch_file devenv.lock
eval "$(devenv print-dev-env)"
and the devenv print-dev-env command takes 4-5 seconds. Also miss the speed of my previous setup using the use flake together with nix-direnv support which has sub-second performance.
Yes, nix-direnv only works with 0.5 if you still take the Flake route of the devenv integration. See the default template's .envrc/flake.nix how that looks.
EDIT: I'm taking a look now how to use nix-direnvs primitives for caching, without having a flake.nix file in the repo (the minimal devenv install).
@shyim reports success using https://github.com/indigoviolet/direnv-cache
I made some progress trying to get the same evaluation path as devenv print-dev-env does in the current .envrc template, while reusing the great effort put into caching and avoiding unnecessary evaluations:
https://github.com/cachix/devenv/pull/237
The problem is the devenv uses a custom, more relaxed nix implementation internally, which it needs to evaluate the hidden flake file that it manages.
The work done so far has been to expose to:
- Expose this hidden nix binary (although this will create conflicts with the system one,
devenv which nixis an alternative implementation) - Add devenv's nix binary to the path
- This way when nix-direnv's
use_flakeruns whennix-direnvdetermines whichnixbinary to use, the correct version is getting picked up - Now the part where i'm stuck is how to get the actual print-dev-env call to consider the devenv managed flake file, it needs a directory where a normal
flake.nixfile exists, not the.devenv.flake.nixthat is currently used. So instead of passing in.withuse flake . --impurewe need a different path, an easy way would be to have a.devenv/flake/flake.nixsymlink file and useuse flake .devenv/flake --impure. The last remaining problem with the above is that since that file is actually updated bydevenv'sassemblebash function. We need to call that anytime any of thenix_direnv_watch'd files are changed. We would need to extendnix-direnvwith abefore-print-dev-envhook, that we could provide.
Any thoughts @domenkozar ?
0.5 seems to work fine for me.
(feel free to reopen if needed, I just thought the original issue was solved)