Nixos-rebuild using /nix/var/nix/builds instead of /tmp for intermediate build outputs?

Is there a way to tell nix where intermediate build outputs should go when it needs to compile software that isn’t available from a binary cache? According to “the internet”, it should be using /tmp with no way to change that (which is exactly what I want!), but in my case it throws everything into a few sub-directories of /nix/var/nix/builds/ instead.

Still fairly new to NixOS, so apologies for this basic question, but I wasn’t able to find anything about this on the internet…

I’m using flakes with nixos-unstable and I’ve also set boot.tmp.useTmpfs = true. I’m aware that that’s discouraged because build outputs can grow fairly large, but in my case I explicitly want to use a tmpfs for those, because I have 4 times more RAM than available disk space (don’t ask… :sweat_smile:) and I have no desire to compile chromium from source :stuck_out_tongue:
I also have everything on ZFS with frequent automatic snapshots, so I would really like to avoid dumping gigabytes of temporary files into a dataset and bloating the snapshots.

New versions of Nix changed build-dir from $TMPDIR (usually /tmp) to /nix/var/nix/builds.

You can change this behaviour by setting build-dir in /etc/nix/nix.conf or on the terminal via --option build-dir /tmp

4 Likes

Wonderful, thanks you so much, that did the trick! And now it’s on the internet aswell :smiley:

I’m not up to date on this. I hope that with this change /nix/var/nix/build was made a tmpfs in NixOS, otherwise it will be very bad for anyone with /nix on a slow disk.

At least with nix 2.31.2 it’s not a tmpfs by default and I can’t imagine it becoming one, since the setting is nix-specific but a tmpfs mount would be OS-specific configuration.

I actually ended up keeping /nix/var/nix/build but making it a tmpfs myself instead of going back to /tmp, because nixos-rebuild refused to use /tmp/nix/builds, complaining that a parent-directory of build-dir was world-writable (which /tmp kinda has to be…)

  fileSystems."/nix/var/nix/builds" = {
    device = "tmpfs";
    fsType = "tmpfs";
    options = [
      "size=40G"  # adjust for your situation and needs
      "mode=700"
    ];
  };
2 Likes

I went for 755 here since usually I’d want to inspect this stuff with keep-failed, but that’s basically what I did as well. (Though I explicitly set nix.settings.build-dir so I could use it in this definition.)