Nix Anti-Patterns
40 recurring Nix mistakes that quietly cost teams time and money, and what to do instead.
Nix rewards teams that use it well and punishes those that don't.
The costly mistakes rarely look dangerous:
An unfiltered src works but causes countless rebuilds and cache misses.
A sleep in a NixOS test mostly works, then fails sometimes in CI under heavy load.
Overrides work well at first, then break after the next update and create a lot of work.
Anti-patterns spread from repository to repository faster than your seniors can review them, and every copy means pointless rebuilds, fewer cache hits, and the same support questions all over again.
This book shows you how to spot them and fix them for good.
Now in its second edition. At NixCon 2025 and Planet Nix 2026, we gave away printed copies of the first edition. The feedback was so good that we kept writing: This edition adds 14 new recipes, and it's the first one you can buy.
Why recipes?
This book takes its format from Herb Sutter's Exceptional C++ series. Few developers read programming books cover to cover, but they read these because each short item names a common mistake. You skim through, spot the pattern, and wonder whether it's hiding in your own code. Nobody wants to be caught red-handed with a named pattern. Nix Anti-Patterns brings that idea to Nix.
What's inside
- 40 anti-patterns in 8 chapters: Nix language idioms, evaluation purity, building software with Nixpkgs, package overrides, overlays, flakes, NixOS modules, and NixOS tests.
- Before and after, side by side. Compare the problematic code with the better version, then follow the explanation to see what goes wrong and why the fix is better.
- Short enough to actually read. 93 pages, one focused recipe at a time. Read one while having coffee, apply it the same afternoon.
- Distilled from real codebases. Since 2023, our Nixcademy trainings have helped companies adopt Nix from Germany to Singapore and across the US. These are the mistakes we see repeatedly.
Who it's for
- Developers who know the Nix basics and want their code to hold up in a growing team. Use this book to strengthen the code you already write.
- Senior engineers and reviewers who want one reference to point colleagues to, instead of explaining the same fix for the tenth time. Keep it handy for repeated reviews.
- Teams adopting Nix that want to stop the pitfalls before they spread
This is not an introduction to Nix: It assumes you can read and write basic Nix expressions. If you're just starting, begin with our video courses.
Table of contents
- Nix language idioms
- Minimize
withscopes - Avoid
@patterns - Avoid
//on nested attribute set updates - Reuse variables explicitly without
rec
- Minimize
- Evaluation purity
- Pin expression sources explicitly
- Evaluate Nixpkgs with explicit configuration
- Import Nixpkgs at one central place
- Building software with Nixpkgs
- Always filter
srcattributes - Remember to set
doCheck - Declare environment variables explicitly
- Also use
pnamewhen usingversion - Changing
makeFlagsArraycorrectly - Keep derivations ready for cross-compilation
- Use specialized flags instead of phase overrides
- Use
substituteInPlacefor safer string substitution - Derive dev shells from packages with
inputsFrom
- Always filter
- Overriding packages
- Write future proof
overrideAttrs - Use
overridefor inputs,overrideAttrsfor attributes - Expose feature flags as function arguments
- Avoid
pkgsincallPackage - Keep Python packages composable
- Use
finalAttrsinstead ofrecorlet in - Leave hooks when overriding phases
- Write future proof
- Nixpkgs overlays
- Overlay nested attributes correctly
- Use
overrideScopeto extend package scopes - Keep package overrides composable
- Flakes
- Derive default systems from
legacyPackages - Define Flake packages via an overlay
- Avoid helper flakes for system boilerplate
- Derive default systems from
- NixOS modules
- Parametrize modules with options
- Avoid
specialArgsfor better composability - Import modules by path
- NixOS tests
- Separate host and guest packages
- Double check service units and open ports
- Always set timeouts
- Fail fast when KVM is unavailable
- Start independent work on all nodes before waiting
- Use the
t.assert*helpers instead of plainassert - Share node configuration with
defaults - Wait for conditions instead of sleeping
About the authors
Written by Jacek Galowicz, active in the NixOS community since 2016, CEO of Applicative Systems Group/Nixcademy, and author of the C++17 STL Cookbook. He writes regularly for heise online and c't magazine and teaches software development to university students. Technical editing by Valentin Gagarin, founding member of the Nix documentation team and the Nix maintainer team.
Written by humans
Every word in this book is written by people who use Nix every day. We use Grammarly to fix language mistakes and LLMs only to check the text against the official documentation for contradictions.
DRM-free PDF, with every future update included. Not sure yet? Start with the free sample, which contains a few complete anti-pattern recipes.
Look inside
Sign up for free to read a few complete recipes right here.
Sign up to read the sample