Does nix compilation write temporary files to RAM or to disk?

Let’s assume that i want to install pkgs.gitMinimal with withSsh = true with minimal data written to my disk.

I could do this by

  1. installing pkgs.git (having a precompiled package with the overhead of unwanted features)
  2. doing an overlay with just the features i want, but thereby triggering a local compilation:
      (final: prev: {
      gitMinimal = prev.gitMinimal.override {
      withSsh = true;

Which method leads to the least amount of data written to my disk (taking temporary writes into account as well)? Does the overlay save data (install size is smaller) or is it actually worse due to temporary writes, build dependencies etc.?

This will depend on the package, the easiest way to determine it is to test, and compare disk space before and after. You may need to toggle a flag to keep build outputs.

Note that this won’t necessarily match writes, hard to tell if some files end up overwritten during a complex build but don’t if you switch to a smaller feature subset or such. It might also differ if you optimize the store. It’d be best to get stats via perf or such, but it can be tricky to capture exactly what you want.

That said, build dependencies and various outputs will end up stored on disk, yes. For other intermediate build outputs that don’t make it to the store, it depends. With default NixOS configuration, builds happen on disk too, since most systems don’t have enough RAM to handle large concurrent builds: NixOS Search

Whether that’s more or less depends on the package and whether deps are already in the store. That said, I think on average a couple MBs from extra features will be smaller than a build environment, even if you do this for a lot of packages.

Either is also likely going to be pretty insignificant compared to everything else a system will do over its lifetime, so you’ll need a pretty specialized use case for this to matter, at least at the scale of individual overrides. Upstream will likely also not carefully optimize to reduce your disk writes if you ever update, so even if you measure something now it might not stay that way over time.

If you really need to be economical about writes, consider remote deployment or a private cache, and perhaps a custom distro altogether.


Thank you for your answer and your reference to tmpfs! I didn’t know about it and it looks promising.

Given that by building git yourself you’d need a bunch of build-time dependencies that you likely wouldn’t have needed otherwise, I find a writes saving highly unlikely.

To answer the actual question however: Nix builds happen in the nix-daemon’s TMPDIR which is /tmp by default (running with root will use the user’s environment’s TMPDIR).

What’s the reason you need to be this economical with writes though?

It’s just out of curiosity, no necessity. I try to build a system as minimal as possible while learning more about Linux/NixOS along the way.

1 Like