reading-errors.md

Reading Nix errors

Before this, you'll want

  1. NixOS configuration
  2. Try Nix without installing
The Nix language13 min

Nix errors have a bad reputation, and it is half deserved. They can be long, quote files you have never opened, and put the useful sentence somewhere near the end.

Treat the trace as a route. It says where evaluation started, which code it travelled through, and where it finally gave up. You are looking for two things: what broke, and the nearest line of your code.

Start with the sentence after error:

The short version can be mercifully direct. Click the line that names what went wrong, not the punctuation pointing at it.

Find itPoint at the sentence that explains the failure.

$ nix eval --expr 'let greet = name: "hello ${name}"; in greet person'

Do not begin by decoding every frame. Begin with the cause, then find the place that belongs to you.

Skip dependency roads, not your destination

A NixOS error travels through the module system before returning to your file. Store paths are usually the road. /etc/nixos and paths under your project are usually where you can act.

Find itPoint at the line that tells you which file to edit.

$ nixos-rebuild switch

A flake source can also be copied under /nix/store/...-source, so do not throw away every store path blindly. Read the source name and look for the last frame you can map back to code you own.

Option errors list every definition

NixOS often prints Definition values after the cause. The message sits above the list. The file you need sits inside it.

Find itPoint at the sentence that names the mistake.

$ nixos-rebuild switch

services.opensh should have been services.openssh. Precise option checking turns a typo into a loud failure instead of a silent, insecure surprise.

Ask for more trace only when something is missing

--show-trace removes the trace filter. It gives you more route, not a smarter diagnosis.

PickWhen is the full trace worth asking for?

If the cause and your file are already visible, the filtered trace has done its job.

Make the failure small

Once you have the sentence and the line, stop scrolling. Copy the failing shape into a tiny expression, replace unrelated values, and keep deleting while the same kind of failure remains.

Try it

Make port a number, so adding one produces 3001.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

A failure this size is one you can reason about, search for, or report. Your whole machine configuration is not a useful minimal example.

Missing attributes are failed lookups

When Nix says an attribute is missing, inspect the set immediately to the left of the final dot.

Try it

Select the attribute the set actually has, so the result is 3000.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

For a.b.c, a failure at c means a.b exists but does not have the shape you assumed.

Type errors show where a value stopped fitting

A type error is the most honest message Nix produces. It names the kind of value a function or option wanted, and the kind it got instead.

Try it

Make both values numbers, so the result is 42.

nix replevaluated as you typeNixA plain Nix expression. Nothing from nixpkgs or a module system is supplied.

Find where the wrong value entered the function or option and fix it there. Do not patch some later expression merely because it makes the message move.

What to keep

  • Start with the cause after error:, then find the nearest source line you own.
  • Dependency frames show the route. Project and /etc/nixos paths are destinations.
  • Option errors put the message above Definition values and the file inside it.
  • Use --show-trace only when the filtered trace never returns to your code.
  • A missing attribute is a failed lookup. A type error means a value did not fit.
  • Shrink the failure before you try to solve it.

Nix errors stay long. They stop being frightening once you know that most of the route was never asking you to edit it.

Useful links

NORMALCOURSE IN BETA