- the default
direnv
Nix integration from projects loads a Nix shell every new terminal or every timeshell.nix
and/ordefault.nix
changes. 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
nixexprs
array in the Bash script above to ensure (see TODO comment in the.direnvrc
file), 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.d
in your dailykos project root thendirenv reload
and thennix-collect-garbage -d
- after collecting the nix “garbage”, you will then
rm -rf .direnv.d
and 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
.direnvrc
file in your$HOME
directory. - put the
.envrc
(and customize as necessary) to your project’s root, adjacent toshell.nix
anddefault.nix
.
- the direnv project wiki contains similar
use_nix
overrides 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 ?