Skip to content
Oeiuwq Faith Blog OpenSource Porfolio

nix-community/nix-direnv

A fast, persistent use_nix/use_flake implementation for direnv [maintainer=@Mic92 / @bbenne10]

nix-community/nix-direnv.json
{
"createdAt": "2019-09-27T07:43:47Z",
"defaultBranch": "master",
"description": "A fast, persistent use_nix/use_flake implementation for direnv [maintainer=@Mic92 / @bbenne10] ",
"fullName": "nix-community/nix-direnv",
"homepage": "",
"language": "Shell",
"name": "nix-direnv",
"pushedAt": "2025-11-24T04:07:01Z",
"stargazersCount": 2416,
"topics": [
"hacktoberfest",
"managed-by-renovate",
"nix-community-buildbot"
],
"updatedAt": "2025-11-25T07:15:35Z",
"url": "https://github.com/nix-community/nix-direnv"
}

Test

A faster, persistent implementation of direnv’s use_nix and use_flake, to replace the built-in one.

Prominent features:

  • significantly faster after the first run by caching the nix-shell environment
  • prevents garbage collection of build dependencies by symlinking the resulting shell derivation in the user’s gcroots (Life is too short to lose your project’s build cache if you are on a flight with no internet connection)

Compared to lorri, nix-direnv is simpler (and requires no external daemon). Additionally, lorri can sometimes re-evaluate the entirety of nixpkgs on every change (leading to perpetual high CPU load).

Requirements:

  • bash 4.4
  • nix 2.4 or newer
  • direnv 2.21.3 or newer

[!WARNING]
We assume that direnv is installed properly because nix-direnv IS NOT a replacement for regular direnv (only some of its functionality).

[!NOTE]
nix-direnv requires a modern Bash. MacOS ships with bash 3.2 from 2007. As a work-around we suggest that macOS users install direnv via Nix or Homebrew. There are different ways to install nix-direnv, pick your favourite:

Via home-manager (Recommended)

Note that while the home-manager integration is recommended, some use cases require the use of features only present in some versions of nix-direnv. It is much harder to control the version of nix-direnv installed with this method. If you require such specific control, please use another method of installing nix-direnv.

In $HOME/.config/home-manager/home.nix add

{
# ...other config, other config...
programs = {
direnv = {
enable = true;
enableBashIntegration = true; # see note on other shells below
nix-direnv.enable = true;
};
bash.enable = true; # see note on other shells below
};
}

Check the current Home Manager Options for integration with shells other than Bash. Be sure to also allow home-manager to manage your shell with programs.<your_shell>.enable = true.

Direnv's source_url

Put the following lines in your .envrc:

Terminal window
if ! has nix_direnv_version || ! nix_direnv_version 3.1.0; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.1.0/direnvrc" "sha256-yMJ2OVMzrFaDPn7q8nCBZFRYpL/f0RcHzhmw/i6btJM="
fi
Via system configuration on NixOS

For NixOS 23.05+ all that’s required is

{
programs.direnv.enable = true;
}

other available options are:

