Interactive debugging of failed builds

Is there a nix command (either old or new) that attempts to build a derivation, and, if this fails, it drops you into an interactive shell with the failed build’s environment? As-if you had manually typed the commands for all the steps up to the point where the build failed.

If this existed, it would allow people to debug build failures without having to remind themselves of all the steps they need to take to unpack, patch, etc. Compare sourcehut’s CI runners letting you ssh into the build VM if the build fails, which is such a killer feature that I tell people to use sourcehut just for that.

3 Likes

The breakpointHook

2 Likes

To use that, it appears to be necessary to modify the nativeBuildInputs of the derivation that’s being debugged. Is there any way to get the same effect from the nix command line, without modifying the derivation at all?

I don’t think so. PR welcome :laughing:

Investigating a little more, nix build already has a --debugger option that does what I’m looking for, but it doesn’t seem to be available to the traditional nix-* commands. I do not have time in the foreseeable future to write a patch myself. However, I filed an issue so this doesn’t get forgotten about: Make `nix build --debugger` available in `nix-build` as well · Issue #16181 · NixOS/nix · GitHub

That flag is for the evaluation of nix language.

2 Likes

Ugh, I keep forgetting that when the nix manual says “evaluation” it means just evaluation of nix expressions and not any intertwined build operations. I revised the issue.

It’s not quite what you want, but e.g.

nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/refs/heads/master.tar.xz --expr 'let pkgs = import <nixpkgs> {}; in pkgs.hello'

and then running

(dontInstall=1 doInstallCheck= genericBuild)

gets the result I think?

The parentheses are a safeguard against exiting the shell on failure; I don’t know why it does that sometimes. As far as I can tell set -o shows errexit: off

EDIT: I should also specify that genericBuild is a stdenv thing, but most packages are written with stdenv so this is still broadly applicable

1 Like

right, there is a section in the manual around this kind of thing: Nixpkgs Reference Manual

this is my typical workflow, just play with the phases variable, i.e. get to build and then repeatedly run build. ofc, there are some classes of error that this won’t cover, but it get most of them. There is also nix-build --keep-failed which is often good enough to get through trivial problems.

1 Like