← Garden

Per-project dev shells with Nix and direnv

Published
Nov 29, 2026
Reading
4 min read
Tags
#nix#linux#tooling

I use NixOS. I could install compilers and language tools system-wide, but I keep them out of my system config instead. Each project defines its own dev shell with the compilers, libraries and tools it needs, and direnv loads that shell when I cd into the project.

A few reasons for doing it this way:

  • Projects can use different versions of the same tool without clashing.
  • The setup lives in the repo. Cloning it on another machine gives the same environment, and anyone else working on it gets it too.
  • My system config stays small. When I stop working on a project, its tools aren’t left installed.

I set these up in two ways: a plain flake, or devenv.

Flake

A sample flake.nix for a Rust project:

flake.nix
{
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
outputs = {nixpkgs, ...}: let
systems = ["x86_64-linux" "aarch64-linux"];
forAll = f: nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
in {
devShells = forAll (pkgs: {
default = pkgs.mkShell {
# tools used while building
nativeBuildInputs = with pkgs; [
cargo
rustc
rustfmt
clippy
rust-analyzer
pkg-config
];
# libraries the project links against
buildInputs = with pkgs; [
openssl
];
# other command-line tools for the project
packages = with pkgs; [
just
];
# lets rust-analyzer find the standard library source
RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}";
};
});
};
}

forAll builds the same shell for each system in the list, so it works on both x86 and ARM machines. nix flake update updates the pinned nixpkgs version in flake.lock.

To load it with direnv:

.envrc
use flake

Run direnv allow once. After that the shell loads on cd.

nix-direnv is worth installing too. It caches the shell, so it only rebuilds when flake.nix or flake.lock changes, and it stops nix-collect-garbage from deleting it.

devenv

devenv is built on Nix and has options for languages, services, processes and git hooks. I use it for app projects where a flake would need a lot of setup.

This one is from a Python project using uv:

devenv.nix
{ pkgs, lib, ... }:
{
languages.python = {
enable = true;
package = pkgs.python312;
uv = {
enable = true;
sync.enable = true; # run `uv sync` on shell entry
};
};
packages = [ pkgs.ffmpeg-headless ];
env = {
LD_LIBRARY_PATH = lib.makeLibraryPath [ pkgs.stdenv.cc.cc pkgs.zlib ];
UV_NO_MANAGED_PYTHON = true;
};
}

And one with a Go API and a web app, each run as a process:

devenv.nix
{
languages.go.enable = true;
languages.javascript = {
enable = true;
npm.enable = true;
};
processes.api.exec = "cd api; go run ./cmd/api";
processes.app.exec = "cd app; npm run dev";
}

devenv up starts both.

To load it with direnv:

.envrc
eval "$(devenv direnvrc)"
use devenv

I use a flake when a project only needs compilers and libraries, and devenv when it also needs services, processes or git hooks.

Things to watch out for

Libraries loaded at runtime

When Nix builds a program, it records the store path of every library the program links against, so the program finds them without any setup. That covers libraries listed at link time. It doesn’t cover two cases:

  • Libraries opened with dlopen. Some programs load a library by name while they run. Many Rust crates do this for Wayland and Vulkan. The program builds fine, then fails on start because nothing told it where libwayland-client.so is.
  • Binaries not built by Nix. Prebuilt Python wheels expect libstdc++ and zlib in the usual system folders, which NixOS doesn’t have.

LD_LIBRARY_PATH fixes both by telling the loader where else to look. In a flake:

LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath (with pkgs; [
wayland
vulkan-loader
]);

This only applies inside the dev shell, for things like cargo run. A program installed as a Nix package needs it set some other way. My desktop shell runs through Amane’s amane command, and Amane’s Nix package wraps it in a script that sets LD_LIBRARY_PATH for Wayland and Vulkan before it starts.

uv’s own Python

uv downloads its own Python builds by default, and those don’t work on NixOS. Set UV_NO_MANAGED_PYTHON so it uses the Python from Nix.

Tools outside the shell

direnv only hooks into your interactive shell. Editors, scripts and AI coding agents might not get the environment. Use direnv exec:

Terminal window
direnv exec . cargo build

Git hooks

Pre-commit hooks that run rustfmt or eslint need those tools on PATH, so commit from inside the shell or with direnv exec . git commit.

If the repo keeps its hooks in a folder, a shellHook in the flake can enable them for anyone who enters the shell:

shellHook = ''
if [ -d .git ]; then
git config core.hooksPath .githooks
fi
'';

.gitignore

.gitignore
.direnv/
.devenv/