{ pkgs, ... }: {
#set to default values
programs.direnv = {
package = pkgs.direnv;
silent = false;
loadInNixShell = true;
direnvrcExtra = "";
nix-direnv = {
enable = true;
package = pkgs.nix-direnv;
};
}
With `nix profile`

As non-root user do the following:

Terminal window
nix profile install nixpkgs#nix-direnv

Then add nix-direnv to $HOME/.config/direnv/direnvrc:

Terminal window
source $HOME/.nix-profile/share/nix-direnv/direnvrc
From source

Clone the repository to some directory and then source the direnvrc from this repository in your own ~/.config/direnv/direnvrc:

Terminal window
# put this in ~/.config/direnv/direnvrc
source $HOME/nix-direnv/direnvrc

Either add shell.nix or a default.nix to the project directory:

# save this as shell.nix
{ pkgs ? import <nixpkgs> {}}:
pkgs.mkShell {
packages = [ pkgs.hello ];
}

Then add the line use nix to your envrc:

Terminal window
echo "use nix" >> .envrc
direnv allow

If you haven’t used direnv before, make sure to hook it into your shell first.

You may use a different file name than shell.nix or default.nix by passing the file name in .envrc, e.g.:

Terminal window
echo "use nix foo.nix" >> .envrc

nix-direnv also comes with an alternative use_flake implementation. The code is tested and does work but the upstream flake api is not finalized, so we cannot guarantee stability after a nix upgrade.

Like use_nix, our use_flake will prevent garbage collection of downloaded packages, including flake inputs.

This repository ships with a flake template. which provides a basic flake with devShell integration and a basic .envrc.

To make use of this template, you may issue the following command:

Terminal window
nix flake new -t github:nix-community/nix-direnv <desired output path>
Terminal window
echo "use flake" >> .envrc && direnv allow

The use flake line also takes an additional arbitrary flake parameter, so you can point at external flakes as follows:

Terminal window
use flake ~/myflakes#project

Under the covers, use_flake calls nix print-dev-env. The first argument to the use_flake function is the flake expression to use, and all other arguments are proxied along to the call to print-dev-env. You may make use of this fact for some more arcane invocations.

For instance, if you have a flake that needs to be called impurely under some conditions, you may wish to pass --impure to the print-dev-env invocation so that the environment of the calling shell is passed in.

You can do that as follows:

Terminal window
echo "use flake . --impure" > .envrc
direnv allow

Like use flake, use nix now uses nix print-dev-env. Due to historical reasons, the argument parsing emulates nix shell.

This leads to some limitations in what we can reasonably parse.

Currently, all single-word arguments and some well-known double arguments will be interpreted or passed along.

By default, nix-direnv will reload a previously working devShell if it discovers that a new version does not evaluate. This can be disabled by calling nix_direnv_disallow_fallback in .envrc, like so:

Terminal window
nix_direnv_disallow_fallback
use nix # or use flake

To avoid delays and time consuming rebuilds at unexpected times, you can use nix-direnv in the “manual reload” mode. nix-direnv will then tell you when the nix environment is no longer up to date. You can then decide yourself when you want to reload the nix environment.

To activate manual mode, use nix_direnv_manual_reload in your .envrc like this:

Terminal window
nix_direnv_manual_reload
use nix # or use flake

To reload your nix environment, use the nix-direnv-reload command:

Terminal window
nix-direnv-reload
  • -p: Starts a list of packages to install; consumes all remaining arguments
  • --include / -I: Add the following path to the list of lookup locations for <...> file names
  • --attr / -A: Specify the output attribute to utilize

--command, --run, --exclude, --pure, -i, and --keep are explicitly ignored.

All single word arguments (-j4, --impure etc) are passed to the underlying nix invocation.

As a convenience, nix-direnv adds common files to direnv’s watched file list automatically.

The list of additionally tracked files is as follows:

  • for use nix:

    • ~/.direnvrc
    • ~/.config/direnv/direnvrc
    • .envrc,
    • A single nix file. In order of preference:
      • The file argument to use nix
      • default.nix if it exists
      • shell.nix if it exists
  • for use flake:

    • ~/.direnvrc
    • ~/.config/direnv/direnvrc
    • .envrc
    • flake.nix
    • flake.lock
    • devshell.toml if it exists

Users are free to use direnv’s builtin watch_file function to track additional files. watch_file must be invoked before either use flake or use nix to take effect.

nix-direnv sets the following environment variables for user consumption. All other environment variables are either a product of the underlying nix invocation or are purely incidental and should not be relied upon.

  • NIX_DIRENV_DID_FALLBACK: Set when the current revision of your nix shell or flake’s devShell are invalid and nix-direnv has loaded the last known working shell.

nix-direnv also respects the following environment variables for configuration.

  • NIX_DIRENV_FALLBACK_NIX: Can be set to a fallback Nix binary location, to be used when a compatible one isn’t available in PATH. Defaults to config.nix.package if installed via the NixOS module, otherwise needs to be set manually. Leave unset or empty to fail immediately when a Nix implementation can’t be found on PATH.
  • [Changing where direnv stores its cache][cache_location]
  • [Quickly setting up direnv in a new nix project][new_project]
  • [Disable the diff notice (requires direnv 2.34+)][hide_diff_notice] !: Note that this goes into direnv’s TOML configuration!

[cache_location] !: https://github.com/direnv/direnv/wiki/Customizing-cache-location [new_project] !: https://github.com/nix-community/nix-direnv/wiki/Shell-integration [hide_diff_notice] !: https://direnv.net/man/direnv.toml.1.html#codehideenvdiffcode

  • direnv-instant - A non-blocking daemon that makes direnv truly instant by running it asynchronously in the background. When combined with nix-direnv’s caching, it provides immediate shell access while environment loading happens in the background, with automatic notifications when the environment is ready.

For commercial support, please contact Mic92 at joerg@thalheim.io or reach out to Numtide.