complete-computing-environment/direnv.org

2.5 KiB

direnv

Before each prompt, direnv checks for the existence of a .envrc file (and optionally a .env file) in the current and parent directories. If the file exists (and is authorized), it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available to the current shell.

It supports hooks for all the common shells like bash, zsh, tcsh and fish. This allows project-specific environment variables without cluttering the ~/.profile file.

Because direnv is compiled into a single static executable, it is fast enough to be unnoticeable on each prompt. It is also language-agnostic and can be used to build solutions similar to rbenv, pyenv and phpenv.

It also supports Nix-shell, though looking at the wiki page makes me want to consider using a different tool like nix-direnv:

Those who have installed and are using the Nix package manager often use its command-line tool nix-shell to enter into a Nix shell, which is just a Bash session with environment variables set up for a project. We can configure Direnv to load environment variables from the Nix shell for a project. Nix has support for lots of different programming languages, so once we get Direnv integrated with Nix, we can do all of our language-specific configuration with Nix. Our .envrc can be short and simple.

Anyways it has an Emacs package which works really well and refreshes the process environment when swapping buffers.

(use-package direnv
 :config
 (direnv-mode))
(provide 'cce/direnv)
{pkgs, ...}:
{
  programs.direnv.enable = true;
  programs.direnv.nix-direnv.enable = true;
}
{ ... }: {
  nix.extraOptions = ''
    keep-outputs = true
    keep-derivations = true
  '';
}