Subject: bug#25242: Cannot build source derivations with a custom TMPDIR

From: [email-masked] (Ludovic Courtès)
Date: Wed, 21 Dec 2016 10:20:20 +0100

Leo Famulari <[email-masked]> skribis:

> On Wed, Dec 21, 2016 at 03:22:40AM -0500, Leo Famulari wrote:
>> I ran the guix-daemon with strace, and I see these relevant lines:
>> 
>> 15337 [pid 30675] mkdir("/home/leo/tmp/guix-build/guix-build-nmap-7.40.tar.bz2.drv-0", 0700) = 0
>> 15338 [pid 30675] getegid()                   = 0
>> 15339 [pid 30675] chown("/home/leo/tmp/guix-build/guix-build-nmap-7.40.tar.bz2.drv-0", -1, 0) = 0
>
> I believe this corresponds to the use of createTempDir() at
> nix/libstore/build.cc:1718. The path of the new directory is saved in
> the tmpDir variable.
>
>> 15438 [pid 30693] chdir("/tmp/guix-build-nmap-7.40.tar.bz2.drv-0") = -1 ENOENT (No such file or directory)
>
> And then later, at nix/libstore/build.cc:2204, we do this:
>
> 2204         if (chdir(tmpDirInSandbox.c_str()) == -1)
> 2205             throw SysError(format("changing into `%1%'") % tmpDir);
>
> It tries to change to the wrong directory (the new "out of band"
> downloader doesn't use a chroot, IIUC), and then prints a misleading
> error message. This explains the discrepancy between what we see in
> strace and on stderr.

Good catch!

AFAICS the flaw is that there’s one place where I wrote:

  if (useChroot && !isBuiltin(drv))

while several other places just do something like:

  if (useChroot)

Could the patch below solve the problem?

diff --git a/nix/libstore/build.cc b/nix/libstore/build.cc
index e823001..38048ce 100644
--- a/nix/libstore/build.cc
+++ b/nix/libstore/build.cc
@@ -1680,7 +1680,11 @@ void DerivationGoal::startBuilder()
             % drv.platform % settings.thisSystem % drvPath);
     }
 
-    useChroot = settings.useChroot;
+    /* Note: built-in builders are *not* running in a chroot environment so
+       that we can easily implement them in Guile without having it as a
+       derivation input (they are running under a separate build user,
+       though).  */
+    useChroot = settings.useChroot && !isBuiltin(drv);
 
     /* Construct the environment passed to the builder. */
     env.clear();
@@ -2048,12 +2052,7 @@ void DerivationGoal::runChild()
         commonChildInit(builderOut);
 
 #if CHROOT_ENABLED
-	/* Note: built-in builders are *not* running in a chroot environment
-	   so that we can easily implement them in Guile without having it as
-	   a derivation input (they are running under a separate build user,
-	   though).  */
-
-        if (useChroot && !isBuiltin(drv)) {
+        if (useChroot) {
             /* Initialise the loopback interface. */
             AutoCloseFD fd(socket(PF_INET, SOCK_DGRAM, IPPROTO_IP));
             if (fd == -1) throw SysError("cannot open IP socket");

Thanks!

Ludo’.