nix: use /var/tmp/ during builds

This commit is contained in:
Casper V. Kristensen 2024-09-29 21:39:31 +02:00
parent 17ae962b1c
commit 7a3535374a
3 changed files with 39 additions and 0 deletions

View file

@ -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;

View file

@ -1,5 +1,6 @@
{...}: {
imports = [
./neovim.nix
./nixos-rebuild.nix
];
}

View file

@ -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
'';
});
})
];
}