diff --git a/guix/utils.scm b/guix/utils.scm
index b61ff2477d93af8823c4bda1b9e9609ffb5b3566..34a5e6c9714ce7f655e6275addba01fdf0518eaf 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -600,8 +600,9 @@ (define (call-with-temporary-output-file proc)
   "Call PROC with a name of a temporary file and open output port to that
 file; close the file and delete it when leaving the dynamic extent of this
 call."
-  (let* ((template (string-copy "guix-file.XXXXXX"))
-         (out      (mkstemp! template)))
+  (let* ((directory (or (getenv "TMPDIR") "/tmp"))
+         (template  (string-append directory "/guix-file.XXXXXX"))
+         (out       (mkstemp! template)))
     (dynamic-wind
       (lambda ()
         #t)