Nix 101 Course / Section 5: Packaging real software with stdenv.mkDerivation
Premium

5.6 Debugging failing Nix builds

Lesson 35 of 68 · 10:15

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Debugging Failed Builds with keep-failed
  2. 2:37 Using --debug and nix debug for More Output
  3. 6:12 Breakpoint Hook and Interactive Sandbox Debugging
Transcript

Debugging Failed Builds with keep-failed

0:00 Okay, so in the last video we just built this package and patched it and tried to override all the different phases. With this we practically already have the right tools to build real software. And of course we still need to pin our inputs and we need to learn about the package functions and stuff like that. But let's keep looking at builds right now and discuss what we can do if they fail. Because imagine you are fed up of looking at this configure call, which takes like half a minute, or you build a package that builds for one hour and then it fails.

0:40 And then you fix something and then you need to wait for another hour to see why it fails now, which is no fun. What does Nix have for us here? There's a few very obvious things that we can do. We can for example use the keep failed parameter, these also work on the new commands, you can just find them in the man pages and they're also explained there in the Nix manual. And keep failed is interesting because let's say we inject the failure here, where we say configure phase equals, I don't know, exit one or something, so that it fails.

1:20 When we run Nix build, we get this fail immediately. And now the question is, if we didn't know that this is because we just injected a failure, but there is, I don't know, some compilation error or something strange, which we cannot explain to ourselves. Maybe the files look wrong now after patching them, but we need to access them. So the stuff is now gone, the sandbox is gone, we have no access to nothing, right? We can just look at the log again and again.

The full transcript comes with the lesson.

About this lesson

A package compiles for an hour, fails, you change one line, and wait another hour for the next error. This lesson shows the debugging tools Nix gives you for failing builds: --keep-failed, --debug, the NIX_DEBUG variable, and a hook that lets you log into the sandbox itself. Each one answers a different question, and you see all of them on a build with a deliberately injected failure.

In this video

  • Keeping the build directory after a failure, so you can inspect the files exactly as the crash left them
  • Finding out which phase a silent, seemingly stuck build is hanging in
  • Making the compiler and linker wrappers verbose: every flag, include folder, and link path that really reaches the tools, gold for linker errors and missing headers
  • The breakpoint hook: freezing a failed build and attaching a shell inside the still running sandbox to poke around
  • A plain bash trick for when you need to watch the build script variable by variable

After this lesson

The next time a build fails deep inside its phases, you stop rereading logs and guessing. You inspect the leftovers, attach to the sandbox, and rerun steps by hand until you find it.

Links: