devenv icon indicating copy to clipboard operation
devenv copied to clipboard

direnv: faster loading

Open kubukoz opened this issue 3 years ago • 2 comments

Currently, if you use direnv you'll get a note saying it takes too long to export:

image

Is it possible to devenv to perform an export of everything it needs, like the normal nix develop does when used with direnv?

kubukoz avatar Nov 13 '22 15:11 kubukoz

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.

domenkozar avatar Nov 14 '22 13:11 domenkozar

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

kubukoz avatar Nov 14 '22 17:11 kubukoz

I did this for a similar problem:

  • create a directory for direnv, direnv_layout_dir gives 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
  • 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

hugosenari avatar Nov 26 '22 06:11 hugosenari

Could you give this one a try: https://github.com/cachix/devenv/commit/4717da802b1868318ab60758c244c4b37774f426

domenkozar avatar Dec 20 '22 12:12 domenkozar

Sorry, I keep marking this as unread to finally check it when I have some time. I'll check it out this weekend.

kubukoz avatar Dec 23 '22 02:12 kubukoz

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.

terlar avatar Dec 23 '22 10:12 terlar

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).

thenonameguy avatar Dec 23 '22 10:12 thenonameguy

@shyim reports success using https://github.com/indigoviolet/direnv-cache

domenkozar avatar Dec 23 '22 12:12 domenkozar

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:

  1. Expose this hidden nix binary (although this will create conflicts with the system one, devenv which nix is an alternative implementation)
  2. Add devenv's nix binary to the path
  3. This way when nix-direnv's use_flake runs when nix-direnv determines which nix binary to use, the correct version is getting picked up
  4. 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.nix file exists, not the .devenv.flake.nix that is currently used. So instead of passing in . with use flake . --impure we need a different path, an easy way would be to have a .devenv/flake/flake.nix symlink file and use use flake .devenv/flake --impure. The last remaining problem with the above is that since that file is actually updated by devenv's assemble bash function. We need to call that anytime any of the nix_direnv_watch'd files are changed. We would need to extend nix-direnv with a before-print-dev-env hook, that we could provide.

Any thoughts @domenkozar ?

thenonameguy avatar Dec 23 '22 13:12 thenonameguy

0.5 seems to work fine for me.

kubukoz avatar Dec 24 '22 18:12 kubukoz

(feel free to reopen if needed, I just thought the original issue was solved)

kubukoz avatar Feb 05 '23 17:02 kubukoz