Direnv

Direnv allows you to execute code when moving around directories in the shell.

Paired with Nix, we can use it to seamlessly enter reproducible development shells.

Enable direnv in your shell

  • system-wide with NixOS or user-wide with Home Manager
    programs.direnv = {
      enable = true;
      nix-direnv.enable = true;
    };
  • other systems with Nix
    $ nix profile install nixpkgs\#{direnv,nix-direnv}
    $ echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
    $ echo 'eval "$(direnv hook zsh)"' >> ~/.zshrc
    $ mkdir -p ~/.config/direnv
    $ echo 'source $HOME/.nix-profile/share/nix-direnv/direnvrc' >> ~/.config/direnv/direnvrc

Use direnv in a project with a flake

Note

Ensure that you have Flakes enabled.

$ echo use flake > .envrc
$ direnv allow
Links to this page