Auto-load a Nix flake dev shell with direnv
A project’s tools should appear when you enter its directory and vanish when you leave. direnv + nix-direnv does exactly that — this .envrc loads a named flake dev shell on cd.
# .envrc — auto-load a named Nix flake dev shell.
# Needs direnv + nix-direnv: https://github.com/nix-community/nix-direnv
if ! has nix_direnv_version || ! nix_direnv_version 3.0.0; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.0/direnvrc" \
"sha256-21TMnI2xWX7HkSTjFFri2UaohXVj854mgvWapWrxRXg="
fi
# Loads devShells.<system>.myshell — drop the suffix for the default shell.
use flake .#myshell
# Untracked local overrides, if any.
[ -f .envrc.local ] && source_env .envrc.local
Run direnv allow once; after that the shell rebuilds itself whenever flake.nix changes.
If direnv complains hash mismatch, the pinned source_url hash is stale — recompute it:
curl -sL <direnvrc-url> | openssl dgst -sha256 -binary | base64