Logging custom info during (failing) build

I have a CI based on Nix and I am searching for a way to provide error logs for the users. In this CI, we have derivations where the build phase in fact runs our tests (basically, we have a 1st derivation that is the build of the system we want to test, and then different additional derivations each dedicated to the tests of a particular module of the entire system).

The execution of the tests logs a lot of things. Most of them are generally not useful for most users (for example all the aspects related to Nix, or commands used to compute the coverage …), but it might be in some situations (for example when I break things in the Nix files :sweat_smile: ) and crawling the log file is not particularly user-friendly. However, I know how to build less detailed logs for the user, using Junit XML format + the viewer embedded in GitLab.

My problem is that I do not know how to log additional information during the execution of the nix build and how to gather them once the build has failed. For successful builds, it is easy: I specify an output directory and I install the log in this directory, but for failed builds it seems to me that there is no way to log custom information.

What can I do?