Debugging Nix Expressions
I wanted to discuss some features of Nix that I don't see discussed very often related to debugging.
1
-L
-L
flag
I think this is still undocumented, but running any nix command with the
-L
-L
flag will print the logs to stdout.
2 Debugging flakes
Each derivation of a flake comes with a nix shell. That is:
nix develop .#packages.x86_64-linux.mypackage --ignore-env
nix develop .#packages.x86_64-linux.mypackage --ignore-env
will drop you into what is
nix
nix
env in your local directory without the sandboxing effects.
You can then run each of the phases (note: this is still impure):
$configurePhase
$buildPhase
$checkPhase
$configurePhase
$buildPhase
$checkPhase
This can be useful for initially debugging nix expressions, and is straight out of the nix manual
3 Inspecting the output
As mentioned in this awesome blog post,
breakpointHook
breakpointHook
and
cntr
cntr
are useful for inspecting build outputs.
In particular, on Linux, inserting
breakpointHook
breakpointHook
to the
*inputs
*inputs
for any phase will run the phase to completion or error, then provide a command for dropping into a shell containing the artifacts of the phase.
This is of course useful for debugging the
buildPhase
buildPhase
, but also can be used in other phases. For example, to inspect
checkPhase
checkPhase
output, inserting
breakpointHook
breakpointHook
into the
checkInputs
checkInputs
will pause the build at the end of the checkPhase. This isn't documented, but is quite useful.
One subtlety: how does cntr fit into this? It turns out there's another undocumented hook called
breakpointHookCntr
breakpointHookCntr
which will use
cntr
cntr
to drop into the derivation and allow for inspection with dev tooling that is already on the PATH (though, it will have to be tooling that is in the nix store). Note that the given
cntr
cntr
command will have to be run as root (I'm not sure how to give the
CAP_SYS_CHROOT
CAP_SYS_CHROOT
capability to a normal user).