Changing nix-build's TMPDIR from shell.nix

I’m trying to use nix-shell with a shell.nix file to get a clean development environment but I don’t know how to change the location of the temporary build directories.

The buildInputs packages are built in /tmp but this path doesn’t have enough space and I get an error: [Errno 28] No space left on device during the build.

I tried running nix-shell with a modified TMPDIR environment variable but it only affects the location of the nix-shell temporary files. The nix-build files are still put in /tmp.

I also tried to export a new value for TMPDIR in the shellHook but it doesn’t work.

How can I change the TMPDIR of nix-build when it’s started by nix-shell?

Here’s my shell.nix:

  pkgs = import <nixpkgs> {};
  pkgs.mkShell {
    name = "something";
    buildInputs = with pkgs; [

    shellHook = ''

I’m not using NixOS.

1 Like

If this is a mutli-user install, you need to modify the Nix daemon’s TMPDIR.