These instructions describe how to get an interactive shell at the point in a derivation when the derivation fails.
This works in a Linux single-user installation of Nix.
Add the breakpointHook
package to the nativeBuildInputs
of a mkDerivation
.
nativeBuildInputs = [ breakpointHook ];
Install cntr
nix-env -i cntr
Run your Nix build command, and when it fails, the breakpointHook will output
a message like this:
build failed in installPhase with exit code 1
To attach install cntr and run the following command as root:
cntr attach -t command cntr-/nix/store/6vwxqrwq5h1fd3nw4mc61wgk7rppn2qw-jupyterlab-extended
So we run that command as root. The root user doesn’t have a PATH
to the
cntr
command which we installed in our Nix profile, so give it the full path to our single-user-installed cntr
.
sudo /home/$USER/.nix-profile/bin/cntr attach -t command cntr-/nix/store/6vwxqrwq5h1fd3nw4mc61wgk7rppn2qw-jupyterlab-extended
And now we’re in the sleeping Nix container. From here, run cntr exec
to fully load the build environment.
/home/$USER/.nix-profile/bin/cntr exec
The $TMPDIR
directory is where Nix has created the temporary directory for the build.
cd $TMPDIR
Now we are fully inside the context of the build, and any commands which we
enter in the shell will be as if we had entered that command on a line
in the phase (for example, buildPhase
) of our derivation when the derivation failed.
References
-
YouTube Jörg ‘Mic92’ Thalheim - About Nix sandboxes and breakpoints
-
There is a subsection on breakpointHook in the nixpkgs manual section 6.7 Package setup hooks