How to run nix commands such as `nix develop`, `nix shell`, and `nixos-rebuild` without making all programs freeze?

I just wanted a quick Rust dev shell, so I ran

nix shell github:oxalica/rust-overlay

and while Nix was building the packages, for some time, all of the programs just froze (probably because the nix commands were using up 100% of the CPU). The same thing happens when running nixos-rebuild switch or home-manager switch after updating the flake. This was very annoying, as it means that my laptop is basically unusable while installing new Nix packages. Is there any fix / planned fix for this? Is there a way of limiting the concurrency / CPU usage so that all 8 cores do not get used for building packages at the same time?

I am using a laptop with i7-10610U and 16GB of RAM, so I think it would be a very smooth experience if the nix commands used just 4/8 cores instead of 8/8 cores.

You can try Tuning Cores and Jobs - Nix Reference Manual

2 Likes

So if I only want to use up to 4 cores, I need to make sure that max-jobs * cores = 4. This will be slow since there are tradeoffs between 1/4, 2/2, and 4/1 combinations of the two settings, but at least I can do other things while Nix is building packages.