I find this is a common complaint, and there doesn’t seem to be a completely satisfactory answer.
The goal here is to: When using Nix in a desktop environment, the build process will not cause
Any part of the desktop environment or other running software is forcibly exited (OOMed)
Other simple UI operations can still be completed smoothly, such as opening a new file manager or using a browser. IO-related operations can still continue.
Obviously, it shouldn’t freeze completely or crash the desktop session.
{
nix = {
daemonIOSchedClass = lib.mkDefault "idle";
daemonCPUSchedPolicy = lib.mkDefault "idle";
};
# put the service in top-level slice
# so that it's lower than system and user slice overall
# instead of only being lower in system slice
systemd.services.nix-daemon.serviceConfig.Slice = "-.slice";
# always use the daemon, even executed with root
environment.variables.NIX_REMOTE = "daemon";
}
Lowering max-jobs and max-cores could help with builds as well, though it won’t help with eval.
Based on my experience I’d say the system should have 8 gigs of RAM minimum and 16 gigs to feel comfortable in general.
Also keeping a web browser (including electron apps) open will massively increase the likelihood of OOMing.
There is cgroup support in Nix which in combination with systemd-oomd could help protect the rest of the system from a single build causing random OOMs. This way OOM will only kill the build causing a lot of memory usage instead of random processes
Enable-cgroups nix setting causes it to create a cgroup per build. It’s relatively new. And only the version in the latest nix version (or maybe master?) has the cgroup tree in the right shape such that systemd works
It depends on what you are evaluating. Evaluating all of nixpkgs, for example, took about 12 gigs last time I tried it (which doesn’t leave much memory for the system with only 16 gigs…). Same with builds, if I try to do a world rebuild of my system’s closure, I will even OOM on a 64 gig system unless I limit jobs.