2.4 Debugging Nix with trace and breakpoints
Lesson 11 of 68 · 9:23
Video chapters
-
0:00 Introduction to debugging Nix expressions
-
0:54 Using builtins.trace for quick value inspection
-
3:06 Using builtins.break and the Nix debugger
-
7:35 Inspecting scopes, backtraces, and continuing execution
Transcript
Introduction to debugging Nix expressions
0:00 So before we write more complex Nix expressions, let's have a quick look how to debug Nix expressions because we will be like in let in scopes, in let in scopes, in let in scopes, maybe in recursion. And then how do I look at variables in a big Nix expression? So you have two easy ways to do this. At first, there is tracing. So we can generally write builtins.trace() and that is a function that accepts two parameters.
0:32 One is the string that is going to be traced. Tracing is basically like printing, but it means that we as the developer who runs something in the terminal, be that either the REPL or any other command, will see the output on the terminal, but it's only like a side effect that does nothing to the program. So this will kind of print it, but the program will behave as if there was no printing.
Using builtins.trace for quick value inspection
0:59 And then the second parameter that is not being printed, but that is being returned. So the whole block here, builtins.trace() and then the string and then this x+x thingy will behave as if there was only x+x in the program. All the noise around it, the trace and the string, will only be visible on the terminal later for the programmer. And again, not to change the program. If we enter this in the REPL, it will look like this here. We basically get this tracing here and that is just for you as a developer.
The full transcript comes with the lesson.
About this lesson
Sooner or later a value buried in nested let blocks is not what you expect, and you need a way to look inside. This lesson covers debugging Nix expressions with builtins.trace and the built-in breakpoint debugger, the two tools you will reach for whenever a Nix expression misbehaves.
In this video
builtins.trace: printing values as a side effect while the program behaves as if nothing happened- A quiz about trace output order, and what the answer reveals about lazy evaluation
- Why leftover trace calls annoy everyone who imports your code, and the quieter alternative
- Setting breakpoints with
builtins.breakand activating them with--debugger - Inspecting variable scopes, stepping, continuing and reading backtraces from a REPL inside the REPL
After this lesson
When a value deep in a Nix expression looks wrong, you can print it or pause right there and poke around, in the REPL and in nix build alike.