General question: how to avoid running out of memory or freezing when building Nix derivations

How so? The closest I found (4 years old, to be fair) was

Did you mean something like this?