- the default
direnvNix integration from projects loads a Nix shell every new terminal or every timeshell.nixand/ordefault.nixchanges. On larger projects with a lot of Nix shell dependencies, that can cause the terminal to take more than 6 seconds to load, which significantly degrades the developer experience (DX). - when a developer garbage collects in their Nix store, often the Nix shell dependencies are deleted also which causes a slow start the next time the project’s Nix shell is requested, also degrading developer experience (DX).
- on first Nix shell load, the Nix shell is evaluated fully such that direnv dumps the environment from the spawn Nix shell process
- on subsequent loads, a cache directory is inspected to see if the shell was already loaded, and applies the environment settings from the original loading of the Nix shell evaluation
- to determine freshness of shell evaluation we update the
nixexprsarray in the Bash script above to ensure (see TODO comment in the.direnvrcfile), which will watch for changes in that array; these are all used to determine the hash of the cached Nix shell evaluation to ensure consistency - to ensure all important GC roots are added to prevent premature GC-ing of the shell’s dependencies, we add roots of all indirects for the shell’s derivation
It has two main caveats right now (for the infrequent case where you manually GC Nix packages from your store, ~every few months):
- to free up old packages no longer referenced in the Nix shell, you need to
rm -rf .direnv.din your dailykos project root thendirenv reloadand thennix-collect-garbage -d - after collecting the nix “garbage”, you will then
rm -rf .direnv.dand then it should take a few seconds only to pull in some bash doc and dev packages that aren’t creating a GC root under.direnv.d(I am out of ideas why it’s just those two packages).
- put the
.direnvrcfile in your$HOMEdirectory. - put the
.envrc(and customize as necessary) to your project’s root, adjacent toshell.nixanddefault.nix.
- the direnv project wiki contains similar
use_nixoverrides but numerous bugs existed based on my requirements, so I put this together to aid my developer experience. Check them out for yourself to compare: https://github.com/direnv/direnv/wiki/Nix
Is there any advantages against https://github.com/target/lorri ?