From 7a3535374a1c60ac4cfa7b3cf07529b3359f3fe4 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sun, 29 Sep 2024 21:39:31 +0200 Subject: [PATCH] nix: use /var/tmp/ during builds --- modules/base/nix.nix | 13 +++++++++++++ overlays/default.nix | 1 + overlays/nixos-rebuild.nix | 25 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 overlays/nixos-rebuild.nix diff --git a/modules/base/nix.nix b/modules/base/nix.nix index 28c1588..cb4fe42 100644 --- a/modules/base/nix.nix +++ b/modules/base/nix.nix @@ -50,6 +50,19 @@ frequency = config.nix.gc.dates; }; + # Nix uses /tmp/ (tmpfs) during builds by default. This may cause 'No space + # left on device' errors with limited system memory or during big builds. Set + # the Nix daemon to use /var/tmp/ instead. Note that /var/tmp/ should ideally + # be on the same filesystem as /nix/store/ for faster copying of files. + # https://github.com/NixOS/nixpkgs/issues/54707 + # + # NOTE: This does not change the directory for builds during `nixos-rebuild`. + # See overlays/nixos-rebuild.nix for workaround. + # https://github.com/NixOS/nixpkgs/issues/293114 + systemd.services.nix-daemon = { + environment.TMPDIR = "/var/tmp"; + }; + # Run unpatched dynamic binaries on NixOS. # https://github.com/Mic92/nix-ld programs.nix-ld.enable = true; diff --git a/overlays/default.nix b/overlays/default.nix index 0042543..db6f818 100644 --- a/overlays/default.nix +++ b/overlays/default.nix @@ -1,5 +1,6 @@ {...}: { imports = [ ./neovim.nix + ./nixos-rebuild.nix ]; } diff --git a/overlays/nixos-rebuild.nix b/overlays/nixos-rebuild.nix new file mode 100644 index 0000000..d2c3e38 --- /dev/null +++ b/overlays/nixos-rebuild.nix @@ -0,0 +1,25 @@ +{...}: { + # The Nix daemon's temporary build directory is changed from /tmp/ to + # /var/tmp in modules/base/nix.nix, but it is only respected by `nix build`, + # not `nixos-rebuild`. + # This overlay wraps `nixos-rebuild` to explicitly set TMPDIR=/var/tmp. + # https://github.com/NixOS/nixpkgs/issues/293114 + nixpkgs.overlays = [ + (final: prev: { + # `overrideAttrs`, instead of simply overriding the `nixos-rebuild` + # package, to ensure `nixos-rebuild.override`, which is used in NixOS, + # works and is overridden. + # https://wiki.nixos.org/wiki/Nix_Cookbook#Wrapping_packages + # TODO: There must be a better way to do this? + nixos-rebuild = prev.nixos-rebuild.overrideAttrs (oldAttrs: { + nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [prev.makeWrapper]; + postInstall = + oldAttrs.postInstall + + '' + wrapProgram $out/bin/nixos-rebuild \ + --set TMPDIR /var/tmp + ''; + }); + }) + ]; +}