I would first try with nix-shell --pure
since it overrides all the environment variables. Sometimes it’s possible to reproduce the error that way. It doesn’t sandbox the build though.
Then the next best thing that I know of is only available on Linux: add breakpointHook
to the derivation buildInputs
. Once the build fails it will halt and display the instructions to run cntr
and enter the build sandbox directly.