Can I set the nice level for Nix build processes?

I have found these two settings: NixOS Search

{
  nix.daemonNiceLevel = 19;
  nix.daemonIONiceLevel = 7;
}

But that doesn’t actually make the builds themselves use these nice values from what I’ve observed.
It appears to affect only the nix-daemon process itself.

Is there a way to also set nice for all build processes Nix daemon spawns?

1 Like

if the nix daemon is spawning the build, in theory the sub process should inherit the parents nice value… unless its spawning the builder in such a way that it’s not honouring this.

it also may/may not - be effected by autogroup … I’ll look into this later when i get a min to try it out.

autogroup is enabled by default on nix/OS …so it maybe a contributing factor?

Maybe I was checking this incorrectly but when I looked at htop the only process with nice value of 19 was nix-daemon.

Sorry for resurrecting this old thread, but I think this information will be valuable for others coming here just like me.

I’m using NixOS on desktop and, while it was doing some automated system upgrade today, my desktop got a bit frozen. So, I got here. The solution was for NixOS 20.09, quite old now.

Quoting from NixOS 21.11 release notes:

So, for more modern NixOS desktop usage, this module should make it snappier:

{
  nix.daemonCPUSchedPolicy = "idle";
  nix.daemonIOSchedClass = "idle";
}

Please read the options’ docs for details. I haven’t tested it intensively yet, FWIW.

1 Like

IME this does very little to improve desktop latency during load.

When rebuilding a lot, I usually run:
sudo watch --interval=5 ‘for i in $(seq 1 32); do renice 20 --pid `ps --no-heading -o tid --user nixbld$i`; done’

It improves desktop responsiveness somewhat.

I believe the issue here is that if you’re doing the builds via something like sudo nixos-rebuild switch then the build is being run as root, directly as a child of that sudo, not via a request to the daemon.

This is why I have the systemd equivalents of those scheduling directives also in my autoupgrade service unit, because that runs as root and the same issue applies.

Perhaps you can compare the effect when running a nixos-rebuild build first as a regular unprivileged user, and then a second switch as root once the closure has already been finished.