Computer crashes when updating system

I haven’t updated my NixOS system in a long time, so I was surprised by the size of the update when I started it today.

I used the following commands:

sudo nix flake update
sudo nixos-rebuild switch --upgrade --flake .

After leaving my computer for a while, I returned to find it had frozen. The fans were running, but the screen was stuck on the package building phase. I had to force a shutdown by holding the power button.

Upon restarting, I ran the update again, this time monitoring the process with btop. I noticed that the build process was consuming all of my available RAM, which leads me to believe the crash was caused by an out-of-memory (OOM) error.

I found a suggestion online to limit the resource usage with the --max-jobs 1 flag:

sudo nixos-rebuild switch --upgrade --max-jobs 1 --flake .

but this still does not really help. I have not run out of memory yet but resource usages are still really high.
Here is a btop screenshot showing the memory usage even with the flag enabled:
image

I’ve never encountered this issue before. Can anyone help me resolve this?

Seems like you get stuck while compiling a C++ program from source, using all CPU cores. You can set the number of cores available per job with the --cores option. I would try with 5 first and see how it goes.

Doesn’t solve your issue, but stop using --upgrade, it does nothing useful for you.

Along with what dsparby said above, you can use nh cli https://github.com/nix-community/nh and see what your nixos system build is getting stuck on.

In this case it is not possible for you to have posted a snippet of code or provide a specific package, to receive help.
But in general if your dotfiles repo is public you can permalink to things when asking support questions.

And ofcourse if you ever make it public keep your secrets encrypted first with sops-nix or some other alternative method that suits you best.

Try:

sudo nix-store --verify --check-contents --repair
and rebuild