diff --git a/nix/libstore/build.cc b/nix/libstore/build.cc
index a1f39d9a8b..61d883639f 100644
--- a/nix/libstore/build.cc
+++ b/nix/libstore/build.cc
@@ -1,40 +1,40 @@
 #include "config.h"
 
-#include "references.hh"
-#include "pathlocks.hh"
-#include "misc.hh"
+#include "affinity.hh"
+#include "archive.hh"
+#include "builtins.hh"
 #include "globals.hh"
 #include "local-store.hh"
+#include "misc.hh"
+#include "pathlocks.hh"
+#include "references.hh"
 #include "util.hh"
-#include "archive.hh"
-#include "affinity.hh"
-#include "builtins.hh"
 
+#include <algorithm>
 #include <map>
 #include <sstream>
-#include <algorithm>
 
+#include <cstring>
+#include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
-#include <time.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/stat.h>
 #include <sys/time.h>
-#include <sys/wait.h>
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/utsname.h>
-#include <fcntl.h>
+#include <sys/wait.h>
+#include <time.h>
 #include <unistd.h>
-#include <errno.h>
-#include <stdio.h>
-#include <cstring>
-#include <stdint.h>
 
-#include <pwd.h>
 #include <grp.h>
+#include <pwd.h>
 
 #include <zlib.h>
 
 #if HAVE_BZLIB_H
-# include <bzlib.h>
+#include <bzlib.h>
 #endif
 
 /* Includes required for chroot support. */
@@ -54,19 +54,36 @@
 #include <sys/prctl.h>
 #endif
 
+#ifdef MS_BIND
+#define _MS_BIND 1
+#else
+#define _MS_BIND 0
+#endif
+#ifdef MS_PRIVATE
+#define _MS_PRIVATE 1
+#else
+#define _MS_PRIVATE 0
+#endif
+#ifdef CLONE_NEWNS
+#define _CLONE_NEWNS 1
+#else
+#define _CLONE_NEWNS 0
+#endif
 
-#define CHROOT_ENABLED HAVE_CHROOT && HAVE_SYS_MOUNT_H && defined(MS_BIND) && defined(MS_PRIVATE)
-#define CLONE_ENABLED defined(CLONE_NEWNS)
+#define CHROOT_ENABLED                                                        \
+  HAVE_CHROOT && HAVE_SYS_MOUNT_H && _MS_BIND && _MS_PRIVATE
+#define CLONE_ENABLED _CLONE_NEWNS
 
 #if defined(SYS_pivot_root)
-#define pivot_root(new_root, put_old) (syscall(SYS_pivot_root, new_root,put_old))
+#define pivot_root(new_root, put_old)                                         \
+  (syscall (SYS_pivot_root, new_root, put_old))
 #endif
 
 #if CHROOT_ENABLED
-#include <sys/socket.h>
-#include <sys/ioctl.h>
 #include <net/if.h>
 #include <netinet/ip.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
 #endif
 
 #if __linux__
@@ -77,24 +94,23 @@
 #include <sys/statvfs.h>
 #endif
 
-
-namespace nix {
+namespace nix
+{
 
 using std::map;
 
-
 /* Forward definition. */
 class Worker;
 
-
 /* A pointer to a goal. */
 class Goal;
 class DerivationGoal;
 typedef std::shared_ptr<Goal> GoalPtr;
 typedef std::weak_ptr<Goal> WeakGoalPtr;
 
-struct CompareGoalPtrs {
-    bool operator() (const GoalPtr & a, const GoalPtr & b);
+struct CompareGoalPtrs
+{
+  bool operator() (const GoalPtr &a, const GoalPtr &b);
 };
 
 /* Set of goals. */
@@ -104,3764 +120,4311 @@ typedef list<WeakGoalPtr> WeakGoals;
 /* A map of paths to goals (and the other way around). */
 typedef map<Path, WeakGoalPtr> WeakGoalMap;
 
-
-
 class Goal : public std::enable_shared_from_this<Goal>
 {
 public:
-    typedef enum {ecBusy, ecSuccess, ecFailed, ecNoSubstituters, ecIncompleteClosure} ExitCode;
+  typedef enum
+  {
+    ecBusy,
+    ecSuccess,
+    ecFailed,
+    ecNoSubstituters,
+    ecIncompleteClosure
+  } ExitCode;
 
 protected:
+  /* Backlink to the worker. */
+  Worker &worker;
 
-    /* Backlink to the worker. */
-    Worker & worker;
-
-    /* Goals that this goal is waiting for. */
-    Goals waitees;
+  /* Goals that this goal is waiting for. */
+  Goals waitees;
 
-    /* Goals waiting for this one to finish.  Must use weak pointers
-       here to prevent cycles. */
-    WeakGoals waiters;
+  /* Goals waiting for this one to finish.  Must use weak pointers
+     here to prevent cycles. */
+  WeakGoals waiters;
 
-    /* Number of goals we are/were waiting for that have failed. */
-    unsigned int nrFailed;
+  /* Number of goals we are/were waiting for that have failed. */
+  unsigned int nrFailed;
 
-    /* Number of substitution goals we are/were waiting for that
-       failed because there are no substituters. */
-    unsigned int nrNoSubstituters;
+  /* Number of substitution goals we are/were waiting for that
+     failed because there are no substituters. */
+  unsigned int nrNoSubstituters;
 
-    /* Number of substitution goals we are/were waiting for that
-       failed because othey had unsubstitutable references. */
-    unsigned int nrIncompleteClosure;
+  /* Number of substitution goals we are/were waiting for that
+     failed because othey had unsubstitutable references. */
+  unsigned int nrIncompleteClosure;
 
-    /* Name of this goal for debugging purposes. */
-    string name;
+  /* Name of this goal for debugging purposes. */
+  string name;
 
-    /* Whether the goal is finished. */
-    ExitCode exitCode;
+  /* Whether the goal is finished. */
+  ExitCode exitCode;
 
-    Goal(Worker & worker) : worker(worker)
-    {
-        nrFailed = nrNoSubstituters = nrIncompleteClosure = 0;
-        exitCode = ecBusy;
-    }
+  Goal (Worker &worker) : worker (worker)
+  {
+    nrFailed = nrNoSubstituters = nrIncompleteClosure = 0;
+    exitCode = ecBusy;
+  }
 
-    virtual ~Goal()
-    {
-        trace("goal destroyed");
-    }
+  virtual ~Goal () { trace ("goal destroyed"); }
 
 public:
-    virtual void work() = 0;
+  virtual void work () = 0;
 
-    void addWaitee(GoalPtr waitee);
+  void addWaitee (GoalPtr waitee);
 
-    virtual void waiteeDone(GoalPtr waitee, ExitCode result);
+  virtual void waiteeDone (GoalPtr waitee, ExitCode result);
 
-    virtual void handleChildOutput(int fd, const string & data)
-    {
-        abort();
-    }
+  virtual void
+  handleChildOutput (int fd, const string &data)
+  {
+    abort ();
+  }
 
-    virtual void handleEOF(int fd)
-    {
-        abort();
-    }
+  virtual void
+  handleEOF (int fd)
+  {
+    abort ();
+  }
 
-    void trace(const format & f);
+  void trace (const format &f);
 
-    string getName()
-    {
-        return name;
-    }
+  string
+  getName ()
+  {
+    return name;
+  }
 
-    ExitCode getExitCode()
-    {
-        return exitCode;
-    }
+  ExitCode
+  getExitCode ()
+  {
+    return exitCode;
+  }
 
-    /* Callback in case of a timeout.  It should wake up its waiters,
-       get rid of any running child processes that are being monitored
-       by the worker (important!), etc. */
-    virtual void timedOut() = 0;
+  /* Callback in case of a timeout.  It should wake up its waiters,
+     get rid of any running child processes that are being monitored
+     by the worker (important!), etc. */
+  virtual void timedOut () = 0;
 
-    virtual string key() = 0;
+  virtual string key () = 0;
 
 protected:
-    void amDone(ExitCode result);
+  void amDone (ExitCode result);
 };
 
-
-bool CompareGoalPtrs::operator() (const GoalPtr & a, const GoalPtr & b) {
-    string s1 = a->key();
-    string s2 = b->key();
-    return s1 < s2;
+bool
+CompareGoalPtrs::operator() (const GoalPtr &a, const GoalPtr &b)
+{
+  string s1 = a->key ();
+  string s2 = b->key ();
+  return s1 < s2;
 }
 
-
 /* A mapping used to remember for each child process to what goal it
    belongs, and file descriptors for receiving log data and output
    path creation commands. */
 struct Child
 {
-    WeakGoalPtr goal;
-    set<int> fds;
-    bool respectTimeouts;
-    bool inBuildSlot;
-    time_t lastOutput; /* time we last got output on stdout/stderr */
-    time_t timeStarted;
+  WeakGoalPtr goal;
+  set<int> fds;
+  bool respectTimeouts;
+  bool inBuildSlot;
+  time_t lastOutput; /* time we last got output on stdout/stderr */
+  time_t timeStarted;
 };
 
 typedef map<pid_t, Child> Children;
 
-
 /* The worker class. */
 class Worker
 {
 private:
+  /* Note: the worker should only have strong pointers to the
+     top-level goals. */
 
-    /* Note: the worker should only have strong pointers to the
-       top-level goals. */
+  /* The top-level goals of the worker. */
+  Goals topGoals;
 
-    /* The top-level goals of the worker. */
-    Goals topGoals;
+  /* Goals that are ready to do some work. */
+  WeakGoals awake;
 
-    /* Goals that are ready to do some work. */
-    WeakGoals awake;
+  /* Goals waiting for a build slot. */
+  WeakGoals wantingToBuild;
 
-    /* Goals waiting for a build slot. */
-    WeakGoals wantingToBuild;
+  /* Child processes currently running. */
+  Children children;
 
-    /* Child processes currently running. */
-    Children children;
+  /* Number of build slots occupied.  This includes local builds and
+     substitutions but not remote builds via the build hook. */
+  unsigned int nrLocalBuilds;
 
-    /* Number of build slots occupied.  This includes local builds and
-       substitutions but not remote builds via the build hook. */
-    unsigned int nrLocalBuilds;
+  /* Maps used to prevent multiple instantiations of a goal for the
+     same derivation / path. */
+  WeakGoalMap derivationGoals;
+  WeakGoalMap substitutionGoals;
 
-    /* Maps used to prevent multiple instantiations of a goal for the
-       same derivation / path. */
-    WeakGoalMap derivationGoals;
-    WeakGoalMap substitutionGoals;
+  /* Goals waiting for busy paths to be unlocked. */
+  WeakGoals waitingForAnyGoal;
 
-    /* Goals waiting for busy paths to be unlocked. */
-    WeakGoals waitingForAnyGoal;
+  /* Goals sleeping for a few seconds (polling a lock). */
+  WeakGoals waitingForAWhile;
 
-    /* Goals sleeping for a few seconds (polling a lock). */
-    WeakGoals waitingForAWhile;
-
-    /* Last time the goals in `waitingForAWhile' where woken up. */
-    time_t lastWokenUp;
+  /* Last time the goals in `waitingForAWhile' where woken up. */
+  time_t lastWokenUp;
 
 public:
+  /* Set if at least one derivation had a BuildError (i.e. permanent
+     failure). */
+  bool permanentFailure;
 
-    /* Set if at least one derivation had a BuildError (i.e. permanent
-       failure). */
-    bool permanentFailure;
-
-    /* Set if at least one derivation had a timeout. */
-    bool timedOut;
+  /* Set if at least one derivation had a timeout. */
+  bool timedOut;
 
-    LocalStore & store;
+  LocalStore &store;
 
-    std::shared_ptr<Agent> hook;
-    std::shared_ptr<Agent> substituter;
+  std::shared_ptr<Agent> hook;
+  std::shared_ptr<Agent> substituter;
 
-    Worker(LocalStore & store);
-    ~Worker();
+  Worker (LocalStore &store);
+  ~Worker ();
 
-    /* Make a goal (with caching). */
-    GoalPtr makeDerivationGoal(const Path & drvPath, const StringSet & wantedOutputs, BuildMode buildMode = bmNormal);
-    GoalPtr makeSubstitutionGoal(const Path & storePath, bool repair = false);
+  /* Make a goal (with caching). */
+  GoalPtr makeDerivationGoal (const Path &drvPath,
+                              const StringSet &wantedOutputs,
+                              BuildMode buildMode = bmNormal);
+  GoalPtr makeSubstitutionGoal (const Path &storePath, bool repair = false);
 
-    /* Remove a dead goal. */
-    void removeGoal(GoalPtr goal);
+  /* Remove a dead goal. */
+  void removeGoal (GoalPtr goal);
 
-    /* Wake up a goal (i.e., there is something for it to do). */
-    void wakeUp(GoalPtr goal);
+  /* Wake up a goal (i.e., there is something for it to do). */
+  void wakeUp (GoalPtr goal);
 
-    /* Return the number of local build and substitution processes
-       currently running (but not remote builds via the build
-       hook). */
-    unsigned int getNrLocalBuilds();
+  /* Return the number of local build and substitution processes
+     currently running (but not remote builds via the build
+     hook). */
+  unsigned int getNrLocalBuilds ();
 
-    /* Registers a running child process.  `inBuildSlot' means that
-       the process counts towards the jobs limit. */
-    void childStarted(GoalPtr goal, pid_t pid,
-        const set<int> & fds, bool inBuildSlot, bool respectTimeouts);
+  /* Registers a running child process.  `inBuildSlot' means that
+     the process counts towards the jobs limit. */
+  void childStarted (GoalPtr goal, pid_t pid, const set<int> &fds,
+                     bool inBuildSlot, bool respectTimeouts);
 
-    /* Unregisters a running child process.  `wakeSleepers' should be
-       false if there is no sense in waking up goals that are sleeping
-       because they can't run yet (e.g., there is no free build slot,
-       or the hook would still say `postpone'). */
-    void childTerminated(pid_t pid, bool wakeSleepers = true);
+  /* Unregisters a running child process.  `wakeSleepers' should be
+     false if there is no sense in waking up goals that are sleeping
+     because they can't run yet (e.g., there is no free build slot,
+     or the hook would still say `postpone'). */
+  void childTerminated (pid_t pid, bool wakeSleepers = true);
 
-    /* Put `goal' to sleep until a build slot becomes available (which
-       might be right away). */
-    void waitForBuildSlot(GoalPtr goal);
+  /* Put `goal' to sleep until a build slot becomes available (which
+     might be right away). */
+  void waitForBuildSlot (GoalPtr goal);
 
-    /* Wait for any goal to finish.  Pretty indiscriminate way to
-       wait for some resource that some other goal is holding. */
-    void waitForAnyGoal(GoalPtr goal);
+  /* Wait for any goal to finish.  Pretty indiscriminate way to
+     wait for some resource that some other goal is holding. */
+  void waitForAnyGoal (GoalPtr goal);
 
-    /* Wait for a few seconds and then retry this goal.  Used when
-       waiting for a lock held by another process.  This kind of
-       polling is inefficient, but POSIX doesn't really provide a way
-       to wait for multiple locks in the main select() loop. */
-    void waitForAWhile(GoalPtr goal);
+  /* Wait for a few seconds and then retry this goal.  Used when
+     waiting for a lock held by another process.  This kind of
+     polling is inefficient, but POSIX doesn't really provide a way
+     to wait for multiple locks in the main select() loop. */
+  void waitForAWhile (GoalPtr goal);
 
-    /* Loop until the specified top-level goals have finished. */
-    void run(const Goals & topGoals);
+  /* Loop until the specified top-level goals have finished. */
+  void run (const Goals &topGoals);
 
-    /* Wait for input to become available. */
-    void waitForInput();
+  /* Wait for input to become available. */
+  void waitForInput ();
 
-    unsigned int exitStatus();
+  unsigned int exitStatus ();
 };
 
-
 //////////////////////////////////////////////////////////////////////
 
-
-void addToWeakGoals(WeakGoals & goals, GoalPtr p)
+void
+addToWeakGoals (WeakGoals &goals, GoalPtr p)
 {
-    // FIXME: necessary?
-    // FIXME: O(n)
-    foreach (WeakGoals::iterator, i, goals)
-        if (i->lock() == p) return;
-    goals.push_back(p);
+  // FIXME: necessary?
+  // FIXME: O(n)
+  foreach (WeakGoals::iterator, i, goals)
+    if (i->lock () == p)
+      return;
+  goals.push_back (p);
 }
 
-
-void Goal::addWaitee(GoalPtr waitee)
+void
+Goal::addWaitee (GoalPtr waitee)
 {
-    waitees.insert(waitee);
-    addToWeakGoals(waitee->waiters, shared_from_this());
+  waitees.insert (waitee);
+  addToWeakGoals (waitee->waiters, shared_from_this ());
 }
 
-
-void Goal::waiteeDone(GoalPtr waitee, ExitCode result)
+void
+Goal::waiteeDone (GoalPtr waitee, ExitCode result)
 {
-    assert(waitees.find(waitee) != waitees.end());
-    waitees.erase(waitee);
+  assert (waitees.find (waitee) != waitees.end ());
+  waitees.erase (waitee);
 
-    trace(format("waitee `%1%' done; %2% left") %
-        waitee->name % waitees.size());
+  trace (format ("waitee `%1%' done; %2% left") % waitee->name
+         % waitees.size ());
 
-    if (result == ecFailed || result == ecNoSubstituters || result == ecIncompleteClosure) ++nrFailed;
+  if (result == ecFailed || result == ecNoSubstituters
+      || result == ecIncompleteClosure)
+    ++nrFailed;
 
-    if (result == ecNoSubstituters) ++nrNoSubstituters;
+  if (result == ecNoSubstituters)
+    ++nrNoSubstituters;
 
-    if (result == ecIncompleteClosure) ++nrIncompleteClosure;
+  if (result == ecIncompleteClosure)
+    ++nrIncompleteClosure;
 
-    if (waitees.empty() || (result == ecFailed && !settings.keepGoing)) {
+  if (waitees.empty () || (result == ecFailed && !settings.keepGoing))
+    {
 
-        /* If we failed and keepGoing is not set, we remove all
-           remaining waitees. */
-        foreach (Goals::iterator, i, waitees) {
-            GoalPtr goal = *i;
-            WeakGoals waiters2;
-            foreach (WeakGoals::iterator, j, goal->waiters)
-                if (j->lock() != shared_from_this()) waiters2.push_back(*j);
-            goal->waiters = waiters2;
+      /* If we failed and keepGoing is not set, we remove all
+         remaining waitees. */
+      foreach (Goals::iterator, i, waitees)
+        {
+          GoalPtr goal = *i;
+          WeakGoals waiters2;
+          foreach (WeakGoals::iterator, j, goal->waiters)
+            if (j->lock () != shared_from_this ())
+              waiters2.push_back (*j);
+          goal->waiters = waiters2;
         }
-        waitees.clear();
+      waitees.clear ();
 
-        worker.wakeUp(shared_from_this());
+      worker.wakeUp (shared_from_this ());
     }
 }
 
-
-void Goal::amDone(ExitCode result)
+void
+Goal::amDone (ExitCode result)
 {
-    trace("done");
-    assert(exitCode == ecBusy);
-    assert(result == ecSuccess || result == ecFailed || result == ecNoSubstituters || result == ecIncompleteClosure);
-    exitCode = result;
-    foreach (WeakGoals::iterator, i, waiters) {
-        GoalPtr goal = i->lock();
-        if (goal) goal->waiteeDone(shared_from_this(), result);
+  trace ("done");
+  assert (exitCode == ecBusy);
+  assert (result == ecSuccess || result == ecFailed
+          || result == ecNoSubstituters || result == ecIncompleteClosure);
+  exitCode = result;
+  foreach (WeakGoals::iterator, i, waiters)
+    {
+      GoalPtr goal = i->lock ();
+      if (goal)
+        goal->waiteeDone (shared_from_this (), result);
     }
-    waiters.clear();
-    worker.removeGoal(shared_from_this());
+  waiters.clear ();
+  worker.removeGoal (shared_from_this ());
 }
 
-
-void Goal::trace(const format & f)
+void
+Goal::trace (const format &f)
 {
-    debug(format("%1%: %2%") % name % f);
+  debug (format ("%1%: %2%") % name % f);
 }
 
-
-
 //////////////////////////////////////////////////////////////////////
 
-
 /* Restore default handling of SIGPIPE, otherwise some programs will
    randomly say "Broken pipe". */
-static void restoreSIGPIPE()
+static void
+restoreSIGPIPE ()
 {
-    struct sigaction act, oact;
-    act.sa_handler = SIG_DFL;
-    act.sa_flags = 0;
-    sigemptyset(&act.sa_mask);
-    if (sigaction(SIGPIPE, &act, &oact)) throw SysError("resetting SIGPIPE");
+  struct sigaction act, oact;
+  act.sa_handler = SIG_DFL;
+  act.sa_flags = 0;
+  sigemptyset (&act.sa_mask);
+  if (sigaction (SIGPIPE, &act, &oact))
+    throw SysError ("resetting SIGPIPE");
 }
 
-
 //////////////////////////////////////////////////////////////////////
 
-
 class UserLock
 {
 private:
-    /* POSIX locks suck.  If we have a lock on a file, and we open and
-       close that file again (without closing the original file
-       descriptor), we lose the lock.  So we have to be *very* careful
-       not to open a lock file on which we are holding a lock. */
-    static PathSet lockedPaths; /* !!! not thread-safe */
+  /* POSIX locks suck.  If we have a lock on a file, and we open and
+     close that file again (without closing the original file
+     descriptor), we lose the lock.  So we have to be *very* careful
+     not to open a lock file on which we are holding a lock. */
+  static PathSet lockedPaths; /* !!! not thread-safe */
 
-    Path fnUserLock;
-    AutoCloseFD fdUserLock;
+  Path fnUserLock;
+  AutoCloseFD fdUserLock;
 
-    string user;
-    uid_t uid;
-    gid_t gid;
-    std::vector<gid_t> supplementaryGIDs;
+  string user;
+  uid_t uid;
+  gid_t gid;
+  std::vector<gid_t> supplementaryGIDs;
 
 public:
-    UserLock();
-    ~UserLock();
+  UserLock ();
+  ~UserLock ();
 
-    void acquire();
-    void release();
+  void acquire ();
+  void release ();
 
-    void kill();
+  void kill ();
 
-    string getUser() { return user; }
-    uid_t getUID() { return uid; }
-    uid_t getGID() { return gid; }
-    std::vector<gid_t> getSupplementaryGIDs() { return supplementaryGIDs; }
-
-    bool enabled() { return uid != 0; }
+  string
+  getUser ()
+  {
+    return user;
+  }
+  uid_t
+  getUID ()
+  {
+    return uid;
+  }
+  uid_t
+  getGID ()
+  {
+    return gid;
+  }
+  std::vector<gid_t>
+  getSupplementaryGIDs ()
+  {
+    return supplementaryGIDs;
+  }
 
+  bool
+  enabled ()
+  {
+    return uid != 0;
+  }
 };
 
-
 PathSet UserLock::lockedPaths;
 
+UserLock::UserLock () { uid = gid = 0; }
 
-UserLock::UserLock()
-{
-    uid = gid = 0;
-}
-
-
-UserLock::~UserLock()
-{
-    release();
-}
-
+UserLock::~UserLock () { release (); }
 
-void UserLock::acquire()
+void
+UserLock::acquire ()
 {
-    assert(uid == 0);
+  assert (uid == 0);
 
-    assert(settings.buildUsersGroup != "");
+  assert (settings.buildUsersGroup != "");
 
-    /* Get the members of the build-users-group. */
-    struct group * gr = getgrnam(settings.buildUsersGroup.c_str());
-    if (!gr)
-        throw Error(format("the group `%1%' specified in `build-users-group' does not exist")
-            % settings.buildUsersGroup);
-    gid = gr->gr_gid;
+  /* Get the members of the build-users-group. */
+  struct group *gr = getgrnam (settings.buildUsersGroup.c_str ());
+  if (!gr)
+    throw Error (
+        format (
+            "the group `%1%' specified in `build-users-group' does not exist")
+        % settings.buildUsersGroup);
+  gid = gr->gr_gid;
 
-    /* Copy the result of getgrnam. */
-    Strings users;
-    for (char * * p = gr->gr_mem; *p; ++p) {
-        debug(format("found build user `%1%'") % *p);
-        users.push_back(*p);
+  /* Copy the result of getgrnam. */
+  Strings users;
+  for (char **p = gr->gr_mem; *p; ++p)
+    {
+      debug (format ("found build user `%1%'") % *p);
+      users.push_back (*p);
     }
 
-    if (users.empty())
-        throw Error(format("the build users group `%1%' has no members")
-            % settings.buildUsersGroup);
-
-    /* Find a user account that isn't currently in use for another
-       build. */
-    foreach (Strings::iterator, i, users) {
-        debug(format("trying user `%1%'") % *i);
-
-        struct passwd * pw = getpwnam(i->c_str());
-        if (!pw)
-            throw Error(format("the user `%1%' in the group `%2%' does not exist")
-                % *i % settings.buildUsersGroup);
+  if (users.empty ())
+    throw Error (format ("the build users group `%1%' has no members")
+                 % settings.buildUsersGroup);
 
-        createDirs(settings.nixStateDir + "/userpool");
-
-        fnUserLock = (format("%1%/userpool/%2%") % settings.nixStateDir % pw->pw_uid).str();
-
-        if (lockedPaths.find(fnUserLock) != lockedPaths.end())
-            /* We already have a lock on this one. */
-            continue;
+  /* Find a user account that isn't currently in use for another
+     build. */
+  foreach (Strings::iterator, i, users)
+    {
+      debug (format ("trying user `%1%'") % *i);
 
-        AutoCloseFD fd = open(fnUserLock.c_str(), O_RDWR | O_CREAT, 0600);
-        if (fd == -1)
-            throw SysError(format("opening user lock `%1%'") % fnUserLock);
-        closeOnExec(fd);
+      struct passwd *pw = getpwnam (i->c_str ());
+      if (!pw)
+        throw Error (
+            format ("the user `%1%' in the group `%2%' does not exist") % *i
+            % settings.buildUsersGroup);
 
-        if (lockFile(fd, ltWrite, false)) {
-            fdUserLock = fd.borrow();
-            lockedPaths.insert(fnUserLock);
-            user = *i;
-            uid = pw->pw_uid;
+      createDirs (settings.nixStateDir + "/userpool");
 
-            /* Sanity check... */
-            if (uid == getuid() || uid == geteuid())
-                throw Error(format("the build user should not be a member of `%1%'")
-                    % settings.buildUsersGroup);
+      fnUserLock
+          = (format ("%1%/userpool/%2%") % settings.nixStateDir % pw->pw_uid)
+                .str ();
 
-            /* Get the list of supplementary groups of this build user.  This
-               is usually either empty or contains a group such as "kvm".  */
-            supplementaryGIDs.resize(10);
-            int ngroups = supplementaryGIDs.size();
-            int err = getgrouplist(pw->pw_name, pw->pw_gid,
-                supplementaryGIDs.data(), &ngroups);
-            if (err == -1)
-                throw Error(format("failed to get list of supplementary groups for ‘%1%’") % pw->pw_name);
+      if (lockedPaths.find (fnUserLock) != lockedPaths.end ())
+        /* We already have a lock on this one. */
+        continue;
 
-            supplementaryGIDs.resize(ngroups);
+      AutoCloseFD fd = open (fnUserLock.c_str (), O_RDWR | O_CREAT, 0600);
+      if (fd == -1)
+        throw SysError (format ("opening user lock `%1%'") % fnUserLock);
+      closeOnExec (fd);
 
-            return;
+      if (lockFile (fd, ltWrite, false))
+        {
+          fdUserLock = fd.borrow ();
+          lockedPaths.insert (fnUserLock);
+          user = *i;
+          uid = pw->pw_uid;
+
+          /* Sanity check... */
+          if (uid == getuid () || uid == geteuid ())
+            throw Error (
+                format ("the build user should not be a member of `%1%'")
+                % settings.buildUsersGroup);
+
+          /* Get the list of supplementary groups of this build user.  This
+             is usually either empty or contains a group such as "kvm".  */
+          supplementaryGIDs.resize (10);
+          int ngroups = supplementaryGIDs.size ();
+          int err = getgrouplist (pw->pw_name, pw->pw_gid,
+                                  supplementaryGIDs.data (), &ngroups);
+          if (err == -1)
+            throw Error (
+                format ("failed to get list of supplementary groups for ‘%1%’")
+                % pw->pw_name);
+
+          supplementaryGIDs.resize (ngroups);
+
+          return;
         }
     }
 
-    throw Error(format("all build users are currently in use; "
-        "consider creating additional users and adding them to the `%1%' group")
-        % settings.buildUsersGroup);
+  throw Error (format ("all build users are currently in use; "
+                       "consider creating additional users and adding them to "
+                       "the `%1%' group")
+               % settings.buildUsersGroup);
 }
 
-
-void UserLock::release()
+void
+UserLock::release ()
 {
-    if (uid == 0) return;
-    fdUserLock.close(); /* releases lock */
-    assert(lockedPaths.find(fnUserLock) != lockedPaths.end());
-    lockedPaths.erase(fnUserLock);
-    fnUserLock = "";
-    uid = 0;
+  if (uid == 0)
+    return;
+  fdUserLock.close (); /* releases lock */
+  assert (lockedPaths.find (fnUserLock) != lockedPaths.end ());
+  lockedPaths.erase (fnUserLock);
+  fnUserLock = "";
+  uid = 0;
 }
 
-
-void UserLock::kill()
+void
+UserLock::kill ()
 {
-    assert(enabled());
-    killUser(uid);
+  assert (enabled ());
+  killUser (uid);
 }
 
 //////////////////////////////////////////////////////////////////////
 
-
 typedef map<string, string> HashRewrites;
 
-
-string rewriteHashes(string s, const HashRewrites & rewrites)
+string
+rewriteHashes (string s, const HashRewrites &rewrites)
 {
-    foreach (HashRewrites::const_iterator, i, rewrites) {
-        assert(i->first.size() == i->second.size());
-        size_t j = 0;
-        while ((j = s.find(i->first, j)) != string::npos) {
-            debug(format("rewriting @ %1%") % j);
-            s.replace(j, i->second.size(), i->second);
+  foreach (HashRewrites::const_iterator, i, rewrites)
+    {
+      assert (i->first.size () == i->second.size ());
+      size_t j = 0;
+      while ((j = s.find (i->first, j)) != string::npos)
+        {
+          debug (format ("rewriting @ %1%") % j);
+          s.replace (j, i->second.size (), i->second);
         }
     }
-    return s;
+  return s;
 }
 
-
 //////////////////////////////////////////////////////////////////////
 
-
-typedef enum {rpAccept, rpDecline, rpPostpone} HookReply;
+typedef enum
+{
+  rpAccept,
+  rpDecline,
+  rpPostpone
+} HookReply;
 
 class SubstitutionGoal;
 
 class DerivationGoal : public Goal
 {
 private:
-    /* The path of the derivation. */
-    Path drvPath;
+  /* The path of the derivation. */
+  Path drvPath;
 
-    /* The specific outputs that we need to build.  Empty means all of
-       them. */
-    StringSet wantedOutputs;
+  /* The specific outputs that we need to build.  Empty means all of
+     them. */
+  StringSet wantedOutputs;
 
-    /* Whether additional wanted outputs have been added. */
-    bool needRestart;
+  /* Whether additional wanted outputs have been added. */
+  bool needRestart;
 
-    /* Whether to retry substituting the outputs after building the
-       inputs. */
-    bool retrySubstitution;
+  /* Whether to retry substituting the outputs after building the
+     inputs. */
+  bool retrySubstitution;
 
-    /* The derivation stored at drvPath. */
-    Derivation drv;
+  /* The derivation stored at drvPath. */
+  Derivation drv;
 
-    /* The remainder is state held during the build. */
+  /* The remainder is state held during the build. */
 
-    /* Locks on the output paths. */
-    PathLocks outputLocks;
+  /* Locks on the output paths. */
+  PathLocks outputLocks;
 
-    /* All input paths (that is, the union of FS closures of the
-       immediate input paths). */
-    PathSet inputPaths;
+  /* All input paths (that is, the union of FS closures of the
+     immediate input paths). */
+  PathSet inputPaths;
 
-    /* Referenceable paths (i.e., input and output paths). */
-    PathSet allPaths;
+  /* Referenceable paths (i.e., input and output paths). */
+  PathSet allPaths;
 
-    /* Outputs that are already valid.  If we're repairing, these are
-       the outputs that are valid *and* not corrupt. */
-    PathSet validPaths;
+  /* Outputs that are already valid.  If we're repairing, these are
+     the outputs that are valid *and* not corrupt. */
+  PathSet validPaths;
 
-    /* Outputs that are corrupt or not valid. */
-    PathSet missingPaths;
+  /* Outputs that are corrupt or not valid. */
+  PathSet missingPaths;
 
-    /* User selected for running the builder. */
-    UserLock buildUser;
+  /* User selected for running the builder. */
+  UserLock buildUser;
 
-    /* The process ID of the builder. */
-    Pid pid;
+  /* The process ID of the builder. */
+  Pid pid;
 
-    /* The temporary directory. */
-    Path tmpDir;
+  /* The temporary directory. */
+  Path tmpDir;
 
-    /* The path of the temporary directory in the sandbox. */
-    Path tmpDirInSandbox;
+  /* The path of the temporary directory in the sandbox. */
+  Path tmpDirInSandbox;
 
-    /* File descriptor for the log file. */
-    FILE * fLogFile;
-    gzFile   gzLogFile;
+  /* File descriptor for the log file. */
+  FILE *fLogFile;
+  gzFile gzLogFile;
 #if HAVE_BZLIB_H
-    BZFILE * bzLogFile;
+  BZFILE *bzLogFile;
 #endif
-    AutoCloseFD fdLogFile;
+  AutoCloseFD fdLogFile;
 
-    /* Number of bytes received from the builder's stdout/stderr. */
-    unsigned long logSize;
+  /* Number of bytes received from the builder's stdout/stderr. */
+  unsigned long logSize;
 
-    /* Pipe for the builder's standard output/error. */
-    Pipe builderOut;
+  /* Pipe for the builder's standard output/error. */
+  Pipe builderOut;
 
-    /* The build hook. */
-    std::shared_ptr<Agent> hook;
+  /* The build hook. */
+  std::shared_ptr<Agent> hook;
 
-    /* Whether we're currently doing a chroot build. */
-    bool useChroot;
+  /* Whether we're currently doing a chroot build. */
+  bool useChroot;
 
-    /* The directory containing the chroot root directory, and the chroot root
-       directory itself--the latter is a sub-directory of the former.  */
-    Path chrootRootTop, chrootRootDir;
+  /* The directory containing the chroot root directory, and the chroot root
+     directory itself--the latter is a sub-directory of the former.  */
+  Path chrootRootTop, chrootRootDir;
 
-    /* RAII object to delete the chroot directory. */
-    std::shared_ptr<AutoDelete> autoDelChroot;
+  /* RAII object to delete the chroot directory. */
+  std::shared_ptr<AutoDelete> autoDelChroot;
 
-    /* Whether this is a fixed-output derivation. */
-    bool fixedOutput;
+  /* Whether this is a fixed-output derivation. */
+  bool fixedOutput;
 
-    typedef void (DerivationGoal::*GoalState)();
-    GoalState state;
+  typedef void (DerivationGoal::*GoalState) ();
+  GoalState state;
 
-    /* Stuff we need to pass to runChild(). */
-    typedef map<Path, Path> DirsInChroot; // maps target path to source path
-    DirsInChroot dirsInChroot;
-    typedef map<string, string> Environment;
-    Environment env;
+  /* Stuff we need to pass to runChild(). */
+  typedef map<Path, Path> DirsInChroot; // maps target path to source path
+  DirsInChroot dirsInChroot;
+  typedef map<string, string> Environment;
+  Environment env;
 
-    /* Hash rewriting. */
-    HashRewrites rewritesToTmp, rewritesFromTmp;
-    typedef map<Path, Path> RedirectedOutputs;
-    RedirectedOutputs redirectedOutputs;
+  /* Hash rewriting. */
+  HashRewrites rewritesToTmp, rewritesFromTmp;
+  typedef map<Path, Path> RedirectedOutputs;
+  RedirectedOutputs redirectedOutputs;
 
-    BuildMode buildMode;
+  BuildMode buildMode;
 
-    /* If we're repairing without a chroot, there may be outputs that
-       are valid but corrupt.  So we redirect these outputs to
-       temporary paths. */
-    PathSet redirectedBadOutputs;
+  /* If we're repairing without a chroot, there may be outputs that
+     are valid but corrupt.  So we redirect these outputs to
+     temporary paths. */
+  PathSet redirectedBadOutputs;
 
-    /* The current round, if we're building multiple times. */
-    unsigned int curRound = 1;
+  /* The current round, if we're building multiple times. */
+  unsigned int curRound = 1;
 
-    unsigned int nrRounds;
+  unsigned int nrRounds;
 
-    /* Path registration info from the previous round, if we're
-       building multiple times. Since this contains the hash, it
-       allows us to compare whether two rounds produced the same
-       result. */
-    ValidPathInfos prevInfos;
+  /* Path registration info from the previous round, if we're
+     building multiple times. Since this contains the hash, it
+     allows us to compare whether two rounds produced the same
+     result. */
+  ValidPathInfos prevInfos;
 
-    BuildResult result;
+  BuildResult result;
 
 public:
-    DerivationGoal(const Path & drvPath, const StringSet & wantedOutputs, Worker & worker, BuildMode buildMode = bmNormal);
-    ~DerivationGoal();
-
-    void timedOut() override;
-
-    string key()
-    {
-        /* Ensure that derivations get built in order of their name,
-           i.e. a derivation named "aardvark" always comes before
-           "baboon". And substitution goals always happen before
-           derivation goals (due to "b$"). */
-        return "b$" + storePathToName(drvPath) + "$" + drvPath;
-    }
+  DerivationGoal (const Path &drvPath, const StringSet &wantedOutputs,
+                  Worker &worker, BuildMode buildMode = bmNormal);
+  ~DerivationGoal ();
+
+  void timedOut () override;
+
+  string
+  key () override
+  {
+    /* Ensure that derivations get built in order of their name,
+       i.e. a derivation named "aardvark" always comes before
+       "baboon". And substitution goals always happen before
+       derivation goals (due to "b$"). */
+    return "b$" + storePathToName (drvPath) + "$" + drvPath;
+  }
 
-    void work();
+  void work () override;
 
-    Path getDrvPath()
-    {
-        return drvPath;
-    }
+  Path
+  getDrvPath ()
+  {
+    return drvPath;
+  }
 
-    /* Add wanted outputs to an already existing derivation goal. */
-    void addWantedOutputs(const StringSet & outputs);
+  /* Add wanted outputs to an already existing derivation goal. */
+  void addWantedOutputs (const StringSet &outputs);
 
-    BuildResult getResult() { return result; }
+  BuildResult
+  getResult ()
+  {
+    return result;
+  }
 
 private:
-    /* The states. */
-    void init();
-    void haveDerivation();
-    void outputsSubstituted();
-    void closureRepaired();
-    void inputsRealised();
-    void tryToBuild();
-    void buildDone();
+  /* The states. */
+  void init ();
+  void haveDerivation ();
+  void outputsSubstituted ();
+  void closureRepaired ();
+  void inputsRealised ();
+  void tryToBuild ();
+  void buildDone ();
 
-    /* Is the build hook willing to perform the build? */
-    HookReply tryBuildHook();
+  /* Is the build hook willing to perform the build? */
+  HookReply tryBuildHook ();
 
-    /* Start building a derivation. */
-    void startBuilder();
+  /* Start building a derivation. */
+  void startBuilder ();
 
-    /* Run the builder's process. */
-    void runChild();
+  /* Run the builder's process. */
+  void runChild ();
 
-    friend int childEntry(void *);
+  friend int childEntry (void *);
 
-    /* Pipe to notify readiness to the child process when using unprivileged
-       user namespaces.  */
-    Pipe readiness;
+  /* Pipe to notify readiness to the child process when using unprivileged
+     user namespaces.  */
+  Pipe readiness;
 
-    /* Check that the derivation outputs all exist and register them
-       as valid. */
-    void registerOutputs();
+  /* Check that the derivation outputs all exist and register them
+     as valid. */
+  void registerOutputs ();
 
-    /* Open a log file and a pipe to it. */
-    Path openLogFile();
+  /* Open a log file and a pipe to it. */
+  Path openLogFile ();
 
-    /* Close the log file. */
-    void closeLogFile();
+  /* Close the log file. */
+  void closeLogFile ();
 
-    /* Delete the temporary directory, if we have one. */
-    void deleteTmpDir(bool force);
+  /* Delete the temporary directory, if we have one. */
+  void deleteTmpDir (bool force);
 
-    /* Callback used by the worker to write to the log. */
-    void handleChildOutput(int fd, const string & data);
-    void handleEOF(int fd);
+  /* Callback used by the worker to write to the log. */
+  void handleChildOutput (int fd, const string &data) override;
+  void handleEOF (int fd) override;
 
-    /* Return the set of (in)valid paths. */
-    PathSet checkPathValidity(bool returnValid, bool checkHash);
+  /* Return the set of (in)valid paths. */
+  PathSet checkPathValidity (bool returnValid, bool checkHash);
 
-    /* Abort the goal if `path' failed to build. */
-    bool pathFailed(const Path & path);
+  /* Abort the goal if `path' failed to build. */
+  bool pathFailed (const Path &path);
 
-    /* Forcibly kill the child process, if any. */
-    void killChild();
+  /* Forcibly kill the child process, if any. */
+  void killChild ();
 
-    Path addHashRewrite(const Path & path);
+  Path addHashRewrite (const Path &path);
 
-    void repairClosure();
+  void repairClosure ();
 
-    void done(BuildResult::Status status, const string & msg = "");
+  void done (BuildResult::Status status, const string &msg = "");
 };
 
-
-DerivationGoal::DerivationGoal(const Path & drvPath, const StringSet & wantedOutputs, Worker & worker, BuildMode buildMode)
-    : Goal(worker)
-    , wantedOutputs(wantedOutputs)
-    , needRestart(false)
-    , retrySubstitution(false)
-    , fLogFile(0)
-    , gzLogFile(0)
+DerivationGoal::DerivationGoal (const Path &drvPath,
+                                const StringSet &wantedOutputs, Worker &worker,
+                                BuildMode buildMode)
+    : Goal (worker), wantedOutputs (wantedOutputs), needRestart (false),
+      retrySubstitution (false), fLogFile (0), gzLogFile (0)
 #if HAVE_BZLIB_H
-    , bzLogFile(0)
+      ,
+      bzLogFile (0)
 #endif
-    , useChroot(false)
-    , buildMode(buildMode)
+      ,
+      useChroot (false), buildMode (buildMode)
 {
-    this->drvPath = drvPath;
-    state = &DerivationGoal::init;
-    name = (format("building of `%1%'") % drvPath).str();
-    trace("created");
-
-    /* Prevent the .chroot directory from being
-       garbage-collected. (See isActiveTempFile() in gc.cc.) */
-    worker.store.addTempRoot(drvPath);
+  this->drvPath = drvPath;
+  state = &DerivationGoal::init;
+  name = (format ("building of `%1%'") % drvPath).str ();
+  trace ("created");
+
+  /* Prevent the .chroot directory from being
+     garbage-collected. (See isActiveTempFile() in gc.cc.) */
+  worker.store.addTempRoot (drvPath);
 }
 
-
-DerivationGoal::~DerivationGoal()
+DerivationGoal::~DerivationGoal ()
 {
-    /* Careful: we should never ever throw an exception from a
-       destructor. */
-    try { killChild(); } catch (...) { ignoreException(); }
-    try { deleteTmpDir(false); } catch (...) { ignoreException(); }
-    try { closeLogFile(); } catch (...) { ignoreException(); }
+  /* Careful: we should never ever throw an exception from a
+     destructor. */
+  try
+    {
+      killChild ();
+    }
+  catch (...)
+    {
+      ignoreException ();
+    }
+  try
+    {
+      deleteTmpDir (false);
+    }
+  catch (...)
+    {
+      ignoreException ();
+    }
+  try
+    {
+      closeLogFile ();
+    }
+  catch (...)
+    {
+      ignoreException ();
+    }
 }
 
-
-void DerivationGoal::killChild()
+void
+DerivationGoal::killChild ()
 {
-    if (pid != -1) {
-        worker.childTerminated(pid);
+  if (pid != -1)
+    {
+      worker.childTerminated (pid);
 
-        if (buildUser.enabled()) {
-            /* If we're using a build user, then there is a tricky
-               race condition: if we kill the build user before the
-               child has done its setuid() to the build user uid, then
-               it won't be killed, and we'll potentially lock up in
-               pid.wait().  So also send a conventional kill to the
-               child. */
-            ::kill(-pid, SIGKILL); /* ignore the result */
-            buildUser.kill();
-            pid.wait(true);
-        } else
-            pid.kill();
+      if (buildUser.enabled ())
+        {
+          /* If we're using a build user, then there is a tricky
+             race condition: if we kill the build user before the
+             child has done its setuid() to the build user uid, then
+             it won't be killed, and we'll potentially lock up in
+             pid.wait().  So also send a conventional kill to the
+             child. */
+          ::kill (-pid, SIGKILL); /* ignore the result */
+          buildUser.kill ();
+          pid.wait (true);
+        }
+      else
+        pid.kill ();
 
-        assert(pid == -1);
+      assert (pid == -1);
     }
 
-    /* If there was a build hook involved, remove it from the worker's
-       children.  */
-    if (hook && hook->pid != -1) {
-	worker.childTerminated(hook->pid);
+  /* If there was a build hook involved, remove it from the worker's
+     children.  */
+  if (hook && hook->pid != -1)
+    {
+      worker.childTerminated (hook->pid);
     }
-    hook.reset();
+  hook.reset ();
 }
 
-
-void DerivationGoal::timedOut()
+void
+DerivationGoal::timedOut ()
 {
-    if (settings.printBuildTrace)
-        printMsg(lvlError, format("@ build-failed %1% - timeout") % drvPath);
-    killChild();
-    done(BuildResult::TimedOut);
+  if (settings.printBuildTrace)
+    printMsg (lvlError, format ("@ build-failed %1% - timeout") % drvPath);
+  killChild ();
+  done (BuildResult::TimedOut);
 }
 
-
-void DerivationGoal::work()
+void
+DerivationGoal::work ()
 {
-    (this->*state)();
+  (this->*state) ();
 }
 
-
-void DerivationGoal::addWantedOutputs(const StringSet & outputs)
+void
+DerivationGoal::addWantedOutputs (const StringSet &outputs)
 {
-    /* If we already want all outputs, there is nothing to do. */
-    if (wantedOutputs.empty()) return;
+  /* If we already want all outputs, there is nothing to do. */
+  if (wantedOutputs.empty ())
+    return;
 
-    if (outputs.empty()) {
-        wantedOutputs.clear();
-        needRestart = true;
-    } else
-        foreach (StringSet::const_iterator, i, outputs)
-            if (wantedOutputs.find(*i) == wantedOutputs.end()) {
-                wantedOutputs.insert(*i);
-                needRestart = true;
-            }
+  if (outputs.empty ())
+    {
+      wantedOutputs.clear ();
+      needRestart = true;
+    }
+  else
+    foreach (StringSet::const_iterator, i, outputs)
+      if (wantedOutputs.find (*i) == wantedOutputs.end ())
+        {
+          wantedOutputs.insert (*i);
+          needRestart = true;
+        }
 }
 
-
-void DerivationGoal::init()
+void
+DerivationGoal::init ()
 {
-    trace("init");
+  trace ("init");
 
-    if (settings.readOnlyMode)
-        throw Error(format("cannot build derivation `%1%' - no write access to the store") % drvPath);
+  if (settings.readOnlyMode)
+    throw Error (
+        format ("cannot build derivation `%1%' - no write access to the store")
+        % drvPath);
 
-    /* The first thing to do is to make sure that the derivation
-       exists.  If it doesn't, it may be created through a
-       substitute. */
-    if (buildMode == bmNormal && worker.store.isValidPath(drvPath)) {
-        haveDerivation();
-        return;
+  /* The first thing to do is to make sure that the derivation
+     exists.  If it doesn't, it may be created through a
+     substitute. */
+  if (buildMode == bmNormal && worker.store.isValidPath (drvPath))
+    {
+      haveDerivation ();
+      return;
     }
 
-    addWaitee(worker.makeSubstitutionGoal(drvPath));
+  addWaitee (worker.makeSubstitutionGoal (drvPath));
 
-    state = &DerivationGoal::haveDerivation;
+  state = &DerivationGoal::haveDerivation;
 }
 
-
-void DerivationGoal::haveDerivation()
+void
+DerivationGoal::haveDerivation ()
 {
-    trace("loading derivation");
+  trace ("loading derivation");
 
-    if (nrFailed != 0) {
-        printMsg(lvlError, format("cannot build missing derivation ‘%1%’") % drvPath);
-        done(BuildResult::MiscFailure);
-        return;
+  if (nrFailed != 0)
+    {
+      printMsg (lvlError,
+                format ("cannot build missing derivation ‘%1%’") % drvPath);
+      done (BuildResult::MiscFailure);
+      return;
     }
 
-    /* `drvPath' should already be a root, but let's be on the safe
-       side: if the user forgot to make it a root, we wouldn't want
-       things being garbage collected while we're busy. */
-    worker.store.addTempRoot(drvPath);
+  /* `drvPath' should already be a root, but let's be on the safe
+     side: if the user forgot to make it a root, we wouldn't want
+     things being garbage collected while we're busy. */
+  worker.store.addTempRoot (drvPath);
 
-    assert(worker.store.isValidPath(drvPath));
+  assert (worker.store.isValidPath (drvPath));
 
-    /* Get the derivation. */
-    drv = derivationFromPath(worker.store, drvPath);
+  /* Get the derivation. */
+  drv = derivationFromPath (worker.store, drvPath);
 
-    foreach (DerivationOutputs::iterator, i, drv.outputs)
-        worker.store.addTempRoot(i->second.path);
+  foreach (DerivationOutputs::iterator, i, drv.outputs)
+    worker.store.addTempRoot (i->second.path);
 
-    /* Check what outputs paths are not already valid. */
-    PathSet invalidOutputs = checkPathValidity(false, buildMode == bmRepair);
+  /* Check what outputs paths are not already valid. */
+  PathSet invalidOutputs = checkPathValidity (false, buildMode == bmRepair);
 
-    /* If they are all valid, then we're done. */
-    if (invalidOutputs.size() == 0 && buildMode == bmNormal) {
-        done(BuildResult::AlreadyValid);
-        return;
+  /* If they are all valid, then we're done. */
+  if (invalidOutputs.size () == 0 && buildMode == bmNormal)
+    {
+      done (BuildResult::AlreadyValid);
+      return;
     }
 
-    /* Check whether any output previously failed to build.  If so,
-       don't bother. */
-    foreach (PathSet::iterator, i, invalidOutputs)
-        if (pathFailed(*i)) return;
+  /* Check whether any output previously failed to build.  If so,
+     don't bother. */
+  foreach (PathSet::iterator, i, invalidOutputs)
+    if (pathFailed (*i))
+      return;
 
-    /* We are first going to try to create the invalid output paths
-       through substitutes.  If that doesn't work, we'll build
-       them. */
-    if (settings.useSubstitutes && substitutesAllowed(drv))
-        foreach (PathSet::iterator, i, invalidOutputs)
-            addWaitee(worker.makeSubstitutionGoal(*i, buildMode == bmRepair));
+  /* We are first going to try to create the invalid output paths
+     through substitutes.  If that doesn't work, we'll build
+     them. */
+  if (settings.useSubstitutes && substitutesAllowed (drv))
+    foreach (PathSet::iterator, i, invalidOutputs)
+      addWaitee (worker.makeSubstitutionGoal (*i, buildMode == bmRepair));
 
-    if (waitees.empty()) /* to prevent hang (no wake-up event) */
-        outputsSubstituted();
-    else
-        state = &DerivationGoal::outputsSubstituted;
+  if (waitees.empty ()) /* to prevent hang (no wake-up event) */
+    outputsSubstituted ();
+  else
+    state = &DerivationGoal::outputsSubstituted;
 }
 
-
-void DerivationGoal::outputsSubstituted()
+void
+DerivationGoal::outputsSubstituted ()
 {
-    trace("all outputs substituted (maybe)");
+  trace ("all outputs substituted (maybe)");
 
-    if (nrFailed > 0 && nrFailed > nrNoSubstituters + nrIncompleteClosure && !settings.tryFallback)
-        throw Error(format("some substitutes for the outputs of derivation `%1%' failed (usually happens due to networking issues); try `--fallback' to build derivation from source ") % drvPath);
+  if (nrFailed > 0 && nrFailed > nrNoSubstituters + nrIncompleteClosure
+      && !settings.tryFallback)
+    throw Error (
+        format ("some substitutes for the outputs of derivation `%1%' failed "
+                "(usually happens due to networking "
+                "issues); try `--fallback' to build derivation from source ")
+        % drvPath);
 
-    /*  If the substitutes form an incomplete closure, then we should
-        build the dependencies of this derivation, but after that, we
-        can still use the substitutes for this derivation itself. */
-    if (nrIncompleteClosure > 0 && !retrySubstitution) retrySubstitution = true;
+  /*  If the substitutes form an incomplete closure, then we should
+      build the dependencies of this derivation, but after that, we
+      can still use the substitutes for this derivation itself. */
+  if (nrIncompleteClosure > 0 && !retrySubstitution)
+    retrySubstitution = true;
 
-    nrFailed = nrNoSubstituters = nrIncompleteClosure = 0;
+  nrFailed = nrNoSubstituters = nrIncompleteClosure = 0;
 
-    if (needRestart) {
-        needRestart = false;
-        haveDerivation();
-        return;
+  if (needRestart)
+    {
+      needRestart = false;
+      haveDerivation ();
+      return;
     }
 
-    unsigned int nrInvalid = checkPathValidity(false, buildMode == bmRepair).size();
-    if (buildMode == bmNormal && nrInvalid == 0) {
-        done(BuildResult::Substituted);
-        return;
+  unsigned int nrInvalid
+      = checkPathValidity (false, buildMode == bmRepair).size ();
+  if (buildMode == bmNormal && nrInvalid == 0)
+    {
+      done (BuildResult::Substituted);
+      return;
     }
-    if (buildMode == bmRepair && nrInvalid == 0) {
-        repairClosure();
-        return;
+  if (buildMode == bmRepair && nrInvalid == 0)
+    {
+      repairClosure ();
+      return;
     }
-    if (buildMode == bmCheck && nrInvalid > 0)
-        throw Error(format("`%1%' is missing outputs; build it normally before using `--check'") % drvPath);
-
-    /* Otherwise, at least one of the output paths could not be
-       produced using a substitute.  So we have to build instead. */
-
-    /* Make sure checkPathValidity() from now on checks all
-       outputs. */
-    wantedOutputs = PathSet();
-
-    /* The inputs must be built before we can build this goal. */
-    foreach (DerivationInputs::iterator, i, drv.inputDrvs)
-        addWaitee(worker.makeDerivationGoal(i->first, i->second, buildMode == bmRepair ? bmRepair : bmNormal));
-
-    foreach (PathSet::iterator, i, drv.inputSrcs)
-        addWaitee(worker.makeSubstitutionGoal(*i));
-
-    if (waitees.empty()) /* to prevent hang (no wake-up event) */
-        inputsRealised();
-    else
-        state = &DerivationGoal::inputsRealised;
+  if (buildMode == bmCheck && nrInvalid > 0)
+    throw Error (format ("`%1%' is missing outputs; build it normally before "
+                         "using `--check'")
+                 % drvPath);
+
+  /* Otherwise, at least one of the output paths could not be
+     produced using a substitute.  So we have to build instead. */
+
+  /* Make sure checkPathValidity() from now on checks all
+     outputs. */
+  wantedOutputs = PathSet ();
+
+  /* The inputs must be built before we can build this goal. */
+  foreach (DerivationInputs::iterator, i, drv.inputDrvs)
+    addWaitee (worker.makeDerivationGoal (
+        i->first, i->second, buildMode == bmRepair ? bmRepair : bmNormal));
+
+  foreach (PathSet::iterator, i, drv.inputSrcs)
+    addWaitee (worker.makeSubstitutionGoal (*i));
+
+  if (waitees.empty ()) /* to prevent hang (no wake-up event) */
+    inputsRealised ();
+  else
+    state = &DerivationGoal::inputsRealised;
 }
 
-
-void DerivationGoal::repairClosure()
+void
+DerivationGoal::repairClosure ()
 {
-    /* If we're repairing, we now know that our own outputs are valid.
-       Now check whether the other paths in the outputs closure are
-       good.  If not, then start derivation goals for the derivations
-       that produced those outputs. */
-
-    /* Get the output closure. */
-    PathSet outputClosure;
-    foreach (DerivationOutputs::iterator, i, drv.outputs) {
-        if (!wantOutput(i->first, wantedOutputs)) continue;
-        computeFSClosure(worker.store, i->second.path, outputClosure);
+  /* If we're repairing, we now know that our own outputs are valid.
+     Now check whether the other paths in the outputs closure are
+     good.  If not, then start derivation goals for the derivations
+     that produced those outputs. */
+
+  /* Get the output closure. */
+  PathSet outputClosure;
+  foreach (DerivationOutputs::iterator, i, drv.outputs)
+    {
+      if (!wantOutput (i->first, wantedOutputs))
+        continue;
+      computeFSClosure (worker.store, i->second.path, outputClosure);
     }
 
-    /* Filter out our own outputs (which we have already checked). */
-    foreach (DerivationOutputs::iterator, i, drv.outputs)
-        outputClosure.erase(i->second.path);
-
-    /* Get all dependencies of this derivation so that we know which
-       derivation is responsible for which path in the output
-       closure. */
-    PathSet inputClosure;
-    computeFSClosure(worker.store, drvPath, inputClosure);
-    std::map<Path, Path> outputsToDrv;
-    foreach (PathSet::iterator, i, inputClosure)
-        if (isDerivation(*i)) {
-            Derivation drv = derivationFromPath(worker.store, *i);
-            foreach (DerivationOutputs::iterator, j, drv.outputs)
-                outputsToDrv[j->second.path] = *i;
-        }
+  /* Filter out our own outputs (which we have already checked). */
+  foreach (DerivationOutputs::iterator, i, drv.outputs)
+    outputClosure.erase (i->second.path);
+
+  /* Get all dependencies of this derivation so that we know which
+     derivation is responsible for which path in the output
+     closure. */
+  PathSet inputClosure;
+  computeFSClosure (worker.store, drvPath, inputClosure);
+  std::map<Path, Path> outputsToDrv;
+  foreach (PathSet::iterator, i, inputClosure)
+    if (isDerivation (*i))
+      {
+        Derivation drv = derivationFromPath (worker.store, *i);
+        foreach (DerivationOutputs::iterator, j, drv.outputs)
+          outputsToDrv[j->second.path] = *i;
+      }
 
-    /* Check each path (slow!). */
-    PathSet broken;
-    foreach (PathSet::iterator, i, outputClosure) {
-        if (worker.store.pathContentsGood(*i)) continue;
-        printMsg(lvlError, format("found corrupted or missing path `%1%' in the output closure of `%2%'") % *i % drvPath);
-        Path drvPath2 = outputsToDrv[*i];
-        if (drvPath2 == "")
-            addWaitee(worker.makeSubstitutionGoal(*i, true));
-        else
-            addWaitee(worker.makeDerivationGoal(drvPath2, PathSet(), bmRepair));
+  /* Check each path (slow!). */
+  PathSet broken;
+  foreach (PathSet::iterator, i, outputClosure)
+    {
+      if (worker.store.pathContentsGood (*i))
+        continue;
+      printMsg (lvlError, format ("found corrupted or missing path `%1%' in "
+                                  "the output closure of `%2%'")
+                              % *i % drvPath);
+      Path drvPath2 = outputsToDrv[*i];
+      if (drvPath2 == "")
+        addWaitee (worker.makeSubstitutionGoal (*i, true));
+      else
+        addWaitee (worker.makeDerivationGoal (drvPath2, PathSet (), bmRepair));
     }
 
-    if (waitees.empty()) {
-        done(BuildResult::AlreadyValid);
-        return;
+  if (waitees.empty ())
+    {
+      done (BuildResult::AlreadyValid);
+      return;
     }
 
-    state = &DerivationGoal::closureRepaired;
+  state = &DerivationGoal::closureRepaired;
 }
 
-
-void DerivationGoal::closureRepaired()
+void
+DerivationGoal::closureRepaired ()
 {
-    trace("closure repaired");
-    if (nrFailed > 0)
-        throw Error(format("some paths in the output closure of derivation ‘%1%’ could not be repaired") % drvPath);
-    done(BuildResult::AlreadyValid);
+  trace ("closure repaired");
+  if (nrFailed > 0)
+    throw Error (format ("some paths in the output closure of derivation "
+                         "‘%1%’ could not be repaired")
+                 % drvPath);
+  done (BuildResult::AlreadyValid);
 }
 
-
-void DerivationGoal::inputsRealised()
+void
+DerivationGoal::inputsRealised ()
 {
-    trace("all inputs realised");
+  trace ("all inputs realised");
 
-    if (nrFailed != 0) {
-        printMsg(lvlError,
-            format("cannot build derivation `%1%': %2% dependencies couldn't be built")
-            % drvPath % nrFailed);
-        done(BuildResult::DependencyFailed);
-        return;
+  if (nrFailed != 0)
+    {
+      printMsg (lvlError, format ("cannot build derivation `%1%': %2% "
+                                  "dependencies couldn't be built")
+                              % drvPath % nrFailed);
+      done (BuildResult::DependencyFailed);
+      return;
     }
 
-    if (retrySubstitution) {
-        haveDerivation();
-        return;
+  if (retrySubstitution)
+    {
+      haveDerivation ();
+      return;
     }
 
-    /* Gather information necessary for computing the closure and/or
-       running the build hook. */
+  /* Gather information necessary for computing the closure and/or
+     running the build hook. */
 
-    /* The outputs are referenceable paths. */
-    foreach (DerivationOutputs::iterator, i, drv.outputs) {
-        debug(format("building path `%1%'") % i->second.path);
-        allPaths.insert(i->second.path);
+  /* The outputs are referenceable paths. */
+  foreach (DerivationOutputs::iterator, i, drv.outputs)
+    {
+      debug (format ("building path `%1%'") % i->second.path);
+      allPaths.insert (i->second.path);
     }
 
-    /* Determine the full set of input paths. */
+  /* Determine the full set of input paths. */
 
-    /* First, the input derivations. */
-    foreach (DerivationInputs::iterator, i, drv.inputDrvs) {
-        /* Add the relevant output closures of the input derivation
-           `*i' as input paths.  Only add the closures of output paths
-           that are specified as inputs. */
-        assert(worker.store.isValidPath(i->first));
-        Derivation inDrv = derivationFromPath(worker.store, i->first);
-        foreach (StringSet::iterator, j, i->second)
-            if (inDrv.outputs.find(*j) != inDrv.outputs.end())
-                computeFSClosure(worker.store, inDrv.outputs[*j].path, inputPaths);
-            else
-                throw Error(
-                    format("derivation `%1%' requires non-existent output `%2%' from input derivation `%3%'")
-                    % drvPath % *j % i->first);
+  /* First, the input derivations. */
+  foreach (DerivationInputs::iterator, i, drv.inputDrvs)
+    {
+      /* Add the relevant output closures of the input derivation
+         `*i' as input paths.  Only add the closures of output paths
+         that are specified as inputs. */
+      assert (worker.store.isValidPath (i->first));
+      Derivation inDrv = derivationFromPath (worker.store, i->first);
+      foreach (StringSet::iterator, j, i->second)
+        if (inDrv.outputs.find (*j) != inDrv.outputs.end ())
+          computeFSClosure (worker.store, inDrv.outputs[*j].path, inputPaths);
+        else
+          throw Error (format ("derivation `%1%' requires non-existent output "
+                               "`%2%' from input derivation `%3%'")
+                       % drvPath % *j % i->first);
     }
 
-    /* Second, the input sources. */
-    foreach (PathSet::iterator, i, drv.inputSrcs)
-        computeFSClosure(worker.store, *i, inputPaths);
+  /* Second, the input sources. */
+  foreach (PathSet::iterator, i, drv.inputSrcs)
+    computeFSClosure (worker.store, *i, inputPaths);
 
-    debug(format("added input paths %1%") % showPaths(inputPaths));
+  debug (format ("added input paths %1%") % showPaths (inputPaths));
 
-    allPaths.insert(inputPaths.begin(), inputPaths.end());
+  allPaths.insert (inputPaths.begin (), inputPaths.end ());
 
-    /* Is this a fixed-output derivation? */
-    fixedOutput = true;
-    for (auto & i : drv.outputs)
-	if (i.second.hash == "") fixedOutput = false;
+  /* Is this a fixed-output derivation? */
+  fixedOutput = true;
+  for (auto &i : drv.outputs)
+    if (i.second.hash == "")
+      fixedOutput = false;
 
-    /* Don't repeat fixed-output derivations since they're already
-       verified by their output hash.*/
-    nrRounds = fixedOutput ? 1 : settings.get("build-repeat", 0) + 1;
+  /* Don't repeat fixed-output derivations since they're already
+     verified by their output hash.*/
+  nrRounds = fixedOutput ? 1 : settings.get ("build-repeat", 0) + 1;
 
-    /* Okay, try to build.  Note that here we don't wait for a build
-       slot to become available, since we don't need one if there is a
-       build hook. */
-    state = &DerivationGoal::tryToBuild;
-    worker.wakeUp(shared_from_this());
+  /* Okay, try to build.  Note that here we don't wait for a build
+     slot to become available, since we don't need one if there is a
+     build hook. */
+  state = &DerivationGoal::tryToBuild;
+  worker.wakeUp (shared_from_this ());
 }
 
-
-static bool canBuildLocally(const string & platform)
+static bool
+canBuildLocally (const string &platform)
 {
-    return platform == settings.thisSystem
+  return platform == settings.thisSystem
 #if __linux__
-        || (platform == "i686-linux" && settings.thisSystem == "x86_64-linux")
-        || (platform == "armhf-linux" && settings.thisSystem == "aarch64-linux")
+         || (platform == "i686-linux" && settings.thisSystem == "x86_64-linux")
+         || (platform == "armhf-linux"
+             && settings.thisSystem == "aarch64-linux")
 #endif
-        ;
+      ;
 }
 
-
-static string get(const StringPairs & map, const string & key, const string & def = "")
+static string
+get (const StringPairs &map, const string &key, const string &def = "")
 {
-    StringPairs::const_iterator i = map.find(key);
-    return i == map.end() ? def : i->second;
+  StringPairs::const_iterator i = map.find (key);
+  return i == map.end () ? def : i->second;
 }
 
-
-bool willBuildLocally(const Derivation & drv)
+bool
+willBuildLocally (const Derivation &drv)
 {
-    return get(drv.env, "preferLocalBuild") == "1" && canBuildLocally(drv.platform);
+  return get (drv.env, "preferLocalBuild") == "1"
+         && canBuildLocally (drv.platform);
 }
 
-
-bool substitutesAllowed(const Derivation & drv)
+bool
+substitutesAllowed (const Derivation &drv)
 {
-    return get(drv.env, "allowSubstitutes", "1") == "1";
+  return get (drv.env, "allowSubstitutes", "1") == "1";
 }
 
-
-void DerivationGoal::tryToBuild()
+void
+DerivationGoal::tryToBuild ()
 {
-    trace("trying to build");
+  trace ("trying to build");
+
+  /* Check for the possibility that some other goal in this process
+     has locked the output since we checked in haveDerivation().
+     (It can't happen between here and the lockPaths() call below
+     because we're not allowing multi-threading.)  If so, put this
+     goal to sleep until another goal finishes, then try again. */
+  foreach (DerivationOutputs::iterator, i, drv.outputs)
+    if (pathIsLockedByMe (i->second.path))
+      {
+        debug (format ("putting derivation `%1%' to sleep because `%2%' is "
+                       "locked by another goal")
+               % drvPath % i->second.path);
+        worker.waitForAnyGoal (shared_from_this ());
+        return;
+      }
 
-    /* Check for the possibility that some other goal in this process
-       has locked the output since we checked in haveDerivation().
-       (It can't happen between here and the lockPaths() call below
-       because we're not allowing multi-threading.)  If so, put this
-       goal to sleep until another goal finishes, then try again. */
-    foreach (DerivationOutputs::iterator, i, drv.outputs)
-        if (pathIsLockedByMe(i->second.path)) {
-            debug(format("putting derivation `%1%' to sleep because `%2%' is locked by another goal")
-                % drvPath % i->second.path);
-            worker.waitForAnyGoal(shared_from_this());
-            return;
-        }
+  /* Obtain locks on all output paths.  The locks are automatically
+     released when we exit this function or the client crashes.  If we
+     can't acquire the lock, then continue; hopefully some other
+     goal can start a build, and if not, the main loop will sleep a
+     few seconds and then retry this goal. */
+  if (!outputLocks.lockPaths (outputPaths (drv), "", false))
+    {
+      worker.waitForAWhile (shared_from_this ());
+      return;
+    }
 
-    /* Obtain locks on all output paths.  The locks are automatically
-       released when we exit this function or the client crashes.  If we
-       can't acquire the lock, then continue; hopefully some other
-       goal can start a build, and if not, the main loop will sleep a
-       few seconds and then retry this goal. */
-    if (!outputLocks.lockPaths(outputPaths(drv), "", false)) {
-        worker.waitForAWhile(shared_from_this());
-        return;
+  /* Now check again whether the outputs are valid.  This is because
+     another process may have started building in parallel.  After
+     it has finished and released the locks, we can (and should)
+     reuse its results.  (Strictly speaking the first check can be
+     omitted, but that would be less efficient.)  Note that since we
+     now hold the locks on the output paths, no other process can
+     build this derivation, so no further checks are necessary. */
+  validPaths = checkPathValidity (true, buildMode == bmRepair);
+  if (buildMode != bmCheck && validPaths.size () == drv.outputs.size ())
+    {
+      debug (
+          format ("skipping build of derivation `%1%', someone beat us to it")
+          % drvPath);
+      outputLocks.setDeletion (true);
+      outputLocks.unlock ();
+      done (BuildResult::AlreadyValid);
+      return;
     }
 
-    /* Now check again whether the outputs are valid.  This is because
-       another process may have started building in parallel.  After
-       it has finished and released the locks, we can (and should)
-       reuse its results.  (Strictly speaking the first check can be
-       omitted, but that would be less efficient.)  Note that since we
-       now hold the locks on the output paths, no other process can
-       build this derivation, so no further checks are necessary. */
-    validPaths = checkPathValidity(true, buildMode == bmRepair);
-    if (buildMode != bmCheck && validPaths.size() == drv.outputs.size()) {
-        debug(format("skipping build of derivation `%1%', someone beat us to it") % drvPath);
-        outputLocks.setDeletion(true);
-        outputLocks.unlock();
-        done(BuildResult::AlreadyValid);
-        return;
+  missingPaths = outputPaths (drv);
+  if (buildMode != bmCheck)
+    foreach (PathSet::iterator, i, validPaths)
+      missingPaths.erase (*i);
+
+  /* If any of the outputs already exist but are not valid, delete
+     them. */
+  foreach (DerivationOutputs::iterator, i, drv.outputs)
+    {
+      Path path = i->second.path;
+      if (worker.store.isValidPath (path))
+        continue;
+      if (!pathExists (path))
+        continue;
+      debug (format ("removing invalid path `%1%'") % path);
+      deletePath (path);
     }
 
-    missingPaths = outputPaths(drv);
-    if (buildMode != bmCheck)
-        foreach (PathSet::iterator, i, validPaths) missingPaths.erase(*i);
-
-    /* If any of the outputs already exist but are not valid, delete
-       them. */
-    foreach (DerivationOutputs::iterator, i, drv.outputs) {
-        Path path = i->second.path;
-        if (worker.store.isValidPath(path)) continue;
-        if (!pathExists(path)) continue;
-        debug(format("removing invalid path `%1%'") % path);
-        deletePath(path);
-    }
-
-    /* Check again whether any output previously failed to build,
-       because some other process may have tried and failed before we
-       acquired the lock. */
-    foreach (DerivationOutputs::iterator, i, drv.outputs)
-        if (pathFailed(i->second.path)) return;
-
-    /* Don't do a remote build if the derivation has the attribute
-       `preferLocalBuild' set.  Also, check and repair modes are only
-       supported for local builds. */
-    bool buildLocally = buildMode != bmNormal || willBuildLocally(drv);
-
-    /* Is the build hook willing to accept this job? */
-    if (!buildLocally) {
-        switch (tryBuildHook()) {
-            case rpAccept:
-                /* Yes, it has started doing so.  Wait until we get
-                   EOF from the hook. */
-                state = &DerivationGoal::buildDone;
-                return;
-            case rpPostpone:
-                /* Not now; wait until at least one child finishes or
-                   the wake-up timeout expires. */
-                worker.waitForAWhile(shared_from_this());
-                outputLocks.unlock();
-                return;
-            case rpDecline:
-                /* We should do it ourselves. */
-                break;
-        }
-    }
-
-    /* Make sure that we are allowed to start a build.  If this
-       derivation prefers to be done locally, do it even if
-       maxBuildJobs is 0. */
-    unsigned int curBuilds = worker.getNrLocalBuilds();
-    if (curBuilds >= settings.maxBuildJobs && !(buildLocally && curBuilds == 0)) {
-        worker.waitForBuildSlot(shared_from_this());
-        outputLocks.unlock();
-        return;
+  /* Check again whether any output previously failed to build,
+     because some other process may have tried and failed before we
+     acquired the lock. */
+  foreach (DerivationOutputs::iterator, i, drv.outputs)
+    if (pathFailed (i->second.path))
+      return;
+
+  /* Don't do a remote build if the derivation has the attribute
+     `preferLocalBuild' set.  Also, check and repair modes are only
+     supported for local builds. */
+  bool buildLocally = buildMode != bmNormal || willBuildLocally (drv);
+
+  /* Is the build hook willing to accept this job? */
+  if (!buildLocally)
+    {
+      switch (tryBuildHook ())
+        {
+        case rpAccept:
+          /* Yes, it has started doing so.  Wait until we get
+             EOF from the hook. */
+          state = &DerivationGoal::buildDone;
+          return;
+        case rpPostpone:
+          /* Not now; wait until at least one child finishes or
+             the wake-up timeout expires. */
+          worker.waitForAWhile (shared_from_this ());
+          outputLocks.unlock ();
+          return;
+        case rpDecline:
+          /* We should do it ourselves. */
+          break;
+        }
     }
 
-    try {
+  /* Make sure that we are allowed to start a build.  If this
+     derivation prefers to be done locally, do it even if
+     maxBuildJobs is 0. */
+  unsigned int curBuilds = worker.getNrLocalBuilds ();
+  if (curBuilds >= settings.maxBuildJobs && !(buildLocally && curBuilds == 0))
+    {
+      worker.waitForBuildSlot (shared_from_this ());
+      outputLocks.unlock ();
+      return;
+    }
 
-        /* Okay, we have to build. */
-        startBuilder();
+  try
+    {
 
-    } catch (BuildError & e) {
-        printMsg(lvlError, e.msg());
-        outputLocks.unlock();
-        buildUser.release();
-        if (settings.printBuildTrace)
-            printMsg(lvlError, format("@ build-failed %1% - %2% %3%")
-                % drvPath % 0 % e.msg());
-        worker.permanentFailure = true;
-        done(BuildResult::InputRejected, e.msg());
-        return;
+      /* Okay, we have to build. */
+      startBuilder ();
+    }
+  catch (BuildError &e)
+    {
+      printMsg (lvlError, e.msg ());
+      outputLocks.unlock ();
+      buildUser.release ();
+      if (settings.printBuildTrace)
+        printMsg (lvlError, format ("@ build-failed %1% - %2% %3%") % drvPath
+                                % 0 % e.msg ());
+      worker.permanentFailure = true;
+      done (BuildResult::InputRejected, e.msg ());
+      return;
     }
 
-    /* This state will be reached when we get EOF on the child's
-       log pipe. */
-    state = &DerivationGoal::buildDone;
+  /* This state will be reached when we get EOF on the child's
+     log pipe. */
+  state = &DerivationGoal::buildDone;
 }
 
-
-void replaceValidPath(const Path & storePath, const Path tmpPath)
+void
+replaceValidPath (const Path &storePath, const Path tmpPath)
 {
-    /* We can't atomically replace storePath (the original) with
-       tmpPath (the replacement), so we have to move it out of the
-       way first.  We'd better not be interrupted here, because if
-       we're repairing (say) Glibc, we end up with a broken system. */
-    Path oldPath = (format("%1%.old-%2%-%3%") % storePath % getpid() % rand()).str();
-    if (pathExists(storePath))
-        rename(storePath.c_str(), oldPath.c_str());
-    if (rename(tmpPath.c_str(), storePath.c_str()) == -1)
-        throw SysError(format("moving `%1%' to `%2%'") % tmpPath % storePath);
-    if (pathExists(oldPath))
-        deletePath(oldPath);
+  /* We can't atomically replace storePath (the original) with
+     tmpPath (the replacement), so we have to move it out of the
+     way first.  We'd better not be interrupted here, because if
+     we're repairing (say) Glibc, we end up with a broken system. */
+  Path oldPath
+      = (format ("%1%.old-%2%-%3%") % storePath % getpid () % rand ()).str ();
+  if (pathExists (storePath))
+    rename (storePath.c_str (), oldPath.c_str ());
+  if (rename (tmpPath.c_str (), storePath.c_str ()) == -1)
+    throw SysError (format ("moving `%1%' to `%2%'") % tmpPath % storePath);
+  if (pathExists (oldPath))
+    deletePath (oldPath);
 }
 
+MakeError (NotDeterministic, BuildError)
 
-MakeError(NotDeterministic, BuildError)
-
-
-/* Recursively make the file permissions of a path safe for exposure to
-   arbitrary users, but without canonicalising its permissions, timestamp, and
-   user.  Throw an exception if a file type that isn't explicitly known to be
-   safe is found. */
-static void secureFilePerms(Path path)
+    /* Recursively make the file permissions of a path safe for exposure to
+       arbitrary users, but without canonicalising its permissions, timestamp,
+       and user.  Throw an exception if a file type that isn't explicitly known
+       to be safe is found. */
+    static void secureFilePerms (Path path)
 {
   struct stat st;
-  if (lstat(path.c_str(), &st)) return;
-
-  switch(st.st_mode & S_IFMT) {
-  case S_IFLNK:
+  if (lstat (path.c_str (), &st))
     return;
 
-  case S_IFDIR:
-    for (auto & i : readDirectory(path)) {
-      secureFilePerms(path + "/" + i.name);
-    }
-    /* FALLTHROUGH */
+  switch (st.st_mode & S_IFMT)
+    {
+    case S_IFLNK:
+      return;
 
-  case S_IFREG:
-    chmod(path.c_str(), (st.st_mode & ~S_IFMT) & ~(S_ISUID | S_ISGID | S_IWOTH));
-    break;
+    case S_IFDIR:
+      for (auto &i : readDirectory (path))
+        {
+          secureFilePerms (path + "/" + i.name);
+        }
+      /* FALLTHROUGH */
 
-  default:
-    throw Error(format("file `%1%' has an unsupported type") % path);
-  }
+    case S_IFREG:
+      chmod (path.c_str (),
+             (st.st_mode & ~S_IFMT) & ~(S_ISUID | S_ISGID | S_IWOTH));
+      break;
+
+    default:
+      throw Error (format ("file `%1%' has an unsupported type") % path);
+    }
 }
 
-void DerivationGoal::buildDone()
+void
+DerivationGoal::buildDone ()
 {
-    trace("build done");
-
-    /* Since we got an EOF on the logger pipe, the builder is presumed
-       to have terminated.  In fact, the builder could also have
-       simply have closed its end of the pipe --- just don't do that
-       :-) */
-    int status;
-    pid_t savedPid;
-    if (hook) {
-        savedPid = hook->pid;
-        status = hook->pid.wait(true);
-    } else {
-        /* !!! this could block! security problem! solution: kill the
-           child */
-        savedPid = pid;
-        status = pid.wait(true);
+  trace ("build done");
+
+  /* Since we got an EOF on the logger pipe, the builder is presumed
+     to have terminated.  In fact, the builder could also have
+     simply have closed its end of the pipe --- just don't do that
+     :-) */
+  int status;
+  pid_t savedPid;
+  if (hook)
+    {
+      savedPid = hook->pid;
+      status = hook->pid.wait (true);
+    }
+  else
+    {
+      /* !!! this could block! security problem! solution: kill the
+         child */
+      savedPid = pid;
+      status = pid.wait (true);
     }
 
-    debug(format("builder process for `%1%' finished") % drvPath);
+  debug (format ("builder process for `%1%' finished") % drvPath);
 
-    /* So the child is gone now. */
-    worker.childTerminated(savedPid);
+  /* So the child is gone now. */
+  worker.childTerminated (savedPid);
 
-    /* Close the read side of the logger pipe. */
-    if (hook) {
-        hook->builderOut.readSide.close();
-        hook->fromAgent.readSide.close();
+  /* Close the read side of the logger pipe. */
+  if (hook)
+    {
+      hook->builderOut.readSide.close ();
+      hook->fromAgent.readSide.close ();
     }
-    else builderOut.readSide.close();
+  else
+    builderOut.readSide.close ();
 
-    /* Close the log file. */
-    closeLogFile();
+  /* Close the log file. */
+  closeLogFile ();
 
-    /* When running under a build user, make sure that all processes
-       running under that uid are gone.  This is to prevent a
-       malicious user from leaving behind a process that keeps files
-       open and modifies them after they have been chown'ed to
-       root. */
-    if (buildUser.enabled()) buildUser.kill();
+  /* When running under a build user, make sure that all processes
+     running under that uid are gone.  This is to prevent a
+     malicious user from leaving behind a process that keeps files
+     open and modifies them after they have been chown'ed to
+     root. */
+  if (buildUser.enabled ())
+    buildUser.kill ();
 
-    bool diskFull = false;
+  bool diskFull = false;
 
-    try {
+  try
+    {
 
-        /* Check the exit status. */
-        if (!statusOk(status)) {
+      /* Check the exit status. */
+      if (!statusOk (status))
+        {
 
-            /* Heuristically check whether the build failure may have
-               been caused by a disk full condition.  We have no way
-               of knowing whether the build actually got an ENOSPC.
-               So instead, check if the disk is (nearly) full now.  If
-               so, we don't mark this build as a permanent failure. */
+          /* Heuristically check whether the build failure may have
+             been caused by a disk full condition.  We have no way
+             of knowing whether the build actually got an ENOSPC.
+             So instead, check if the disk is (nearly) full now.  If
+             so, we don't mark this build as a permanent failure. */
 #if HAVE_STATVFS
-            unsigned long long required = 8ULL * 1024 * 1024; // FIXME: make configurable
-            struct statvfs st;
-            if (statvfs(settings.nixStore.c_str(), &st) == 0 &&
-                (unsigned long long) st.f_bavail * st.f_bsize < required)
-                diskFull = true;
-            if (statvfs(tmpDir.c_str(), &st) == 0 &&
-                (unsigned long long) st.f_bavail * st.f_bsize < required)
-                diskFull = true;
+          unsigned long long required
+              = 8ULL * 1024 * 1024; // FIXME: make configurable
+          struct statvfs st;
+          if (statvfs (settings.nixStore.c_str (), &st) == 0
+              && (unsigned long long)st.f_bavail * st.f_bsize < required)
+            diskFull = true;
+          if (statvfs (tmpDir.c_str (), &st) == 0
+              && (unsigned long long)st.f_bavail * st.f_bsize < required)
+            diskFull = true;
 #endif
 
-            deleteTmpDir(false);
-
-            /* Move paths out of the chroot for easier debugging of
-               build failures. */
-            if (useChroot && buildMode == bmNormal)
-                foreach (PathSet::iterator, i, missingPaths)
-                    if (pathExists(chrootRootDir + *i)) {
-                      try {
-                        secureFilePerms(chrootRootDir + *i);
-                        rename((chrootRootDir + *i).c_str(), i->c_str());
-                      } catch(Error & e) {
-                        printMsg(lvlError, e.msg());
-                      }
+          deleteTmpDir (false);
+
+          /* Move paths out of the chroot for easier debugging of
+             build failures. */
+          if (useChroot && buildMode == bmNormal)
+            foreach (PathSet::iterator, i, missingPaths)
+              if (pathExists (chrootRootDir + *i))
+                {
+                  try
+                    {
+                      secureFilePerms (chrootRootDir + *i);
+                      rename ((chrootRootDir + *i).c_str (), i->c_str ());
+                    }
+                  catch (Error &e)
+                    {
+                      printMsg (lvlError, e.msg ());
                     }
+                }
 
-            if (diskFull)
-                printMsg(lvlError, "note: build failure may have been caused by lack of free disk space");
-
-            throw BuildError(format("builder for `%1%' %2%")
-                % drvPath % statusToString(status));
-        }
-
-	if (fixedOutput) {
-	    /* Replace the output, if it exists, by a fresh copy of itself to
-               make sure that there's no stale file descriptor pointing to it
-               (CVE-2024-27297).  */
-	    foreach (DerivationOutputs::iterator, i, drv.outputs) {
-		Path output = chrootRootDir + i->second.path;
-		if (pathExists(output)) {
-		    Path pivot = output + ".tmp";
-		    copyFileRecursively(output, pivot, true);
-		    int err = rename(pivot.c_str(), output.c_str());
-		    if (err != 0)
-			throw SysError(format("renaming `%1%' to `%2%'")
-				       % pivot % output);
-		}
-	    }
-	}
-
-        /* Compute the FS closure of the outputs and register them as
-           being valid. */
-        registerOutputs();
-
-        /* Delete unused redirected outputs (when doing hash rewriting). */
-        foreach (RedirectedOutputs::iterator, i, redirectedOutputs)
-            if (pathExists(i->second)) deletePath(i->second);
-
-        /* Delete the chroot (if we were using one). */
-        autoDelChroot.reset(); /* this runs the destructor */
-
-        deleteTmpDir(true);
-
-        /* Repeat the build if necessary. */
-        if (curRound++ < nrRounds) {
-            outputLocks.unlock();
-            buildUser.release();
-            state = &DerivationGoal::tryToBuild;
-            worker.wakeUp(shared_from_this());
-            return;
-        }
-
-        /* It is now safe to delete the lock files, since all future
-           lockers will see that the output paths are valid; they will
-           not create new lock files with the same names as the old
-           (unlinked) lock files. */
-        outputLocks.setDeletion(true);
-        outputLocks.unlock();
-
-    } catch (BuildError & e) {
-        if (!hook)
-            printMsg(lvlError, e.msg());
-        outputLocks.unlock();
-        buildUser.release();
-
-        BuildResult::Status st = BuildResult::MiscFailure;
-
-        if (hook && WIFEXITED(status) && WEXITSTATUS(status) == 101) {
-            if (settings.printBuildTrace)
-                printMsg(lvlError, format("@ build-failed %1% - timeout") % drvPath);
-            st = BuildResult::TimedOut;
-        }
-
-        else if (hook && (!WIFEXITED(status) || WEXITSTATUS(status) != 100)) {
-            if (settings.printBuildTrace)
-                printMsg(lvlError, format("@ hook-failed %1% - %2% %3%")
-                    % drvPath % status % e.msg());
-        }
-
-        else {
-            if (settings.printBuildTrace)
-                printMsg(lvlError, format("@ build-failed %1% - %2% %3%")
-                    % drvPath % 1 % e.msg());
-
-            st =
-                statusOk(status) ? BuildResult::OutputRejected :
-                fixedOutput || diskFull ? BuildResult::TransientFailure :
-                BuildResult::PermanentFailure;
-
-            /* Register the outputs of this build as "failed" so we
-               won't try to build them again (negative caching).
-               However, don't do this for fixed-output derivations,
-               since they're likely to fail for transient reasons
-               (e.g., fetchurl not being able to access the network).
-               Hook errors (like communication problems with the
-               remote machine) shouldn't be cached either. */
-            if (settings.cacheFailure && !fixedOutput && !diskFull)
-                foreach (DerivationOutputs::iterator, i, drv.outputs)
-                    worker.store.registerFailedPath(i->second.path);
-        }
-
-        done(st, e.msg());
-        return;
-    }
+          if (diskFull)
+            printMsg (lvlError, "note: build failure may have been caused by "
+                                "lack of free disk space");
 
-    /* Release the build user, if applicable. */
-    buildUser.release();
+          throw BuildError (format ("builder for `%1%' %2%") % drvPath
+                            % statusToString (status));
+        }
 
-    if (settings.printBuildTrace)
-        printMsg(lvlError, format("@ build-succeeded %1% -") % drvPath);
+      if (fixedOutput)
+        {
+          /* Replace the output, if it exists, by a fresh copy of itself to
+             make sure that there's no stale file descriptor pointing to it
+             (CVE-2024-27297).  */
+          foreach (DerivationOutputs::iterator, i, drv.outputs)
+            {
+              Path output = chrootRootDir + i->second.path;
+              if (pathExists (output))
+                {
+                  Path pivot = output + ".tmp";
+                  copyFileRecursively (output, pivot, true);
+                  int err = rename (pivot.c_str (), output.c_str ());
+                  if (err != 0)
+                    throw SysError (format ("renaming `%1%' to `%2%'") % pivot
+                                    % output);
+                }
+            }
+        }
 
-    done(BuildResult::Built);
-}
+      /* Compute the FS closure of the outputs and register them as
+         being valid. */
+      registerOutputs ();
 
+      /* Delete unused redirected outputs (when doing hash rewriting). */
+      foreach (RedirectedOutputs::iterator, i, redirectedOutputs)
+        if (pathExists (i->second))
+          deletePath (i->second);
 
-HookReply DerivationGoal::tryBuildHook()
-{
-    if (!settings.useBuildHook) return rpDecline;
+      /* Delete the chroot (if we were using one). */
+      autoDelChroot.reset (); /* this runs the destructor */
+
+      deleteTmpDir (true);
 
-    if (!worker.hook) {
-	Strings args = {
-	    "offload",
-	    settings.thisSystem.c_str(),
-            (format("%1%") % settings.maxSilentTime).str().c_str(),
-            (format("%1%") % settings.printBuildTrace).str().c_str(),
-            (format("%1%") % settings.buildTimeout).str().c_str()
-	};
+      /* Repeat the build if necessary. */
+      if (curRound++ < nrRounds)
+        {
+          outputLocks.unlock ();
+          buildUser.release ();
+          state = &DerivationGoal::tryToBuild;
+          worker.wakeUp (shared_from_this ());
+          return;
+        }
 
-        worker.hook = std::make_shared<Agent>(settings.guixProgram, args);
+      /* It is now safe to delete the lock files, since all future
+         lockers will see that the output paths are valid; they will
+         not create new lock files with the same names as the old
+         (unlinked) lock files. */
+      outputLocks.setDeletion (true);
+      outputLocks.unlock ();
     }
+  catch (BuildError &e)
+    {
+      if (!hook)
+        printMsg (lvlError, e.msg ());
+      outputLocks.unlock ();
+      buildUser.release ();
+
+      BuildResult::Status st = BuildResult::MiscFailure;
 
-    /* Tell the hook about system features (beyond the system type)
-       required from the build machine.  (The hook could parse the
-       drv file itself, but this is easier.) */
-    Strings features = tokenizeString<Strings>(get(drv.env, "requiredSystemFeatures"));
-    foreach (Strings::iterator, i, features) checkStoreName(*i); /* !!! abuse */
+      if (hook && WIFEXITED (status) && WEXITSTATUS (status) == 101)
+        {
+          if (settings.printBuildTrace)
+            printMsg (lvlError,
+                      format ("@ build-failed %1% - timeout") % drvPath);
+          st = BuildResult::TimedOut;
+        }
 
-    /* Send the request to the hook. */
-    writeLine(worker.hook->toAgent.writeSide, (format("%1% %2% %3% %4%")
-        % (worker.getNrLocalBuilds() < settings.maxBuildJobs ? "1" : "0")
-        % drv.platform % drvPath % concatStringsSep(",", features)).str());
+      else if (hook && (!WIFEXITED (status) || WEXITSTATUS (status) != 100))
+        {
+          if (settings.printBuildTrace)
+            printMsg (lvlError, format ("@ hook-failed %1% - %2% %3%")
+                                    % drvPath % status % e.msg ());
+        }
 
-    /* Read the first line of input, which should be a word indicating
-       whether the hook wishes to perform the build. */
-    string reply;
-    while (true) {
-        string s = readLine(worker.hook->fromAgent.readSide);
-        if (string(s, 0, 2) == "# ") {
-            reply = string(s, 2);
-            break;
+      else
+        {
+          if (settings.printBuildTrace)
+            printMsg (lvlError, format ("@ build-failed %1% - %2% %3%")
+                                    % drvPath % 1 % e.msg ());
+
+          st = statusOk (status)         ? BuildResult::OutputRejected
+               : fixedOutput || diskFull ? BuildResult::TransientFailure
+                                         : BuildResult::PermanentFailure;
+
+          /* Register the outputs of this build as "failed" so we
+             won't try to build them again (negative caching).
+             However, don't do this for fixed-output derivations,
+             since they're likely to fail for transient reasons
+             (e.g., fetchurl not being able to access the network).
+             Hook errors (like communication problems with the
+             remote machine) shouldn't be cached either. */
+          if (settings.cacheFailure && !fixedOutput && !diskFull)
+            foreach (DerivationOutputs::iterator, i, drv.outputs)
+              worker.store.registerFailedPath (i->second.path);
         }
-        s += "\n";
-        writeToStderr(s);
+
+      done (st, e.msg ());
+      return;
     }
 
-    debug(format("hook reply is `%1%'") % reply);
+  /* Release the build user, if applicable. */
+  buildUser.release ();
 
-    if (reply == "decline" || reply == "postpone")
-        return reply == "decline" ? rpDecline : rpPostpone;
-    else if (reply != "accept")
-        throw Error(format("bad hook reply `%1%'") % reply);
+  if (settings.printBuildTrace)
+    printMsg (lvlError, format ("@ build-succeeded %1% -") % drvPath);
 
-    printMsg(lvlTalkative, format("using hook to build path(s) %1%") % showPaths(missingPaths));
+  done (BuildResult::Built);
+}
 
-    hook = worker.hook;
-    worker.hook.reset();
+HookReply
+DerivationGoal::tryBuildHook ()
+{
+  if (!settings.useBuildHook)
+    return rpDecline;
 
-    /* Tell the hook all the inputs that have to be copied to the
-       remote system.  This unfortunately has to contain the entire
-       derivation closure to ensure that the validity invariant holds
-       on the remote system.  (I.e., it's unfortunate that we have to
-       list it since the remote system *probably* already has it.) */
-    PathSet allInputs;
-    allInputs.insert(inputPaths.begin(), inputPaths.end());
-    computeFSClosure(worker.store, drvPath, allInputs);
+  if (!worker.hook)
+    {
+      Strings args
+          = { "offload", settings.thisSystem.c_str (),
+              (format ("%1%") % settings.maxSilentTime).str ().c_str (),
+              (format ("%1%") % settings.printBuildTrace).str ().c_str (),
+              (format ("%1%") % settings.buildTimeout).str ().c_str () };
 
-    string s;
-    foreach (PathSet::iterator, i, allInputs) { s += *i; s += ' '; }
-    writeLine(hook->toAgent.writeSide, s);
+      worker.hook = std::make_shared<Agent> (settings.guixProgram, args);
+    }
 
-    /* Tell the hooks the missing outputs that have to be copied back
-       from the remote system. */
-    s = "";
-    foreach (PathSet::iterator, i, missingPaths) { s += *i; s += ' '; }
-    writeLine(hook->toAgent.writeSide, s);
+  /* Tell the hook about system features (beyond the system type)
+     required from the build machine.  (The hook could parse the
+     drv file itself, but this is easier.) */
+  Strings features
+      = tokenizeString<Strings> (get (drv.env, "requiredSystemFeatures"));
+  foreach (Strings::iterator, i, features)
+    checkStoreName (*i); /* !!! abuse */
+
+  /* Send the request to the hook. */
+  writeLine (
+      worker.hook->toAgent.writeSide,
+      (format ("%1% %2% %3% %4%")
+       % (worker.getNrLocalBuilds () < settings.maxBuildJobs ? "1" : "0")
+       % drv.platform % drvPath % concatStringsSep (",", features))
+          .str ());
+
+  /* Read the first line of input, which should be a word indicating
+     whether the hook wishes to perform the build. */
+  string reply;
+  while (true)
+    {
+      string s = readLine (worker.hook->fromAgent.readSide);
+      if (string (s, 0, 2) == "# ")
+        {
+          reply = string (s, 2);
+          break;
+        }
+      s += "\n";
+      writeToStderr (s);
+    }
 
-    hook->toAgent.writeSide.close();
+  debug (format ("hook reply is `%1%'") % reply);
 
-    /* Create the log file and pipe. */
-    Path logFile = openLogFile();
+  if (reply == "decline" || reply == "postpone")
+    return reply == "decline" ? rpDecline : rpPostpone;
+  else if (reply != "accept")
+    throw Error (format ("bad hook reply `%1%'") % reply);
 
-    set<int> fds;
-    fds.insert(hook->fromAgent.readSide);
-    fds.insert(hook->builderOut.readSide);
-    worker.childStarted(shared_from_this(), hook->pid, fds, false, true);
+  printMsg (lvlTalkative, format ("using hook to build path(s) %1%")
+                              % showPaths (missingPaths));
 
-    if (settings.printBuildTrace)
-        printMsg(lvlError, format("@ build-started %1% - %2% %3% %4%")
-            % drvPath % drv.platform % logFile % hook->pid);
+  hook = worker.hook;
+  worker.hook.reset ();
 
-    return rpAccept;
-}
+  /* Tell the hook all the inputs that have to be copied to the
+     remote system.  This unfortunately has to contain the entire
+     derivation closure to ensure that the validity invariant holds
+     on the remote system.  (I.e., it's unfortunate that we have to
+     list it since the remote system *probably* already has it.) */
+  PathSet allInputs;
+  allInputs.insert (inputPaths.begin (), inputPaths.end ());
+  computeFSClosure (worker.store, drvPath, allInputs);
 
+  string s;
+  foreach (PathSet::iterator, i, allInputs)
+    {
+      s += *i;
+      s += ' ';
+    }
+  writeLine (hook->toAgent.writeSide, s);
 
-void chmod_(const Path & path, mode_t mode)
-{
-    if (chmod(path.c_str(), mode) == -1)
-        throw SysError(format("setting permissions on `%1%'") % path);
-}
+  /* Tell the hooks the missing outputs that have to be copied back
+     from the remote system. */
+  s = "";
+  foreach (PathSet::iterator, i, missingPaths)
+    {
+      s += *i;
+      s += ' ';
+    }
+  writeLine (hook->toAgent.writeSide, s);
+
+  hook->toAgent.writeSide.close ();
 
+  /* Create the log file and pipe. */
+  Path logFile = openLogFile ();
 
-int childEntry(void * arg)
+  set<int> fds;
+  fds.insert (hook->fromAgent.readSide);
+  fds.insert (hook->builderOut.readSide);
+  worker.childStarted (shared_from_this (), hook->pid, fds, false, true);
+
+  if (settings.printBuildTrace)
+    printMsg (lvlError, format ("@ build-started %1% - %2% %3% %4%") % drvPath
+                            % drv.platform % logFile % hook->pid);
+
+  return rpAccept;
+}
+
+void
+chmod_ (const Path &path, mode_t mode)
 {
-    ((DerivationGoal *) arg)->runChild();
-    return 1;
+  if (chmod (path.c_str (), mode) == -1)
+    throw SysError (format ("setting permissions on `%1%'") % path);
 }
 
+int
+childEntry (void *arg)
+{
+  ((DerivationGoal *)arg)->runChild ();
+  return 1;
+}
 
 /* UID and GID of the build user inside its own user namespace.  */
 static const uid_t guestUID = 30001;
 static const gid_t guestGID = 30000;
 
 /* Initialize the user namespace of CHILD.  */
-static void initializeUserNamespace(pid_t child,
-				    uid_t hostUID = getuid(),
-				    gid_t hostGID = getgid())
-{
-    writeFile("/proc/" + std::to_string(child) + "/uid_map",
-	      (format("%d %d 1") % guestUID % hostUID).str());
-
-    writeFile("/proc/" + std::to_string(child) + "/setgroups", "deny");
-
-    writeFile("/proc/" + std::to_string(child) + "/gid_map",
-	      (format("%d %d 1") % guestGID % hostGID).str());
-}
-
-void DerivationGoal::startBuilder()
-{
-    auto f = format(
-        buildMode == bmRepair ? "repairing path(s) %1%" :
-        buildMode == bmCheck ? "checking path(s) %1%" :
-        nrRounds > 1 ? "building path(s) %1% (round %2%/%3%)" :
-        "building path(s) %1%");
-    f.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
-    startNest(nest, lvlInfo, f % showPaths(missingPaths) % curRound % nrRounds);
-
-    /* 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();
-
-    /* Most shells initialise PATH to some default (/bin:/usr/bin:...) when
-       PATH is not set.  We don't want this, so we fill it in with some dummy
-       value. */
-    env["PATH"] = "/path-not-set";
-
-    /* Set HOME to a non-existing path to prevent certain programs from using
-       /etc/passwd (or NIS, or whatever) to locate the home directory (for
-       example, wget looks for ~/.wgetrc).  I.e., these tools use /etc/passwd
-       if HOME is not set, but they will just assume that the settings file
-       they are looking for does not exist if HOME is set but points to some
-       non-existing path. */
-    Path homeDir = "/homeless-shelter";
-    env["HOME"] = homeDir;
-
-    /* Tell the builder where the store is.  Usually they
-       shouldn't care, but this is useful for purity checking (e.g.,
-       the compiler or linker might only want to accept paths to files
-       in the store or in the build directory). */
-    env["NIX_STORE"] = settings.nixStore;
-
-    /* The maximum number of cores to utilize for parallel building. */
-    env["NIX_BUILD_CORES"] = (format("%d") % settings.buildCores).str();
-
-    /* Add all bindings specified in the derivation. */
-    foreach (StringPairs::iterator, i, drv.env)
-        env[i->first] = i->second;
-
-    /* Create a temporary directory where the build will take
-       place. */
-    auto drvName = storePathToName(drvPath);
-    tmpDir = createTempDir("", "guix-build-" + drvName, false, false, 0700);
-
-    if (useChroot) {
-	/* Make the build directory seen by the build process a sub-directory.
-	   That way, "/tmp/guix-build-foo.drv-0" is root-owned, and thus its
-	   permissions cannot be changed by the build process, while
-	   "/tmp/guix-build-foo.drv-0/top" is owned by the build user.  This
-	   cannot be done when !useChroot because then $NIX_BUILD_TOP would
-	   be inaccessible to the build user by its full file name.
-
-	   If the build user could make the build directory world-writable,
-	   then an attacker could create in it a hardlink to a root-owned file
-	   such as /etc/shadow.  If 'keepFailed' is true, the daemon would
-	   then chown that hardlink to the user, giving them write access to
-	   that file.  See CVE-2021-27851.  */
-	tmpDir += "/top";
-	if (mkdir(tmpDir.c_str(), 0700) == 1)
-	    throw SysError("creating top-level build directory");
-    }
-
-    /* In a sandbox, for determinism, always use the same temporary
-       directory. */
-    tmpDirInSandbox = useChroot ? canonPath("/tmp", true) + "/guix-build-" + drvName + "-0" : tmpDir;
-
-    /* For convenience, set an environment pointing to the top build
-       directory. */
-    env["NIX_BUILD_TOP"] = tmpDirInSandbox;
-
-    /* Also set TMPDIR and variants to point to this directory. */
-    env["TMPDIR"] = env["TEMPDIR"] = env["TMP"] = env["TEMP"] = tmpDirInSandbox;
-
-    /* Explicitly set PWD to prevent problems with chroot builds.  In
-       particular, dietlibc cannot figure out the cwd because the
-       inode of the current directory doesn't appear in .. (because
-       getdents returns the inode of the mount point). */
-    env["PWD"] = tmpDirInSandbox;
-
-    /* *Only* if this is a fixed-output derivation, propagate the
-       values of the environment variables specified in the
-       `impureEnvVars' attribute to the builder.  This allows for
-       instance environment variables for proxy configuration such as
-       `http_proxy' to be easily passed to downloaders like
-       `fetchurl'.  Passing such environment variables from the caller
-       to the builder is generally impure, but the output of
-       fixed-output derivations is by definition pure (since we
-       already know the cryptographic hash of the output). */
-    if (fixedOutput) {
-        Strings varNames = tokenizeString<Strings>(get(drv.env, "impureEnvVars"));
-        foreach (Strings::iterator, i, varNames) env[*i] = getEnv(*i);
-    }
-
-    /* The `exportReferencesGraph' feature allows the references graph
-       to be passed to a builder.  This attribute should be a list of
-       pairs [name1 path1 name2 path2 ...].  The references graph of
-       each `pathN' will be stored in a text file `nameN' in the
-       temporary build directory.  The text files have the format used
-       by `nix-store --register-validity'.  However, the deriver
-       fields are left empty. */
-    string s = get(drv.env, "exportReferencesGraph");
-    Strings ss = tokenizeString<Strings>(s);
-    if (ss.size() % 2 != 0)
-        throw BuildError(format("odd number of tokens in `exportReferencesGraph': `%1%'") % s);
-    for (Strings::iterator i = ss.begin(); i != ss.end(); ) {
-        string fileName = *i++;
-        checkStoreName(fileName); /* !!! abuse of this function */
-
-        /* Check that the store path is valid. */
-        Path storePath = *i++;
-        if (!isInStore(storePath))
-            throw BuildError(format("`exportReferencesGraph' contains a non-store path `%1%'")
-                % storePath);
-        storePath = toStorePath(storePath);
-        if (!worker.store.isValidPath(storePath))
-            throw BuildError(format("`exportReferencesGraph' contains an invalid path `%1%'")
-                % storePath);
-
-        /* If there are derivations in the graph, then include their
-           outputs as well.  This is useful if you want to do things
-           like passing all build-time dependencies of some path to a
-           derivation that builds a NixOS DVD image. */
-        PathSet paths, paths2;
-        computeFSClosure(worker.store, storePath, paths);
-        paths2 = paths;
-
-        foreach (PathSet::iterator, j, paths2) {
-            if (isDerivation(*j)) {
-                Derivation drv = derivationFromPath(worker.store, *j);
-                foreach (DerivationOutputs::iterator, k, drv.outputs)
-                    computeFSClosure(worker.store, k->second.path, paths);
-            }
-        }
+static void
+initializeUserNamespace (pid_t child, uid_t hostUID = getuid (),
+                         gid_t hostGID = getgid ())
+{
+  writeFile ("/proc/" + std::to_string (child) + "/uid_map",
+             (format ("%d %d 1") % guestUID % hostUID).str ());
+
+  writeFile ("/proc/" + std::to_string (child) + "/setgroups", "deny");
 
-        /* Write closure info to `fileName'. */
-        writeFile(tmpDir + "/" + fileName,
-            worker.store.makeValidityRegistration(paths, false, false));
+  writeFile ("/proc/" + std::to_string (child) + "/gid_map",
+             (format ("%d %d 1") % guestGID % hostGID).str ());
+}
+
+void
+DerivationGoal::startBuilder ()
+{
+  auto f = format (buildMode == bmRepair  ? "repairing path(s) %1%"
+                   : buildMode == bmCheck ? "checking path(s) %1%"
+                   : nrRounds > 1 ? "building path(s) %1% (round %2%/%3%)"
+                                  : "building path(s) %1%");
+  f.exceptions (boost::io::all_error_bits ^ boost::io::too_many_args_bit);
+  startNest (nest, lvlInfo,
+             f % showPaths (missingPaths) % curRound % nrRounds);
+
+  /* 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 ();
+
+  /* Most shells initialise PATH to some default (/bin:/usr/bin:...) when
+     PATH is not set.  We don't want this, so we fill it in with some dummy
+     value. */
+  env["PATH"] = "/path-not-set";
+
+  /* Set HOME to a non-existing path to prevent certain programs from using
+     /etc/passwd (or NIS, or whatever) to locate the home directory (for
+     example, wget looks for ~/.wgetrc).  I.e., these tools use /etc/passwd
+     if HOME is not set, but they will just assume that the settings file
+     they are looking for does not exist if HOME is set but points to some
+     non-existing path. */
+  Path homeDir = "/homeless-shelter";
+  env["HOME"] = homeDir;
+
+  /* Tell the builder where the store is.  Usually they
+     shouldn't care, but this is useful for purity checking (e.g.,
+     the compiler or linker might only want to accept paths to files
+     in the store or in the build directory). */
+  env["NIX_STORE"] = settings.nixStore;
+
+  /* The maximum number of cores to utilize for parallel building. */
+  env["NIX_BUILD_CORES"] = (format ("%d") % settings.buildCores).str ();
+
+  /* Add all bindings specified in the derivation. */
+  foreach (StringPairs::iterator, i, drv.env)
+    env[i->first] = i->second;
+
+  /* Create a temporary directory where the build will take
+     place. */
+  auto drvName = storePathToName (drvPath);
+  tmpDir = createTempDir ("", "guix-build-" + drvName, false, false, 0700);
+
+  if (useChroot)
+    {
+      /* Make the build directory seen by the build process a sub-directory.
+         That way, "/tmp/guix-build-foo.drv-0" is root-owned, and thus its
+         permissions cannot be changed by the build process, while
+         "/tmp/guix-build-foo.drv-0/top" is owned by the build user.  This
+         cannot be done when !useChroot because then $NIX_BUILD_TOP would
+         be inaccessible to the build user by its full file name.
+
+         If the build user could make the build directory world-writable,
+         then an attacker could create in it a hardlink to a root-owned file
+         such as /etc/shadow.  If 'keepFailed' is true, the daemon would
+         then chown that hardlink to the user, giving them write access to
+         that file.  See CVE-2021-27851.  */
+      tmpDir += "/top";
+      if (mkdir (tmpDir.c_str (), 0700) == 1)
+        throw SysError ("creating top-level build directory");
+    }
+
+  /* In a sandbox, for determinism, always use the same temporary
+     directory. */
+  tmpDirInSandbox
+      = useChroot ? canonPath ("/tmp", true) + "/guix-build-" + drvName + "-0"
+                  : tmpDir;
+
+  /* For convenience, set an environment pointing to the top build
+     directory. */
+  env["NIX_BUILD_TOP"] = tmpDirInSandbox;
+
+  /* Also set TMPDIR and variants to point to this directory. */
+  env["TMPDIR"] = env["TEMPDIR"] = env["TMP"] = env["TEMP"] = tmpDirInSandbox;
+
+  /* Explicitly set PWD to prevent problems with chroot builds.  In
+     particular, dietlibc cannot figure out the cwd because the
+     inode of the current directory doesn't appear in .. (because
+     getdents returns the inode of the mount point). */
+  env["PWD"] = tmpDirInSandbox;
+
+  /* *Only* if this is a fixed-output derivation, propagate the
+     values of the environment variables specified in the
+     `impureEnvVars' attribute to the builder.  This allows for
+     instance environment variables for proxy configuration such as
+     `http_proxy' to be easily passed to downloaders like
+     `fetchurl'.  Passing such environment variables from the caller
+     to the builder is generally impure, but the output of
+     fixed-output derivations is by definition pure (since we
+     already know the cryptographic hash of the output). */
+  if (fixedOutput)
+    {
+      Strings varNames
+          = tokenizeString<Strings> (get (drv.env, "impureEnvVars"));
+      foreach (Strings::iterator, i, varNames)
+        env[*i] = getEnv (*i);
     }
 
+  /* The `exportReferencesGraph' feature allows the references graph
+     to be passed to a builder.  This attribute should be a list of
+     pairs [name1 path1 name2 path2 ...].  The references graph of
+     each `pathN' will be stored in a text file `nameN' in the
+     temporary build directory.  The text files have the format used
+     by `nix-store --register-validity'.  However, the deriver
+     fields are left empty. */
+  string s = get (drv.env, "exportReferencesGraph");
+  Strings ss = tokenizeString<Strings> (s);
+  if (ss.size () % 2 != 0)
+    throw BuildError (
+        format ("odd number of tokens in `exportReferencesGraph': `%1%'") % s);
+  for (Strings::iterator i = ss.begin (); i != ss.end ();)
+    {
+      string fileName = *i++;
+      checkStoreName (fileName); /* !!! abuse of this function */
+
+      /* Check that the store path is valid. */
+      Path storePath = *i++;
+      if (!isInStore (storePath))
+        throw BuildError (
+            format ("`exportReferencesGraph' contains a non-store path `%1%'")
+            % storePath);
+      storePath = toStorePath (storePath);
+      if (!worker.store.isValidPath (storePath))
+        throw BuildError (
+            format ("`exportReferencesGraph' contains an invalid path `%1%'")
+            % storePath);
+
+      /* If there are derivations in the graph, then include their
+         outputs as well.  This is useful if you want to do things
+         like passing all build-time dependencies of some path to a
+         derivation that builds a NixOS DVD image. */
+      PathSet paths, paths2;
+      computeFSClosure (worker.store, storePath, paths);
+      paths2 = paths;
 
-    /* If `build-users-group' is not empty, then we have to build as
-       one of the members of that group. */
-    if (settings.buildUsersGroup != "") {
-        buildUser.acquire();
-        assert(buildUser.getUID() != 0);
-        assert(buildUser.getGID() != 0);
+      foreach (PathSet::iterator, j, paths2)
+        {
+          if (isDerivation (*j))
+            {
+              Derivation drv = derivationFromPath (worker.store, *j);
+              foreach (DerivationOutputs::iterator, k, drv.outputs)
+                computeFSClosure (worker.store, k->second.path, paths);
+            }
+        }
 
-        /* Make sure that no other processes are executing under this
-           uid. */
-        buildUser.kill();
+      /* Write closure info to `fileName'. */
+      writeFile (tmpDir + "/" + fileName,
+                 worker.store.makeValidityRegistration (paths, false, false));
+    }
 
-        /* Change ownership of the temporary build directory. */
-        if (chown(tmpDir.c_str(), buildUser.getUID(), buildUser.getGID()) == -1)
-            throw SysError(format("cannot change ownership of '%1%'") % tmpDir);
+  /* If `build-users-group' is not empty, then we have to build as
+     one of the members of that group. */
+  if (settings.buildUsersGroup != "")
+    {
+      buildUser.acquire ();
+      assert (buildUser.getUID () != 0);
+      assert (buildUser.getGID () != 0);
+
+      /* Make sure that no other processes are executing under this
+         uid. */
+      buildUser.kill ();
+
+      /* Change ownership of the temporary build directory. */
+      if (chown (tmpDir.c_str (), buildUser.getUID (), buildUser.getGID ())
+          == -1)
+        throw SysError (format ("cannot change ownership of '%1%'") % tmpDir);
     }
 
-    if (useChroot) {
+  if (useChroot)
+    {
 #if CHROOT_ENABLED
-        /* Create a temporary directory in which we set up the chroot
-           environment using bind-mounts.  Put it in the store to ensure it
-           can be atomically moved to the store.  */
-        chrootRootTop = drvPath + ".chroot";
-        chrootRootDir = chrootRootTop + "/top";
-        if (pathExists(chrootRootTop)) deletePath(chrootRootTop);
-
-        /* Clean up the chroot directory automatically. */
-        autoDelChroot = std::shared_ptr<AutoDelete>(new AutoDelete(chrootRootTop));
-
-        printMsg(lvlChatty, format("setting up chroot environment in `%1%'") % chrootRootDir);
-
-	if (mkdir(chrootRootTop.c_str(), 0750) == -1)
-	    throw SysError(format("cannot create build root container '%1%'") % chrootRootTop);
-        if (mkdir(chrootRootDir.c_str(), 0750) == -1)
-            throw SysError(format("cannot create build root '%1%'") % chrootRootDir);
-
-        if (buildUser.enabled() && chown(chrootRootDir.c_str(), 0, buildUser.getGID()) == -1)
-            throw SysError(format("cannot change ownership of ‘%1%’") % chrootRootDir);
-
-        /* Create a writable /tmp in the chroot.  Many builders need
-           this.  (Of course they should really respect $TMPDIR
-           instead.) */
-        Path chrootTmpDir = chrootRootDir + "/tmp";
-        createDirs(chrootTmpDir);
-        chmod_(chrootTmpDir, 01777);
-
-        /* Create a /etc/passwd with entries for the build user and the
-           nobody account.  The latter is kind of a hack to support
-           Samba-in-QEMU. */
-        createDirs(chrootRootDir + "/etc");
-
-        writeFile(chrootRootDir + "/etc/passwd",
-            (format(
-                "nixbld:x:%1%:%2%:Nix build user:/:/noshell\n"
-                "nobody:x:65534:65534:Nobody:/:/noshell\n")
-                % (buildUser.enabled() ? buildUser.getUID() : guestUID)
-                % (buildUser.enabled() ? buildUser.getGID() : guestGID)).str());
-
-        /* Declare the build user's group so that programs get a consistent
-           view of the system (e.g., "id -gn"). */
-        writeFile(chrootRootDir + "/etc/group",
-            (format("nixbld:!:%1%:\n")
-                % (buildUser.enabled() ? buildUser.getGID() : guestGID)).str());
-
-        /* Create /etc/hosts with localhost entry. */
-        if (!fixedOutput)
-            writeFile(chrootRootDir + "/etc/hosts", "127.0.0.1 localhost\n");
-
-        /* Bind-mount a user-configurable set of directories from the
-           host file system. */
-        PathSet dirs = tokenizeString<StringSet>(settings.get("build-chroot-dirs", string(DEFAULT_CHROOT_DIRS)));
-        PathSet dirs2 = tokenizeString<StringSet>(settings.get("build-extra-chroot-dirs", string("")));
-        dirs.insert(dirs2.begin(), dirs2.end());
-        for (auto & i : dirs) {
-            size_t p = i.find('=');
-            if (p == string::npos)
-                dirsInChroot[i] = i;
-            else
-                dirsInChroot[string(i, 0, p)] = string(i, p + 1);
-        }
-        dirsInChroot[tmpDirInSandbox] = tmpDir;
-
-	/* Create the fake store.  */
-        Path chrootStoreDir = chrootRootDir + settings.nixStore;
-        createDirs(chrootStoreDir);
-        chmod_(chrootStoreDir, 01775);
-
-        if (buildUser.enabled() && chown(chrootStoreDir.c_str(), 0, buildUser.getGID()) == -1)
-	     /* As an extra security precaution, make the fake store only
-		writable by the build user.  */
-	     throw SysError(format("cannot change ownership of ‘%1%’") % chrootStoreDir);
-
-        /* Make the closure of the inputs available in the chroot, rather than
-           the whole store.  This prevents any access to undeclared
-           dependencies. */
-        foreach (PathSet::iterator, i, inputPaths) {
-	    struct stat st;
-            if (lstat(i->c_str(), &st))
-                throw SysError(format("getting attributes of path `%1%'") % *i);
-
-	    if (S_ISLNK(st.st_mode)) {
-		/* Since bind-mounts follow symlinks, thus representing their
-		   target and not the symlink itself, special-case
-		   symlinks. XXX: When running unprivileged, TARGET can be
-		   deleted by the build process.  Use 'open_tree' & co. when
-		   it's more widely available.  */
-                Path target = chrootRootDir + *i;
-		if (symlink(readLink(*i).c_str(), target.c_str()) == -1)
-		    throw SysError(format("failed to create symlink '%1%' to '%2%'") % target % readLink(*i));
-	    }
-	    else
-		dirsInChroot[*i] = *i;
-        }
-
-        /* If we're repairing, checking or rebuilding part of a
-           multiple-outputs derivation, it's possible that we're
-           rebuilding a path that is in settings.dirsInChroot
-           (typically the dependencies of /bin/sh).  Throw them
-           out. */
-        for (auto & i : drv.outputs)
-            dirsInChroot.erase(i.second.path);
+      /* Create a temporary directory in which we set up the chroot
+         environment using bind-mounts.  Put it in the store to ensure it
+         can be atomically moved to the store.  */
+      chrootRootTop = drvPath + ".chroot";
+      chrootRootDir = chrootRootTop + "/top";
+      if (pathExists (chrootRootTop))
+        deletePath (chrootRootTop);
+
+      /* Clean up the chroot directory automatically. */
+      autoDelChroot
+          = std::shared_ptr<AutoDelete> (new AutoDelete (chrootRootTop));
+
+      printMsg (lvlChatty, format ("setting up chroot environment in `%1%'")
+                               % chrootRootDir);
+
+      if (mkdir (chrootRootTop.c_str (), 0750) == -1)
+        throw SysError (format ("cannot create build root container '%1%'")
+                        % chrootRootTop);
+      if (mkdir (chrootRootDir.c_str (), 0750) == -1)
+        throw SysError (format ("cannot create build root '%1%'")
+                        % chrootRootDir);
+
+      if (buildUser.enabled ()
+          && chown (chrootRootDir.c_str (), 0, buildUser.getGID ()) == -1)
+        throw SysError (format ("cannot change ownership of ‘%1%’")
+                        % chrootRootDir);
+
+      /* Create a writable /tmp in the chroot.  Many builders need
+         this.  (Of course they should really respect $TMPDIR
+         instead.) */
+      Path chrootTmpDir = chrootRootDir + "/tmp";
+      createDirs (chrootTmpDir);
+      chmod_ (chrootTmpDir, 01777);
+
+      /* Create a /etc/passwd with entries for the build user and the
+         nobody account.  The latter is kind of a hack to support
+         Samba-in-QEMU. */
+      createDirs (chrootRootDir + "/etc");
+
+      writeFile (chrootRootDir + "/etc/passwd",
+                 (format ("nixbld:x:%1%:%2%:Nix build user:/:/noshell\n"
+                          "nobody:x:65534:65534:Nobody:/:/noshell\n")
+                  % (buildUser.enabled () ? buildUser.getUID () : guestUID)
+                  % (buildUser.enabled () ? buildUser.getGID () : guestGID))
+                     .str ());
+
+      /* Declare the build user's group so that programs get a consistent
+         view of the system (e.g., "id -gn"). */
+      writeFile (chrootRootDir + "/etc/group",
+                 (format ("nixbld:!:%1%:\n")
+                  % (buildUser.enabled () ? buildUser.getGID () : guestGID))
+                     .str ());
+
+      /* Create /etc/hosts with localhost entry. */
+      if (!fixedOutput)
+        writeFile (chrootRootDir + "/etc/hosts", "127.0.0.1 localhost\n");
+
+      /* Bind-mount a user-configurable set of directories from the
+         host file system. */
+      PathSet dirs = tokenizeString<StringSet> (
+          settings.get ("build-chroot-dirs", string (DEFAULT_CHROOT_DIRS)));
+      PathSet dirs2 = tokenizeString<StringSet> (
+          settings.get ("build-extra-chroot-dirs", string ("")));
+      dirs.insert (dirs2.begin (), dirs2.end ());
+      for (auto &i : dirs)
+        {
+          size_t p = i.find ('=');
+          if (p == string::npos)
+            dirsInChroot[i] = i;
+          else
+            dirsInChroot[string (i, 0, p)] = string (i, p + 1);
+        }
+      dirsInChroot[tmpDirInSandbox] = tmpDir;
+
+      /* Create the fake store.  */
+      Path chrootStoreDir = chrootRootDir + settings.nixStore;
+      createDirs (chrootStoreDir);
+      chmod_ (chrootStoreDir, 01775);
+
+      if (buildUser.enabled ()
+          && chown (chrootStoreDir.c_str (), 0, buildUser.getGID ()) == -1)
+        /* As an extra security precaution, make the fake store only
+           writable by the build user.  */
+        throw SysError (format ("cannot change ownership of ‘%1%’")
+                        % chrootStoreDir);
+
+      /* Make the closure of the inputs available in the chroot, rather than
+         the whole store.  This prevents any access to undeclared
+         dependencies. */
+      foreach (PathSet::iterator, i, inputPaths)
+        {
+          struct stat st;
+          if (lstat (i->c_str (), &st))
+            throw SysError (format ("getting attributes of path `%1%'") % *i);
+
+          if (S_ISLNK (st.st_mode))
+            {
+              /* Since bind-mounts follow symlinks, thus representing their
+                 target and not the symlink itself, special-case
+                 symlinks. XXX: When running unprivileged, TARGET can be
+                 deleted by the build process.  Use 'open_tree' & co. when
+                 it's more widely available.  */
+              Path target = chrootRootDir + *i;
+              if (symlink (readLink (*i).c_str (), target.c_str ()) == -1)
+                throw SysError (
+                    format ("failed to create symlink '%1%' to '%2%'") % target
+                    % readLink (*i));
+            }
+          else
+            dirsInChroot[*i] = *i;
+        }
+
+      /* If we're repairing, checking or rebuilding part of a
+         multiple-outputs derivation, it's possible that we're
+         rebuilding a path that is in settings.dirsInChroot
+         (typically the dependencies of /bin/sh).  Throw them
+         out. */
+      for (auto &i : drv.outputs)
+        dirsInChroot.erase (i.second.path);
 
 #else
-        throw Error("chroot builds are not supported on this platform");
+      throw Error ("chroot builds are not supported on this platform");
 #endif
     }
 
-    else {
-
-        if (pathExists(homeDir))
-            throw Error(format("directory `%1%' exists; please remove it") % homeDir);
-
-        /* We're not doing a chroot build, but we have some valid
-           output paths.  Since we can't just overwrite or delete
-           them, we have to do hash rewriting: i.e. in the
-           environment/arguments passed to the build, we replace the
-           hashes of the valid outputs with unique dummy strings;
-           after the build, we discard the redirected outputs
-           corresponding to the valid outputs, and rewrite the
-           contents of the new outputs to replace the dummy strings
-           with the actual hashes. */
-        if (validPaths.size() > 0)
-            foreach (PathSet::iterator, i, validPaths)
-                addHashRewrite(*i);
+  else
+    {
 
-        /* If we're repairing, then we don't want to delete the
-           corrupt outputs in advance.  So rewrite them as well. */
-        if (buildMode == bmRepair)
-            foreach (PathSet::iterator, i, missingPaths)
-                if (worker.store.isValidPath(*i) && pathExists(*i)) {
-                    addHashRewrite(*i);
-                    redirectedBadOutputs.insert(*i);
-                }
+      if (pathExists (homeDir))
+        throw Error (format ("directory `%1%' exists; please remove it")
+                     % homeDir);
+
+      /* We're not doing a chroot build, but we have some valid
+         output paths.  Since we can't just overwrite or delete
+         them, we have to do hash rewriting: i.e. in the
+         environment/arguments passed to the build, we replace the
+         hashes of the valid outputs with unique dummy strings;
+         after the build, we discard the redirected outputs
+         corresponding to the valid outputs, and rewrite the
+         contents of the new outputs to replace the dummy strings
+         with the actual hashes. */
+      if (validPaths.size () > 0)
+        foreach (PathSet::iterator, i, validPaths)
+          addHashRewrite (*i);
+
+      /* If we're repairing, then we don't want to delete the
+         corrupt outputs in advance.  So rewrite them as well. */
+      if (buildMode == bmRepair)
+        foreach (PathSet::iterator, i, missingPaths)
+          if (worker.store.isValidPath (*i) && pathExists (*i))
+            {
+              addHashRewrite (*i);
+              redirectedBadOutputs.insert (*i);
+            }
     }
 
+  /* Run the builder. */
+  printMsg (lvlChatty, format ("executing builder `%1%'") % drv.builder);
 
-    /* Run the builder. */
-    printMsg(lvlChatty, format("executing builder `%1%'") % drv.builder);
+  /* Create the log file. */
+  Path logFile = openLogFile ();
 
-    /* Create the log file. */
-    Path logFile = openLogFile();
+  /* Create a pipe to get the output of the builder. */
+  builderOut.create ();
 
-    /* Create a pipe to get the output of the builder. */
-    builderOut.create();
+  /* Fork a child to build the package.  Note that while we
+     currently use forks to run and wait for the children, it
+     shouldn't be hard to use threads for this on systems where
+     fork() is unavailable or inefficient.
 
-    /* Fork a child to build the package.  Note that while we
-       currently use forks to run and wait for the children, it
-       shouldn't be hard to use threads for this on systems where
-       fork() is unavailable or inefficient.
+     If we're building in a chroot, then also set up private
+     namespaces for the build:
 
-       If we're building in a chroot, then also set up private
-       namespaces for the build:
+     - The PID namespace causes the build to start as PID 1.
+       Processes outside of the chroot are not visible to those on
+       the inside, but processes inside the chroot are visible from
+       the outside (though with different PIDs).
 
-       - The PID namespace causes the build to start as PID 1.
-         Processes outside of the chroot are not visible to those on
-         the inside, but processes inside the chroot are visible from
-         the outside (though with different PIDs).
+     - The private mount namespace ensures that all the bind mounts
+       we do will only show up in this process and its children, and
+       will disappear automatically when we're done.
 
-       - The private mount namespace ensures that all the bind mounts
-         we do will only show up in this process and its children, and
-         will disappear automatically when we're done.
+     - The private network namespace ensures that the builder cannot
+       talk to the outside world (or vice versa).  It only has a
+       private loopback interface.
 
-       - The private network namespace ensures that the builder cannot
-         talk to the outside world (or vice versa).  It only has a
-         private loopback interface.
+     - The IPC namespace prevents the builder from communicating
+       with outside processes using SysV IPC mechanisms (shared
+       memory, message queues, semaphores).  It also ensures that
+       all IPC objects are destroyed when the builder exits.
 
-       - The IPC namespace prevents the builder from communicating
-         with outside processes using SysV IPC mechanisms (shared
-         memory, message queues, semaphores).  It also ensures that
-         all IPC objects are destroyed when the builder exits.
-
-       - The UTS namespace ensures that builders see a hostname of
-         localhost rather than the actual hostname.
-    */
+     - The UTS namespace ensures that builders see a hostname of
+       localhost rather than the actual hostname.
+  */
 #if __linux__
-    if (useChroot) {
-	char stack[32 * 1024];
-	int flags = CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | CLONE_NEWUTS | SIGCHLD;
-	if (!fixedOutput) {
-	    flags |= CLONE_NEWNET;
-	}
-	if (!buildUser.enabled() || getuid() != 0) {
-	    flags |= CLONE_NEWUSER;
-	    readiness.create();
-	}
-
-	/* Ensure proper alignment on the stack.  On aarch64, it has to be 16
-	   bytes.  */
- 	pid = clone(childEntry,
-		    (char *)(((uintptr_t)stack + sizeof(stack) - 8) & ~(uintptr_t)0xf),
-		    flags, this);
-	if (pid == -1) {
-	    if ((flags & CLONE_NEWUSER) != 0 && getuid() != 0)
-		/* 'clone' fails with EPERM on distros where unprivileged user
-		   namespaces are disabled.  Error out instead of giving up on
-		   isolation.  */
-		throw SysError("cannot create process in unprivileged user namespace");
-	    else
-		throw SysError("cloning builder process");
-	}
-
-	readiness.readSide.close();
-	if ((flags & CLONE_NEWUSER) != 0) {
-	     /* Initialize the UID/GID mapping of the child process.  */
-	     initializeUserNamespace(pid);
-	     writeFull(readiness.writeSide, (unsigned char*)"go\n", 3);
-	}
-	readiness.writeSide.close();
-    } else
+  if (useChroot)
+    {
+      char stack[32 * 1024];
+      int flags
+          = CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWIPC | CLONE_NEWUTS | SIGCHLD;
+      if (!fixedOutput)
+        {
+          flags |= CLONE_NEWNET;
+        }
+      if (!buildUser.enabled () || getuid () != 0)
+        {
+          flags |= CLONE_NEWUSER;
+          readiness.create ();
+        }
+
+      /* Ensure proper alignment on the stack.  On aarch64, it has to be 16
+         bytes.  */
+      pid = clone (
+          childEntry,
+          (char *)(((uintptr_t)stack + sizeof (stack) - 8) & ~(uintptr_t)0xf),
+          flags, this);
+      if (pid == -1)
+        {
+          if ((flags & CLONE_NEWUSER) != 0 && getuid () != 0)
+            /* 'clone' fails with EPERM on distros where unprivileged user
+               namespaces are disabled.  Error out instead of giving up on
+               isolation.  */
+            throw SysError (
+                "cannot create process in unprivileged user namespace");
+          else
+            throw SysError ("cloning builder process");
+        }
+
+      readiness.readSide.close ();
+      if ((flags & CLONE_NEWUSER) != 0)
+        {
+          /* Initialize the UID/GID mapping of the child process.  */
+          initializeUserNamespace (pid);
+          writeFull (readiness.writeSide, (unsigned char *)"go\n", 3);
+        }
+      readiness.writeSide.close ();
+    }
+  else
 #endif
     {
-        pid = fork();
-        if (pid == 0) runChild();
+      pid = fork ();
+      if (pid == 0)
+        runChild ();
     }
 
-    if (pid == -1) throw SysError("unable to fork");
+  if (pid == -1)
+    throw SysError ("unable to fork");
 
-    /* parent */
-    pid.setSeparatePG(true);
-    builderOut.writeSide.close();
-    worker.childStarted(shared_from_this(), pid,
-        singleton<set<int> >(builderOut.readSide), true, true);
+  /* parent */
+  pid.setSeparatePG (true);
+  builderOut.writeSide.close ();
+  worker.childStarted (shared_from_this (), pid,
+                       singleton<set<int> > (builderOut.readSide), true, true);
 
-    /* Check if setting up the build environment failed. */
-    string msg = readLine(builderOut.readSide);
-    if (!msg.empty()) throw Error(msg);
+  /* Check if setting up the build environment failed. */
+  string msg = readLine (builderOut.readSide);
+  if (!msg.empty ())
+    throw Error (msg);
 
-    if (settings.printBuildTrace) {
-        printMsg(lvlError, format("@ build-started %1% - %2% %3% %4%")
-            % drvPath % drv.platform % logFile % pid);
+  if (settings.printBuildTrace)
+    {
+      printMsg (lvlError, format ("@ build-started %1% - %2% %3% %4%")
+                              % drvPath % drv.platform % logFile % pid);
     }
-
 }
 
 /* Return true if the operating system kernel part of SYSTEM1 and SYSTEM2 (the
    bit that comes after the hyphen in system types such as "i686-linux") is
    the same.  */
-static bool sameOperatingSystemKernel(const std::string& system1, const std::string& system2)
+static bool
+sameOperatingSystemKernel (const std::string &system1,
+                           const std::string &system2)
 {
-    auto os1 = system1.substr(system1.find("-"));
-    auto os2 = system2.substr(system2.find("-"));
-    return os1 == os2;
+  auto os1 = system1.substr (system1.find ("-"));
+  auto os2 = system2.substr (system2.find ("-"));
+  return os1 == os2;
 }
 
-void DerivationGoal::runChild()
+void
+DerivationGoal::runChild ()
 {
-    /* Warning: in the child we should absolutely not make any SQLite
-       calls! */
+  /* Warning: in the child we should absolutely not make any SQLite
+     calls! */
 
-    try { /* child */
+  try
+    { /* child */
 
-        _writeToStderr = 0;
+      _writeToStderr = 0;
 
-	if (readiness.writeSide >= 0) readiness.writeSide.close();
+      if (readiness.writeSide >= 0)
+        readiness.writeSide.close ();
 
-	if (readiness.readSide >= 0) {
-	     /* Wait for the parent process to initialize the UID/GID mapping
-		of our user namespace.  */
-	     char str[20] = { '\0' };
-	     readFull(readiness.readSide, (unsigned char*)str, 3);
-	     readiness.readSide.close();
-	     if (strcmp(str, "go\n") != 0)
-		  throw Error("failed to initialize process in unprivileged user namespace");
-	}
+      if (readiness.readSide >= 0)
+        {
+          /* Wait for the parent process to initialize the UID/GID mapping
+             of our user namespace.  */
+          char str[20] = { '\0' };
+          readFull (readiness.readSide, (unsigned char *)str, 3);
+          readiness.readSide.close ();
+          if (strcmp (str, "go\n") != 0)
+            throw Error (
+                "failed to initialize process in unprivileged user namespace");
+        }
 
-        restoreAffinity();
+      restoreAffinity ();
 
-        commonChildInit(builderOut);
+      commonChildInit (builderOut);
 
 #if CHROOT_ENABLED
-        if (useChroot) {
-# if HAVE_SYS_PRCTL_H
-	    /* Drop ambient capabilities such as CAP_CHOWN that might have
-	       been granted when starting guix-daemon.  */
-	    prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
-# endif
-
-	    if (!fixedOutput) {
-		/* Initialise the loopback interface. */
-		AutoCloseFD fd(socket(PF_INET, SOCK_DGRAM, IPPROTO_IP));
-		if (fd == -1) throw SysError("cannot open IP socket");
-
-		struct ifreq ifr;
-		strcpy(ifr.ifr_name, "lo");
-		ifr.ifr_flags = IFF_UP | IFF_LOOPBACK | IFF_RUNNING;
-		if (ioctl(fd, SIOCSIFFLAGS, &ifr) == -1)
-		    throw SysError("cannot set loopback interface flags");
-
-		fd.close();
-	    }
-
-            /* Set the hostname etc. to fixed values. */
-            char hostname[] = "localhost";
-            if (sethostname(hostname, sizeof(hostname)) == -1)
-                throw SysError("cannot set host name");
-            char domainname[] = "(none)"; // kernel default
-            if (setdomainname(domainname, sizeof(domainname)) == -1)
-                throw SysError("cannot set domain name");
-
-            /* Make all filesystems private.  This is necessary
-               because subtrees may have been mounted as "shared"
-               (MS_SHARED).  (Systemd does this, for instance.)  Even
-               though we have a private mount namespace, mounting
-               filesystems on top of a shared subtree still propagates
-               outside of the namespace.  Making a subtree private is
-               local to the namespace, though, so setting MS_PRIVATE
-               does not affect the outside world. */
-            if (mount(0, "/", 0, MS_REC|MS_PRIVATE, 0) == -1) {
-                throw SysError("unable to make ‘/’ private mount");
+      if (useChroot)
+        {
+#if HAVE_SYS_PRCTL_H
+          /* Drop ambient capabilities such as CAP_CHOWN that might have
+             been granted when starting guix-daemon.  */
+          prctl (PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
+#endif
+
+          if (!fixedOutput)
+            {
+              /* Initialise the loopback interface. */
+              AutoCloseFD fd (socket (PF_INET, SOCK_DGRAM, IPPROTO_IP));
+              if (fd == -1)
+                throw SysError ("cannot open IP socket");
+
+              struct ifreq ifr;
+              strcpy (ifr.ifr_name, "lo");
+              ifr.ifr_flags = IFF_UP | IFF_LOOPBACK | IFF_RUNNING;
+              if (ioctl (fd, SIOCSIFFLAGS, &ifr) == -1)
+                throw SysError ("cannot set loopback interface flags");
+
+              fd.close ();
             }
 
-            /* Bind-mount chroot directory to itself, to treat it as a
-               different filesystem from /, as needed for pivot_root. */
-            if (mount(chrootRootDir.c_str(), chrootRootDir.c_str(), 0, MS_BIND, 0) == -1)
-                throw SysError(format("unable to bind mount ‘%1%’") % chrootRootDir);
-
-            /* Set up a nearly empty /dev, unless the user asked to
-               bind-mount the host /dev. */
-            Strings ss;
-            if (dirsInChroot.find("/dev") == dirsInChroot.end()) {
-                createDirs(chrootRootDir + "/dev/shm");
-                createDirs(chrootRootDir + "/dev/pts");
-                ss.push_back("/dev/full");
+          /* Set the hostname etc. to fixed values. */
+          char hostname[] = "localhost";
+          if (sethostname (hostname, sizeof (hostname)) == -1)
+            throw SysError ("cannot set host name");
+          char domainname[] = "(none)"; // kernel default
+          if (setdomainname (domainname, sizeof (domainname)) == -1)
+            throw SysError ("cannot set domain name");
+
+          /* Make all filesystems private.  This is necessary
+             because subtrees may have been mounted as "shared"
+             (MS_SHARED).  (Systemd does this, for instance.)  Even
+             though we have a private mount namespace, mounting
+             filesystems on top of a shared subtree still propagates
+             outside of the namespace.  Making a subtree private is
+             local to the namespace, though, so setting MS_PRIVATE
+             does not affect the outside world. */
+          if (mount (0, "/", 0, MS_REC | MS_PRIVATE, 0) == -1)
+            {
+              throw SysError ("unable to make ‘/’ private mount");
+            }
+
+          /* Bind-mount chroot directory to itself, to treat it as a
+             different filesystem from /, as needed for pivot_root. */
+          if (mount (chrootRootDir.c_str (), chrootRootDir.c_str (), 0,
+                     MS_BIND, 0)
+              == -1)
+            throw SysError (format ("unable to bind mount ‘%1%’")
+                            % chrootRootDir);
+
+          /* Set up a nearly empty /dev, unless the user asked to
+             bind-mount the host /dev. */
+          Strings ss;
+          if (dirsInChroot.find ("/dev") == dirsInChroot.end ())
+            {
+              createDirs (chrootRootDir + "/dev/shm");
+              createDirs (chrootRootDir + "/dev/pts");
+              ss.push_back ("/dev/full");
 #ifdef __linux__
-                if (pathExists("/dev/kvm"))
-                    ss.push_back("/dev/kvm");
+              if (pathExists ("/dev/kvm"))
+                ss.push_back ("/dev/kvm");
 #endif
-                ss.push_back("/dev/null");
-                ss.push_back("/dev/random");
-                ss.push_back("/dev/tty");
-                ss.push_back("/dev/urandom");
-                ss.push_back("/dev/zero");
-                createSymlink("/proc/self/fd", chrootRootDir + "/dev/fd");
-                createSymlink("/proc/self/fd/0", chrootRootDir + "/dev/stdin");
-                createSymlink("/proc/self/fd/1", chrootRootDir + "/dev/stdout");
-                createSymlink("/proc/self/fd/2", chrootRootDir + "/dev/stderr");
+              ss.push_back ("/dev/null");
+              ss.push_back ("/dev/random");
+              ss.push_back ("/dev/tty");
+              ss.push_back ("/dev/urandom");
+              ss.push_back ("/dev/zero");
+              createSymlink ("/proc/self/fd", chrootRootDir + "/dev/fd");
+              createSymlink ("/proc/self/fd/0", chrootRootDir + "/dev/stdin");
+              createSymlink ("/proc/self/fd/1", chrootRootDir + "/dev/stdout");
+              createSymlink ("/proc/self/fd/2", chrootRootDir + "/dev/stderr");
             }
 
-            /* Fixed-output derivations typically need to access the
-               network, so give them access to /etc/resolv.conf and so
-               on. */
-            if (fixedOutput) {
-		auto files = { "/etc/resolv.conf", "/etc/nsswitch.conf",
-			       "/etc/services", "/etc/hosts" };
-		for (auto & file: files) {
-		    if (pathExists(file)) ss.push_back(file);
-		}
+          /* Fixed-output derivations typically need to access the
+             network, so give them access to /etc/resolv.conf and so
+             on. */
+          if (fixedOutput)
+            {
+              auto files = { "/etc/resolv.conf", "/etc/nsswitch.conf",
+                             "/etc/services", "/etc/hosts" };
+              for (auto &file : files)
+                {
+                  if (pathExists (file))
+                    ss.push_back (file);
+                }
             }
 
-            for (auto & i : ss) dirsInChroot[i] = i;
-
-	    /* Make new mounts for the store and for /tmp.  That way, when
-	       'chrootRootDir' is made read-only below, these two mounts will
-	       remain writable (the store needs to be writable so derivation
-	       outputs can be written to it, and /tmp is writable by
-	       convention).  */
-	    auto chrootStoreDir = chrootRootDir + settings.nixStore;
-	    if (mount(chrootStoreDir.c_str(), chrootStoreDir.c_str(), 0, MS_BIND, 0) == -1)
-                throw SysError(format("read-write mount of store '%1%' failed") % chrootStoreDir);
-	    auto chrootTmpDir = chrootRootDir + "/tmp";
-	    if (mount(chrootTmpDir.c_str(), chrootTmpDir.c_str(), 0, MS_BIND, 0) == -1)
-                throw SysError(format("read-write mount of temporary directory '%1%' failed") % chrootTmpDir);
-
-            /* Bind-mount all the directories from the "host"
-               filesystem that we want in the chroot
-               environment. */
-            foreach (DirsInChroot::iterator, i, dirsInChroot) {
-                struct stat st;
-                Path source = i->second;
-                Path target = chrootRootDir + i->first;
-                if (source == "/proc") continue; // backwards compatibility
-                if (stat(source.c_str(), &st) == -1)
-                    throw SysError(format("getting attributes of path `%1%'") % source);
-                if (S_ISDIR(st.st_mode))
-                    createDirs(target);
-                else {
-                    createDirs(dirOf(target));
-                    writeFile(target, "");
+          for (auto &i : ss)
+            dirsInChroot[i] = i;
+
+          /* Make new mounts for the store and for /tmp.  That way, when
+             'chrootRootDir' is made read-only below, these two mounts will
+             remain writable (the store needs to be writable so derivation
+             outputs can be written to it, and /tmp is writable by
+             convention).  */
+          auto chrootStoreDir = chrootRootDir + settings.nixStore;
+          if (mount (chrootStoreDir.c_str (), chrootStoreDir.c_str (), 0,
+                     MS_BIND, 0)
+              == -1)
+            throw SysError (format ("read-write mount of store '%1%' failed")
+                            % chrootStoreDir);
+          auto chrootTmpDir = chrootRootDir + "/tmp";
+          if (mount (chrootTmpDir.c_str (), chrootTmpDir.c_str (), 0, MS_BIND,
+                     0)
+              == -1)
+            throw SysError (
+                format ("read-write mount of temporary directory '%1%' failed")
+                % chrootTmpDir);
+
+          /* Bind-mount all the directories from the "host"
+             filesystem that we want in the chroot
+             environment. */
+          foreach (DirsInChroot::iterator, i, dirsInChroot)
+            {
+              struct stat st;
+              Path source = i->second;
+              Path target = chrootRootDir + i->first;
+              if (source == "/proc")
+                continue; // backwards compatibility
+              if (stat (source.c_str (), &st) == -1)
+                throw SysError (format ("getting attributes of path `%1%'")
+                                % source);
+              if (S_ISDIR (st.st_mode))
+                createDirs (target);
+              else
+                {
+                  createDirs (dirOf (target));
+                  writeFile (target, "");
                 }
 
-		/* Extra flags passed with MS_BIND are ignored, hence the
-		   extra MS_REMOUNT.  */
-                if (mount(source.c_str(), target.c_str(), "", MS_BIND, 0) == -1)
-                    throw SysError(format("bind mount from `%1%' to `%2%' failed") % source % target);
-		if (source.compare(0, settings.nixStore.length(), settings.nixStore) == 0) {
-		     if (mount(source.c_str(), target.c_str(), "", MS_BIND | MS_REMOUNT | MS_RDONLY, 0) == -1)
-			  throw SysError(format("read-only remount of `%1%' failed") % target);
-		}
+              /* Extra flags passed with MS_BIND are ignored, hence the
+                 extra MS_REMOUNT.  */
+              if (mount (source.c_str (), target.c_str (), "", MS_BIND, 0)
+                  == -1)
+                throw SysError (
+                    format ("bind mount from `%1%' to `%2%' failed") % source
+                    % target);
+              if (source.compare (0, settings.nixStore.length (),
+                                  settings.nixStore)
+                  == 0)
+                {
+                  if (mount (source.c_str (), target.c_str (), "",
+                             MS_BIND | MS_REMOUNT | MS_RDONLY, 0)
+                      == -1)
+                    throw SysError (
+                        format ("read-only remount of `%1%' failed") % target);
+                }
             }
 
-            /* Bind a new instance of procfs on /proc to reflect our
-               private PID namespace. */
-            createDirs(chrootRootDir + "/proc");
-            if (mount("none", (chrootRootDir + "/proc").c_str(), "proc", 0, 0) == -1)
-                throw SysError("mounting /proc");
-
-            /* Mount a new tmpfs on /dev/shm to ensure that whatever
-               the builder puts in /dev/shm is cleaned up automatically. */
-            if (pathExists("/dev/shm") && mount("none", (chrootRootDir + "/dev/shm").c_str(), "tmpfs", 0, 0) == -1)
-                throw SysError("mounting /dev/shm");
-
-            /* Mount a new devpts on /dev/pts.  Note that this
-               requires the kernel to be compiled with
-               CONFIG_DEVPTS_MULTIPLE_INSTANCES=y (which is the case
-               if /dev/ptx/ptmx exists). */
-            if (pathExists("/dev/pts/ptmx") &&
-                !pathExists(chrootRootDir + "/dev/ptmx")
-                && dirsInChroot.find("/dev/pts") == dirsInChroot.end())
+          /* Bind a new instance of procfs on /proc to reflect our
+             private PID namespace. */
+          createDirs (chrootRootDir + "/proc");
+          if (mount ("none", (chrootRootDir + "/proc").c_str (), "proc", 0, 0)
+              == -1)
+            throw SysError ("mounting /proc");
+
+          /* Mount a new tmpfs on /dev/shm to ensure that whatever
+             the builder puts in /dev/shm is cleaned up automatically. */
+          if (pathExists ("/dev/shm")
+              && mount ("none", (chrootRootDir + "/dev/shm").c_str (), "tmpfs",
+                        0, 0)
+                     == -1)
+            throw SysError ("mounting /dev/shm");
+
+          /* Mount a new devpts on /dev/pts.  Note that this
+             requires the kernel to be compiled with
+             CONFIG_DEVPTS_MULTIPLE_INSTANCES=y (which is the case
+             if /dev/ptx/ptmx exists). */
+          if (pathExists ("/dev/pts/ptmx")
+              && !pathExists (chrootRootDir + "/dev/ptmx")
+              && dirsInChroot.find ("/dev/pts") == dirsInChroot.end ())
             {
-                if (mount("none", (chrootRootDir + "/dev/pts").c_str(), "devpts", 0, "newinstance,mode=0620") == -1)
-                    throw SysError("mounting /dev/pts");
-                createSymlink("/dev/pts/ptmx", chrootRootDir + "/dev/ptmx");
-
-                /* Make sure /dev/pts/ptmx is world-writable.  With some
-                   Linux versions, it is created with permissions 0.  */
-                chmod_(chrootRootDir + "/dev/pts/ptmx", 0666);
+              if (mount ("none", (chrootRootDir + "/dev/pts").c_str (),
+                         "devpts", 0, "newinstance,mode=0620")
+                  == -1)
+                throw SysError ("mounting /dev/pts");
+              createSymlink ("/dev/pts/ptmx", chrootRootDir + "/dev/ptmx");
+
+              /* Make sure /dev/pts/ptmx is world-writable.  With some
+                 Linux versions, it is created with permissions 0.  */
+              chmod_ (chrootRootDir + "/dev/pts/ptmx", 0666);
             }
 
-            /* Do the chroot(). */
-            if (chdir(chrootRootDir.c_str()) == -1)
-                throw SysError(format("cannot change directory to '%1%'") % chrootRootDir);
+          /* Do the chroot(). */
+          if (chdir (chrootRootDir.c_str ()) == -1)
+            throw SysError (format ("cannot change directory to '%1%'")
+                            % chrootRootDir);
 
-            if (mkdir("real-root", 0) == -1)
-                throw SysError("cannot create real-root directory");
+          if (mkdir ("real-root", 0) == -1)
+            throw SysError ("cannot create real-root directory");
 
-            if (pivot_root(".", "real-root") == -1)
-                throw SysError(format("cannot pivot old root directory onto '%1%'") % (chrootRootDir + "/real-root"));
+          if (pivot_root (".", "real-root") == -1)
+            throw SysError (
+                format ("cannot pivot old root directory onto '%1%'")
+                % (chrootRootDir + "/real-root"));
 
-            if (chroot(".") == -1)
-                throw SysError(format("cannot change root directory to '%1%'") % chrootRootDir);
+          if (chroot (".") == -1)
+            throw SysError (format ("cannot change root directory to '%1%'")
+                            % chrootRootDir);
 
-            if (umount2("real-root", MNT_DETACH) == -1)
-                throw SysError("cannot unmount real root filesystem");
+          if (umount2 ("real-root", MNT_DETACH) == -1)
+            throw SysError ("cannot unmount real root filesystem");
 
-            if (rmdir("real-root") == -1)
-                throw SysError("cannot remove real-root directory");
+          if (rmdir ("real-root") == -1)
+            throw SysError ("cannot remove real-root directory");
 
-	    /* Make the root read-only.
+          /* Make the root read-only.
 
-	       When build users are disabled, the build process could make it
-	       world-accessible, but that's OK: since 'chrootRootTop' is *not*
-	       world-accessible, a world-accessible 'chrootRootDir' cannot be
-	       used to grant access to the build environment to external
-	       processes.
+             When build users are disabled, the build process could make it
+             world-accessible, but that's OK: since 'chrootRootTop' is *not*
+             world-accessible, a world-accessible 'chrootRootDir' cannot be
+             used to grant access to the build environment to external
+             processes.
 
-	       Remounting the root as read-only was rejected because it makes
-	       write access fail with EROFS instead of EACCES, which goes
-	       against what some test suites expect (Go, Ruby, SCons,
-	       Shepherd, to name a few).  */
-	    chmod_("/", 0555);
+             Remounting the root as read-only was rejected because it makes
+             write access fail with EROFS instead of EACCES, which goes
+             against what some test suites expect (Go, Ruby, SCons,
+             Shepherd, to name a few).  */
+          chmod_ ("/", 0555);
 
-	    if (getuid() != 0) {
-		/* Create a new mount namespace to "lock" previous mounts.
-		   See mount_namespaces(7).  */
-		auto uid = getuid();
-		auto gid = getgid();
+          if (getuid () != 0)
+            {
+              /* Create a new mount namespace to "lock" previous mounts.
+                 See mount_namespaces(7).  */
+              auto uid = getuid ();
+              auto gid = getgid ();
+
+              if (unshare (CLONE_NEWNS | CLONE_NEWUSER) == -1)
+                throw SysError (
+                    format ("creating new user and mount namespaces"));
+
+              initializeUserNamespace (getpid (), uid, gid);
+
+              /* Check that mounts within the build environment are "locked"
+                 together and cannot be separated from within the build
+                 environment namespace.  Since
+                 umount(2) is documented to fail with EINVAL when attempting
+                 to unmount one of the mounts that are locked together,
+                 check that this is what we get.  */
+              int ret = umount (tmpDirInSandbox.c_str ());
+              assert (ret == -1 && errno == EINVAL);
+            }
+        }
+#endif
 
-		if (unshare(CLONE_NEWNS | CLONE_NEWUSER) == -1)
-		    throw SysError(format("creating new user and mount namespaces"));
+      if (chdir (tmpDirInSandbox.c_str ()) == -1)
+        throw SysError (format ("changing into `%1%'") % tmpDir);
 
-		initializeUserNamespace(getpid(), uid, gid);
+      /* Close all other file descriptors. */
+      closeMostFDs (set<int> ());
 
-		/* Check that mounts within the build environment are "locked"
-		   together and cannot be separated from within the build
-		   environment namespace.  Since
-		   umount(2) is documented to fail with EINVAL when attempting
-		   to unmount one of the mounts that are locked together,
-		   check that this is what we get.  */
-		int ret = umount(tmpDirInSandbox.c_str());
-		assert(ret == -1 && errno == EINVAL);
-	    }
+#if __linux__
+      /* Change the personality to 32-bit if we're doing an
+         i686-linux build on an x86_64-linux machine. */
+      struct utsname utsbuf;
+      uname (&utsbuf);
+      if (drv.platform == "i686-linux"
+          && (settings.thisSystem == "x86_64-linux"
+              || (!strcmp (utsbuf.sysname, "Linux")
+                  && !strcmp (utsbuf.machine, "x86_64"))))
+        {
+          if (personality (PER_LINUX32) == -1)
+            throw SysError ("cannot set i686-linux personality");
         }
-#endif
 
-        if (chdir(tmpDirInSandbox.c_str()) == -1)
-            throw SysError(format("changing into `%1%'") % tmpDir);
+      if (drv.platform == "armhf-linux"
+          && (settings.thisSystem == "aarch64-linux"
+              || (!strcmp (utsbuf.sysname, "Linux")
+                  && !strcmp (utsbuf.machine, "aarch64"))))
+        {
+          if (personality (PER_LINUX32) == -1)
+            throw SysError ("cannot set armhf-linux personality");
+        }
 
-        /* Close all other file descriptors. */
-        closeMostFDs(set<int>());
+      /* Impersonate a Linux 2.6 machine to get some determinism in
+         builds that depend on the kernel version. */
+      if ((drv.platform == "i686-linux" || drv.platform == "x86_64-linux")
+          && settings.impersonateLinux26)
+        {
+          int cur = personality (0xffffffff);
+          if (cur != -1)
+            personality (cur | 0x0020000 /* == UNAME26 */);
+        }
 
-#if __linux__
-        /* Change the personality to 32-bit if we're doing an
-           i686-linux build on an x86_64-linux machine. */
-        struct utsname utsbuf;
-        uname(&utsbuf);
-        if (drv.platform == "i686-linux" &&
-            (settings.thisSystem == "x86_64-linux" ||
-             (!strcmp(utsbuf.sysname, "Linux") && !strcmp(utsbuf.machine, "x86_64")))) {
-            if (personality(PER_LINUX32) == -1)
-                throw SysError("cannot set i686-linux personality");
-        }
-
-        if (drv.platform == "armhf-linux" &&
-            (settings.thisSystem == "aarch64-linux" ||
-             (!strcmp(utsbuf.sysname, "Linux") && !strcmp(utsbuf.machine, "aarch64")))) {
-            if (personality(PER_LINUX32) == -1)
-                throw SysError("cannot set armhf-linux personality");
-        }
-
-        /* Impersonate a Linux 2.6 machine to get some determinism in
-           builds that depend on the kernel version. */
-        if ((drv.platform == "i686-linux" || drv.platform == "x86_64-linux") && settings.impersonateLinux26) {
-            int cur = personality(0xffffffff);
-            if (cur != -1) personality(cur | 0x0020000 /* == UNAME26 */);
-        }
-
-        /* Disable address space randomization for improved
-           determinism. */
-        int cur = personality(0xffffffff);
-        if (cur != -1) personality(cur | ADDR_NO_RANDOMIZE);
-
-        /* Ask the kernel to eagerly kill us & our children if it runs out of
-           memory, regardless of blame, to preserve ‘real’ user data & state. */
-        try {
-            writeFile("/proc/self/oom_score_adj", "1000"); // 100%
-        } catch (...) { ignoreException(); }
+      /* Disable address space randomization for improved
+         determinism. */
+      int cur = personality (0xffffffff);
+      if (cur != -1)
+        personality (cur | ADDR_NO_RANDOMIZE);
+
+      /* Ask the kernel to eagerly kill us & our children if it runs out of
+         memory, regardless of blame, to preserve ‘real’ user data & state. */
+      try
+        {
+          writeFile ("/proc/self/oom_score_adj", "1000"); // 100%
+        }
+      catch (...)
+        {
+          ignoreException ();
+        }
 #endif
 
-        /* Fill in the environment. */
-        Strings envStrs;
-        foreach (Environment::const_iterator, i, env)
-            envStrs.push_back(rewriteHashes(i->first + "=" + i->second, rewritesToTmp));
-
-        /* If we are running in `build-users' mode, then switch to the
-           user we allocated above.  Make sure that we drop all root
-           privileges.  Note that above we have closed all file
-           descriptors except std*, so that's safe.  Also note that
-           setuid() when run as root sets the real, effective and
-           saved UIDs. */
-        if (buildUser.enabled()) {
-            /* Preserve supplementary groups of the build user, to allow
-               admins to specify groups such as "kvm".  */
-            if (setgroups(buildUser.getSupplementaryGIDs().size(),
-                          buildUser.getSupplementaryGIDs().data()) == -1)
-                throw SysError("cannot set supplementary groups of build user");
-
-            if (setgid(buildUser.getGID()) == -1 ||
-                getgid() != buildUser.getGID() ||
-                getegid() != buildUser.getGID())
-                throw SysError("setgid failed");
-
-            if (setuid(buildUser.getUID()) == -1 ||
-                getuid() != buildUser.getUID() ||
-                geteuid() != buildUser.getUID())
-                throw SysError("setuid failed");
-        }
-
-        restoreSIGPIPE();
-
-        /* Indicate that we managed to set up the build environment. */
-        writeFull(STDERR_FILENO, "\n");
-
-        /* Execute the program.  This should not return. */
-	string builderBasename;
-        if (isBuiltin(drv)) {
-            try {
-                logType = ltFlat;
-
-		auto buildDrv = lookupBuiltinBuilder(drv.builder);
-                if (buildDrv != NULL) {
-		    /* Check what the output file name is.  When doing a
-		       'bmCheck' build, the output file name is different from
-		       that specified in DRV due to hash rewriting.  */
-		    Path output = drv.outputs["out"].path;
-		    auto redirected = redirectedOutputs.find(output);
-		    if (redirected != redirectedOutputs.end())
-			output = redirected->second;
-
-                    buildDrv(drv, drvPath, output);
-		}
-                else
-                    throw Error(format("unsupported builtin function '%1%'") % string(drv.builder, 8));
-                _exit(0);
-            } catch (std::exception & e) {
-                writeFull(STDERR_FILENO, "error: " + string(e.what()) + "\n");
-                _exit(1);
+      /* Fill in the environment. */
+      Strings envStrs;
+      foreach (Environment::const_iterator, i, env)
+        envStrs.push_back (
+            rewriteHashes (i->first + "=" + i->second, rewritesToTmp));
+
+      /* If we are running in `build-users' mode, then switch to the
+         user we allocated above.  Make sure that we drop all root
+         privileges.  Note that above we have closed all file
+         descriptors except std*, so that's safe.  Also note that
+         setuid() when run as root sets the real, effective and
+         saved UIDs. */
+      if (buildUser.enabled ())
+        {
+          /* Preserve supplementary groups of the build user, to allow
+             admins to specify groups such as "kvm".  */
+          if (setgroups (buildUser.getSupplementaryGIDs ().size (),
+                         buildUser.getSupplementaryGIDs ().data ())
+              == -1)
+            throw SysError ("cannot set supplementary groups of build user");
+
+          if (setgid (buildUser.getGID ()) == -1
+              || getgid () != buildUser.getGID ()
+              || getegid () != buildUser.getGID ())
+            throw SysError ("setgid failed");
+
+          if (setuid (buildUser.getUID ()) == -1
+              || getuid () != buildUser.getUID ()
+              || geteuid () != buildUser.getUID ())
+            throw SysError ("setuid failed");
+        }
+
+      restoreSIGPIPE ();
+
+      /* Indicate that we managed to set up the build environment. */
+      writeFull (STDERR_FILENO, "\n");
+
+      /* Execute the program.  This should not return. */
+      string builderBasename;
+      if (isBuiltin (drv))
+        {
+          try
+            {
+              logType = ltFlat;
+
+              auto buildDrv = lookupBuiltinBuilder (drv.builder);
+              if (buildDrv != NULL)
+                {
+                  /* Check what the output file name is.  When doing a
+                     'bmCheck' build, the output file name is different from
+                     that specified in DRV due to hash rewriting.  */
+                  Path output = drv.outputs["out"].path;
+                  auto redirected = redirectedOutputs.find (output);
+                  if (redirected != redirectedOutputs.end ())
+                    output = redirected->second;
+
+                  buildDrv (drv, drvPath, output);
+                }
+              else
+                throw Error (format ("unsupported builtin function '%1%'")
+                             % string (drv.builder, 8));
+              _exit (0);
             }
-        } else {
-	    /* Ensure that the builder is within the store.  This prevents
-	       users from using /proc/self/exe (or a symlink to it) as their
-	       builder, which could allow them to overwrite the guix-daemon
-	       binary (CVE-2019-5736).
-
-	       This attack is possible even if the target of /proc/self/exe is
-	       outside the chroot (it's as if it were a hard link), though it
-	       requires that its ELF interpreter and dependencies be in the
-	       chroot.
-
-	       Note: 'canonPath' throws if 'drv.builder' cannot be resolved
-	       within the chroot.  */
-	    builderBasename = baseNameOf(drv.builder);
-	    drv.builder = canonPath(drv.builder, true);
-
-	    if (!isInStore(drv.builder))
-		throw Error(format("derivation builder '%1%' is outside the store") % drv.builder);
-	}
-
-        /* Fill in the arguments. */
-        Strings args;
-        args.push_back(builderBasename);
-        foreach (Strings::iterator, i, drv.args)
-            args.push_back(rewriteHashes(*i, rewritesToTmp));
-
-	/* If DRV targets the same operating system kernel, try to execute it:
-	   there might be binfmt_misc set up for user-land emulation of other
-	   architectures.  However, if it targets a different operating
-	   system--e.g., "i586-gnu" vs. "x86_64-linux"--do not try executing
-	   it: the ELF file for that OS is likely indistinguishable from a
-	   native ELF binary and it would just crash at run time.  */
-	int error;
-	if (sameOperatingSystemKernel(drv.platform, settings.thisSystem)) {
-	    execve(drv.builder.c_str(), stringsToCharPtrs(args).data(),
-		   stringsToCharPtrs(envStrs).data());
-	    error = errno;
-	} else {
-	    error = ENOEXEC;
-	}
-
-	/* Right platform?  Check this after we've tried 'execve' to allow for
-	   transparent emulation of different platforms with binfmt_misc
-	   handlers that invoke QEMU.  */
-	if (error == ENOEXEC && !canBuildLocally(drv.platform)) {
-	    if (settings.printBuildTrace)
-		printMsg(lvlError, format("@ unsupported-platform %1% %2%") % drvPath % drv.platform);
-	    throw Error(
-		format("a `%1%' is required to build `%3%', but I am a `%2%'")
-		% drv.platform % settings.thisSystem % drvPath);
-	}
-
-	errno = error;
-        throw SysError(format("executing `%1%'") % drv.builder);
-
-    } catch (std::exception & e) {
-        writeFull(STDERR_FILENO, "while setting up the build environment: " + string(e.what()) + "\n");
-        _exit(1);
-    }
-
-    abort(); /* never reached */
-}
+          catch (std::exception &e)
+            {
+              writeFull (STDERR_FILENO, "error: " + string (e.what ()) + "\n");
+              _exit (1);
+            }
+        }
+      else
+        {
+          /* Ensure that the builder is within the store.  This prevents
+             users from using /proc/self/exe (or a symlink to it) as their
+             builder, which could allow them to overwrite the guix-daemon
+             binary (CVE-2019-5736).
+
+             This attack is possible even if the target of /proc/self/exe is
+             outside the chroot (it's as if it were a hard link), though it
+             requires that its ELF interpreter and dependencies be in the
+             chroot.
+
+             Note: 'canonPath' throws if 'drv.builder' cannot be resolved
+             within the chroot.  */
+          builderBasename = baseNameOf (drv.builder);
+          drv.builder = canonPath (drv.builder, true);
+
+          if (!isInStore (drv.builder))
+            throw Error (
+                format ("derivation builder '%1%' is outside the store")
+                % drv.builder);
+        }
 
+      /* Fill in the arguments. */
+      Strings args;
+      args.push_back (builderBasename);
+      foreach (Strings::iterator, i, drv.args)
+        args.push_back (rewriteHashes (*i, rewritesToTmp));
+
+      /* If DRV targets the same operating system kernel, try to execute it:
+         there might be binfmt_misc set up for user-land emulation of other
+         architectures.  However, if it targets a different operating
+         system--e.g., "i586-gnu" vs. "x86_64-linux"--do not try executing
+         it: the ELF file for that OS is likely indistinguishable from a
+         native ELF binary and it would just crash at run time.  */
+      int error;
+      if (sameOperatingSystemKernel (drv.platform, settings.thisSystem))
+        {
+          execve (drv.builder.c_str (), stringsToCharPtrs (args).data (),
+                  stringsToCharPtrs (envStrs).data ());
+          error = errno;
+        }
+      else
+        {
+          error = ENOEXEC;
+        }
+
+      /* Right platform?  Check this after we've tried 'execve' to allow for
+         transparent emulation of different platforms with binfmt_misc
+         handlers that invoke QEMU.  */
+      if (error == ENOEXEC && !canBuildLocally (drv.platform))
+        {
+          if (settings.printBuildTrace)
+            printMsg (lvlError, format ("@ unsupported-platform %1% %2%")
+                                    % drvPath % drv.platform);
+          throw Error (
+              format ("a `%1%' is required to build `%3%', but I am a `%2%'")
+              % drv.platform % settings.thisSystem % drvPath);
+        }
+
+      errno = error;
+      throw SysError (format ("executing `%1%'") % drv.builder);
+    }
+  catch (std::exception &e)
+    {
+      writeFull (STDERR_FILENO, "while setting up the build environment: "
+                                    + string (e.what ()) + "\n");
+      _exit (1);
+    }
+
+  abort (); /* never reached */
+}
 
 /* Parse a list of reference specifiers.  Each element must either be
    a store path, or the symbolic name of the output of the derivation
    (such as `out'). */
-PathSet parseReferenceSpecifiers(const Derivation & drv, string attr)
-{
-    PathSet result;
-    Paths paths = tokenizeString<Paths>(attr);
-    foreach (Strings::iterator, i, paths) {
-        if (isStorePath(*i))
-            result.insert(*i);
-        else if (drv.outputs.find(*i) != drv.outputs.end())
-            result.insert(drv.outputs.find(*i)->second.path);
-        else throw BuildError(
-            format("derivation contains an invalid reference specifier `%1%'")
+PathSet
+parseReferenceSpecifiers (const Derivation &drv, string attr)
+{
+  PathSet result;
+  Paths paths = tokenizeString<Paths> (attr);
+  foreach (Strings::iterator, i, paths)
+    {
+      if (isStorePath (*i))
+        result.insert (*i);
+      else if (drv.outputs.find (*i) != drv.outputs.end ())
+        result.insert (drv.outputs.find (*i)->second.path);
+      else
+        throw BuildError (
+            format ("derivation contains an invalid reference specifier `%1%'")
             % *i);
     }
-    return result;
+  return result;
 }
 
-
-void DerivationGoal::registerOutputs()
+void
+DerivationGoal::registerOutputs ()
 {
-    /* When using a build hook, the build hook can register the output
-       as valid (by doing `nix-store --import').  If so we don't have
-       to do anything here. */
-    if (hook) {
-        bool allValid = true;
-        foreach (DerivationOutputs::iterator, i, drv.outputs)
-            if (!worker.store.isValidPath(i->second.path)) allValid = false;
-        if (allValid) return;
+  /* When using a build hook, the build hook can register the output
+     as valid (by doing `nix-store --import').  If so we don't have
+     to do anything here. */
+  if (hook)
+    {
+      bool allValid = true;
+      foreach (DerivationOutputs::iterator, i, drv.outputs)
+        if (!worker.store.isValidPath (i->second.path))
+          allValid = false;
+      if (allValid)
+        return;
     }
 
-    ValidPathInfos infos;
+  ValidPathInfos infos;
 
-    /* Set of inodes seen during calls to canonicalisePathMetaData()
-       for this build's outputs.  This needs to be shared between
-       outputs to allow hard links between outputs. */
-    InodesSeen inodesSeen;
+  /* Set of inodes seen during calls to canonicalisePathMetaData()
+     for this build's outputs.  This needs to be shared between
+     outputs to allow hard links between outputs. */
+  InodesSeen inodesSeen;
 
-    Path checkSuffix = "-check";
+  Path checkSuffix = "-check";
 
-    /* Check whether the output paths were created, and grep each
-       output path to determine what other paths it references.  Also make all
-       output paths read-only. */
-    foreach (DerivationOutputs::iterator, i, drv.outputs) {
-        Path path = i->second.path;
-        if (missingPaths.find(path) == missingPaths.end()) continue;
+  /* Check whether the output paths were created, and grep each
+     output path to determine what other paths it references.  Also make all
+     output paths read-only. */
+  foreach (DerivationOutputs::iterator, i, drv.outputs)
+    {
+      Path path = i->second.path;
+      if (missingPaths.find (path) == missingPaths.end ())
+        continue;
 
-        Path actualPath = path;
-        if (useChroot) {
-            actualPath = chrootRootDir + path;
-        } else {
-            Path redirected = redirectedOutputs[path];
-            if (buildMode == bmRepair
-                && redirectedBadOutputs.find(path) != redirectedBadOutputs.end()
-                && pathExists(redirected))
-                replaceValidPath(path, redirected);
-            if (buildMode == bmCheck && redirected != "")
-                actualPath = redirected;
+      Path actualPath = path;
+      if (useChroot)
+        {
+          actualPath = chrootRootDir + path;
+        }
+      else
+        {
+          Path redirected = redirectedOutputs[path];
+          if (buildMode == bmRepair
+              && redirectedBadOutputs.find (path)
+                     != redirectedBadOutputs.end ()
+              && pathExists (redirected))
+            replaceValidPath (path, redirected);
+          if (buildMode == bmCheck && redirected != "")
+            actualPath = redirected;
         }
 
-        struct stat st;
-        if (lstat(actualPath.c_str(), &st) == -1) {
-            if (errno == ENOENT)
-                throw BuildError(
-                    format("builder for `%1%' failed to produce output path `%2%'")
-                    % drvPath % path);
-            throw SysError(format("getting attributes of path `%1%'") % actualPath);
+      struct stat st;
+      if (lstat (actualPath.c_str (), &st) == -1)
+        {
+          if (errno == ENOENT)
+            throw BuildError (
+                format (
+                    "builder for `%1%' failed to produce output path `%2%'")
+                % drvPath % path);
+          throw SysError (format ("getting attributes of path `%1%'")
+                          % actualPath);
         }
 
 #ifndef __CYGWIN__
-        /* Check that the output is not group or world writable, as
-           that means that someone else can have interfered with the
-           build.  Also, the output should be owned by the build
-           user. */
-        if ((!S_ISLNK(st.st_mode) && (st.st_mode & (S_IWGRP | S_IWOTH))) ||
-            (buildUser.enabled() && st.st_uid != buildUser.getUID()))
-            throw BuildError(format("suspicious ownership or permission on `%1%'; rejecting this build output") % path);
+      /* Check that the output is not group or world writable, as
+         that means that someone else can have interfered with the
+         build.  Also, the output should be owned by the build
+         user. */
+      if ((!S_ISLNK (st.st_mode) && (st.st_mode & (S_IWGRP | S_IWOTH)))
+          || (buildUser.enabled () && st.st_uid != buildUser.getUID ()))
+        throw BuildError (format ("suspicious ownership or permission on "
+                                  "`%1%'; rejecting this build output")
+                          % path);
 #endif
 
-        /* Apply hash rewriting if necessary. */
-        bool rewritten = false;
-        if (!rewritesFromTmp.empty()) {
-            printMsg(lvlError, format("warning: rewriting hashes in `%1%'; cross fingers") % path);
+      /* Apply hash rewriting if necessary. */
+      bool rewritten = false;
+      if (!rewritesFromTmp.empty ())
+        {
+          printMsg (
+              lvlError,
+              format ("warning: rewriting hashes in `%1%'; cross fingers")
+                  % path);
+
+          /* Canonicalise first.  This ensures that the path we're
+             rewriting doesn't contain a hard link to /etc/shadow or
+             something like that. */
+          canonicalisePathMetaData (
+              actualPath, buildUser.enabled () ? buildUser.getUID () : -1,
+              inodesSeen);
+
+          /* FIXME: this is in-memory. */
+          StringSink sink;
+          dumpPath (actualPath, sink);
+          deletePath (actualPath);
+          sink.s = rewriteHashes (sink.s, rewritesFromTmp);
+          StringSource source (sink.s);
+          restorePath (actualPath, source);
+
+          rewritten = true;
+        }
 
-            /* Canonicalise first.  This ensures that the path we're
-               rewriting doesn't contain a hard link to /etc/shadow or
-               something like that. */
-            canonicalisePathMetaData(actualPath, buildUser.enabled() ? buildUser.getUID() : -1, inodesSeen);
+      startNest (nest, lvlTalkative,
+                 format ("scanning for references inside `%1%'") % path);
 
-            /* FIXME: this is in-memory. */
-            StringSink sink;
-            dumpPath(actualPath, sink);
-            deletePath(actualPath);
-            sink.s = rewriteHashes(sink.s, rewritesFromTmp);
-            StringSource source(sink.s);
-            restorePath(actualPath, source);
+      /* Check that fixed-output derivations produced the right
+         outputs (i.e., the content hash should match the specified
+         hash). */
+      if (i->second.hash != "")
+        {
+
+          bool recursive;
+          HashType ht;
+          Hash h;
+          i->second.parseHashInfo (recursive, ht, h);
 
-            rewritten = true;
+          if (!recursive)
+            {
+              /* The output path should be a regular file without
+                 execute permission. */
+              if (!S_ISREG (st.st_mode) || (st.st_mode & S_IXUSR) != 0)
+                throw BuildError (format ("output path `%1% should be a "
+                                          "non-executable regular file")
+                                  % path);
+            }
+
+          /* Check the hash. */
+          Hash h2 = recursive ? hashPath (ht, actualPath).first
+                              : hashFile (ht, actualPath);
+          if (h != h2)
+            {
+              if (settings.printBuildTrace)
+                printMsg (lvlError, format ("@ hash-mismatch %1% %2% %3% %4%")
+                                        % path % i->second.hashAlgo
+                                        % printHash16or32 (h)
+                                        % printHash16or32 (h2));
+              throw BuildError (format ("hash mismatch for store item '%1%'")
+                                % path);
+            }
         }
 
-        startNest(nest, lvlTalkative,
-            format("scanning for references inside `%1%'") % path);
+      /* Get rid of all weird permissions.  This also checks that
+         all files are owned by the build user, if applicable. */
+      canonicalisePathMetaData (
+          actualPath,
+          buildUser.enabled () && !rewritten ? buildUser.getUID () : -1,
+          inodesSeen);
 
-        /* Check that fixed-output derivations produced the right
-           outputs (i.e., the content hash should match the specified
-           hash). */
-        if (i->second.hash != "") {
+      if (useChroot)
+        {
+          if (pathExists (actualPath))
+            {
+              /* Now that output paths have been canonicalized (in particular
+                 there are no setuid files left), move them outside of the
+                 chroot and to the store. */
+              if (buildMode == bmRepair)
+                replaceValidPath (path, actualPath);
+              else if (buildMode != bmCheck)
+                {
+                  if (S_ISDIR (st.st_mode))
+                    /* Change mode on the directory to allow for
+                       rename(2).  */
+                    chmod (actualPath.c_str (), st.st_mode | 0700);
+                  if (rename (actualPath.c_str (), path.c_str ()) == -1)
+                    throw SysError (format ("moving build output `%1%' from "
+                                            "the chroot to the store")
+                                    % path);
+                  if (S_ISDIR (st.st_mode)
+                      && chmod (path.c_str (), st.st_mode) == -1)
+                    throw SysError (
+                        format ("restoring permissions on directory `%1%'")
+                        % actualPath);
+                }
+            }
+          if (buildMode != bmCheck)
+            actualPath = path;
+        }
 
-            bool recursive; HashType ht; Hash h;
-            i->second.parseHashInfo(recursive, ht, h);
+      /* For this output path, find the references to other paths
+         contained in it.  Compute the SHA-256 NAR hash at the same
+         time.  The hash is stored in the database so that we can
+         verify later on whether nobody has messed with the store. */
+      HashResult hash;
+      PathSet references = scanForReferences (actualPath, allPaths, hash);
 
-            if (!recursive) {
-                /* The output path should be a regular file without
-                   execute permission. */
-                if (!S_ISREG(st.st_mode) || (st.st_mode & S_IXUSR) != 0)
-                    throw BuildError(
-                        format("output path `%1% should be a non-executable regular file") % path);
+      if (buildMode == bmCheck)
+        {
+          if (!store->isValidPath (path))
+            continue;
+          ValidPathInfo info = worker.store.queryPathInfo (path);
+          if (hash.first != info.hash)
+            {
+              if (settings.keepFailed)
+                {
+                  Path dst = path + checkSuffix;
+                  if (pathExists (dst))
+                    deletePath (dst);
+                  if (rename (actualPath.c_str (), dst.c_str ()))
+                    throw SysError (format ("renaming `%1%' to `%2%'")
+                                    % actualPath % dst);
+                  throw Error (
+                      format ("derivation `%1%' may not be deterministic: "
+                              "output `%2%' differs from `%3%'")
+                      % drvPath % path % dst);
+                }
+              else
+                throw Error (format ("derivation `%1%' may not be "
+                                     "deterministic: output `%2%' differs")
+                             % drvPath % path);
             }
 
-            /* Check the hash. */
-            Hash h2 = recursive ? hashPath(ht, actualPath).first : hashFile(ht, actualPath);
-            if (h != h2) {
-		if (settings.printBuildTrace)
-		    printMsg(lvlError, format("@ hash-mismatch %1% %2% %3% %4%")
-			     % path % i->second.hashAlgo
-			     % printHash16or32(h) % printHash16or32(h2));
-                throw BuildError(format("hash mismatch for store item '%1%'") % path);
-	    }
-        }
-
-        /* Get rid of all weird permissions.  This also checks that
-           all files are owned by the build user, if applicable. */
-        canonicalisePathMetaData(actualPath,
-            buildUser.enabled() && !rewritten ? buildUser.getUID() : -1, inodesSeen);
-
-        if (useChroot) {
-          if (pathExists(actualPath)) {
-            /* Now that output paths have been canonicalized (in particular
-               there are no setuid files left), move them outside of the
-               chroot and to the store. */
-            if (buildMode == bmRepair)
-              replaceValidPath(path, actualPath);
-            else
-		if (buildMode != bmCheck) {
-		    if (S_ISDIR(st.st_mode))
-			/* Change mode on the directory to allow for
-			   rename(2).  */
-			chmod(actualPath.c_str(), st.st_mode | 0700);
-		    if (rename(actualPath.c_str(), path.c_str()) == -1)
-			throw SysError(format("moving build output `%1%' from the chroot to the store") % path);
-		    if (S_ISDIR(st.st_mode) && chmod(path.c_str(), st.st_mode) == -1)
-			throw SysError(format("restoring permissions on directory `%1%'") % actualPath);
-		}
+          if (settings.printBuildTrace)
+            printMsg (lvlError, format ("@ build-succeeded %1% -") % drvPath);
+
+          continue;
+        }
+
+      /* For debugging, print out the referenced and unreferenced
+         paths. */
+      foreach (PathSet::iterator, i, inputPaths)
+        {
+          PathSet::iterator j = references.find (*i);
+          if (j == references.end ())
+            debug (format ("unreferenced input: `%1%'") % *i);
+          else
+            debug (format ("referenced input: `%1%'") % *i);
+        }
+
+      /* Enforce `allowedReferences' and friends. */
+      auto checkRefs = [&] (const string &attrName, bool allowed,
+                            bool recursive) {
+        if (drv.env.find (attrName) == drv.env.end ())
+          return;
+
+        PathSet spec = parseReferenceSpecifiers (drv, get (drv.env, attrName));
+
+        PathSet used;
+        if (recursive)
+          {
+            /* Our requisites are the union of the closures of our references.
+             */
+            for (auto &i : references)
+              /* Don't call computeFSClosure on ourselves. */
+              if (actualPath != i)
+                computeFSClosure (worker.store, i, used);
           }
-          if (buildMode != bmCheck) actualPath = path;
-        }
-
-        /* For this output path, find the references to other paths
-           contained in it.  Compute the SHA-256 NAR hash at the same
-           time.  The hash is stored in the database so that we can
-           verify later on whether nobody has messed with the store. */
-        HashResult hash;
-        PathSet references = scanForReferences(actualPath, allPaths, hash);
-
-        if (buildMode == bmCheck) {
-            if (!store->isValidPath(path)) continue;
-            ValidPathInfo info = worker.store.queryPathInfo(path);
-            if (hash.first != info.hash) {
-                if (settings.keepFailed) {
-                    Path dst = path + checkSuffix;
-                    if (pathExists(dst)) deletePath(dst);
-                    if (rename(actualPath.c_str(), dst.c_str()))
-                        throw SysError(format("renaming `%1%' to `%2%'") % actualPath % dst);
-                    throw Error(format("derivation `%1%' may not be deterministic: output `%2%' differs from `%3%'")
-                        % drvPath % path % dst);
-                } else
-                    throw Error(format("derivation `%1%' may not be deterministic: output `%2%' differs")
-                        % drvPath % path);
+        else
+          used = references;
+
+        for (auto &i : used)
+          if (allowed)
+            {
+              if (spec.find (i) == spec.end ())
+                throw BuildError (
+                    format (
+                        "output (`%1%') is not allowed to refer to path `%2%'")
+                    % actualPath % i);
             }
+          else
+            {
+              if (spec.find (i) != spec.end ())
+                throw BuildError (
+                    format (
+                        "output (`%1%') is not allowed to refer to path `%2%'")
+                    % actualPath % i);
+            }
+      };
 
-            if (settings.printBuildTrace)
-                printMsg(lvlError, format("@ build-succeeded %1% -") % drvPath);
+      checkRefs ("allowedReferences", true, false);
+      checkRefs ("allowedRequisites", true, true);
+      checkRefs ("disallowedReferences", false, false);
+      checkRefs ("disallowedRequisites", false, true);
 
-            continue;
+      if (curRound == nrRounds)
+        {
+          worker.store.optimisePath (
+              path); // FIXME: combine with scanForReferences()
+
+          worker.store.markContentsGood (path);
         }
 
-        /* For debugging, print out the referenced and unreferenced
-           paths. */
-        foreach (PathSet::iterator, i, inputPaths) {
-            PathSet::iterator j = references.find(*i);
-            if (j == references.end())
-                debug(format("unreferenced input: `%1%'") % *i);
+      ValidPathInfo info;
+      info.path = path;
+      info.hash = hash.first;
+      info.narSize = hash.second;
+      info.references = references;
+      info.deriver = drvPath;
+      infos.push_back (info);
+    }
+
+  /* Compare the result with the previous round, and report which
+     path is different, if any.*/
+  if (curRound > 1 && prevInfos != infos)
+    {
+      assert (prevInfos.size () == infos.size ());
+      for (auto i = prevInfos.begin (), j = infos.begin ();
+           i != prevInfos.end (); ++i, ++j)
+        if (!(*i == *j))
+          {
+            Path prev = i->path + checkSuffix;
+            if (pathExists (prev))
+              throw NotDeterministic (format ("output ‘%1%’ of ‘%2%’ differs "
+                                              "from ‘%3%’ from previous round")
+                                      % i->path % drvPath % prev);
             else
-                debug(format("referenced input: `%1%'") % *i);
-        }
-
-        /* Enforce `allowedReferences' and friends. */
-        auto checkRefs = [&](const string & attrName, bool allowed, bool recursive) {
-            if (drv.env.find(attrName) == drv.env.end()) return;
-
-            PathSet spec = parseReferenceSpecifiers(drv, get(drv.env, attrName));
-
-            PathSet used;
-            if (recursive) {
-                /* Our requisites are the union of the closures of our references. */
-                for (auto & i : references)
-                    /* Don't call computeFSClosure on ourselves. */
-                    if (actualPath != i)
-                        computeFSClosure(worker.store, i, used);
-            } else
-                used = references;
-
-            for (auto & i : used)
-                if (allowed) {
-                    if (spec.find(i) == spec.end())
-                        throw BuildError(format("output (`%1%') is not allowed to refer to path `%2%'") % actualPath % i);
-                } else {
-                    if (spec.find(i) != spec.end())
-                        throw BuildError(format("output (`%1%') is not allowed to refer to path `%2%'") % actualPath % i);
-                }
-        };
-
-        checkRefs("allowedReferences", true, false);
-        checkRefs("allowedRequisites", true, true);
-        checkRefs("disallowedReferences", false, false);
-        checkRefs("disallowedRequisites", false, true);
-
-        if (curRound == nrRounds) {
-            worker.store.optimisePath(path); // FIXME: combine with scanForReferences()
-
-            worker.store.markContentsGood(path);
-        }
-
-        ValidPathInfo info;
-        info.path = path;
-        info.hash = hash.first;
-        info.narSize = hash.second;
-        info.references = references;
-        info.deriver = drvPath;
-        infos.push_back(info);
-    }
-
-    /* Compare the result with the previous round, and report which
-       path is different, if any.*/
-    if (curRound > 1 && prevInfos != infos) {
-        assert(prevInfos.size() == infos.size());
-        for (auto i = prevInfos.begin(), j = infos.begin(); i != prevInfos.end(); ++i, ++j)
-            if (!(*i == *j)) {
-                Path prev = i->path + checkSuffix;
-                if (pathExists(prev))
-                    throw NotDeterministic(
-                        format("output ‘%1%’ of ‘%2%’ differs from ‘%3%’ from previous round")
-                        % i->path % drvPath % prev);
-                else
-                    throw NotDeterministic(
-                        format("output ‘%1%’ of ‘%2%’ differs from previous round")
-                        % i->path % drvPath);
-            }
-        assert(false); // shouldn't happen
+              throw NotDeterministic (
+                  format ("output ‘%1%’ of ‘%2%’ differs from previous round")
+                  % i->path % drvPath);
+          }
+      assert (false); // shouldn't happen
     }
 
-    if (settings.keepFailed) {
-        for (auto & i : drv.outputs) {
-            Path prev = i.second.path + checkSuffix;
-            if (pathExists(prev)) deletePath(prev);
-            if (curRound < nrRounds) {
-                Path dst = i.second.path + checkSuffix;
-                if (rename(i.second.path.c_str(), dst.c_str()))
-                    throw SysError(format("renaming ‘%1%’ to ‘%2%’") % i.second.path % dst);
+  if (settings.keepFailed)
+    {
+      for (auto &i : drv.outputs)
+        {
+          Path prev = i.second.path + checkSuffix;
+          if (pathExists (prev))
+            deletePath (prev);
+          if (curRound < nrRounds)
+            {
+              Path dst = i.second.path + checkSuffix;
+              if (rename (i.second.path.c_str (), dst.c_str ()))
+                throw SysError (format ("renaming ‘%1%’ to ‘%2%’")
+                                % i.second.path % dst);
             }
         }
-
     }
 
-    if (curRound < nrRounds) {
-        prevInfos = infos;
-        return;
+  if (curRound < nrRounds)
+    {
+      prevInfos = infos;
+      return;
     }
 
-    /* Register each output path as valid, and register the sets of
-       paths referenced by each of them.  If there are cycles in the
-       outputs, this will fail. */
-    worker.store.registerValidPaths(infos);
+  /* Register each output path as valid, and register the sets of
+     paths referenced by each of them.  If there are cycles in the
+     outputs, this will fail. */
+  worker.store.registerValidPaths (infos);
 }
 
-
 string drvsLogDir = "drvs";
 
-
-Path DerivationGoal::openLogFile()
+Path
+DerivationGoal::openLogFile ()
 {
-    logSize = 0;
+  logSize = 0;
 
-    if (!settings.keepLog) return "";
+  if (!settings.keepLog)
+    return "";
 
-    string baseName = baseNameOf(drvPath);
+  string baseName = baseNameOf (drvPath);
 
-    /* Create a log file. */
-    Path dir = (format("%1%/%2%/%3%/") % settings.nixLogDir % drvsLogDir % string(baseName, 0, 2)).str();
-    createDirs(dir);
+  /* Create a log file. */
+  Path dir = (format ("%1%/%2%/%3%/") % settings.nixLogDir % drvsLogDir
+              % string (baseName, 0, 2))
+                 .str ();
+  createDirs (dir);
 
-    switch (settings.logCompression)
+  switch (settings.logCompression)
+    {
+    case COMPRESSION_GZIP:
       {
-      case COMPRESSION_GZIP: {
-        Path logFileName = (format("%1%/%2%.gz") % dir % string(baseName, 2)).str();
-        AutoCloseFD fd = open(logFileName.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
-        if (fd == -1) throw SysError(format("creating log file `%1%'") % logFileName);
-        closeOnExec(fd);
+        Path logFileName
+            = (format ("%1%/%2%.gz") % dir % string (baseName, 2)).str ();
+        AutoCloseFD fd
+            = open (logFileName.c_str (), O_CREAT | O_WRONLY | O_TRUNC, 0666);
+        if (fd == -1)
+          throw SysError (format ("creating log file `%1%'") % logFileName);
+        closeOnExec (fd);
 
-	/* Note: FD will be closed by 'gzclose'.  */
-        if (!(gzLogFile = gzdopen(fd.borrow(), "w")))
-            throw Error(format("cannot open compressed log file `%1%'") % logFileName);
+        /* Note: FD will be closed by 'gzclose'.  */
+        if (!(gzLogFile = gzdopen (fd.borrow (), "w")))
+          throw Error (format ("cannot open compressed log file `%1%'")
+                       % logFileName);
 
-        gzbuffer(gzLogFile, 32768);
-        gzsetparams(gzLogFile, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY);
+        gzbuffer (gzLogFile, 32768);
+        gzsetparams (gzLogFile, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY);
 
         return logFileName;
       }
 
 #if HAVE_BZLIB_H
-      case COMPRESSION_BZIP2: {
-        Path logFileName = (format("%1%/%2%.bz2") % dir % string(baseName, 2)).str();
-        AutoCloseFD fd = open(logFileName.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
-        if (fd == -1) throw SysError(format("creating log file `%1%'") % logFileName);
-        closeOnExec(fd);
+    case COMPRESSION_BZIP2:
+      {
+        Path logFileName
+            = (format ("%1%/%2%.bz2") % dir % string (baseName, 2)).str ();
+        AutoCloseFD fd
+            = open (logFileName.c_str (), O_CREAT | O_WRONLY | O_TRUNC, 0666);
+        if (fd == -1)
+          throw SysError (format ("creating log file `%1%'") % logFileName);
+        closeOnExec (fd);
 
-        if (!(fLogFile = fdopen(fd.borrow(), "w")))
-            throw SysError(format("opening log file `%1%'") % logFileName);
+        if (!(fLogFile = fdopen (fd.borrow (), "w")))
+          throw SysError (format ("opening log file `%1%'") % logFileName);
 
         int err;
-        if (!(bzLogFile = BZ2_bzWriteOpen(&err, fLogFile, 9, 0, 0)))
-            throw Error(format("cannot open compressed log file `%1%'") % logFileName);
+        if (!(bzLogFile = BZ2_bzWriteOpen (&err, fLogFile, 9, 0, 0)))
+          throw Error (format ("cannot open compressed log file `%1%'")
+                       % logFileName);
 
         return logFileName;
       }
 #endif
 
-      case COMPRESSION_NONE: {
-        Path logFileName = (format("%1%/%2%") % dir % string(baseName, 2)).str();
-        fdLogFile = open(logFileName.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
-        if (fdLogFile == -1) throw SysError(format("creating log file `%1%'") % logFileName);
-        closeOnExec(fdLogFile);
+    case COMPRESSION_NONE:
+      {
+        Path logFileName
+            = (format ("%1%/%2%") % dir % string (baseName, 2)).str ();
+        fdLogFile
+            = open (logFileName.c_str (), O_CREAT | O_WRONLY | O_TRUNC, 0666);
+        if (fdLogFile == -1)
+          throw SysError (format ("creating log file `%1%'") % logFileName);
+        closeOnExec (fdLogFile);
         return logFileName;
       }
     }
 
-    abort();
+  abort ();
 }
 
-
-void DerivationGoal::closeLogFile()
+void
+DerivationGoal::closeLogFile ()
 {
-    if (gzLogFile) {
-	int err;
-	err = gzclose(gzLogFile);
-	gzLogFile = NULL;
-	if (err != Z_OK) throw Error(format("cannot close compressed log file (gzip error = %1%)") % err);
+  if (gzLogFile)
+    {
+      int err;
+      err = gzclose (gzLogFile);
+      gzLogFile = NULL;
+      if (err != Z_OK)
+        throw Error (
+            format ("cannot close compressed log file (gzip error = %1%)")
+            % err);
     }
 #if HAVE_BZLIB_H
-    else if (bzLogFile) {
-        int err;
-        BZ2_bzWriteClose(&err, bzLogFile, 0, 0, 0);
-        bzLogFile = 0;
-        if (err != BZ_OK) throw Error(format("cannot close compressed log file (BZip2 error = %1%)") % err);
+  else if (bzLogFile)
+    {
+      int err;
+      BZ2_bzWriteClose (&err, bzLogFile, 0, 0, 0);
+      bzLogFile = 0;
+      if (err != BZ_OK)
+        throw Error (
+            format ("cannot close compressed log file (BZip2 error = %1%)")
+            % err);
     }
 #endif
 
-    if (fLogFile) {
-        fclose(fLogFile);
-        fLogFile = 0;
+  if (fLogFile)
+    {
+      fclose (fLogFile);
+      fLogFile = 0;
     }
 
-    fdLogFile.close();
+  fdLogFile.close ();
 }
 
-
-static void _chown(const Path & path, uid_t uid, gid_t gid)
+static void
+_chown (const Path &path, uid_t uid, gid_t gid)
 {
-    checkInterrupt();
+  checkInterrupt ();
 
-    if (lchown(path.c_str(), uid, gid) == -1) {
-	throw SysError(format("change owner and group of `%1%'") % path);
+  if (lchown (path.c_str (), uid, gid) == -1)
+    {
+      throw SysError (format ("change owner and group of `%1%'") % path);
     }
-    struct stat st = lstat(path);
-    if (S_ISDIR(st.st_mode)) {
-        for (auto & i : readDirectory(path))
-            _chown(path + "/" + i.name, uid, gid);
+  struct stat st = lstat (path);
+  if (S_ISDIR (st.st_mode))
+    {
+      for (auto &i : readDirectory (path))
+        _chown (path + "/" + i.name, uid, gid);
     }
 }
 
-
-void DerivationGoal::deleteTmpDir(bool force)
+void
+DerivationGoal::deleteTmpDir (bool force)
 {
-    if (tmpDir != "") {
-	// When useChroot is true, tmpDir looks like
-	// "/tmp/guix-build-foo.drv-0/top".  Its parent is root-owned.
-	string top;
-	if (useChroot) {
-	    if (baseNameOf(tmpDir) != "top") abort();
-	    top = dirOf(tmpDir);
-	} else top = tmpDir;
-
-        if (settings.keepFailed && !force) {
-            printMsg(lvlError,
-                format("note: keeping build directory `%2%'")
-                % drvPath % top);
-            chmod(tmpDir.c_str(), 0755);
-
-            // Change the ownership if clientUid is set. Never change the
-            // ownership or the group to "root" for security reasons.
-            if (settings.clientUid != (uid_t) -1 && settings.clientUid != 0) {
-		uid_t uid = settings.clientUid;
-		gid_t gid = settings.clientGid != 0 ? settings.clientGid : -1;
-		bool reown = false;
-
-		/* First remove setuid/setgid bits.  */
-		secureFilePerms(tmpDir);
-
-		try {
-		    _chown(tmpDir, uid, gid);
+  if (tmpDir != "")
+    {
+      // When useChroot is true, tmpDir looks like
+      // "/tmp/guix-build-foo.drv-0/top".  Its parent is root-owned.
+      string top;
+      if (useChroot)
+        {
+          if (baseNameOf (tmpDir) != "top")
+            abort ();
+          top = dirOf (tmpDir);
+        }
+      else
+        top = tmpDir;
 
-		    if (getuid() != 0) {
-			/* If, without being root, the '_chown' call above
-			   succeeded, then it means we have CAP_CHOWN.  Retake
-			   ownership of tmpDir itself so it can be renamed
-			   below.  */
-			reown = true;
-		    }
+      if (settings.keepFailed && !force)
+        {
+          printMsg (lvlError, format ("note: keeping build directory `%2%'")
+                                  % drvPath % top);
+          chmod (tmpDir.c_str (), 0755);
 
-		} catch (SysError & e) {
-		    /* When running as an unprivileged user and without
-		       CAP_CHOWN, we cannot chown the build tree.  Print a
-		       message and keep going.  */
-		    printMsg(lvlInfo, format("cannot change ownership of build directory '%1%': %2%")
-			     % tmpDir % strerror(e.errNo));
-		}
+          // Change the ownership if clientUid is set. Never change the
+          // ownership or the group to "root" for security reasons.
+          if (settings.clientUid != (uid_t)-1 && settings.clientUid != 0)
+            {
+              uid_t uid = settings.clientUid;
+              gid_t gid = settings.clientGid != 0 ? settings.clientGid : -1;
+              bool reown = false;
+
+              /* First remove setuid/setgid bits.  */
+              secureFilePerms (tmpDir);
+
+              try
+                {
+                  _chown (tmpDir, uid, gid);
+
+                  if (getuid () != 0)
+                    {
+                      /* If, without being root, the '_chown' call above
+                         succeeded, then it means we have CAP_CHOWN.  Retake
+                         ownership of tmpDir itself so it can be renamed
+                         below.  */
+                      reown = true;
+                    }
+                }
+              catch (SysError &e)
+                {
+                  /* When running as an unprivileged user and without
+                     CAP_CHOWN, we cannot chown the build tree.  Print a
+                     message and keep going.  */
+                  printMsg (lvlInfo, format ("cannot change ownership of "
+                                             "build directory '%1%': %2%")
+                                         % tmpDir % strerror (e.errNo));
+                }
 
-		if (top != tmpDir) {
-		    if (reown) chown(tmpDir.c_str(), getuid(), getgid());
+              if (top != tmpDir)
+                {
+                  if (reown)
+                    chown (tmpDir.c_str (), getuid (), getgid ());
 
-		    // Rename tmpDir to its parent, with an intermediate step.
-		    string pivot = top + ".pivot";
-		    if (rename(top.c_str(), pivot.c_str()) == -1)
-			throw SysError("pivoting failed build tree");
-		    if (rename((pivot + "/top").c_str(), top.c_str()) == -1)
-			throw SysError("renaming failed build tree");
+                  // Rename tmpDir to its parent, with an intermediate step.
+                  string pivot = top + ".pivot";
+                  if (rename (top.c_str (), pivot.c_str ()) == -1)
+                    throw SysError ("pivoting failed build tree");
+                  if (rename ((pivot + "/top").c_str (), top.c_str ()) == -1)
+                    throw SysError ("renaming failed build tree");
 
-		    if (reown)
-			/* Running unprivileged but with CAP_CHOWN.  */
-			chown(top.c_str(), uid, gid);
+                  if (reown)
+                    /* Running unprivileged but with CAP_CHOWN.  */
+                    chown (top.c_str (), uid, gid);
 
-		    rmdir(pivot.c_str());
-		}
+                  rmdir (pivot.c_str ());
+                }
             }
         }
-        else {
-            deletePath(tmpDir);
-	    if (top != tmpDir) rmdir(dirOf(tmpDir).c_str());
-	}
-        tmpDir = "";
+      else
+        {
+          deletePath (tmpDir);
+          if (top != tmpDir)
+            rmdir (dirOf (tmpDir).c_str ());
+        }
+      tmpDir = "";
     }
 }
 
-
-void DerivationGoal::handleChildOutput(int fd, const string & data)
+void
+DerivationGoal::handleChildOutput (int fd, const string &data)
 {
-    string prefix;
+  string prefix;
 
-    if (settings.multiplexedBuildOutput) {
-	/* Print a prefix that allows clients to determine whether a message
-	   comes from the daemon or from a build process, and in the latter
-	   case, which build process it comes from.  The PID here matches the
-	   one given in "@ build-started" traces; it's shorter that the
-	   derivation file name, hence this choice.  */
-	prefix = "@ build-log "
-	    + std::to_string(pid < 0 ? hook->pid : pid)
-	    + " " + std::to_string(data.size()) + "\n";
+  if (settings.multiplexedBuildOutput)
+    {
+      /* Print a prefix that allows clients to determine whether a message
+         comes from the daemon or from a build process, and in the latter
+         case, which build process it comes from.  The PID here matches the
+         one given in "@ build-started" traces; it's shorter that the
+         derivation file name, hence this choice.  */
+      prefix = "@ build-log " + std::to_string (pid < 0 ? hook->pid : pid)
+               + " " + std::to_string (data.size ()) + "\n";
     }
 
-    if ((hook && fd == hook->builderOut.readSide) ||
-        (!hook && fd == builderOut.readSide))
+  if ((hook && fd == hook->builderOut.readSide)
+      || (!hook && fd == builderOut.readSide))
     {
-        logSize += data.size();
-        if (settings.maxLogSize && logSize > settings.maxLogSize) {
-            printMsg(lvlError,
-                format("%1% killed after writing more than %2% bytes of log output")
-                % getName() % settings.maxLogSize);
-            timedOut(); // not really a timeout, but close enough
-            return;
+      logSize += data.size ();
+      if (settings.maxLogSize && logSize > settings.maxLogSize)
+        {
+          printMsg (
+              lvlError,
+              format (
+                  "%1% killed after writing more than %2% bytes of log output")
+                  % getName () % settings.maxLogSize);
+          timedOut (); // not really a timeout, but close enough
+          return;
         }
-        if (verbosity >= settings.buildVerbosity)
-            writeToStderr(prefix + data);
+      if (verbosity >= settings.buildVerbosity)
+        writeToStderr (prefix + data);
 
-	if (gzLogFile) {
-	    if (data.size() > 0) {
-		int count, err;
-		count = gzwrite(gzLogFile, data.data(), data.size());
-		if (count == 0) throw Error(format("cannot write to compressed log file (gzip error = %1%)") % gzerror(gzLogFile, &err));
-	    }
+      if (gzLogFile)
+        {
+          if (data.size () > 0)
+            {
+              int count, err;
+              count = gzwrite (gzLogFile, data.data (), data.size ());
+              if (count == 0)
+                throw Error (format ("cannot write to compressed log file "
+                                     "(gzip error = %1%)")
+                             % gzerror (gzLogFile, &err));
+            }
 #if HAVE_BZLIB_H
-	} else if (bzLogFile) {
-            int err;
-            BZ2_bzWrite(&err, bzLogFile, (unsigned char *) data.data(), data.size());
-            if (err != BZ_OK) throw Error(format("cannot write to compressed log file (BZip2 error = %1%)") % err);
+        }
+      else if (bzLogFile)
+        {
+          int err;
+          BZ2_bzWrite (&err, bzLogFile, (unsigned char *)data.data (),
+                       data.size ());
+          if (err != BZ_OK)
+            throw Error (
+                format (
+                    "cannot write to compressed log file (BZip2 error = %1%)")
+                % err);
 #endif
-        } else if (fdLogFile != -1)
-            writeFull(fdLogFile, data);
+        }
+      else if (fdLogFile != -1)
+        writeFull (fdLogFile, data);
     }
 
-    if (hook && fd == hook->fromAgent.readSide)
-        writeToStderr(prefix + data);
+  if (hook && fd == hook->fromAgent.readSide)
+    writeToStderr (prefix + data);
 }
 
-
-void DerivationGoal::handleEOF(int fd)
+void
+DerivationGoal::handleEOF (int fd)
 {
-    worker.wakeUp(shared_from_this());
+  worker.wakeUp (shared_from_this ());
 }
 
-
-PathSet DerivationGoal::checkPathValidity(bool returnValid, bool checkHash)
+PathSet
+DerivationGoal::checkPathValidity (bool returnValid, bool checkHash)
 {
-    PathSet result;
-    foreach (DerivationOutputs::iterator, i, drv.outputs) {
-        if (!wantOutput(i->first, wantedOutputs)) continue;
-        bool good =
-            worker.store.isValidPath(i->second.path) &&
-            (!checkHash || worker.store.pathContentsGood(i->second.path));
-        if (good == returnValid) result.insert(i->second.path);
+  PathSet result;
+  foreach (DerivationOutputs::iterator, i, drv.outputs)
+    {
+      if (!wantOutput (i->first, wantedOutputs))
+        continue;
+      bool good
+          = worker.store.isValidPath (i->second.path)
+            && (!checkHash || worker.store.pathContentsGood (i->second.path));
+      if (good == returnValid)
+        result.insert (i->second.path);
     }
-    return result;
+  return result;
 }
 
-
-bool DerivationGoal::pathFailed(const Path & path)
+bool
+DerivationGoal::pathFailed (const Path &path)
 {
-    if (!settings.cacheFailure) return false;
+  if (!settings.cacheFailure)
+    return false;
 
-    if (!worker.store.hasPathFailed(path)) return false;
+  if (!worker.store.hasPathFailed (path))
+    return false;
 
-    printMsg(lvlError, format("builder for `%1%' failed previously (cached)") % path);
+  printMsg (lvlError,
+            format ("builder for `%1%' failed previously (cached)") % path);
 
-    if (settings.printBuildTrace)
-        printMsg(lvlError, format("@ build-failed %1% - cached") % drvPath);
+  if (settings.printBuildTrace)
+    printMsg (lvlError, format ("@ build-failed %1% - cached") % drvPath);
 
-    done(BuildResult::CachedFailure);
+  done (BuildResult::CachedFailure);
 
-    return true;
+  return true;
 }
 
-
-Path DerivationGoal::addHashRewrite(const Path & path)
+Path
+DerivationGoal::addHashRewrite (const Path &path)
 {
-    string h1 = string(path, settings.nixStore.size() + 1, 32);
-    string h2 = string(printHash32(hashString(htSHA256, "rewrite:" + drvPath + ":" + path)), 0, 32);
-    Path p = settings.nixStore + "/" + h2 + string(path, settings.nixStore.size() + 33);
-    if (pathExists(p)) deletePath(p);
-    assert(path.size() == p.size());
-    rewritesToTmp[h1] = h2;
-    rewritesFromTmp[h2] = h1;
-    redirectedOutputs[path] = p;
-    printMsg(lvlChatty, format("output '%1%' redirected to '%2%'")
-	     % path % p);
-    return p;
+  string h1 = string (path, settings.nixStore.size () + 1, 32);
+  string h2 = string (
+      printHash32 (hashString (htSHA256, "rewrite:" + drvPath + ":" + path)),
+      0, 32);
+  Path p = settings.nixStore + "/" + h2
+           + string (path, settings.nixStore.size () + 33);
+  if (pathExists (p))
+    deletePath (p);
+  assert (path.size () == p.size ());
+  rewritesToTmp[h1] = h2;
+  rewritesFromTmp[h2] = h1;
+  redirectedOutputs[path] = p;
+  printMsg (lvlChatty, format ("output '%1%' redirected to '%2%'") % path % p);
+  return p;
 }
 
-
-void DerivationGoal::done(BuildResult::Status status, const string & msg)
+void
+DerivationGoal::done (BuildResult::Status status, const string &msg)
 {
-    result.status = status;
-    result.errorMsg = msg;
-    amDone(result.success() ? ecSuccess : ecFailed);
-    if (result.status == BuildResult::TimedOut)
-        worker.timedOut = true;
-    if (result.status == BuildResult::PermanentFailure || result.status == BuildResult::CachedFailure)
-        worker.permanentFailure = true;
+  result.status = status;
+  result.errorMsg = msg;
+  amDone (result.success () ? ecSuccess : ecFailed);
+  if (result.status == BuildResult::TimedOut)
+    worker.timedOut = true;
+  if (result.status == BuildResult::PermanentFailure
+      || result.status == BuildResult::CachedFailure)
+    worker.permanentFailure = true;
 }
 
-
 //////////////////////////////////////////////////////////////////////
 
-
 class SubstitutionGoal : public Goal
 {
-    friend class Worker;
+  friend class Worker;
 
 private:
-    /* The store path that should be realised through a substitute. */
-    Path storePath;
+  /* The store path that should be realised through a substitute. */
+  Path storePath;
 
-    /* Path info returned by the substituter's query info operation. */
-    SubstitutablePathInfo info;
+  /* Path info returned by the substituter's query info operation. */
+  SubstitutablePathInfo info;
 
-    /* Lock on the store path. */
-    std::shared_ptr<PathLocks> outputLock;
+  /* Lock on the store path. */
+  std::shared_ptr<PathLocks> outputLock;
 
-    /* Whether to try to repair a valid path. */
-    bool repair;
+  /* Whether to try to repair a valid path. */
+  bool repair;
 
-    /* Location where we're downloading the substitute.  Differs from
-       storePath when doing a repair. */
-    Path destPath;
+  /* Location where we're downloading the substitute.  Differs from
+     storePath when doing a repair. */
+  Path destPath;
 
-    typedef void (SubstitutionGoal::*GoalState)();
-    GoalState state;
+  typedef void (SubstitutionGoal::*GoalState) ();
+  GoalState state;
 
-    /* The substituter. */
-    std::shared_ptr<Agent> substituter;
+  /* The substituter. */
+  std::shared_ptr<Agent> substituter;
 
-    /* Either the empty string, or the status phrase returned by the
-       substituter.  */
-    string status;
+  /* Either the empty string, or the status phrase returned by the
+     substituter.  */
+  string status;
 
-    void tryNext();
+  void tryNext ();
 
 public:
-    SubstitutionGoal(const Path & storePath, Worker & worker, bool repair = false);
-    ~SubstitutionGoal();
-
-    void timedOut();
-
-    string key()
-    {
-        /* "a$" ensures substitution goals happen before derivation
-           goals. */
-        return "a$" + storePathToName(storePath) + "$" + storePath;
-    }
+  SubstitutionGoal (const Path &storePath, Worker &worker,
+                    bool repair = false);
+  ~SubstitutionGoal ();
+
+  void timedOut ();
+
+  string
+  key ()
+  {
+    /* "a$" ensures substitution goals happen before derivation
+       goals. */
+    return "a$" + storePathToName (storePath) + "$" + storePath;
+  }
 
-    void work();
+  void work ();
 
-    /* The states. */
-    void init();
-    void gotInfo();
-    void referencesValid();
-    void tryToRun();
-    void finished();
+  /* The states. */
+  void init ();
+  void gotInfo ();
+  void referencesValid ();
+  void tryToRun ();
+  void finished ();
 
-    /* Callback used by the worker to write to the log. */
-    void handleChildOutput(int fd, const string & data);
-    void handleEOF(int fd);
+  /* Callback used by the worker to write to the log. */
+  void handleChildOutput (int fd, const string &data);
+  void handleEOF (int fd);
 
-    Path getStorePath() { return storePath; }
+  Path
+  getStorePath ()
+  {
+    return storePath;
+  }
 };
 
-
-SubstitutionGoal::SubstitutionGoal(const Path & storePath, Worker & worker, bool repair)
-    : Goal(worker)
-    , repair(repair)
+SubstitutionGoal::SubstitutionGoal (const Path &storePath, Worker &worker,
+                                    bool repair)
+    : Goal (worker), repair (repair)
 {
-    this->storePath = storePath;
-    state = &SubstitutionGoal::init;
-    name = (format("substitution of `%1%'") % storePath).str();
-    trace("created");
+  this->storePath = storePath;
+  state = &SubstitutionGoal::init;
+  name = (format ("substitution of `%1%'") % storePath).str ();
+  trace ("created");
 }
 
-
-SubstitutionGoal::~SubstitutionGoal()
+SubstitutionGoal::~SubstitutionGoal ()
 {
-    if (substituter) worker.childTerminated(substituter->pid);
+  if (substituter)
+    worker.childTerminated (substituter->pid);
 }
 
-
-void SubstitutionGoal::timedOut()
+void
+SubstitutionGoal::timedOut ()
 {
-    if (settings.printBuildTrace)
-        printMsg(lvlError, format("@ substituter-failed %1% timeout") % storePath);
-    if (substituter) {
-        pid_t savedPid = substituter->pid;
-	substituter.reset();
-        worker.childTerminated(savedPid);
+  if (settings.printBuildTrace)
+    printMsg (lvlError,
+              format ("@ substituter-failed %1% timeout") % storePath);
+  if (substituter)
+    {
+      pid_t savedPid = substituter->pid;
+      substituter.reset ();
+      worker.childTerminated (savedPid);
     }
-    amDone(ecFailed);
+  amDone (ecFailed);
 }
 
-
-void SubstitutionGoal::work()
+void
+SubstitutionGoal::work ()
 {
-    (this->*state)();
+  (this->*state) ();
 }
 
-
-void SubstitutionGoal::init()
+void
+SubstitutionGoal::init ()
 {
-    trace("init");
+  trace ("init");
 
-    worker.store.addTempRoot(storePath);
+  worker.store.addTempRoot (storePath);
 
-    /* If the path already exists we're done. */
-    if (!repair && worker.store.isValidPath(storePath)) {
-        amDone(ecSuccess);
-        return;
+  /* If the path already exists we're done. */
+  if (!repair && worker.store.isValidPath (storePath))
+    {
+      amDone (ecSuccess);
+      return;
     }
 
-    if (settings.readOnlyMode)
-        throw Error(format("cannot substitute path `%1%' - no write access to the store") % storePath);
+  if (settings.readOnlyMode)
+    throw Error (
+        format ("cannot substitute path `%1%' - no write access to the store")
+        % storePath);
 
-    tryNext();
+  tryNext ();
 }
 
-
-void SubstitutionGoal::tryNext()
+void
+SubstitutionGoal::tryNext ()
 {
-    trace("trying substituter");
+  trace ("trying substituter");
 
-    SubstitutablePathInfos infos;
-    PathSet dummy(singleton<PathSet>(storePath));
-    worker.store.querySubstitutablePathInfos(dummy, infos);
-    SubstitutablePathInfos::iterator k = infos.find(storePath);
-    if (k == infos.end()) {
-        /* None left.  Terminate this goal and let someone else deal
-           with it. */
-        debug(format("path `%1%' is required, but there is no substituter that can build it") % storePath);
-        /* Hack: don't indicate failure if there were no substituters.
-           In that case the calling derivation should just do a
-           build. */
-        amDone(ecNoSubstituters);
-	return;
+  SubstitutablePathInfos infos;
+  PathSet dummy (singleton<PathSet> (storePath));
+  worker.store.querySubstitutablePathInfos (dummy, infos);
+  SubstitutablePathInfos::iterator k = infos.find (storePath);
+  if (k == infos.end ())
+    {
+      /* None left.  Terminate this goal and let someone else deal
+         with it. */
+      debug (format ("path `%1%' is required, but there is no substituter "
+                     "that can build it")
+             % storePath);
+      /* Hack: don't indicate failure if there were no substituters.
+         In that case the calling derivation should just do a
+         build. */
+      amDone (ecNoSubstituters);
+      return;
     }
 
-    /* Found a substitute.  */
-    info = k->second;
+  /* Found a substitute.  */
+  info = k->second;
 
-    /* To maintain the closure invariant, we first have to realise the
-       paths referenced by this one. */
-    foreach (PathSet::iterator, i, info.references)
-        if (*i != storePath) /* ignore self-references */
-            addWaitee(worker.makeSubstitutionGoal(*i));
+  /* To maintain the closure invariant, we first have to realise the
+     paths referenced by this one. */
+  foreach (PathSet::iterator, i, info.references)
+    if (*i != storePath) /* ignore self-references */
+      addWaitee (worker.makeSubstitutionGoal (*i));
 
-    if (waitees.empty()) /* to prevent hang (no wake-up event) */
-        referencesValid();
-    else
-        state = &SubstitutionGoal::referencesValid;
+  if (waitees.empty ()) /* to prevent hang (no wake-up event) */
+    referencesValid ();
+  else
+    state = &SubstitutionGoal::referencesValid;
 }
 
-
-void SubstitutionGoal::referencesValid()
+void
+SubstitutionGoal::referencesValid ()
 {
-    trace("all references realised");
+  trace ("all references realised");
 
-    if (nrFailed > 0) {
-        debug(format("some references of path `%1%' could not be realised") % storePath);
-        amDone(nrNoSubstituters > 0 || nrIncompleteClosure > 0 ? ecIncompleteClosure : ecFailed);
-        return;
+  if (nrFailed > 0)
+    {
+      debug (format ("some references of path `%1%' could not be realised")
+             % storePath);
+      amDone (nrNoSubstituters > 0 || nrIncompleteClosure > 0
+                  ? ecIncompleteClosure
+                  : ecFailed);
+      return;
     }
 
-    foreach (PathSet::iterator, i, info.references)
-        if (*i != storePath) /* ignore self-references */
-            assert(worker.store.isValidPath(*i));
+  foreach (PathSet::iterator, i, info.references)
+    if (*i != storePath) /* ignore self-references */
+      assert (worker.store.isValidPath (*i));
 
-    state = &SubstitutionGoal::tryToRun;
-    worker.wakeUp(shared_from_this());
+  state = &SubstitutionGoal::tryToRun;
+  worker.wakeUp (shared_from_this ());
 }
 
-
-void SubstitutionGoal::tryToRun()
+void
+SubstitutionGoal::tryToRun ()
 {
-    trace("trying to run");
-
-    /* Make sure that we are allowed to start a build.  Note that even
-       is maxBuildJobs == 0 (no local builds allowed), we still allow
-       a substituter to run.  This is because substitutions cannot be
-       distributed to another machine via the build hook. */
-    if (worker.getNrLocalBuilds() >= (settings.maxBuildJobs == 0 ? 1 : settings.maxBuildJobs)) {
-        worker.waitForBuildSlot(shared_from_this());
-        return;
+  trace ("trying to run");
+
+  /* Make sure that we are allowed to start a build.  Note that even
+     is maxBuildJobs == 0 (no local builds allowed), we still allow
+     a substituter to run.  This is because substitutions cannot be
+     distributed to another machine via the build hook. */
+  if (worker.getNrLocalBuilds ()
+      >= (settings.maxBuildJobs == 0 ? 1 : settings.maxBuildJobs))
+    {
+      worker.waitForBuildSlot (shared_from_this ());
+      return;
     }
 
-    /* Maybe a derivation goal has already locked this path
-       (exceedingly unlikely, since it should have used a substitute
-       first, but let's be defensive). */
-    outputLock.reset(); // make sure this goal's lock is gone
-    if (pathIsLockedByMe(storePath)) {
-        debug(format("restarting substitution of `%1%' because it's locked by another goal")
-            % storePath);
-        worker.waitForAnyGoal(shared_from_this());
-        return; /* restart in the tryToRun() state when another goal finishes */
+  /* Maybe a derivation goal has already locked this path
+     (exceedingly unlikely, since it should have used a substitute
+     first, but let's be defensive). */
+  outputLock.reset (); // make sure this goal's lock is gone
+  if (pathIsLockedByMe (storePath))
+    {
+      debug (format ("restarting substitution of `%1%' because it's locked by "
+                     "another goal")
+             % storePath);
+      worker.waitForAnyGoal (shared_from_this ());
+      return; /* restart in the tryToRun() state when another goal finishes */
     }
 
-    /* Acquire a lock on the output path. */
-    outputLock = std::shared_ptr<PathLocks>(new PathLocks);
-    if (!outputLock->lockPaths(singleton<PathSet>(storePath), "", false)) {
-        worker.waitForAWhile(shared_from_this());
-        return;
+  /* Acquire a lock on the output path. */
+  outputLock = std::shared_ptr<PathLocks> (new PathLocks);
+  if (!outputLock->lockPaths (singleton<PathSet> (storePath), "", false))
+    {
+      worker.waitForAWhile (shared_from_this ());
+      return;
     }
 
-    /* Check again whether the path is invalid. */
-    if (!repair && worker.store.isValidPath(storePath)) {
-        debug(format("store path `%1%' has become valid") % storePath);
-        outputLock->setDeletion(true);
-        outputLock.reset();
-        amDone(ecSuccess);
-        return;
+  /* Check again whether the path is invalid. */
+  if (!repair && worker.store.isValidPath (storePath))
+    {
+      debug (format ("store path `%1%' has become valid") % storePath);
+      outputLock->setDeletion (true);
+      outputLock.reset ();
+      amDone (ecSuccess);
+      return;
     }
 
-    printMsg(lvlInfo, format("fetching path `%1%'...") % storePath);
-
-    destPath = repair ? storePath + ".tmp" : storePath;
-
-    /* Remove the (stale) output path if it exists. */
-    if (pathExists(destPath))
-        deletePath(destPath);
-
-    if (!worker.substituter) {
-	const Strings args = { "substitute", "--substitute" };
-	const std::map<string, string> env = {
-	    { "_NIX_OPTIONS",
-	      settings.pack() + "deduplicate="
-	      + (settings.autoOptimiseStore ? "yes" : "no")
-	    }
-	};
-	worker.substituter = std::make_shared<Agent>(settings.guixProgram, args, env);
-    }
-
-    /* Borrow the worker's substituter.  */
-    if (!substituter) substituter.swap(worker.substituter);
-
-    /* Send the request to the substituter.  */
-    writeLine(substituter->toAgent.writeSide,
-	      (format("substitute %1% %2%") % storePath % destPath).str());
-
-    set<int> fds;
-    fds.insert(substituter->fromAgent.readSide);
-    fds.insert(substituter->builderOut.readSide);
-    worker.childStarted(shared_from_this(), substituter->pid, fds, true, true);
-
-    state = &SubstitutionGoal::finished;
-
-    if (settings.printBuildTrace)
-	/* The second element in the message used to be the name of the
-	   substituter but we're left with only one.  */
-        printMsg(lvlError, format("@ substituter-started %1% substitute") % storePath);
-}
-
-
-void SubstitutionGoal::finished()
-{
-    trace("substitute finished");
-
-    /* Remove the 'guix substitute' process from the list of children.  */
-    worker.childTerminated(substituter->pid);
-
-    /* If max-jobs > 1, the worker might have created a new 'substitute'
-       process in the meantime.  If that is the case, terminate ours;
-       otherwise, give it back to the worker.  */
-    if (worker.substituter) {
-	substituter.reset ();
-    } else {
-	worker.substituter.swap(substituter);
-    }
-
-    /* Check the exit status and the build result. */
-    HashResult hash;
-    try {
-	auto statusList = tokenizeString<vector<string> >(status);
-
-	if (statusList.empty()) {
-            throw SubstError(format("fetching path `%1%' (empty status)")
-			     % storePath);
-	} else if (statusList[0] == "hash-mismatch") {
-	    if (settings.printBuildTrace) {
-		auto hashType = statusList[1];
-		auto expectedHash = statusList[2];
-		auto actualHash = statusList[3];
-		printMsg(lvlError, format("@ hash-mismatch %1% %2% %3% %4%")
-			 % storePath
-			 % hashType % expectedHash % actualHash);
-	    }
-	    throw SubstError(format("hash mismatch for substituted item `%1%'") % storePath);
-	} else if (statusList[0] == "success") {
-	    if (!pathExists(destPath))
-		throw SubstError(format("substitute did not produce path `%1%'") % destPath);
-
-	    std::string hashStr = statusList[1];
-	    size_t n = hashStr.find(':');
-	    if (n == string::npos)
-		throw Error(format("bad hash from substituter: %1%") % hashStr);
-
-	    HashType hashType = parseHashType(string(hashStr, 0, n));
-	    switch (hashType) {
-	    case htUnknown:
-		throw Error(format("unknown hash algorithm in `%1%'") % hashStr);
-	    case htSHA256:
-		hash.first = parseHash16or32(hashType, string(hashStr, n + 1));
-		if (!string2Int(statusList[2], hash.second))
-		    throw Error(format("invalid nar size for '%1%' substitute") % storePath);
-		break;
-	    default:
-		/* The database only stores SHA256 hashes, so compute it.  */
-		hash = hashPath(htSHA256, destPath);
-		break;
-	    }
-	}
-	else
-            throw SubstError(format("fetching path `%1%' (status: '%2%')")
-                % storePath % status);
-
-    } catch (SubstError & e) {
-
-        printMsg(lvlInfo, e.msg());
-
-        if (settings.printBuildTrace) {
-            printMsg(lvlError, format("@ substituter-failed %1% %2% %3%")
-                % storePath % status % e.msg());
-        }
-
-	amDone(ecFailed);
-        return;
-    }
+  printMsg (lvlInfo, format ("fetching path `%1%'...") % storePath);
 
-    if (repair) replaceValidPath(storePath, destPath);
+  destPath = repair ? storePath + ".tmp" : storePath;
 
-    /* Note: 'guix substitute' takes care of resetting timestamps and of
-       deduplicating 'destPath', so no need to do it here.  */
+  /* Remove the (stale) output path if it exists. */
+  if (pathExists (destPath))
+    deletePath (destPath);
 
-    ValidPathInfo info2;
-    info2.path = storePath;
-    info2.hash = hash.first;
-    info2.narSize = hash.second;
-    info2.references = info.references;
-    info2.deriver = info.deriver;
-    worker.store.registerValidPath(info2);
+  if (!worker.substituter)
+    {
+      const Strings args = { "substitute", "--substitute" };
+      const std::map<string, string> env
+          = { { "_NIX_OPTIONS",
+                settings.pack () + "deduplicate="
+                    + (settings.autoOptimiseStore ? "yes" : "no") } };
+      worker.substituter
+          = std::make_shared<Agent> (settings.guixProgram, args, env);
+    }
 
-    outputLock->setDeletion(true);
-    outputLock.reset();
+  /* Borrow the worker's substituter.  */
+  if (!substituter)
+    substituter.swap (worker.substituter);
 
-    worker.store.markContentsGood(storePath);
+  /* Send the request to the substituter.  */
+  writeLine (substituter->toAgent.writeSide,
+             (format ("substitute %1% %2%") % storePath % destPath).str ());
 
-    printMsg(lvlChatty,
-        format("substitution of path `%1%' succeeded") % storePath);
+  set<int> fds;
+  fds.insert (substituter->fromAgent.readSide);
+  fds.insert (substituter->builderOut.readSide);
+  worker.childStarted (shared_from_this (), substituter->pid, fds, true, true);
 
-    if (settings.printBuildTrace)
-        printMsg(lvlError, format("@ substituter-succeeded %1%") % storePath);
+  state = &SubstitutionGoal::finished;
 
-    amDone(ecSuccess);
+  if (settings.printBuildTrace)
+    /* The second element in the message used to be the name of the
+       substituter but we're left with only one.  */
+    printMsg (lvlError,
+              format ("@ substituter-started %1% substitute") % storePath);
 }
 
-
-void SubstitutionGoal::handleChildOutput(int fd, const string & data)
+void
+SubstitutionGoal::finished ()
 {
-    if (verbosity >= settings.buildVerbosity
-	&& fd == substituter->fromAgent.readSide) {
-	writeToStderr(data);
-	/* Don't write substitution output to a log file for now.  We
-	   probably should, though. */
+  trace ("substitute finished");
+
+  /* Remove the 'guix substitute' process from the list of children.  */
+  worker.childTerminated (substituter->pid);
+
+  /* If max-jobs > 1, the worker might have created a new 'substitute'
+     process in the meantime.  If that is the case, terminate ours;
+     otherwise, give it back to the worker.  */
+  if (worker.substituter)
+    {
+      substituter.reset ();
+    }
+  else
+    {
+      worker.substituter.swap (substituter);
     }
 
-    if (fd == substituter->builderOut.readSide) {
-	/* DATA may consist of several lines.  Process them one by one.  */
-	string input = data;
-	while (!input.empty()) {
-	    /* Process up to the first newline.  */
-	    size_t end = input.find_first_of("\n");
-	    string trimmed = (end != string::npos) ? input.substr(0, end) : input;
+  /* Check the exit status and the build result. */
+  HashResult hash;
+  try
+    {
+      auto statusList = tokenizeString<vector<string> > (status);
 
-	    /* Update the goal's state accordingly.  */
-	    if (status == "") {
-		status = trimmed;
-		worker.wakeUp(shared_from_this());
-	    } else {
-		printMsg(lvlError, format("unexpected substituter message '%1%'") % input);
-	    }
+      if (statusList.empty ())
+        {
+          throw SubstError (format ("fetching path `%1%' (empty status)")
+                            % storePath);
+        }
+      else if (statusList[0] == "hash-mismatch")
+        {
+          if (settings.printBuildTrace)
+            {
+              auto hashType = statusList[1];
+              auto expectedHash = statusList[2];
+              auto actualHash = statusList[3];
+              printMsg (lvlError, format ("@ hash-mismatch %1% %2% %3% %4%")
+                                      % storePath % hashType % expectedHash
+                                      % actualHash);
+            }
+          throw SubstError (format ("hash mismatch for substituted item `%1%'")
+                            % storePath);
+        }
+      else if (statusList[0] == "success")
+        {
+          if (!pathExists (destPath))
+            throw SubstError (format ("substitute did not produce path `%1%'")
+                              % destPath);
 
-	    input = (end != string::npos) ? input.substr(end + 1) : "";
-	}
+          std::string hashStr = statusList[1];
+          size_t n = hashStr.find (':');
+          if (n == string::npos)
+            throw Error (format ("bad hash from substituter: %1%") % hashStr);
+
+          HashType hashType = parseHashType (string (hashStr, 0, n));
+          switch (hashType)
+            {
+            case htUnknown:
+              throw Error (format ("unknown hash algorithm in `%1%'")
+                           % hashStr);
+            case htSHA256:
+              hash.first = parseHash16or32 (hashType, string (hashStr, n + 1));
+              if (!string2Int (statusList[2], hash.second))
+                throw Error (format ("invalid nar size for '%1%' substitute")
+                             % storePath);
+              break;
+            default:
+              /* The database only stores SHA256 hashes, so compute it.  */
+              hash = hashPath (htSHA256, destPath);
+              break;
+            }
+        }
+      else
+        throw SubstError (format ("fetching path `%1%' (status: '%2%')")
+                          % storePath % status);
     }
-}
+  catch (SubstError &e)
+    {
 
+      printMsg (lvlInfo, e.msg ());
 
-void SubstitutionGoal::handleEOF(int fd)
-{
-    worker.wakeUp(shared_from_this());
-}
+      if (settings.printBuildTrace)
+        {
+          printMsg (lvlError, format ("@ substituter-failed %1% %2% %3%")
+                                  % storePath % status % e.msg ());
+        }
+
+      amDone (ecFailed);
+      return;
+    }
 
+  if (repair)
+    replaceValidPath (storePath, destPath);
 
+  /* Note: 'guix substitute' takes care of resetting timestamps and of
+     deduplicating 'destPath', so no need to do it here.  */
 
-//////////////////////////////////////////////////////////////////////
+  ValidPathInfo info2;
+  info2.path = storePath;
+  info2.hash = hash.first;
+  info2.narSize = hash.second;
+  info2.references = info.references;
+  info2.deriver = info.deriver;
+  worker.store.registerValidPath (info2);
 
+  outputLock->setDeletion (true);
+  outputLock.reset ();
 
-static bool working = false;
+  worker.store.markContentsGood (storePath);
 
+  printMsg (lvlChatty,
+            format ("substitution of path `%1%' succeeded") % storePath);
 
-Worker::Worker(LocalStore & store)
-    : store(store)
-{
-    /* Debugging: prevent recursive workers. */
-    if (working) abort();
-    working = true;
-    nrLocalBuilds = 0;
-    lastWokenUp = 0;
-    permanentFailure = false;
-    timedOut = false;
-}
+  if (settings.printBuildTrace)
+    printMsg (lvlError, format ("@ substituter-succeeded %1%") % storePath);
 
+  amDone (ecSuccess);
+}
 
-Worker::~Worker()
+void
+SubstitutionGoal::handleChildOutput (int fd, const string &data)
 {
-    working = false;
+  if (verbosity >= settings.buildVerbosity
+      && fd == substituter->fromAgent.readSide)
+    {
+      writeToStderr (data);
+      /* Don't write substitution output to a log file for now.  We
+         probably should, though. */
+    }
 
-    /* Explicitly get rid of all strong pointers now.  After this all
-       goals that refer to this worker should be gone.  (Otherwise we
-       are in trouble, since goals may call childTerminated() etc. in
-       their destructors). */
-    topGoals.clear();
+  if (fd == substituter->builderOut.readSide)
+    {
+      /* DATA may consist of several lines.  Process them one by one.  */
+      string input = data;
+      while (!input.empty ())
+        {
+          /* Process up to the first newline.  */
+          size_t end = input.find_first_of ("\n");
+          string trimmed
+              = (end != string::npos) ? input.substr (0, end) : input;
+
+          /* Update the goal's state accordingly.  */
+          if (status == "")
+            {
+              status = trimmed;
+              worker.wakeUp (shared_from_this ());
+            }
+          else
+            {
+              printMsg (lvlError,
+                        format ("unexpected substituter message '%1%'")
+                            % input);
+            }
+
+          input = (end != string::npos) ? input.substr (end + 1) : "";
+        }
+    }
 }
 
+void
+SubstitutionGoal::handleEOF (int fd)
+{
+  worker.wakeUp (shared_from_this ());
+}
 
-GoalPtr Worker::makeDerivationGoal(const Path & path,
-    const StringSet & wantedOutputs, BuildMode buildMode)
+//////////////////////////////////////////////////////////////////////
+
+static bool working = false;
+
+Worker::Worker (LocalStore &store) : store (store)
 {
-    GoalPtr goal = derivationGoals[path].lock();
-    if (!goal) {
-        goal = GoalPtr(new DerivationGoal(path, wantedOutputs, *this, buildMode));
-        derivationGoals[path] = goal;
-        wakeUp(goal);
-    } else
-        (dynamic_cast<DerivationGoal *>(goal.get()))->addWantedOutputs(wantedOutputs);
-    return goal;
+  /* Debugging: prevent recursive workers. */
+  if (working)
+    abort ();
+  working = true;
+  nrLocalBuilds = 0;
+  lastWokenUp = 0;
+  permanentFailure = false;
+  timedOut = false;
 }
 
+Worker::~Worker ()
+{
+  working = false;
+
+  /* Explicitly get rid of all strong pointers now.  After this all
+     goals that refer to this worker should be gone.  (Otherwise we
+     are in trouble, since goals may call childTerminated() etc. in
+     their destructors). */
+  topGoals.clear ();
+}
 
-GoalPtr Worker::makeSubstitutionGoal(const Path & path, bool repair)
+GoalPtr
+Worker::makeDerivationGoal (const Path &path, const StringSet &wantedOutputs,
+                            BuildMode buildMode)
 {
-    GoalPtr goal = substitutionGoals[path].lock();
-    if (!goal) {
-        goal = GoalPtr(new SubstitutionGoal(path, *this, repair));
-        substitutionGoals[path] = goal;
-        wakeUp(goal);
+  GoalPtr goal = derivationGoals[path].lock ();
+  if (!goal)
+    {
+      goal = GoalPtr (
+          new DerivationGoal (path, wantedOutputs, *this, buildMode));
+      derivationGoals[path] = goal;
+      wakeUp (goal);
     }
-    return goal;
+  else
+    (dynamic_cast<DerivationGoal *> (goal.get ()))
+        ->addWantedOutputs (wantedOutputs);
+  return goal;
 }
 
-
-static void removeGoal(GoalPtr goal, WeakGoalMap & goalMap)
+GoalPtr
+Worker::makeSubstitutionGoal (const Path &path, bool repair)
 {
-    /* !!! inefficient */
-    for (WeakGoalMap::iterator i = goalMap.begin();
-         i != goalMap.end(); )
-        if (i->second.lock() == goal) {
-            WeakGoalMap::iterator j = i; ++j;
-            goalMap.erase(i);
-            i = j;
-        }
-        else ++i;
+  GoalPtr goal = substitutionGoals[path].lock ();
+  if (!goal)
+    {
+      goal = GoalPtr (new SubstitutionGoal (path, *this, repair));
+      substitutionGoals[path] = goal;
+      wakeUp (goal);
+    }
+  return goal;
 }
 
+static void
+removeGoal (GoalPtr goal, WeakGoalMap &goalMap)
+{
+  /* !!! inefficient */
+  for (WeakGoalMap::iterator i = goalMap.begin (); i != goalMap.end ();)
+    if (i->second.lock () == goal)
+      {
+        WeakGoalMap::iterator j = i;
+        ++j;
+        goalMap.erase (i);
+        i = j;
+      }
+    else
+      ++i;
+}
 
-void Worker::removeGoal(GoalPtr goal)
+void
+Worker::removeGoal (GoalPtr goal)
 {
-    nix::removeGoal(goal, derivationGoals);
-    nix::removeGoal(goal, substitutionGoals);
-    if (topGoals.find(goal) != topGoals.end()) {
-        topGoals.erase(goal);
-        /* If a top-level goal failed, then kill all other goals
-           (unless keepGoing was set). */
-        if (goal->getExitCode() == Goal::ecFailed && !settings.keepGoing)
-            topGoals.clear();
+  nix::removeGoal (goal, derivationGoals);
+  nix::removeGoal (goal, substitutionGoals);
+  if (topGoals.find (goal) != topGoals.end ())
+    {
+      topGoals.erase (goal);
+      /* If a top-level goal failed, then kill all other goals
+         (unless keepGoing was set). */
+      if (goal->getExitCode () == Goal::ecFailed && !settings.keepGoing)
+        topGoals.clear ();
     }
 
-    /* Wake up goals waiting for any goal to finish. */
-    foreach (WeakGoals::iterator, i, waitingForAnyGoal) {
-        GoalPtr goal = i->lock();
-        if (goal) wakeUp(goal);
+  /* Wake up goals waiting for any goal to finish. */
+  foreach (WeakGoals::iterator, i, waitingForAnyGoal)
+    {
+      GoalPtr goal = i->lock ();
+      if (goal)
+        wakeUp (goal);
     }
 
-    waitingForAnyGoal.clear();
+  waitingForAnyGoal.clear ();
 }
 
-
-void Worker::wakeUp(GoalPtr goal)
+void
+Worker::wakeUp (GoalPtr goal)
 {
-    goal->trace("woken up");
-    addToWeakGoals(awake, goal);
+  goal->trace ("woken up");
+  addToWeakGoals (awake, goal);
 }
 
-
-unsigned Worker::getNrLocalBuilds()
+unsigned
+Worker::getNrLocalBuilds ()
 {
-    return nrLocalBuilds;
+  return nrLocalBuilds;
 }
 
-
-void Worker::childStarted(GoalPtr goal,
-    pid_t pid, const set<int> & fds, bool inBuildSlot,
-    bool respectTimeouts)
+void
+Worker::childStarted (GoalPtr goal, pid_t pid, const set<int> &fds,
+                      bool inBuildSlot, bool respectTimeouts)
 {
-    Child child;
-    child.goal = goal;
-    child.fds = fds;
-    child.timeStarted = child.lastOutput = time(0);
-    child.inBuildSlot = inBuildSlot;
-    child.respectTimeouts = respectTimeouts;
-    children[pid] = child;
-    if (inBuildSlot) nrLocalBuilds++;
+  Child child;
+  child.goal = goal;
+  child.fds = fds;
+  child.timeStarted = child.lastOutput = time (0);
+  child.inBuildSlot = inBuildSlot;
+  child.respectTimeouts = respectTimeouts;
+  children[pid] = child;
+  if (inBuildSlot)
+    nrLocalBuilds++;
 }
 
-
-void Worker::childTerminated(pid_t pid, bool wakeSleepers)
+void
+Worker::childTerminated (pid_t pid, bool wakeSleepers)
 {
-    assert(pid != -1); /* common mistake */
+  assert (pid != -1); /* common mistake */
 
-    Children::iterator i = children.find(pid);
-    assert(i != children.end());
+  Children::iterator i = children.find (pid);
+  assert (i != children.end ());
 
-    if (i->second.inBuildSlot) {
-        assert(nrLocalBuilds > 0);
-        nrLocalBuilds--;
+  if (i->second.inBuildSlot)
+    {
+      assert (nrLocalBuilds > 0);
+      nrLocalBuilds--;
     }
 
-    children.erase(pid);
+  children.erase (pid);
 
-    if (wakeSleepers) {
+  if (wakeSleepers)
+    {
 
-        /* Wake up goals waiting for a build slot. */
-        foreach (WeakGoals::iterator, i, wantingToBuild) {
-            GoalPtr goal = i->lock();
-            if (goal) wakeUp(goal);
+      /* Wake up goals waiting for a build slot. */
+      foreach (WeakGoals::iterator, i, wantingToBuild)
+        {
+          GoalPtr goal = i->lock ();
+          if (goal)
+            wakeUp (goal);
         }
 
-        wantingToBuild.clear();
+      wantingToBuild.clear ();
     }
 }
 
-
-void Worker::waitForBuildSlot(GoalPtr goal)
+void
+Worker::waitForBuildSlot (GoalPtr goal)
 {
-    debug("wait for build slot");
-    if (getNrLocalBuilds() < settings.maxBuildJobs)
-        wakeUp(goal); /* we can do it right away */
-    else
-        addToWeakGoals(wantingToBuild, goal);
+  debug ("wait for build slot");
+  if (getNrLocalBuilds () < settings.maxBuildJobs)
+    wakeUp (goal); /* we can do it right away */
+  else
+    addToWeakGoals (wantingToBuild, goal);
 }
 
-
-void Worker::waitForAnyGoal(GoalPtr goal)
+void
+Worker::waitForAnyGoal (GoalPtr goal)
 {
-    debug("wait for any goal");
-    addToWeakGoals(waitingForAnyGoal, goal);
+  debug ("wait for any goal");
+  addToWeakGoals (waitingForAnyGoal, goal);
 }
 
-
-void Worker::waitForAWhile(GoalPtr goal)
+void
+Worker::waitForAWhile (GoalPtr goal)
 {
-    debug("wait for a while");
-    addToWeakGoals(waitingForAWhile, goal);
+  debug ("wait for a while");
+  addToWeakGoals (waitingForAWhile, goal);
 }
 
-
-void Worker::run(const Goals & _topGoals)
+void
+Worker::run (const Goals &_topGoals)
 {
-    foreach (Goals::iterator, i,  _topGoals) topGoals.insert(*i);
+  foreach (Goals::iterator, i, _topGoals)
+    topGoals.insert (*i);
 
-    startNest(nest, lvlDebug, format("entered goal loop"));
+  startNest (nest, lvlDebug, format ("entered goal loop"));
 
-    while (1) {
+  while (1)
+    {
 
-        checkInterrupt();
+      checkInterrupt ();
 
-        /* Call every wake goal (in the ordering established by
-           CompareGoalPtrs). */
-        while (!awake.empty() && !topGoals.empty()) {
-            Goals awake2;
-            for (auto & i : awake) {
-                GoalPtr goal = i.lock();
-                if (goal) awake2.insert(goal);
+      /* Call every wake goal (in the ordering established by
+         CompareGoalPtrs). */
+      while (!awake.empty () && !topGoals.empty ())
+        {
+          Goals awake2;
+          for (auto &i : awake)
+            {
+              GoalPtr goal = i.lock ();
+              if (goal)
+                awake2.insert (goal);
             }
-            awake.clear();
-            for (auto & goal : awake2) {
-                checkInterrupt();
-                goal->work();
-                if (topGoals.empty()) break; // stuff may have been cancelled
+          awake.clear ();
+          for (auto &goal : awake2)
+            {
+              checkInterrupt ();
+              goal->work ();
+              if (topGoals.empty ())
+                break; // stuff may have been cancelled
             }
         }
 
-        if (topGoals.empty()) break;
+      if (topGoals.empty ())
+        break;
 
-        /* Wait for input. */
-        if (!children.empty() || !waitingForAWhile.empty())
-            waitForInput();
-        else {
-            if (awake.empty() && settings.maxBuildJobs == 0) throw Error(
+      /* Wait for input. */
+      if (!children.empty () || !waitingForAWhile.empty ())
+        waitForInput ();
+      else
+        {
+          if (awake.empty () && settings.maxBuildJobs == 0)
+            throw Error (
                 "unable to start any build; either increase `--max-jobs' "
                 "or enable distributed builds");
-            assert(!awake.empty());
-        }
-    }
-
-    /* If --keep-going is not set, it's possible that the main goal
-       exited while some of its subgoals were still active.  But if
-       --keep-going *is* set, then they must all be finished now. */
-    assert(!settings.keepGoing || awake.empty());
-    assert(!settings.keepGoing || wantingToBuild.empty());
-    assert(!settings.keepGoing || children.empty());
-}
-
-
-void Worker::waitForInput()
-{
-    printMsg(lvlVomit, "waiting for children");
-
-    /* Process output from the file descriptors attached to the
-       children, namely log output and output path creation commands.
-       We also use this to detect child termination: if we get EOF on
-       the logger pipe of a build, we assume that the builder has
-       terminated. */
-
-    bool useTimeout = false;
-    struct timeval timeout;
-    timeout.tv_usec = 0;
-    time_t before = time(0);
-
-    /* If we're monitoring for silence on stdout/stderr, or if there
-       is a build timeout, then wait for input until the first
-       deadline for any child. */
-    assert(sizeof(time_t) >= sizeof(long));
-    time_t nearest = LONG_MAX; // nearest deadline
-    foreach (Children::iterator, i, children) {
-        if (!i->second.respectTimeouts) continue;
-        if (settings.maxSilentTime != 0)
-            nearest = std::min(nearest, i->second.lastOutput + settings.maxSilentTime);
-        if (settings.buildTimeout != 0)
-            nearest = std::min(nearest, i->second.timeStarted + settings.buildTimeout);
-    }
-    if (nearest != LONG_MAX) {
-        timeout.tv_sec = std::max((time_t) 1, nearest - before);
-        useTimeout = true;
-        printMsg(lvlVomit, format("sleeping %1% seconds") % timeout.tv_sec);
-    }
-
-    /* If we are polling goals that are waiting for a lock, then wake
-       up after a few seconds at most. */
-    if (!waitingForAWhile.empty()) {
-        useTimeout = true;
-        if (lastWokenUp == 0)
-            printMsg(lvlError, "waiting for locks or build slots...");
-        if (lastWokenUp == 0 || lastWokenUp > before) lastWokenUp = before;
-        timeout.tv_sec = std::max((time_t) 1, (time_t) (lastWokenUp + settings.pollInterval - before));
-    } else lastWokenUp = 0;
-
-    using namespace std;
-    /* Use select() to wait for the input side of any logger pipe to
-       become `available'.  Note that `available' (i.e., non-blocking)
-       includes EOF. */
-    fd_set fds;
-    FD_ZERO(&fds);
-    int fdMax = 0;
-    foreach (Children::iterator, i, children) {
-        foreach (set<int>::iterator, j, i->second.fds) {
-            FD_SET(*j, &fds);
-            if (*j >= fdMax) fdMax = *j + 1;
-        }
-    }
-
-    if (select(fdMax, &fds, 0, 0, useTimeout ? &timeout : 0) == -1) {
-        if (errno == EINTR) return;
-        throw SysError("waiting for input");
-    }
-
-    time_t after = time(0);
-
-    /* Process all available file descriptors. */
-
-    /* Since goals may be canceled from inside the loop below (causing
-       them go be erased from the `children' map), we have to be
-       careful that we don't keep iterators alive across calls to
-       timedOut(). */
-    set<pid_t> pids;
-    foreach (Children::iterator, i, children) pids.insert(i->first);
-
-    foreach (set<pid_t>::iterator, i, pids) {
-        checkInterrupt();
-        Children::iterator j = children.find(*i);
-        if (j == children.end()) continue; // child destroyed
-        GoalPtr goal = j->second.goal.lock();
-        assert(goal);
-
-        set<int> fds2(j->second.fds);
-        foreach (set<int>::iterator, k, fds2) {
-            if (FD_ISSET(*k, &fds)) {
-                unsigned char buffer[4096];
-                ssize_t rd = read(*k, buffer, sizeof(buffer));
-                if (rd == -1) {
-                    if (errno != EINTR)
-                        throw SysError(format("reading from %1%")
-                            % goal->getName());
-                } else if (rd == 0) {
-                    debug(format("%1%: got EOF") % goal->getName());
-                    goal->handleEOF(*k);
-                    j->second.fds.erase(*k);
-                } else {
-                    printMsg(lvlVomit, format("%1%: read %2% bytes")
-                        % goal->getName() % rd);
-                    string data((char *) buffer, rd);
-                    j->second.lastOutput = after;
-                    goal->handleChildOutput(*k, data);
+          assert (!awake.empty ());
+        }
+    }
+
+  /* If --keep-going is not set, it's possible that the main goal
+     exited while some of its subgoals were still active.  But if
+     --keep-going *is* set, then they must all be finished now. */
+  assert (!settings.keepGoing || awake.empty ());
+  assert (!settings.keepGoing || wantingToBuild.empty ());
+  assert (!settings.keepGoing || children.empty ());
+}
+
+void
+Worker::waitForInput ()
+{
+  printMsg (lvlVomit, "waiting for children");
+
+  /* Process output from the file descriptors attached to the
+     children, namely log output and output path creation commands.
+     We also use this to detect child termination: if we get EOF on
+     the logger pipe of a build, we assume that the builder has
+     terminated. */
+
+  bool useTimeout = false;
+  struct timeval timeout;
+  timeout.tv_usec = 0;
+  time_t before = time (0);
+
+  /* If we're monitoring for silence on stdout/stderr, or if there
+     is a build timeout, then wait for input until the first
+     deadline for any child. */
+  assert (sizeof (time_t) >= sizeof (long));
+  time_t nearest = LONG_MAX; // nearest deadline
+  foreach (Children::iterator, i, children)
+    {
+      if (!i->second.respectTimeouts)
+        continue;
+      if (settings.maxSilentTime != 0)
+        nearest = std::min (nearest,
+                            i->second.lastOutput + settings.maxSilentTime);
+      if (settings.buildTimeout != 0)
+        nearest = std::min (nearest,
+                            i->second.timeStarted + settings.buildTimeout);
+    }
+  if (nearest != LONG_MAX)
+    {
+      timeout.tv_sec = std::max ((time_t)1, nearest - before);
+      useTimeout = true;
+      printMsg (lvlVomit, format ("sleeping %1% seconds") % timeout.tv_sec);
+    }
+
+  /* If we are polling goals that are waiting for a lock, then wake
+     up after a few seconds at most. */
+  if (!waitingForAWhile.empty ())
+    {
+      useTimeout = true;
+      if (lastWokenUp == 0)
+        printMsg (lvlError, "waiting for locks or build slots...");
+      if (lastWokenUp == 0 || lastWokenUp > before)
+        lastWokenUp = before;
+      timeout.tv_sec = std::max (
+          (time_t)1, (time_t)(lastWokenUp + settings.pollInterval - before));
+    }
+  else
+    lastWokenUp = 0;
+
+  using namespace std;
+  /* Use select() to wait for the input side of any logger pipe to
+     become `available'.  Note that `available' (i.e., non-blocking)
+     includes EOF. */
+  fd_set fds;
+  FD_ZERO (&fds);
+  int fdMax = 0;
+  foreach (Children::iterator, i, children)
+    {
+      foreach (set<int>::iterator, j, i->second.fds)
+        {
+          FD_SET (*j, &fds);
+          if (*j >= fdMax)
+            fdMax = *j + 1;
+        }
+    }
+
+  if (select (fdMax, &fds, 0, 0, useTimeout ? &timeout : 0) == -1)
+    {
+      if (errno == EINTR)
+        return;
+      throw SysError ("waiting for input");
+    }
+
+  time_t after = time (0);
+
+  /* Process all available file descriptors. */
+
+  /* Since goals may be canceled from inside the loop below (causing
+     them go be erased from the `children' map), we have to be
+     careful that we don't keep iterators alive across calls to
+     timedOut(). */
+  set<pid_t> pids;
+  foreach (Children::iterator, i, children)
+    pids.insert (i->first);
+
+  foreach (set<pid_t>::iterator, i, pids)
+    {
+      checkInterrupt ();
+      Children::iterator j = children.find (*i);
+      if (j == children.end ())
+        continue; // child destroyed
+      GoalPtr goal = j->second.goal.lock ();
+      assert (goal);
+
+      set<int> fds2 (j->second.fds);
+      foreach (set<int>::iterator, k, fds2)
+        {
+          if (FD_ISSET (*k, &fds))
+            {
+              unsigned char buffer[4096];
+              ssize_t rd = read (*k, buffer, sizeof (buffer));
+              if (rd == -1)
+                {
+                  if (errno != EINTR)
+                    throw SysError (format ("reading from %1%")
+                                    % goal->getName ());
+                }
+              else if (rd == 0)
+                {
+                  debug (format ("%1%: got EOF") % goal->getName ());
+                  goal->handleEOF (*k);
+                  j->second.fds.erase (*k);
+                }
+              else
+                {
+                  printMsg (lvlVomit, format ("%1%: read %2% bytes")
+                                          % goal->getName () % rd);
+                  string data ((char *)buffer, rd);
+                  j->second.lastOutput = after;
+                  goal->handleChildOutput (*k, data);
                 }
             }
         }
 
-        if (goal->getExitCode() == Goal::ecBusy &&
-            settings.maxSilentTime != 0 &&
-            j->second.respectTimeouts &&
-            after - j->second.lastOutput >= (time_t) settings.maxSilentTime)
+      if (goal->getExitCode () == Goal::ecBusy && settings.maxSilentTime != 0
+          && j->second.respectTimeouts
+          && after - j->second.lastOutput >= (time_t)settings.maxSilentTime)
         {
-            printMsg(lvlError,
-                format("%1% timed out after %2% seconds of silence")
-                % goal->getName() % settings.maxSilentTime);
-            goal->timedOut();
+          printMsg (lvlError,
+                    format ("%1% timed out after %2% seconds of silence")
+                        % goal->getName () % settings.maxSilentTime);
+          goal->timedOut ();
         }
 
-        else if (goal->getExitCode() == Goal::ecBusy &&
-            settings.buildTimeout != 0 &&
-            j->second.respectTimeouts &&
-            after - j->second.timeStarted >= (time_t) settings.buildTimeout)
+      else if (goal->getExitCode () == Goal::ecBusy
+               && settings.buildTimeout != 0 && j->second.respectTimeouts
+               && after - j->second.timeStarted
+                      >= (time_t)settings.buildTimeout)
         {
-            printMsg(lvlError,
-                format("%1% timed out after %2% seconds")
-                % goal->getName() % settings.buildTimeout);
-            goal->timedOut();
+          printMsg (lvlError, format ("%1% timed out after %2% seconds")
+                                  % goal->getName () % settings.buildTimeout);
+          goal->timedOut ();
         }
     }
 
-    if (!waitingForAWhile.empty() && lastWokenUp + settings.pollInterval <= after) {
-        lastWokenUp = after;
-        foreach (WeakGoals::iterator, i, waitingForAWhile) {
-            GoalPtr goal = i->lock();
-            if (goal) wakeUp(goal);
+  if (!waitingForAWhile.empty ()
+      && lastWokenUp + settings.pollInterval <= after)
+    {
+      lastWokenUp = after;
+      foreach (WeakGoals::iterator, i, waitingForAWhile)
+        {
+          GoalPtr goal = i->lock ();
+          if (goal)
+            wakeUp (goal);
         }
-        waitingForAWhile.clear();
+      waitingForAWhile.clear ();
     }
 }
 
-
-unsigned int Worker::exitStatus()
+unsigned int
+Worker::exitStatus ()
 {
-    return timedOut ? 101 : (permanentFailure ? 100 : 1);
+  return timedOut ? 101 : (permanentFailure ? 100 : 1);
 }
 
-
 //////////////////////////////////////////////////////////////////////
 
-
-void LocalStore::buildPaths(const PathSet & drvPaths, BuildMode buildMode)
+void
+LocalStore::buildPaths (const PathSet &drvPaths, BuildMode buildMode)
 {
-    startNest(nest, lvlDebug,
-        format("building %1%") % showPaths(drvPaths));
+  startNest (nest, lvlDebug, format ("building %1%") % showPaths (drvPaths));
 
-    Worker worker(*this);
+  Worker worker (*this);
 
-    Goals goals;
-    foreach (PathSet::const_iterator, i, drvPaths) {
-        DrvPathWithOutputs i2 = parseDrvPathWithOutputs(*i);
-        if (isDerivation(i2.first))
-            goals.insert(worker.makeDerivationGoal(i2.first, i2.second, buildMode));
-        else
-            goals.insert(worker.makeSubstitutionGoal(*i, buildMode));
+  Goals goals;
+  foreach (PathSet::const_iterator, i, drvPaths)
+    {
+      DrvPathWithOutputs i2 = parseDrvPathWithOutputs (*i);
+      if (isDerivation (i2.first))
+        goals.insert (
+            worker.makeDerivationGoal (i2.first, i2.second, buildMode));
+      else
+        goals.insert (worker.makeSubstitutionGoal (*i, buildMode));
     }
 
-    worker.run(goals);
+  worker.run (goals);
 
-    PathSet failed;
-    foreach (Goals::iterator, i, goals)
-        if ((*i)->getExitCode() == Goal::ecFailed) {
-            DerivationGoal * i2 = dynamic_cast<DerivationGoal *>(i->get());
-            if (i2) failed.insert(i2->getDrvPath());
-            else failed.insert(dynamic_cast<SubstitutionGoal *>(i->get())->getStorePath());
-        }
+  PathSet failed;
+  foreach (Goals::iterator, i, goals)
+    if ((*i)->getExitCode () == Goal::ecFailed)
+      {
+        DerivationGoal *i2 = dynamic_cast<DerivationGoal *> (i->get ());
+        if (i2)
+          failed.insert (i2->getDrvPath ());
+        else
+          failed.insert (
+              dynamic_cast<SubstitutionGoal *> (i->get ())->getStorePath ());
+      }
 
-    if (!failed.empty())
-        throw Error(format("build of %1% failed") % showPaths(failed), worker.exitStatus());
+  if (!failed.empty ())
+    throw Error (format ("build of %1% failed") % showPaths (failed),
+                 worker.exitStatus ());
 }
 
-
-void LocalStore::ensurePath(const Path & path)
+void
+LocalStore::ensurePath (const Path &path)
 {
-    /* If the path is already valid, we're done. */
-    if (isValidPath(path)) return;
+  /* If the path is already valid, we're done. */
+  if (isValidPath (path))
+    return;
 
-    Worker worker(*this);
-    GoalPtr goal = worker.makeSubstitutionGoal(path);
-    Goals goals = singleton<Goals>(goal);
+  Worker worker (*this);
+  GoalPtr goal = worker.makeSubstitutionGoal (path);
+  Goals goals = singleton<Goals> (goal);
 
-    worker.run(goals);
+  worker.run (goals);
 
-    if (goal->getExitCode() != Goal::ecSuccess)
-        throw Error(format("path `%1%' does not exist and cannot be created") % path, worker.exitStatus());
+  if (goal->getExitCode () != Goal::ecSuccess)
+    throw Error (format ("path `%1%' does not exist and cannot be created")
+                     % path,
+                 worker.exitStatus ());
 }
 
-
-void LocalStore::repairPath(const Path & path)
+void
+LocalStore::repairPath (const Path &path)
 {
-    Worker worker(*this);
-    GoalPtr goal = worker.makeSubstitutionGoal(path, true);
-    Goals goals = singleton<Goals>(goal);
+  Worker worker (*this);
+  GoalPtr goal = worker.makeSubstitutionGoal (path, true);
+  Goals goals = singleton<Goals> (goal);
 
-    worker.run(goals);
+  worker.run (goals);
 
-    if (goal->getExitCode() != Goal::ecSuccess) {
-        /* Since substituting the path didn't work, if we have a valid
-           deriver, then rebuild the deriver. */
-        Path deriver = queryDeriver(path);
-        if (deriver != "" && isValidPath(deriver)) {
-            goals.clear();
-            goals.insert(worker.makeDerivationGoal(deriver, StringSet(), bmRepair));
-            worker.run(goals);
-        } else
-            throw Error(format("cannot repair path `%1%'") % path, worker.exitStatus());
+  if (goal->getExitCode () != Goal::ecSuccess)
+    {
+      /* Since substituting the path didn't work, if we have a valid
+         deriver, then rebuild the deriver. */
+      Path deriver = queryDeriver (path);
+      if (deriver != "" && isValidPath (deriver))
+        {
+          goals.clear ();
+          goals.insert (
+              worker.makeDerivationGoal (deriver, StringSet (), bmRepair));
+          worker.run (goals);
+        }
+      else
+        throw Error (format ("cannot repair path `%1%'") % path,
+                     worker.exitStatus ());
     }
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libstore/builtins.cc b/nix/libstore/builtins.cc
index 6bf467354a..b07b8a8f78 100644
--- a/nix/libstore/builtins.cc
+++ b/nix/libstore/builtins.cc
@@ -17,71 +17,69 @@
    along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <builtins.hh>
-#include <util.hh>
 #include <globals.hh>
+#include <util.hh>
 
-#include <unistd.h>
 #include <cstdlib>
+#include <unistd.h>
 
-namespace nix {
+namespace nix
+{
 
-static void builtinDownload(const Derivation &drv,
-			    const std::string &drvPath,
-			    const std::string &output)
+static void
+builtinDownload (const Derivation &drv, const std::string &drvPath,
+                 const std::string &output)
 {
-    /* Invoke 'guix perform-download'.  */
-    Strings args;
-    args.push_back("perform-download");
-    args.push_back(drvPath);
+  /* Invoke 'guix perform-download'.  */
+  Strings args;
+  args.push_back ("perform-download");
+  args.push_back (drvPath);
 
-    /* Close all other file descriptors. */
-    closeMostFDs(set<int>());
+  /* Close all other file descriptors. */
+  closeMostFDs (set<int> ());
 
-    const char *const argv[] =
-      {
-	  "guix", "perform-download", drvPath.c_str(), output.c_str(), NULL
-      };
+  const char *const argv[] = { "guix", "perform-download", drvPath.c_str (),
+                               output.c_str (), NULL };
 
-    /* Tell the script what the store file name is, so that
-       'strip-store-file-name' (used for instance to determine the URL of
-       content-addressed mirrors) works correctly.  */
-    setenv("NIX_STORE", settings.nixStore.c_str(), 1);
+  /* Tell the script what the store file name is, so that
+     'strip-store-file-name' (used for instance to determine the URL of
+     content-addressed mirrors) works correctly.  */
+  setenv ("NIX_STORE", settings.nixStore.c_str (), 1);
 
-    /* Tell it about options such as "print-extended-build-trace".  */
-    setenv("_NIX_OPTIONS", settings.pack().c_str(), 1);
+  /* Tell it about options such as "print-extended-build-trace".  */
+  setenv ("_NIX_OPTIONS", settings.pack ().c_str (), 1);
 
-    const string program = settings.guixProgram;
-    execv(program.c_str(), (char *const *) argv);
+  const string program = settings.guixProgram;
+  execv (program.c_str (), (char *const *)argv);
 
-    throw SysError(format("failed to run download program '%1%'") % program);
+  throw SysError (format ("failed to run download program '%1%'") % program);
 }
 
-static const std::map<std::string, derivationBuilder> builtins =
-{
-    { "download", builtinDownload },
-    { "git-download", builtinDownload }
-};
+static const std::map<std::string, derivationBuilder> builtins
+    = { { "download", builtinDownload }, { "git-download", builtinDownload } };
 
-derivationBuilder lookupBuiltinBuilder(const std::string & name)
+derivationBuilder
+lookupBuiltinBuilder (const std::string &name)
 {
-    if (name.substr(0, 8) == "builtin:")
+  if (name.substr (0, 8) == "builtin:")
     {
-	auto realName = name.substr(8);
-	auto builder = builtins.find(realName);
-	return builder == builtins.end() ? NULL : builder->second;
+      auto realName = name.substr (8);
+      auto builder = builtins.find (realName);
+      return builder == builtins.end () ? NULL : builder->second;
     }
-    else
-	return NULL;
+  else
+    return NULL;
 }
 
-std::list<std::string> builtinBuilderNames()
+std::list<std::string>
+builtinBuilderNames ()
 {
-    std::list<std::string> result;
-    for(auto&& iter: builtins)
+  std::list<std::string> result;
+  for (auto &&iter : builtins)
     {
-	result.push_back(iter.first);
+      result.push_back (iter.first);
     }
-    return result;
+  return result;
 }
 
-}
+} // namespace nix
diff --git a/nix/libstore/builtins.hh b/nix/libstore/builtins.hh
index 396ea14ebc..e470342d54 100644
--- a/nix/libstore/builtins.hh
+++ b/nix/libstore/builtins.hh
@@ -24,22 +24,24 @@
 #include <map>
 #include <string>
 
-namespace nix {
+namespace nix
+{
 
-    inline bool isBuiltin(const Derivation & drv)
-    {
-	return string(drv.builder, 0, 8) == "builtin:";
-    }
+inline bool
+isBuiltin (const Derivation &drv)
+{
+  return string (drv.builder, 0, 8) == "builtin:";
+}
 
-    /* Build DRV, which lives at DRVPATH.  */
-    typedef void (*derivationBuilder) (const Derivation &drv,
-				       const std::string &drvPath,
-				       const std::string &output);
+/* Build DRV, which lives at DRVPATH.  */
+typedef void (*derivationBuilder) (const Derivation &drv,
+                                   const std::string &drvPath,
+                                   const std::string &output);
 
-    /* Return the built-in builder called BUILDER, or NULL if none was
-       found.  */
-    derivationBuilder lookupBuiltinBuilder(const std::string &builder);
+/* Return the built-in builder called BUILDER, or NULL if none was
+   found.  */
+derivationBuilder lookupBuiltinBuilder (const std::string &builder);
 
-    /* Return the list of supported built-in builder names.  */
-    std::list<std::string> builtinBuilderNames();
+/* Return the list of supported built-in builder names.  */
+std::list<std::string> builtinBuilderNames ();
 }
diff --git a/nix/libstore/derivations.cc b/nix/libstore/derivations.cc
index d316f6c7bf..df607f9c24 100644
--- a/nix/libstore/derivations.cc
+++ b/nix/libstore/derivations.cc
@@ -1,217 +1,267 @@
 #include "derivations.hh"
-#include "store-api.hh"
 #include "globals.hh"
-#include "util.hh"
 #include "misc.hh"
+#include "store-api.hh"
+#include "util.hh"
 
+namespace nix
+{
 
-namespace nix {
-
-
-void DerivationOutput::parseHashInfo(bool & recursive, HashType & hashType, Hash & hash) const
+void
+DerivationOutput::parseHashInfo (bool &recursive, HashType &hashType,
+                                 Hash &hash) const
 {
-    recursive = false;
-    string algo = hashAlgo;
+  recursive = false;
+  string algo = hashAlgo;
 
-    if (string(algo, 0, 2) == "r:") {
-        recursive = true;
-        algo = string(algo, 2);
+  if (string (algo, 0, 2) == "r:")
+    {
+      recursive = true;
+      algo = string (algo, 2);
     }
 
-    hashType = parseHashType(algo);
-    if (hashType == htUnknown)
-        throw Error(format("unknown hash algorithm `%1%'") % algo);
+  hashType = parseHashType (algo);
+  if (hashType == htUnknown)
+    throw Error (format ("unknown hash algorithm `%1%'") % algo);
 
-    hash = parseHash(hashType, this->hash);
+  hash = parseHash (hashType, this->hash);
 }
 
-
-Path writeDerivation(StoreAPI & store,
-    const Derivation & drv, const string & name, bool repair)
+Path
+writeDerivation (StoreAPI &store, const Derivation &drv, const string &name,
+                 bool repair)
 {
-    PathSet references;
-    references.insert(drv.inputSrcs.begin(), drv.inputSrcs.end());
-    foreach (DerivationInputs::const_iterator, i, drv.inputDrvs)
-        references.insert(i->first);
-    /* Note that the outputs of a derivation are *not* references
-       (that can be missing (of course) and should not necessarily be
-       held during a garbage collection). */
-    string suffix = name + drvExtension;
-    string contents = unparseDerivation(drv);
-    return settings.readOnlyMode
-        ? computeStorePathForText(suffix, contents, references)
-        : store.addTextToStore(suffix, contents, references, repair);
+  PathSet references;
+  references.insert (drv.inputSrcs.begin (), drv.inputSrcs.end ());
+  foreach (DerivationInputs::const_iterator, i, drv.inputDrvs)
+    references.insert (i->first);
+  /* Note that the outputs of a derivation are *not* references
+     (that can be missing (of course) and should not necessarily be
+     held during a garbage collection). */
+  string suffix = name + drvExtension;
+  string contents = unparseDerivation (drv);
+  return settings.readOnlyMode
+             ? computeStorePathForText (suffix, contents, references)
+             : store.addTextToStore (suffix, contents, references, repair);
 }
 
-
-static Path parsePath(std::istream & str)
+static Path
+parsePath (std::istream &str)
 {
-    string s = parseString(str);
-    if (s.size() == 0 || s[0] != '/')
-        throw FormatError(format("bad path `%1%' in derivation") % s);
-    return s;
+  string s = parseString (str);
+  if (s.size () == 0 || s[0] != '/')
+    throw FormatError (format ("bad path `%1%' in derivation") % s);
+  return s;
 }
 
-
-static StringSet parseStrings(std::istream & str, bool arePaths)
+static StringSet
+parseStrings (std::istream &str, bool arePaths)
 {
-    StringSet res;
-    while (!endOfList(str))
-        res.insert(arePaths ? parsePath(str) : parseString(str));
-    return res;
+  StringSet res;
+  while (!endOfList (str))
+    res.insert (arePaths ? parsePath (str) : parseString (str));
+  return res;
 }
 
-
-static Derivation parseDerivation(const string & s)
+static Derivation
+parseDerivation (const string &s)
 {
-    Derivation drv;
-    std::istringstream str(s);
-    expect(str, "Derive([");
-
-    /* Parse the list of outputs. */
-    while (!endOfList(str)) {
-        DerivationOutput out;
-        expect(str, "("); string id = parseString(str);
-        expect(str, ","); out.path = parsePath(str);
-        expect(str, ","); out.hashAlgo = parseString(str);
-        expect(str, ","); out.hash = parseString(str);
-        expect(str, ")");
-        drv.outputs[id] = out;
+  Derivation drv;
+  std::istringstream str (s);
+  expect (str, "Derive([");
+
+  /* Parse the list of outputs. */
+  while (!endOfList (str))
+    {
+      DerivationOutput out;
+      expect (str, "(");
+      string id = parseString (str);
+      expect (str, ",");
+      out.path = parsePath (str);
+      expect (str, ",");
+      out.hashAlgo = parseString (str);
+      expect (str, ",");
+      out.hash = parseString (str);
+      expect (str, ")");
+      drv.outputs[id] = out;
     }
 
-    /* Parse the list of input derivations. */
-    expect(str, ",[");
-    while (!endOfList(str)) {
-        expect(str, "(");
-        Path drvPath = parsePath(str);
-        expect(str, ",[");
-        drv.inputDrvs[drvPath] = parseStrings(str, false);
-        expect(str, ")");
+  /* Parse the list of input derivations. */
+  expect (str, ",[");
+  while (!endOfList (str))
+    {
+      expect (str, "(");
+      Path drvPath = parsePath (str);
+      expect (str, ",[");
+      drv.inputDrvs[drvPath] = parseStrings (str, false);
+      expect (str, ")");
     }
 
-    expect(str, ",["); drv.inputSrcs = parseStrings(str, true);
-    expect(str, ","); drv.platform = parseString(str);
-    expect(str, ","); drv.builder = parseString(str);
-
-    /* Parse the builder arguments. */
-    expect(str, ",[");
-    while (!endOfList(str))
-        drv.args.push_back(parseString(str));
-
-    /* Parse the environment variables. */
-    expect(str, ",[");
-    while (!endOfList(str)) {
-        expect(str, "("); string name = parseString(str);
-        expect(str, ","); string value = parseString(str);
-        expect(str, ")");
-        drv.env[name] = value;
+  expect (str, ",[");
+  drv.inputSrcs = parseStrings (str, true);
+  expect (str, ",");
+  drv.platform = parseString (str);
+  expect (str, ",");
+  drv.builder = parseString (str);
+
+  /* Parse the builder arguments. */
+  expect (str, ",[");
+  while (!endOfList (str))
+    drv.args.push_back (parseString (str));
+
+  /* Parse the environment variables. */
+  expect (str, ",[");
+  while (!endOfList (str))
+    {
+      expect (str, "(");
+      string name = parseString (str);
+      expect (str, ",");
+      string value = parseString (str);
+      expect (str, ")");
+      drv.env[name] = value;
     }
 
-    expect(str, ")");
-    return drv;
+  expect (str, ")");
+  return drv;
 }
 
-
-Derivation readDerivation(const Path & drvPath)
+Derivation
+readDerivation (const Path &drvPath)
 {
-    try {
-        return parseDerivation(readFile(drvPath));
-    } catch (FormatError & e) {
-        throw Error(format("error parsing derivation `%1%': %2%") % drvPath % e.msg());
+  try
+    {
+      return parseDerivation (readFile (drvPath));
+    }
+  catch (FormatError &e)
+    {
+      throw Error (format ("error parsing derivation `%1%': %2%") % drvPath
+                   % e.msg ());
     }
 }
 
-
-static void printString(string & res, const string & s)
+static void
+printString (string &res, const string &s)
 {
-    res += '"';
-    for (const char * i = s.c_str(); *i; i++)
-        if (*i == '\"' || *i == '\\') { res += "\\"; res += *i; }
-        else if (*i == '\n') res += "\\n";
-        else if (*i == '\r') res += "\\r";
-        else if (*i == '\t') res += "\\t";
-        else res += *i;
-    res += '"';
+  res += '"';
+  for (const char *i = s.c_str (); *i; i++)
+    if (*i == '\"' || *i == '\\')
+      {
+        res += "\\";
+        res += *i;
+      }
+    else if (*i == '\n')
+      res += "\\n";
+    else if (*i == '\r')
+      res += "\\r";
+    else if (*i == '\t')
+      res += "\\t";
+    else
+      res += *i;
+  res += '"';
 }
 
-
-template<class ForwardIterator>
-static void printStrings(string & res, ForwardIterator i, ForwardIterator j)
+template <class ForwardIterator>
+static void
+printStrings (string &res, ForwardIterator i, ForwardIterator j)
 {
-    res += '[';
-    bool first = true;
-    for ( ; i != j; ++i) {
-        if (first) first = false; else res += ',';
-        printString(res, *i);
+  res += '[';
+  bool first = true;
+  for (; i != j; ++i)
+    {
+      if (first)
+        first = false;
+      else
+        res += ',';
+      printString (res, *i);
     }
-    res += ']';
+  res += ']';
 }
 
-
-string unparseDerivation(const Derivation & drv)
+string
+unparseDerivation (const Derivation &drv)
 {
-    string s;
-    s.reserve(65536);
-    s += "Derive([";
-
-    bool first = true;
-    foreach (DerivationOutputs::const_iterator, i, drv.outputs) {
-        if (first) first = false; else s += ',';
-        s += '('; printString(s, i->first);
-        s += ','; printString(s, i->second.path);
-        s += ','; printString(s, i->second.hashAlgo);
-        s += ','; printString(s, i->second.hash);
-        s += ')';
+  string s;
+  s.reserve (65536);
+  s += "Derive([";
+
+  bool first = true;
+  foreach (DerivationOutputs::const_iterator, i, drv.outputs)
+    {
+      if (first)
+        first = false;
+      else
+        s += ',';
+      s += '(';
+      printString (s, i->first);
+      s += ',';
+      printString (s, i->second.path);
+      s += ',';
+      printString (s, i->second.hashAlgo);
+      s += ',';
+      printString (s, i->second.hash);
+      s += ')';
     }
 
-    s += "],[";
-    first = true;
-    foreach (DerivationInputs::const_iterator, i, drv.inputDrvs) {
-        if (first) first = false; else s += ',';
-        s += '('; printString(s, i->first);
-        s += ','; printStrings(s, i->second.begin(), i->second.end());
-        s += ')';
+  s += "],[";
+  first = true;
+  foreach (DerivationInputs::const_iterator, i, drv.inputDrvs)
+    {
+      if (first)
+        first = false;
+      else
+        s += ',';
+      s += '(';
+      printString (s, i->first);
+      s += ',';
+      printStrings (s, i->second.begin (), i->second.end ());
+      s += ')';
     }
 
-    s += "],";
-    printStrings(s, drv.inputSrcs.begin(), drv.inputSrcs.end());
-
-    s += ','; printString(s, drv.platform);
-    s += ','; printString(s, drv.builder);
-    s += ','; printStrings(s, drv.args.begin(), drv.args.end());
-
-    s += ",[";
-    first = true;
-    foreach (StringPairs::const_iterator, i, drv.env) {
-        if (first) first = false; else s += ',';
-        s += '('; printString(s, i->first);
-        s += ','; printString(s, i->second);
-        s += ')';
+  s += "],";
+  printStrings (s, drv.inputSrcs.begin (), drv.inputSrcs.end ());
+
+  s += ',';
+  printString (s, drv.platform);
+  s += ',';
+  printString (s, drv.builder);
+  s += ',';
+  printStrings (s, drv.args.begin (), drv.args.end ());
+
+  s += ",[";
+  first = true;
+  foreach (StringPairs::const_iterator, i, drv.env)
+    {
+      if (first)
+        first = false;
+      else
+        s += ',';
+      s += '(';
+      printString (s, i->first);
+      s += ',';
+      printString (s, i->second);
+      s += ')';
     }
 
-    s += "])";
+  s += "])";
 
-    return s;
+  return s;
 }
 
-
-bool isDerivation(const string & fileName)
+bool
+isDerivation (const string &fileName)
 {
-    return hasSuffix(fileName, drvExtension);
+  return hasSuffix (fileName, drvExtension);
 }
 
-
-bool isFixedOutputDrv(const Derivation & drv)
+bool
+isFixedOutputDrv (const Derivation &drv)
 {
-    return drv.outputs.size() == 1 &&
-        drv.outputs.begin()->first == "out" &&
-        drv.outputs.begin()->second.hash != "";
+  return drv.outputs.size () == 1 && drv.outputs.begin ()->first == "out"
+         && drv.outputs.begin ()->second.hash != "";
 }
 
-
 DrvHashes drvHashes;
 
-
 /* Returns the hash of a derivation modulo fixed-output
    subderivations.  A fixed-output derivation is a derivation with one
    output (`out') for which an expected hash and hash algorithm are
@@ -232,66 +282,68 @@ DrvHashes drvHashes;
    paths have been replaced by the result of a recursive call to this
    function, and that for fixed-output derivations we return a hash of
    its output path. */
-Hash hashDerivationModulo(StoreAPI & store, Derivation drv)
+Hash
+hashDerivationModulo (StoreAPI &store, Derivation drv)
 {
-    /* Return a fixed hash for fixed-output derivations. */
-    if (isFixedOutputDrv(drv)) {
-        DerivationOutputs::const_iterator i = drv.outputs.begin();
-        return hashString(htSHA256, "fixed:out:"
-            + i->second.hashAlgo + ":"
-            + i->second.hash + ":"
-            + i->second.path);
+  /* Return a fixed hash for fixed-output derivations. */
+  if (isFixedOutputDrv (drv))
+    {
+      DerivationOutputs::const_iterator i = drv.outputs.begin ();
+      return hashString (htSHA256, "fixed:out:" + i->second.hashAlgo + ":"
+                                       + i->second.hash + ":"
+                                       + i->second.path);
     }
 
-    /* For other derivations, replace the inputs paths with recursive
-       calls to this function.*/
-    DerivationInputs inputs2;
-    foreach (DerivationInputs::const_iterator, i, drv.inputDrvs) {
-        Hash h = drvHashes[i->first];
-        if (h.type == htUnknown) {
-            assert(store.isValidPath(i->first));
-            Derivation drv2 = readDerivation(i->first);
-            h = hashDerivationModulo(store, drv2);
-            drvHashes[i->first] = h;
+  /* For other derivations, replace the inputs paths with recursive
+     calls to this function.*/
+  DerivationInputs inputs2;
+  foreach (DerivationInputs::const_iterator, i, drv.inputDrvs)
+    {
+      Hash h = drvHashes[i->first];
+      if (h.type == htUnknown)
+        {
+          assert (store.isValidPath (i->first));
+          Derivation drv2 = readDerivation (i->first);
+          h = hashDerivationModulo (store, drv2);
+          drvHashes[i->first] = h;
         }
-        inputs2[printHash(h)] = i->second;
+      inputs2[printHash (h)] = i->second;
     }
-    drv.inputDrvs = inputs2;
+  drv.inputDrvs = inputs2;
 
-    return hashString(htSHA256, unparseDerivation(drv));
+  return hashString (htSHA256, unparseDerivation (drv));
 }
 
-
-DrvPathWithOutputs parseDrvPathWithOutputs(const string & s)
+DrvPathWithOutputs
+parseDrvPathWithOutputs (const string &s)
 {
-    size_t n = s.find("!");
-    return n == s.npos
-        ? DrvPathWithOutputs(s, std::set<string>())
-        : DrvPathWithOutputs(string(s, 0, n), tokenizeString<std::set<string> >(string(s, n + 1), ","));
+  size_t n = s.find ("!");
+  return n == s.npos ? DrvPathWithOutputs (s, std::set<string> ())
+                     : DrvPathWithOutputs (string (s, 0, n),
+                                           tokenizeString<std::set<string> > (
+                                               string (s, n + 1), ","));
 }
 
-
-Path makeDrvPathWithOutputs(const Path & drvPath, const std::set<string> & outputs)
+Path
+makeDrvPathWithOutputs (const Path &drvPath, const std::set<string> &outputs)
 {
-    return outputs.empty()
-        ? drvPath
-        : drvPath + "!" + concatStringsSep(",", outputs);
+  return outputs.empty () ? drvPath
+                          : drvPath + "!" + concatStringsSep (",", outputs);
 }
 
-
-bool wantOutput(const string & output, const std::set<string> & wanted)
+bool
+wantOutput (const string &output, const std::set<string> &wanted)
 {
-    return wanted.empty() || wanted.find(output) != wanted.end();
+  return wanted.empty () || wanted.find (output) != wanted.end ();
 }
 
-
-PathSet outputPaths(const Derivation & drv)
+PathSet
+outputPaths (const Derivation &drv)
 {
-    PathSet paths;
-    for (auto & i : drv.outputs)
-        paths.insert(i.second.path);
-    return paths;
+  PathSet paths;
+  for (auto &i : drv.outputs)
+    paths.insert (i.second.path);
+  return paths;
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libstore/derivations.hh b/nix/libstore/derivations.hh
index 8d5e4d05d4..6105e2cdb3 100644
--- a/nix/libstore/derivations.hh
+++ b/nix/libstore/derivations.hh
@@ -1,35 +1,31 @@
 #pragma once
 
-#include "types.hh"
 #include "hash.hh"
+#include "types.hh"
 
 #include <map>
 
-
-namespace nix {
-
+namespace nix
+{
 
 /* Extension of derivations in the Nix store. */
 const string drvExtension = ".drv";
 
-
 /* Abstract syntax of derivations. */
 
 struct DerivationOutput
 {
-    Path path;
-    string hashAlgo; /* hash used for expected hash computation */
-    string hash; /* expected hash, may be null */
-    DerivationOutput()
-    {
-    }
-    DerivationOutput(Path path, string hashAlgo, string hash)
-    {
-        this->path = path;
-        this->hashAlgo = hashAlgo;
-        this->hash = hash;
-    }
-    void parseHashInfo(bool & recursive, HashType & hashType, Hash & hash) const;
+  Path path;
+  string hashAlgo; /* hash used for expected hash computation */
+  string hash;     /* expected hash, may be null */
+  DerivationOutput () {}
+  DerivationOutput (Path path, string hashAlgo, string hash)
+  {
+    this->path = path;
+    this->hashAlgo = hashAlgo;
+    this->hash = hash;
+  }
+  void parseHashInfo (bool &recursive, HashType &hashType, Hash &hash) const;
 };
 
 typedef std::map<string, DerivationOutput> DerivationOutputs;
@@ -42,37 +38,35 @@ typedef std::map<string, string> StringPairs;
 
 struct Derivation
 {
-    DerivationOutputs outputs; /* keyed on symbolic IDs */
-    DerivationInputs inputDrvs; /* inputs that are sub-derivations */
-    PathSet inputSrcs; /* inputs that are sources */
-    string platform;
-    Path builder;
-    Strings args;
-    StringPairs env;
+  DerivationOutputs outputs;  /* keyed on symbolic IDs */
+  DerivationInputs inputDrvs; /* inputs that are sub-derivations */
+  PathSet inputSrcs;          /* inputs that are sources */
+  string platform;
+  Path builder;
+  Strings args;
+  StringPairs env;
 };
 
-
 class StoreAPI;
 
-
 /* Write a derivation to the Nix store, and return its path. */
-Path writeDerivation(StoreAPI & store,
-    const Derivation & drv, const string & name, bool repair = false);
+Path writeDerivation (StoreAPI &store, const Derivation &drv,
+                      const string &name, bool repair = false);
 
 /* Read a derivation from a file. */
-Derivation readDerivation(const Path & drvPath);
+Derivation readDerivation (const Path &drvPath);
 
 /* Print a derivation. */
-string unparseDerivation(const Derivation & drv);
+string unparseDerivation (const Derivation &drv);
 
 /* Check whether a file name ends with the extensions for
    derivations. */
-bool isDerivation(const string & fileName);
+bool isDerivation (const string &fileName);
 
 /* Return true iff this is a fixed-output derivation. */
-bool isFixedOutputDrv(const Derivation & drv);
+bool isFixedOutputDrv (const Derivation &drv);
 
-Hash hashDerivationModulo(StoreAPI & store, Derivation drv);
+Hash hashDerivationModulo (StoreAPI &store, Derivation drv);
 
 /* Memoisation of hashDerivationModulo(). */
 typedef std::map<Path, Hash> DrvHashes;
@@ -83,12 +77,13 @@ extern DrvHashes drvHashes;
    (/nix/store/hash-foo!out1,out2,...) into the derivation path and
    the outputs. */
 typedef std::pair<string, std::set<string> > DrvPathWithOutputs;
-DrvPathWithOutputs parseDrvPathWithOutputs(const string & s);
+DrvPathWithOutputs parseDrvPathWithOutputs (const string &s);
 
-Path makeDrvPathWithOutputs(const Path & drvPath, const std::set<string> & outputs);
+Path makeDrvPathWithOutputs (const Path &drvPath,
+                             const std::set<string> &outputs);
 
-bool wantOutput(const string & output, const std::set<string> & wanted);
+bool wantOutput (const string &output, const std::set<string> &wanted);
 
-PathSet outputPaths(const Derivation & drv);
+PathSet outputPaths (const Derivation &drv);
 
 }
diff --git a/nix/libstore/gc.cc b/nix/libstore/gc.cc
index 16519116e4..3488c8c76a 100644
--- a/nix/libstore/gc.cc
+++ b/nix/libstore/gc.cc
@@ -1,786 +1,906 @@
 #include "globals.hh"
-#include "misc.hh"
 #include "local-store.hh"
+#include "misc.hh"
 
+#include <algorithm>
 #include <functional>
 #include <queue>
-#include <algorithm>
 
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <climits>
 #include <errno.h>
 #include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
-#include <climits>
-
-
-namespace nix {
 
+namespace nix
+{
 
 static string gcLockName = "gc.lock";
 static string tempRootsDir = "temproots";
 static string gcRootsDir = "gcroots";
 
-
 /* Acquire the global GC lock.  This is used to prevent new build
    processes from starting after the temporary root files have been
    read.  To be precise: when they try to create a new temporary root
    file, they will block until the garbage collector has finished /
    yielded the GC lock. */
-int LocalStore::openGCLock(LockType lockType)
+int
+LocalStore::openGCLock (LockType lockType)
 {
-    Path fnGCLock = (format("%1%/%2%")
-        % settings.nixStateDir % gcLockName).str();
+  Path fnGCLock
+      = (format ("%1%/%2%") % settings.nixStateDir % gcLockName).str ();
 
-    debug(format("acquiring global GC lock `%1%'") % fnGCLock);
+  debug (format ("acquiring global GC lock `%1%'") % fnGCLock);
 
-    AutoCloseFD fdGCLock = open(fnGCLock.c_str(), O_RDWR | O_CREAT, 0600);
-    if (fdGCLock == -1)
-        throw SysError(format("opening global GC lock `%1%'") % fnGCLock);
-    closeOnExec(fdGCLock);
+  AutoCloseFD fdGCLock = open (fnGCLock.c_str (), O_RDWR | O_CREAT, 0600);
+  if (fdGCLock == -1)
+    throw SysError (format ("opening global GC lock `%1%'") % fnGCLock);
+  closeOnExec (fdGCLock);
 
-    if (!lockFile(fdGCLock, lockType, false)) {
-        printMsg(lvlError, format("waiting for the big garbage collector lock..."));
-        lockFile(fdGCLock, lockType, true);
+  if (!lockFile (fdGCLock, lockType, false))
+    {
+      printMsg (lvlError,
+                format ("waiting for the big garbage collector lock..."));
+      lockFile (fdGCLock, lockType, true);
     }
 
-    /* !!! Restrict read permission on the GC root.  Otherwise any
-       process that can open the file for reading can DoS the
-       collector. */
+  /* !!! Restrict read permission on the GC root.  Otherwise any
+     process that can open the file for reading can DoS the
+     collector. */
 
-    return fdGCLock.borrow();
+  return fdGCLock.borrow ();
 }
 
-
-static void makeSymlink(const Path & link, const Path & target)
+static void
+makeSymlink (const Path &link, const Path &target)
 {
-    /* Create directories up to `gcRoot'. */
-    createDirs(dirOf(link));
-
-    /* Create the new symlink. */
-    Path tempLink = (format("%1%.tmp-%2%-%3%")
-        % link % getpid() % rand()).str();
-    createSymlink(target, tempLink);
-
-    /* Atomically replace the old one. */
-    if (rename(tempLink.c_str(), link.c_str()) == -1)
-        throw SysError(format("cannot rename `%1%' to `%2%'")
-            % tempLink % link);
-}
+  /* Create directories up to `gcRoot'. */
+  createDirs (dirOf (link));
 
+  /* Create the new symlink. */
+  Path tempLink
+      = (format ("%1%.tmp-%2%-%3%") % link % getpid () % rand ()).str ();
+  createSymlink (target, tempLink);
 
-void LocalStore::syncWithGC()
-{
-    AutoCloseFD fdGCLock = openGCLock(ltRead);
+  /* Atomically replace the old one. */
+  if (rename (tempLink.c_str (), link.c_str ()) == -1)
+    throw SysError (format ("cannot rename `%1%' to `%2%'") % tempLink % link);
 }
 
-
-void LocalStore::addIndirectRoot(const Path & path)
+void
+LocalStore::syncWithGC ()
 {
-    string hash = printHash32(hashString(htSHA1, path));
-    Path realRoot = canonPath((format("%1%/%2%/auto/%3%")
-        % settings.nixStateDir % gcRootsDir % hash).str());
-    makeSymlink(realRoot, path);
+  AutoCloseFD fdGCLock = openGCLock (ltRead);
 }
 
+void
+LocalStore::addIndirectRoot (const Path &path)
+{
+  string hash = printHash32 (hashString (htSHA1, path));
+  Path realRoot = canonPath (
+      (format ("%1%/%2%/auto/%3%") % settings.nixStateDir % gcRootsDir % hash)
+          .str ());
+  makeSymlink (realRoot, path);
+}
 
-Path addPermRoot(StoreAPI & store, const Path & _storePath,
-    const Path & _gcRoot, bool indirect, bool allowOutsideRootsDir)
+Path
+addPermRoot (StoreAPI &store, const Path &_storePath, const Path &_gcRoot,
+             bool indirect, bool allowOutsideRootsDir)
 {
-    Path storePath(canonPath(_storePath));
-    Path gcRoot(canonPath(_gcRoot));
-    assertStorePath(storePath);
-
-    if (isInStore(gcRoot))
-        throw Error(format(
-                "creating a garbage collector root (%1%) in the store is forbidden "
-                "(are you running nix-build inside the store?)") % gcRoot);
-
-    if (indirect) {
-        /* Don't clobber the link if it already exists and doesn't
-           point to the store. */
-        if (pathExists(gcRoot) && (!isLink(gcRoot) || !isInStore(readLink(gcRoot))))
-            throw Error(format("cannot create symlink `%1%'; already exists") % gcRoot);
-        makeSymlink(gcRoot, storePath);
-        store.addIndirectRoot(gcRoot);
+  Path storePath (canonPath (_storePath));
+  Path gcRoot (canonPath (_gcRoot));
+  assertStorePath (storePath);
+
+  if (isInStore (gcRoot))
+    throw Error (format ("creating a garbage collector root (%1%) in the "
+                         "store is forbidden "
+                         "(are you running nix-build inside the store?)")
+                 % gcRoot);
+
+  if (indirect)
+    {
+      /* Don't clobber the link if it already exists and doesn't
+         point to the store. */
+      if (pathExists (gcRoot)
+          && (!isLink (gcRoot) || !isInStore (readLink (gcRoot))))
+        throw Error (format ("cannot create symlink `%1%'; already exists")
+                     % gcRoot);
+      makeSymlink (gcRoot, storePath);
+      store.addIndirectRoot (gcRoot);
     }
 
-    else {
-        if (!allowOutsideRootsDir) {
-            Path rootsDir = canonPath((format("%1%/%2%") % settings.nixStateDir % gcRootsDir).str());
-
-            if (string(gcRoot, 0, rootsDir.size() + 1) != rootsDir + "/")
-                throw Error(format(
-                    "path `%1%' is not a valid garbage collector root; "
-                    "it's not in the directory `%2%'")
-                    % gcRoot % rootsDir);
+  else
+    {
+      if (!allowOutsideRootsDir)
+        {
+          Path rootsDir = canonPath (
+              (format ("%1%/%2%") % settings.nixStateDir % gcRootsDir).str ());
+
+          if (string (gcRoot, 0, rootsDir.size () + 1) != rootsDir + "/")
+            throw Error (
+                format ("path `%1%' is not a valid garbage collector root; "
+                        "it's not in the directory `%2%'")
+                % gcRoot % rootsDir);
         }
 
-        if (baseNameOf(gcRoot) == baseNameOf(storePath))
-            writeFile(gcRoot, "");
-        else
-            makeSymlink(gcRoot, storePath);
+      if (baseNameOf (gcRoot) == baseNameOf (storePath))
+        writeFile (gcRoot, "");
+      else
+        makeSymlink (gcRoot, storePath);
     }
 
-    /* Check that the root can be found by the garbage collector.
-       !!! This can be very slow on machines that have many roots.
-       Instead of reading all the roots, it would be more efficient to
-       check if the root is in a directory in or linked from the
-       gcroots directory. */
-    if (settings.checkRootReachability) {
-        Roots roots = store.findRoots();
-        if (roots.find(gcRoot) == roots.end())
-            printMsg(lvlError,
-                format(
-                    "warning: `%1%' is not in a directory where the garbage collector looks for roots; "
-                    "therefore, `%2%' might be removed by the garbage collector")
+  /* Check that the root can be found by the garbage collector.
+     !!! This can be very slow on machines that have many roots.
+     Instead of reading all the roots, it would be more efficient to
+     check if the root is in a directory in or linked from the
+     gcroots directory. */
+  if (settings.checkRootReachability)
+    {
+      Roots roots = store.findRoots ();
+      if (roots.find (gcRoot) == roots.end ())
+        printMsg (
+            lvlError,
+            format (
+                "warning: `%1%' is not in a directory where the garbage "
+                "collector looks for roots; "
+                "therefore, `%2%' might be removed by the garbage collector")
                 % gcRoot % storePath);
     }
 
-    /* Grab the global GC root, causing us to block while a GC is in
-       progress.  This prevents the set of permanent roots from
-       increasing while a GC is in progress. */
-    store.syncWithGC();
+  /* Grab the global GC root, causing us to block while a GC is in
+     progress.  This prevents the set of permanent roots from
+     increasing while a GC is in progress. */
+  store.syncWithGC ();
 
-    return gcRoot;
+  return gcRoot;
 }
 
-
-void LocalStore::addTempRoot(const Path & path)
+void
+LocalStore::addTempRoot (const Path &path)
 {
-    /* Create the temporary roots file for this process. */
-    if (fdTempRoots == -1) {
+  /* Create the temporary roots file for this process. */
+  if (fdTempRoots == -1)
+    {
 
-        while (1) {
-            Path dir = (format("%1%/%2%") % settings.nixStateDir % tempRootsDir).str();
-            createDirs(dir);
+      while (1)
+        {
+          Path dir = (format ("%1%/%2%") % settings.nixStateDir % tempRootsDir)
+                         .str ();
+          createDirs (dir);
 
-            fnTempRoots = (format("%1%/%2%")
-                % dir % getpid()).str();
+          fnTempRoots = (format ("%1%/%2%") % dir % getpid ()).str ();
 
-            AutoCloseFD fdGCLock = openGCLock(ltRead);
+          AutoCloseFD fdGCLock = openGCLock (ltRead);
 
-            if (pathExists(fnTempRoots))
-                /* It *must* be stale, since there can be no two
-                   processes with the same pid. */
-                unlink(fnTempRoots.c_str());
+          if (pathExists (fnTempRoots))
+            /* It *must* be stale, since there can be no two
+               processes with the same pid. */
+            unlink (fnTempRoots.c_str ());
 
-            fdTempRoots = openLockFile(fnTempRoots, true);
+          fdTempRoots = openLockFile (fnTempRoots, true);
 
-            fdGCLock.close();
+          fdGCLock.close ();
 
-            debug(format("acquiring read lock on `%1%'") % fnTempRoots);
-            lockFile(fdTempRoots, ltRead, true);
+          debug (format ("acquiring read lock on `%1%'") % fnTempRoots);
+          lockFile (fdTempRoots, ltRead, true);
 
-            /* Check whether the garbage collector didn't get in our
-               way. */
-            struct stat st;
-            if (fstat(fdTempRoots, &st) == -1)
-                throw SysError(format("statting `%1%'") % fnTempRoots);
-            if (st.st_size == 0) break;
+          /* Check whether the garbage collector didn't get in our
+             way. */
+          struct stat st;
+          if (fstat (fdTempRoots, &st) == -1)
+            throw SysError (format ("statting `%1%'") % fnTempRoots);
+          if (st.st_size == 0)
+            break;
 
-            /* The garbage collector deleted this file before we could
-               get a lock.  (It won't delete the file after we get a
-               lock.)  Try again. */
+          /* The garbage collector deleted this file before we could
+             get a lock.  (It won't delete the file after we get a
+             lock.)  Try again. */
         }
-
     }
 
-    /* Upgrade the lock to a write lock.  This will cause us to block
-       if the garbage collector is holding our lock. */
-    debug(format("acquiring write lock on `%1%'") % fnTempRoots);
-    lockFile(fdTempRoots, ltWrite, true);
+  /* Upgrade the lock to a write lock.  This will cause us to block
+     if the garbage collector is holding our lock. */
+  debug (format ("acquiring write lock on `%1%'") % fnTempRoots);
+  lockFile (fdTempRoots, ltWrite, true);
 
-    string s = path + '\0';
-    writeFull(fdTempRoots, s);
+  string s = path + '\0';
+  writeFull (fdTempRoots, s);
 
-    /* Downgrade to a read lock. */
-    debug(format("downgrading to read lock on `%1%'") % fnTempRoots);
-    lockFile(fdTempRoots, ltRead, true);
+  /* Downgrade to a read lock. */
+  debug (format ("downgrading to read lock on `%1%'") % fnTempRoots);
+  lockFile (fdTempRoots, ltRead, true);
 }
 
-
 typedef std::shared_ptr<AutoCloseFD> FDPtr;
 typedef list<FDPtr> FDs;
 
-
-static void readTempRoots(PathSet & tempRoots, FDs & fds)
+static void
+readTempRoots (PathSet &tempRoots, FDs &fds)
 {
-    /* Read the `temproots' directory for per-process temporary root
-       files. */
-    DirEntries tempRootFiles = readDirectory(
-        (format("%1%/%2%") % settings.nixStateDir % tempRootsDir).str());
-
-    for (auto & i : tempRootFiles) {
-        Path path = (format("%1%/%2%/%3%") % settings.nixStateDir % tempRootsDir % i.name).str();
-
-        debug(format("reading temporary root file `%1%'") % path);
-        FDPtr fd(new AutoCloseFD(open(path.c_str(), O_RDWR, 0666)));
-        if (*fd == -1) {
-            /* It's okay if the file has disappeared. */
-            if (errno == ENOENT) continue;
-            throw SysError(format("opening temporary roots file `%1%'") % path);
-        }
-
-        /* This should work, but doesn't, for some reason. */
-        //FDPtr fd(new AutoCloseFD(openLockFile(path, false)));
-        //if (*fd == -1) continue;
-
-        /* Try to acquire a write lock without blocking.  This can
-           only succeed if the owning process has died.  In that case
-           we don't care about its temporary roots. */
-        if (lockFile(*fd, ltWrite, false)) {
-            printMsg(lvlError, format("removing stale temporary roots file `%1%'") % path);
-            unlink(path.c_str());
-            writeFull(*fd, "d");
+  /* Read the `temproots' directory for per-process temporary root
+     files. */
+  DirEntries tempRootFiles = readDirectory (
+      (format ("%1%/%2%") % settings.nixStateDir % tempRootsDir).str ());
+
+  for (auto &i : tempRootFiles)
+    {
+      Path path = (format ("%1%/%2%/%3%") % settings.nixStateDir % tempRootsDir
+                   % i.name)
+                      .str ();
+
+      debug (format ("reading temporary root file `%1%'") % path);
+      FDPtr fd (new AutoCloseFD (open (path.c_str (), O_RDWR, 0666)));
+      if (*fd == -1)
+        {
+          /* It's okay if the file has disappeared. */
+          if (errno == ENOENT)
             continue;
+          throw SysError (format ("opening temporary roots file `%1%'")
+                          % path);
         }
 
-        /* Acquire a read lock.  This will prevent the owning process
-           from upgrading to a write lock, therefore it will block in
-           addTempRoot(). */
-        debug(format("waiting for read lock on `%1%'") % path);
-        lockFile(*fd, ltRead, true);
-
-        /* Read the entire file. */
-        string contents = readFile(*fd);
-
-        /* Extract the roots. */
-        string::size_type pos = 0, end;
+      /* This should work, but doesn't, for some reason. */
+      // FDPtr fd(new AutoCloseFD(openLockFile(path, false)));
+      // if (*fd == -1) continue;
+
+      /* Try to acquire a write lock without blocking.  This can
+         only succeed if the owning process has died.  In that case
+         we don't care about its temporary roots. */
+      if (lockFile (*fd, ltWrite, false))
+        {
+          printMsg (lvlError,
+                    format ("removing stale temporary roots file `%1%'")
+                        % path);
+          unlink (path.c_str ());
+          writeFull (*fd, "d");
+          continue;
+        }
 
-        while ((end = contents.find((char) 0, pos)) != string::npos) {
-            Path root(contents, pos, end - pos);
-            debug(format("got temporary root `%1%'") % root);
-            assertStorePath(root);
-            tempRoots.insert(root);
-            pos = end + 1;
+      /* Acquire a read lock.  This will prevent the owning process
+         from upgrading to a write lock, therefore it will block in
+         addTempRoot(). */
+      debug (format ("waiting for read lock on `%1%'") % path);
+      lockFile (*fd, ltRead, true);
+
+      /* Read the entire file. */
+      string contents = readFile (*fd);
+
+      /* Extract the roots. */
+      string::size_type pos = 0, end;
+
+      while ((end = contents.find ((char)0, pos)) != string::npos)
+        {
+          Path root (contents, pos, end - pos);
+          debug (format ("got temporary root `%1%'") % root);
+          assertStorePath (root);
+          tempRoots.insert (root);
+          pos = end + 1;
         }
 
-        fds.push_back(fd); /* keep open */
+      fds.push_back (fd); /* keep open */
     }
 }
 
-
-static void foundRoot(StoreAPI & store,
-    const Path & path, const Path & target, Roots & roots)
+static void
+foundRoot (StoreAPI &store, const Path &path, const Path &target, Roots &roots)
 {
-    Path storePath = toStorePath(target);
-    if (store.isValidPath(storePath))
-        roots[path] = storePath;
-    else
-        printMsg(lvlInfo, format("skipping invalid root from `%1%' to `%2%'") % path % storePath);
+  Path storePath = toStorePath (target);
+  if (store.isValidPath (storePath))
+    roots[path] = storePath;
+  else
+    printMsg (lvlInfo, format ("skipping invalid root from `%1%' to `%2%'")
+                           % path % storePath);
 }
 
-
-static void findRoots(StoreAPI & store, const Path & path, unsigned char type, Roots & roots)
+static void
+findRoots (StoreAPI &store, const Path &path, unsigned char type, Roots &roots)
 {
-    try {
+  try
+    {
 
-        if (type == DT_UNKNOWN)
-            type = getFileType(path);
+      if (type == DT_UNKNOWN)
+        type = getFileType (path);
 
-        if (type == DT_DIR) {
-            for (auto & i : readDirectory(path))
-                findRoots(store, path + "/" + i.name, i.type, roots);
+      if (type == DT_DIR)
+        {
+          for (auto &i : readDirectory (path))
+            findRoots (store, path + "/" + i.name, i.type, roots);
         }
 
-        else if (type == DT_LNK) {
-            Path target = readLink(path);
-            if (isInStore(target))
-                foundRoot(store, path, target, roots);
-
-            /* Handle indirect roots. */
-            else {
-                target = absPath(target, dirOf(path));
-                if (!pathExists(target)) {
-                    if (isInDir(path, settings.nixStateDir + "/" + gcRootsDir + "/auto")) {
-                        printMsg(lvlInfo, format("removing stale link from `%1%' to `%2%'") % path % target);
-                        unlink(path.c_str());
+      else if (type == DT_LNK)
+        {
+          Path target = readLink (path);
+          if (isInStore (target))
+            foundRoot (store, path, target, roots);
+
+          /* Handle indirect roots. */
+          else
+            {
+              target = absPath (target, dirOf (path));
+              if (!pathExists (target))
+                {
+                  if (isInDir (path, settings.nixStateDir + "/" + gcRootsDir
+                                         + "/auto"))
+                    {
+                      printMsg (
+                          lvlInfo,
+                          format ("removing stale link from `%1%' to `%2%'")
+                              % path % target);
+                      unlink (path.c_str ());
                     }
-                } else {
-                    struct stat st2 = lstat(target);
-                    if (!S_ISLNK(st2.st_mode)) return;
-                    Path target2 = readLink(target);
-                    if (isInStore(target2)) foundRoot(store, target, target2, roots);
+                }
+              else
+                {
+                  struct stat st2 = lstat (target);
+                  if (!S_ISLNK (st2.st_mode))
+                    return;
+                  Path target2 = readLink (target);
+                  if (isInStore (target2))
+                    foundRoot (store, target, target2, roots);
                 }
             }
         }
 
-        else if (type == DT_REG) {
-            Path storePath = settings.nixStore + "/" + baseNameOf(path);
-            if (store.isValidPath(storePath))
-                roots[path] = storePath;
+      else if (type == DT_REG)
+        {
+          Path storePath = settings.nixStore + "/" + baseNameOf (path);
+          if (store.isValidPath (storePath))
+            roots[path] = storePath;
         }
-
     }
 
-    catch (SysError & e) {
-        /* We only ignore permanent failures. */
-        if (e.errNo == EACCES || e.errNo == ENOENT || e.errNo == ENOTDIR)
-            printMsg(lvlInfo, format("cannot read potential root `%1%'") % path);
-        else
-            throw;
+  catch (SysError &e)
+    {
+      /* We only ignore permanent failures. */
+      if (e.errNo == EACCES || e.errNo == ENOENT || e.errNo == ENOTDIR)
+        printMsg (lvlInfo, format ("cannot read potential root `%1%'") % path);
+      else
+        throw;
     }
 }
 
-
-Roots LocalStore::findRoots()
+Roots
+LocalStore::findRoots ()
 {
-    Roots roots;
-
-    /* Process direct roots in {gcroots,manifests,profiles}. */
-    nix::findRoots(*this, settings.nixStateDir + "/" + gcRootsDir, DT_UNKNOWN, roots);
-    if (pathExists(settings.nixStateDir + "/manifests"))
-        nix::findRoots(*this, settings.nixStateDir + "/manifests", DT_UNKNOWN, roots);
-    nix::findRoots(*this, settings.nixStateDir + "/profiles", DT_UNKNOWN, roots);
-
-    return roots;
+  Roots roots;
+
+  /* Process direct roots in {gcroots,manifests,profiles}. */
+  nix::findRoots (*this, settings.nixStateDir + "/" + gcRootsDir, DT_UNKNOWN,
+                  roots);
+  if (pathExists (settings.nixStateDir + "/manifests"))
+    nix::findRoots (*this, settings.nixStateDir + "/manifests", DT_UNKNOWN,
+                    roots);
+  nix::findRoots (*this, settings.nixStateDir + "/profiles", DT_UNKNOWN,
+                  roots);
+
+  return roots;
 }
 
-
-static void addAdditionalRoots(StoreAPI & store, PathSet & roots)
+static void
+addAdditionalRoots (StoreAPI &store, PathSet &roots)
 {
-    debug(format("executing `%1% gc --list-busy' to find additional roots")
-	  % settings.guixProgram);
-
-    const Strings args = { "gc", "--list-busy" };
-    string result = runProgram(settings.guixProgram, false, args);
-
-    StringSet paths = tokenizeString<StringSet>(result, "\n");
-
-    foreach (StringSet::iterator, i, paths) {
-        if (isInStore(*i)) {
-            Path path = toStorePath(*i);
-            if (roots.find(path) == roots.end() && store.isValidPath(path)) {
-                debug(format("got additional root `%1%'") % path);
-                roots.insert(path);
+  debug (format ("executing `%1% gc --list-busy' to find additional roots")
+         % settings.guixProgram);
+
+  const Strings args = { "gc", "--list-busy" };
+  string result = runProgram (settings.guixProgram, false, args);
+
+  StringSet paths = tokenizeString<StringSet> (result, "\n");
+
+  foreach (StringSet::iterator, i, paths)
+    {
+      if (isInStore (*i))
+        {
+          Path path = toStorePath (*i);
+          if (roots.find (path) == roots.end () && store.isValidPath (path))
+            {
+              debug (format ("got additional root `%1%'") % path);
+              roots.insert (path);
             }
         }
     }
 }
 
-
-struct GCLimitReached { };
-
+struct GCLimitReached
+{
+};
 
 struct LocalStore::GCState
 {
-    GCOptions options;
-    GCResults & results;
-    PathSet roots;
-    PathSet tempRoots;
-    PathSet dead;
-    PathSet alive;
-    bool gcKeepOutputs;
-    bool gcKeepDerivations;
-    unsigned long long bytesInvalidated;
-    bool moveToTrash = true;
-    Path trashDir;
-    bool shouldDelete;
-    GCState(GCResults & results_) : results(results_), bytesInvalidated(0) { }
+  GCOptions options;
+  GCResults &results;
+  PathSet roots;
+  PathSet tempRoots;
+  PathSet dead;
+  PathSet alive;
+  bool gcKeepOutputs;
+  bool gcKeepDerivations;
+  unsigned long long bytesInvalidated;
+  bool moveToTrash = true;
+  Path trashDir;
+  bool shouldDelete;
+  GCState (GCResults &results_) : results (results_), bytesInvalidated (0) {}
 };
 
-
-bool LocalStore::isActiveTempFile(const GCState & state,
-    const Path & path, const string & suffix)
+bool
+LocalStore::isActiveTempFile (const GCState &state, const Path &path,
+                              const string &suffix)
 {
-    return hasSuffix(path, suffix)
-        && state.tempRoots.find(string(path, 0, path.size() - suffix.size())) != state.tempRoots.end();
+  return hasSuffix (path, suffix)
+         && state.tempRoots.find (
+                string (path, 0, path.size () - suffix.size ()))
+                != state.tempRoots.end ();
 }
 
-
-void LocalStore::deleteGarbage(GCState & state, const Path & path)
+void
+LocalStore::deleteGarbage (GCState &state, const Path &path)
 {
-    unsigned long long bytesFreed;
+  unsigned long long bytesFreed;
 
-    /* When deduplication is on, store items always have at least two links:
-       the one at PATH, and one in /gnu/store/.links.  In that case, increase
-       bytesFreed when PATH has two or fewer links.  */
-    size_t linkThreshold =
-	(settings.autoOptimiseStore && isStorePath(path)) ? 2 : 1;
+  /* When deduplication is on, store items always have at least two links:
+     the one at PATH, and one in /gnu/store/.links.  In that case, increase
+     bytesFreed when PATH has two or fewer links.  */
+  size_t linkThreshold
+      = (settings.autoOptimiseStore && isStorePath (path)) ? 2 : 1;
 
-    deletePath(path, bytesFreed, linkThreshold);
-    state.results.bytesFreed += bytesFreed;
+  deletePath (path, bytesFreed, linkThreshold);
+  state.results.bytesFreed += bytesFreed;
 }
 
-
-void LocalStore::deletePathRecursive(GCState & state, const Path & path)
+void
+LocalStore::deletePathRecursive (GCState &state, const Path &path)
 {
-    checkInterrupt();
+  checkInterrupt ();
 
-    unsigned long long size = 0;
+  unsigned long long size = 0;
 
-    if (isValidPath(path)) {
-        PathSet referrers;
-        queryReferrers(path, referrers);
-        foreach (PathSet::iterator, i, referrers)
-            if (*i != path) deletePathRecursive(state, *i);
-        size = queryPathInfo(path).narSize;
-        invalidatePathChecked(path);
+  if (isValidPath (path))
+    {
+      PathSet referrers;
+      queryReferrers (path, referrers);
+      foreach (PathSet::iterator, i, referrers)
+        if (*i != path)
+          deletePathRecursive (state, *i);
+      size = queryPathInfo (path).narSize;
+      invalidatePathChecked (path);
     }
 
-    struct stat st;
-    if (lstat(path.c_str(), &st)) {
-        if (errno == ENOENT) return;
-        throw SysError(format("getting status of %1%") % path);
+  struct stat st;
+  if (lstat (path.c_str (), &st))
+    {
+      if (errno == ENOENT)
+        return;
+      throw SysError (format ("getting status of %1%") % path);
     }
 
-    if (state.options.maxFreed != ULLONG_MAX) {
-	auto freed = state.results.bytesFreed + state.bytesInvalidated;
-	double fraction = ((double) freed) / (double) state.options.maxFreed;
-	unsigned int percentage = (fraction > 1. ? 1. : fraction) * 100.;
-	printMsg(lvlInfo, format("[%1%%%] deleting '%2%'") % percentage % path);
-    } else {
-	auto freed = state.results.bytesFreed + state.bytesInvalidated;
-	freed /=  1024ULL * 1024ULL;
-	printMsg(lvlInfo, format("[%1% MiB] deleting '%2%'") % freed % path);
+  if (state.options.maxFreed != ULLONG_MAX)
+    {
+      auto freed = state.results.bytesFreed + state.bytesInvalidated;
+      double fraction = ((double)freed) / (double)state.options.maxFreed;
+      unsigned int percentage = (fraction > 1. ? 1. : fraction) * 100.;
+      printMsg (lvlInfo,
+                format ("[%1%%%] deleting '%2%'") % percentage % path);
+    }
+  else
+    {
+      auto freed = state.results.bytesFreed + state.bytesInvalidated;
+      freed /= 1024ULL * 1024ULL;
+      printMsg (lvlInfo, format ("[%1% MiB] deleting '%2%'") % freed % path);
     }
 
-    state.results.paths.insert(path);
-
-    /* If the path is not a regular file or symlink, move it to the
-       trash directory.  The move is to ensure that later (when we're
-       not holding the global GC lock) we can delete the path without
-       being afraid that the path has become alive again.  Otherwise
-       delete it right away. */
-    if (state.moveToTrash && S_ISDIR(st.st_mode)) {
-        // Estimate the amount freed using the narSize field.  FIXME:
-        // if the path was not valid, need to determine the actual
-        // size.
-        try {
-            if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1)
-                throw SysError(format("making `%1%' writable") % path);
-            Path tmp = state.trashDir + "/" + baseNameOf(path);
-            if (rename(path.c_str(), tmp.c_str()))
-                throw SysError(format("unable to rename `%1%' to `%2%'") % path % tmp);
-            state.bytesInvalidated += size;
-        } catch (SysError & e) {
-            /* In a Docker container, rename(2) returns EXDEV when the source
-               and destination are not both on the "top layer".  See:
-               https://bugs.gnu.org/41607 */
-            if (e.errNo == ENOSPC || e.errNo == EXDEV) {
-                printMsg(lvlInfo, format("note: can't create move `%1%': %2%") % path % e.msg());
-                deleteGarbage(state, path);
+  state.results.paths.insert (path);
+
+  /* If the path is not a regular file or symlink, move it to the
+     trash directory.  The move is to ensure that later (when we're
+     not holding the global GC lock) we can delete the path without
+     being afraid that the path has become alive again.  Otherwise
+     delete it right away. */
+  if (state.moveToTrash && S_ISDIR (st.st_mode))
+    {
+      // Estimate the amount freed using the narSize field.  FIXME:
+      // if the path was not valid, need to determine the actual
+      // size.
+      try
+        {
+          if (chmod (path.c_str (), st.st_mode | S_IWUSR) == -1)
+            throw SysError (format ("making `%1%' writable") % path);
+          Path tmp = state.trashDir + "/" + baseNameOf (path);
+          if (rename (path.c_str (), tmp.c_str ()))
+            throw SysError (format ("unable to rename `%1%' to `%2%'") % path
+                            % tmp);
+          state.bytesInvalidated += size;
+        }
+      catch (SysError &e)
+        {
+          /* In a Docker container, rename(2) returns EXDEV when the source
+             and destination are not both on the "top layer".  See:
+             https://bugs.gnu.org/41607 */
+          if (e.errNo == ENOSPC || e.errNo == EXDEV)
+            {
+              printMsg (lvlInfo, format ("note: can't create move `%1%': %2%")
+                                     % path % e.msg ());
+              deleteGarbage (state, path);
             }
         }
-    } else
-        deleteGarbage(state, path);
-
-    if (state.results.bytesFreed + state.bytesInvalidated > state.options.maxFreed) {
-        printMsg(lvlInfo, format("deleted or invalidated more than %1% bytes; stopping") % state.options.maxFreed);
-        throw GCLimitReached();
+    }
+  else
+    deleteGarbage (state, path);
+
+  if (state.results.bytesFreed + state.bytesInvalidated
+      > state.options.maxFreed)
+    {
+      printMsg (lvlInfo,
+                format ("deleted or invalidated more than %1% bytes; stopping")
+                    % state.options.maxFreed);
+      throw GCLimitReached ();
     }
 }
 
-
-bool LocalStore::canReachRoot(GCState & state, PathSet & visited, const Path & path)
+bool
+LocalStore::canReachRoot (GCState &state, PathSet &visited, const Path &path)
 {
-    if (visited.find(path) != visited.end()) return false;
+  if (visited.find (path) != visited.end ())
+    return false;
 
-    if (state.alive.find(path) != state.alive.end()) {
-        return true;
+  if (state.alive.find (path) != state.alive.end ())
+    {
+      return true;
     }
 
-    if (state.dead.find(path) != state.dead.end()) {
-        return false;
+  if (state.dead.find (path) != state.dead.end ())
+    {
+      return false;
     }
 
-    if (state.roots.find(path) != state.roots.end()) {
-        printMsg(lvlDebug, format("cannot delete `%1%' because it's a root") % path);
-        state.alive.insert(path);
-        return true;
+  if (state.roots.find (path) != state.roots.end ())
+    {
+      printMsg (lvlDebug,
+                format ("cannot delete `%1%' because it's a root") % path);
+      state.alive.insert (path);
+      return true;
     }
 
-    visited.insert(path);
+  visited.insert (path);
 
-    if (!isValidPath(path)) return false;
+  if (!isValidPath (path))
+    return false;
 
-    PathSet incoming;
+  PathSet incoming;
 
-    /* Don't delete this path if any of its referrers are alive. */
-    queryReferrers(path, incoming);
+  /* Don't delete this path if any of its referrers are alive. */
+  queryReferrers (path, incoming);
 
-    /* If gc-keep-derivations is set and this is a derivation, then
-       don't delete the derivation if any of the outputs are alive. */
-    if (state.gcKeepDerivations && isDerivation(path)) {
-        PathSet outputs = queryDerivationOutputs(path);
-        foreach (PathSet::iterator, i, outputs)
-            if (isValidPath(*i) && queryDeriver(*i) == path)
-                incoming.insert(*i);
+  /* If gc-keep-derivations is set and this is a derivation, then
+     don't delete the derivation if any of the outputs are alive. */
+  if (state.gcKeepDerivations && isDerivation (path))
+    {
+      PathSet outputs = queryDerivationOutputs (path);
+      foreach (PathSet::iterator, i, outputs)
+        if (isValidPath (*i) && queryDeriver (*i) == path)
+          incoming.insert (*i);
     }
 
-    /* If gc-keep-outputs is set, then don't delete this path if there
-       are derivers of this path that are not garbage. */
-    if (state.gcKeepOutputs) {
-        PathSet derivers = queryValidDerivers(path);
-        foreach (PathSet::iterator, i, derivers)
-            incoming.insert(*i);
+  /* If gc-keep-outputs is set, then don't delete this path if there
+     are derivers of this path that are not garbage. */
+  if (state.gcKeepOutputs)
+    {
+      PathSet derivers = queryValidDerivers (path);
+      foreach (PathSet::iterator, i, derivers)
+        incoming.insert (*i);
     }
 
-    foreach (PathSet::iterator, i, incoming)
-        if (*i != path)
-            if (canReachRoot(state, visited, *i)) {
-                state.alive.insert(path);
-                return true;
-            }
+  foreach (PathSet::iterator, i, incoming)
+    if (*i != path)
+      if (canReachRoot (state, visited, *i))
+        {
+          state.alive.insert (path);
+          return true;
+        }
 
-    return false;
+  return false;
 }
 
-
-void LocalStore::tryToDelete(GCState & state, const Path & path)
+void
+LocalStore::tryToDelete (GCState &state, const Path &path)
 {
-    checkInterrupt();
+  checkInterrupt ();
 
-    if (path == linksDir || path == state.trashDir) return;
+  if (path == linksDir || path == state.trashDir)
+    return;
 
-    startNest(nest, lvlDebug, format("considering whether to delete `%1%'") % path);
+  startNest (nest, lvlDebug,
+             format ("considering whether to delete `%1%'") % path);
 
-    if (!isValidPath(path)) {
-        /* A lock file belonging to a path that we're building right
-           now isn't garbage. */
-        if (isActiveTempFile(state, path, ".lock")) return;
+  if (!isValidPath (path))
+    {
+      /* A lock file belonging to a path that we're building right
+         now isn't garbage. */
+      if (isActiveTempFile (state, path, ".lock"))
+        return;
 
-        /* Don't delete .chroot directories for derivations that are
-           currently being built. */
-        if (isActiveTempFile(state, path, ".chroot")) return;
+      /* Don't delete .chroot directories for derivations that are
+         currently being built. */
+      if (isActiveTempFile (state, path, ".chroot"))
+        return;
     }
 
-    PathSet visited;
-
-    if (canReachRoot(state, visited, path)) {
-        printMsg(lvlDebug, format("cannot delete `%1%' because it's still reachable") % path);
-    } else {
-        /* No path we visited was a root, so everything is garbage.
-           But we only delete ‘path’ and its referrers here so that
-           ‘nix-store --delete’ doesn't have the unexpected effect of
-           recursing into derivations and outputs. */
-        state.dead.insert(visited.begin(), visited.end());
-        if (state.shouldDelete)
-            deletePathRecursive(state, path);
+  PathSet visited;
+
+  if (canReachRoot (state, visited, path))
+    {
+      printMsg (lvlDebug,
+                format ("cannot delete `%1%' because it's still reachable")
+                    % path);
+    }
+  else
+    {
+      /* No path we visited was a root, so everything is garbage.
+         But we only delete ‘path’ and its referrers here so that
+         ‘nix-store --delete’ doesn't have the unexpected effect of
+         recursing into derivations and outputs. */
+      state.dead.insert (visited.begin (), visited.end ());
+      if (state.shouldDelete)
+        deletePathRecursive (state, path);
     }
 }
 
-
 /* Unlink all files in /nix/store/.links that have a link count of 1,
    which indicates that there are no other links and so they can be
    safely deleted.  FIXME: race condition with optimisePath(): we
    might see a link count of 1 just before optimisePath() increases
    the link count. */
-void LocalStore::removeUnusedLinks(const GCState & state)
+void
+LocalStore::removeUnusedLinks (const GCState &state)
 {
-    AutoCloseDir dir = opendir(linksDir.c_str());
-    if (!dir) throw SysError(format("opening directory `%1%'") % linksDir);
+  AutoCloseDir dir = opendir (linksDir.c_str ());
+  if (!dir)
+    throw SysError (format ("opening directory `%1%'") % linksDir);
 
-    long long actualSize = 0, unsharedSize = 0;
+  long long actualSize = 0, unsharedSize = 0;
 
-    struct dirent * dirent;
-    while (errno = 0, dirent = readdir(dir)) {
-        checkInterrupt();
-        string name = dirent->d_name;
-        if (name == "." || name == "..") continue;
-        Path path = linksDir + "/" + name;
+  struct dirent *dirent;
+  while (errno = 0, dirent = readdir (dir))
+    {
+      checkInterrupt ();
+      string name = dirent->d_name;
+      if (name == "." || name == "..")
+        continue;
+      Path path = linksDir + "/" + name;
 
 #ifdef HAVE_STATX
-# define st_size stx_size
-# define st_nlink stx_nlink
-	static int statx_flags = AT_SYMLINK_NOFOLLOW | AT_STATX_DONT_SYNC;
-	struct statx st;
-
-	if (statx(AT_FDCWD, path.c_str(), statx_flags,
-		  STATX_SIZE | STATX_NLINK, &st) == -1) {
-	    if (errno == EINVAL) {
-		/* Old 3.10 kernels (CentOS 7) don't support
-		   AT_STATX_DONT_SYNC, so try again without it.  */
-		statx_flags &= ~AT_STATX_DONT_SYNC;
-		if (statx(AT_FDCWD, path.c_str(), statx_flags,
-			  STATX_SIZE | STATX_NLINK, &st) == -1)
-		    throw SysError(format("statting `%1%'") % path);
-	    } else {
-		throw SysError(format("statting `%1%'") % path);
-	    }
-	}
+#define st_size stx_size
+#define st_nlink stx_nlink
+      static int statx_flags = AT_SYMLINK_NOFOLLOW | AT_STATX_DONT_SYNC;
+      struct statx st;
+
+      if (statx (AT_FDCWD, path.c_str (), statx_flags,
+                 STATX_SIZE | STATX_NLINK, &st)
+          == -1)
+        {
+          if (errno == EINVAL)
+            {
+              /* Old 3.10 kernels (CentOS 7) don't support
+                 AT_STATX_DONT_SYNC, so try again without it.  */
+              statx_flags &= ~AT_STATX_DONT_SYNC;
+              if (statx (AT_FDCWD, path.c_str (), statx_flags,
+                         STATX_SIZE | STATX_NLINK, &st)
+                  == -1)
+                throw SysError (format ("statting `%1%'") % path);
+            }
+          else
+            {
+              throw SysError (format ("statting `%1%'") % path);
+            }
+        }
 #else
-        struct stat st;
-        if (lstat(path.c_str(), &st) == -1)
-            throw SysError(format("statting `%1%'") % path);
+      struct stat st;
+      if (lstat (path.c_str (), &st) == -1)
+        throw SysError (format ("statting `%1%'") % path);
 #endif
 
-	/* Drop links for files smaller than 'deduplicationMinSize', even if
-	   they have more than one hard link.  */
-        if (st.st_nlink != 1 && st.st_size >= deduplicationMinSize) {
-            actualSize += st.st_size;
-            unsharedSize += (st.st_nlink - 1) * st.st_size;
-            continue;
+      /* Drop links for files smaller than 'deduplicationMinSize', even if
+         they have more than one hard link.  */
+      if (st.st_nlink != 1 && st.st_size >= deduplicationMinSize)
+        {
+          actualSize += st.st_size;
+          unsharedSize += (st.st_nlink - 1) * st.st_size;
+          continue;
         }
 
-        printMsg(lvlTalkative, format("deleting unused link `%1%'") % path);
+      printMsg (lvlTalkative, format ("deleting unused link `%1%'") % path);
 
-        if (unlink(path.c_str()) == -1)
-            throw SysError(format("deleting `%1%'") % path);
+      if (unlink (path.c_str ()) == -1)
+        throw SysError (format ("deleting `%1%'") % path);
 
-        state.results.bytesFreed += st.st_size;
+      state.results.bytesFreed += st.st_size;
 #undef st_size
 #undef st_nlink
     }
 
-    struct stat st;
-    if (stat(linksDir.c_str(), &st) == -1)
-        throw SysError(format("statting `%1%'") % linksDir);
-    long long overhead = st.st_size;
+  struct stat st;
+  if (stat (linksDir.c_str (), &st) == -1)
+    throw SysError (format ("statting `%1%'") % linksDir);
+  long long overhead = st.st_size;
 
-    printMsg(lvlInfo, format("note: currently hard linking saves %.2f MiB")
-        % ((unsharedSize - actualSize - overhead) / (1024.0 * 1024.0)));
+  printMsg (lvlInfo, format ("note: currently hard linking saves %.2f MiB")
+                         % ((unsharedSize - actualSize - overhead)
+                            / (1024.0 * 1024.0)));
 }
 
-
-void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
+void
+LocalStore::collectGarbage (const GCOptions &options, GCResults &results)
 {
-    GCState state(results);
-    state.options = options;
-    state.trashDir = settings.nixStore + "/trash";
-    state.gcKeepOutputs = settings.gcKeepOutputs;
-    state.gcKeepDerivations = settings.gcKeepDerivations;
-
-    /* Using `--ignore-liveness' with `--delete' can have unintended
-       consequences if `gc-keep-outputs' or `gc-keep-derivations' are
-       true (the garbage collector will recurse into deleting the
-       outputs or derivers, respectively).  So disable them. */
-    if (options.action == GCOptions::gcDeleteSpecific && options.ignoreLiveness) {
-        state.gcKeepOutputs = false;
-        state.gcKeepDerivations = false;
+  GCState state (results);
+  state.options = options;
+  state.trashDir = settings.nixStore + "/trash";
+  state.gcKeepOutputs = settings.gcKeepOutputs;
+  state.gcKeepDerivations = settings.gcKeepDerivations;
+
+  /* Using `--ignore-liveness' with `--delete' can have unintended
+     consequences if `gc-keep-outputs' or `gc-keep-derivations' are
+     true (the garbage collector will recurse into deleting the
+     outputs or derivers, respectively).  So disable them. */
+  if (options.action == GCOptions::gcDeleteSpecific && options.ignoreLiveness)
+    {
+      state.gcKeepOutputs = false;
+      state.gcKeepDerivations = false;
     }
 
-    state.shouldDelete = options.action == GCOptions::gcDeleteDead || options.action == GCOptions::gcDeleteSpecific;
-
-    /* Acquire the global GC root.  This prevents
-       a) New roots from being added.
-       b) Processes from creating new temporary root files. */
-    AutoCloseFD fdGCLock = openGCLock(ltWrite);
-
-    /* Find the roots.  Since we've grabbed the GC lock, the set of
-       permanent roots cannot increase now. */
-    printMsg(lvlError, format("finding garbage collector roots..."));
-    Roots rootMap = options.ignoreLiveness ? Roots() : findRoots();
-
-    foreach (Roots::iterator, i, rootMap) state.roots.insert(i->second);
-
-    /* Add additional roots returned by 'guix gc --list-busy'.  This is
-       typically used to add running programs to the set of roots (to prevent
-       them from being garbage collected). */
-    if (!options.ignoreLiveness)
-        addAdditionalRoots(*this, state.roots);
-
-    /* Read the temporary roots.  This acquires read locks on all
-       per-process temporary root files.  So after this point no paths
-       can be added to the set of temporary roots. */
-    FDs fds;
-    readTempRoots(state.tempRoots, fds);
-    state.roots.insert(state.tempRoots.begin(), state.tempRoots.end());
-
-    /* After this point the set of roots or temporary roots cannot
-       increase, since we hold locks on everything.  So everything
-       that is not reachable from `roots' is garbage. */
-
-    if (state.shouldDelete) {
-        if (pathExists(state.trashDir)) deleteGarbage(state, state.trashDir);
-        try {
-            createDirs(state.trashDir);
-        } catch (SysError & e) {
-            if (e.errNo == ENOSPC) {
-                printMsg(lvlInfo, format("note: can't create trash directory: %1%") % e.msg());
-                state.moveToTrash = false;
+  state.shouldDelete = options.action == GCOptions::gcDeleteDead
+                       || options.action == GCOptions::gcDeleteSpecific;
+
+  /* Acquire the global GC root.  This prevents
+     a) New roots from being added.
+     b) Processes from creating new temporary root files. */
+  AutoCloseFD fdGCLock = openGCLock (ltWrite);
+
+  /* Find the roots.  Since we've grabbed the GC lock, the set of
+     permanent roots cannot increase now. */
+  printMsg (lvlError, format ("finding garbage collector roots..."));
+  Roots rootMap = options.ignoreLiveness ? Roots () : findRoots ();
+
+  foreach (Roots::iterator, i, rootMap)
+    state.roots.insert (i->second);
+
+  /* Add additional roots returned by 'guix gc --list-busy'.  This is
+     typically used to add running programs to the set of roots (to prevent
+     them from being garbage collected). */
+  if (!options.ignoreLiveness)
+    addAdditionalRoots (*this, state.roots);
+
+  /* Read the temporary roots.  This acquires read locks on all
+     per-process temporary root files.  So after this point no paths
+     can be added to the set of temporary roots. */
+  FDs fds;
+  readTempRoots (state.tempRoots, fds);
+  state.roots.insert (state.tempRoots.begin (), state.tempRoots.end ());
+
+  /* After this point the set of roots or temporary roots cannot
+     increase, since we hold locks on everything.  So everything
+     that is not reachable from `roots' is garbage. */
+
+  if (state.shouldDelete)
+    {
+      if (pathExists (state.trashDir))
+        deleteGarbage (state, state.trashDir);
+      try
+        {
+          createDirs (state.trashDir);
+        }
+      catch (SysError &e)
+        {
+          if (e.errNo == ENOSPC)
+            {
+              printMsg (lvlInfo,
+                        format ("note: can't create trash directory: %1%")
+                            % e.msg ());
+              state.moveToTrash = false;
             }
         }
     }
 
-    /* Now either delete all garbage paths, or just the specified
-       paths (for gcDeleteSpecific). */
+  /* Now either delete all garbage paths, or just the specified
+     paths (for gcDeleteSpecific). */
 
-    if (options.action == GCOptions::gcDeleteSpecific) {
+  if (options.action == GCOptions::gcDeleteSpecific)
+    {
 
-        foreach (PathSet::iterator, i, options.pathsToDelete) {
-            assertStorePath(*i);
-            tryToDelete(state, *i);
-            if (state.dead.find(*i) == state.dead.end())
-                throw Error(format("cannot delete path `%1%' since it is still alive") % *i);
+      foreach (PathSet::iterator, i, options.pathsToDelete)
+        {
+          assertStorePath (*i);
+          tryToDelete (state, *i);
+          if (state.dead.find (*i) == state.dead.end ())
+            throw Error (
+                format ("cannot delete path `%1%' since it is still alive")
+                % *i);
         }
-
-    } else if (options.maxFreed > 0) {
-
-        if (state.shouldDelete)
-            printMsg(lvlError, format("deleting garbage..."));
-        else
-            printMsg(lvlError, format("determining live/dead paths..."));
-
-        try {
-
-            AutoCloseDir dir = opendir(settings.nixStore.c_str());
-            if (!dir) throw SysError(format("opening directory `%1%'") % settings.nixStore);
-
-            /* Read the store and immediately delete all paths that
-               aren't valid.  When using --max-freed etc., deleting
-               invalid paths is preferred over deleting unreachable
-               paths, since unreachable paths could become reachable
-               again.  We don't use readDirectory() here so that GCing
-               can start faster. */
-            Paths entries;
-            struct dirent * dirent;
-            while (errno = 0, dirent = readdir(dir)) {
-                checkInterrupt();
-                string name = dirent->d_name;
-                if (name == "." || name == "..") continue;
-                Path path = settings.nixStore + "/" + name;
-                if (isValidPath(path))
-                    entries.push_back(path);
-                else
-                    tryToDelete(state, path);
+    }
+  else if (options.maxFreed > 0)
+    {
+
+      if (state.shouldDelete)
+        printMsg (lvlError, format ("deleting garbage..."));
+      else
+        printMsg (lvlError, format ("determining live/dead paths..."));
+
+      try
+        {
+
+          AutoCloseDir dir = opendir (settings.nixStore.c_str ());
+          if (!dir)
+            throw SysError (format ("opening directory `%1%'")
+                            % settings.nixStore);
+
+          /* Read the store and immediately delete all paths that
+             aren't valid.  When using --max-freed etc., deleting
+             invalid paths is preferred over deleting unreachable
+             paths, since unreachable paths could become reachable
+             again.  We don't use readDirectory() here so that GCing
+             can start faster. */
+          Paths entries;
+          struct dirent *dirent;
+          while (errno = 0, dirent = readdir (dir))
+            {
+              checkInterrupt ();
+              string name = dirent->d_name;
+              if (name == "." || name == "..")
+                continue;
+              Path path = settings.nixStore + "/" + name;
+              if (isValidPath (path))
+                entries.push_back (path);
+              else
+                tryToDelete (state, path);
             }
 
-            dir.close();
-
-            /* Now delete the unreachable valid paths.  Randomise the
-               order in which we delete entries to make the collector
-               less biased towards deleting paths that come
-               alphabetically first (e.g. /nix/store/000...).  This
-               matters when using --max-freed etc. */
-            vector<Path> entries_(entries.begin(), entries.end());
-            random_shuffle(entries_.begin(), entries_.end());
+          dir.close ();
 
-            foreach (vector<Path>::iterator, i, entries_)
-                tryToDelete(state, *i);
+          /* Now delete the unreachable valid paths.  Randomise the
+             order in which we delete entries to make the collector
+             less biased towards deleting paths that come
+             alphabetically first (e.g. /nix/store/000...).  This
+             matters when using --max-freed etc. */
+          vector<Path> entries_ (entries.begin (), entries.end ());
+          random_shuffle (entries_.begin (), entries_.end ());
 
-        } catch (GCLimitReached & e) {
+          foreach (vector<Path>::iterator, i, entries_)
+            tryToDelete (state, *i);
+        }
+      catch (GCLimitReached &e)
+        {
         }
     }
 
-    if (state.options.action == GCOptions::gcReturnLive) {
-        state.results.paths = state.alive;
-        return;
+  if (state.options.action == GCOptions::gcReturnLive)
+    {
+      state.results.paths = state.alive;
+      return;
     }
 
-    if (state.options.action == GCOptions::gcReturnDead) {
-        state.results.paths = state.dead;
-        return;
+  if (state.options.action == GCOptions::gcReturnDead)
+    {
+      state.results.paths = state.dead;
+      return;
     }
 
-    /* Allow other processes to add to the store from here on. */
-    fdGCLock.close();
-    fds.clear();
+  /* Allow other processes to add to the store from here on. */
+  fdGCLock.close ();
+  fds.clear ();
 
-    /* Delete the trash directory. */
-    printMsg(lvlInfo, format("deleting `%1%'") % state.trashDir);
-    deleteGarbage(state, state.trashDir);
+  /* Delete the trash directory. */
+  printMsg (lvlInfo, format ("deleting `%1%'") % state.trashDir);
+  deleteGarbage (state, state.trashDir);
 
-    /* Clean up the links directory. */
-    if (options.action == GCOptions::gcDeleteDead || options.action == GCOptions::gcDeleteSpecific) {
-        printMsg(lvlError, format("deleting unused links..."));
-        removeUnusedLinks(state);
+  /* Clean up the links directory. */
+  if (options.action == GCOptions::gcDeleteDead
+      || options.action == GCOptions::gcDeleteSpecific)
+    {
+      printMsg (lvlError, format ("deleting unused links..."));
+      removeUnusedLinks (state);
     }
 
-    /* While we're at it, vacuum the database. */
-    //if (options.action == GCOptions::gcDeleteDead) vacuumDB();
+  /* While we're at it, vacuum the database. */
+  // if (options.action == GCOptions::gcDeleteDead) vacuumDB();
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libstore/globals.cc b/nix/libstore/globals.cc
index 89add1f107..849c1e0b1b 100644
--- a/nix/libstore/globals.cc
+++ b/nix/libstore/globals.cc
@@ -1,15 +1,14 @@
 #include "config.h"
 
+#include "archive.hh"
 #include "globals.hh"
 #include "util.hh"
-#include "archive.hh"
 
-#include <map>
 #include <algorithm>
+#include <map>
 
-
-namespace nix {
-
+namespace nix
+{
 
 /* The default location of the daemon socket, relative to nixStateDir.
    The socket is in a directory to allow you to control access to the
@@ -18,194 +17,212 @@ namespace nix {
    must be deleted and recreated on startup.) */
 #define DEFAULT_SOCKET_PATH "/daemon-socket/socket"
 
-
 Settings settings;
 
-
-Settings::Settings()
-{
-    keepFailed = false;
-    keepGoing = false;
-    tryFallback = false;
-    buildVerbosity = lvlError;
-    maxBuildJobs = 1;
-    buildCores = 1;
-    readOnlyMode = false;
-    thisSystem = SYSTEM;
-    maxSilentTime = 3600;
-    buildTimeout = 3600 * 24;
-    useBuildHook = true;
-    printBuildTrace = false;
-    multiplexedBuildOutput = false;
-    reservedSize = 8 * 1024 * 1024;
-    fsyncMetadata = true;
-    useSQLiteWAL = true;
-    syncBeforeRegistering = false;
-    useSubstitutes = true;
-    useChroot = false;
-    impersonateLinux26 = false;
-    keepLog = true;
-    logCompression = COMPRESSION_GZIP;
-    maxLogSize = 0;
-    cacheFailure = false;
-    pollInterval = 5;
-    checkRootReachability = false;
-    gcKeepOutputs = false;
-    gcKeepDerivations = true;
-    autoOptimiseStore = false;
-    envKeepDerivations = false;
-    lockCPU = getEnv("NIX_AFFINITY_HACK", "1") == "1";
-    showTrace = false;
-}
-
-
-void Settings::processEnvironment()
+Settings::Settings ()
 {
-    nixStore = canonPath(getEnv("NIX_STORE_DIR", getEnv("NIX_STORE", NIX_STORE_DIR)));
-    nixLogDir = canonPath(getEnv("GUIX_LOG_DIRECTORY", NIX_LOG_DIR));
-    nixStateDir = canonPath(getEnv("GUIX_STATE_DIRECTORY", NIX_STATE_DIR));
-    nixDBPath = getEnv("GUIX_DATABASE_DIRECTORY", nixStateDir + "/db");
-    nixConfDir = canonPath(getEnv("GUIX_CONFIGURATION_DIRECTORY", GUIX_CONFIGURATION_DIRECTORY));
-    nixBinDir = canonPath(getEnv("NIX_BIN_DIR", NIX_BIN_DIR));
-    nixDaemonSocketFile = canonPath(nixStateDir + DEFAULT_SOCKET_PATH);
-    guixProgram = canonPath(getEnv("GUIX", nixBinDir + "/guix"));
-}
-
-
-void Settings::set(const string & name, const string & value)
+  keepFailed = false;
+  keepGoing = false;
+  tryFallback = false;
+  buildVerbosity = lvlError;
+  maxBuildJobs = 1;
+  buildCores = 1;
+  readOnlyMode = false;
+  thisSystem = SYSTEM;
+  maxSilentTime = 3600;
+  buildTimeout = 3600 * 24;
+  useBuildHook = true;
+  printBuildTrace = false;
+  multiplexedBuildOutput = false;
+  reservedSize = 8 * 1024 * 1024;
+  fsyncMetadata = true;
+  useSQLiteWAL = true;
+  syncBeforeRegistering = false;
+  useSubstitutes = true;
+  useChroot = false;
+  impersonateLinux26 = false;
+  keepLog = true;
+  logCompression = COMPRESSION_GZIP;
+  maxLogSize = 0;
+  cacheFailure = false;
+  pollInterval = 5;
+  checkRootReachability = false;
+  gcKeepOutputs = false;
+  gcKeepDerivations = true;
+  autoOptimiseStore = false;
+  envKeepDerivations = false;
+  lockCPU = getEnv ("NIX_AFFINITY_HACK", "1") == "1";
+  showTrace = false;
+}
+
+void
+Settings::processEnvironment ()
 {
-    settings[name] = value;
-    overrides[name] = value;
-}
-
-
-string Settings::get(const string & name, const string & def)
+  nixStore = canonPath (
+      getEnv ("NIX_STORE_DIR", getEnv ("NIX_STORE", NIX_STORE_DIR)));
+  nixLogDir = canonPath (getEnv ("GUIX_LOG_DIRECTORY", NIX_LOG_DIR));
+  nixStateDir = canonPath (getEnv ("GUIX_STATE_DIRECTORY", NIX_STATE_DIR));
+  nixDBPath = getEnv ("GUIX_DATABASE_DIRECTORY", nixStateDir + "/db");
+  nixConfDir = canonPath (
+      getEnv ("GUIX_CONFIGURATION_DIRECTORY", GUIX_CONFIGURATION_DIRECTORY));
+  nixBinDir = canonPath (getEnv ("NIX_BIN_DIR", NIX_BIN_DIR));
+  nixDaemonSocketFile = canonPath (nixStateDir + DEFAULT_SOCKET_PATH);
+  guixProgram = canonPath (getEnv ("GUIX", nixBinDir + "/guix"));
+}
+
+void
+Settings::set (const string &name, const string &value)
 {
-    auto i = settings.find(name);
-    if (i == settings.end()) return def;
-    return i->second;
+  settings[name] = value;
+  overrides[name] = value;
 }
 
-
-Strings Settings::get(const string & name, const Strings & def)
+string
+Settings::get (const string &name, const string &def)
 {
-    auto i = settings.find(name);
-    if (i == settings.end()) return def;
-    return tokenizeString<Strings>(i->second);
+  auto i = settings.find (name);
+  if (i == settings.end ())
+    return def;
+  return i->second;
 }
 
-
-bool Settings::get(const string & name, bool def)
+Strings
+Settings::get (const string &name, const Strings &def)
 {
-    bool res = def;
-    _get(res, name);
-    return res;
+  auto i = settings.find (name);
+  if (i == settings.end ())
+    return def;
+  return tokenizeString<Strings> (i->second);
 }
 
-int Settings::get(const string & name, int def)
+bool
+Settings::get (const string &name, bool def)
 {
-    int res = def;
-    _get(res, name);
-    return res;
+  bool res = def;
+  _get (res, name);
+  return res;
 }
 
-
-void Settings::update()
-{
-    _get(tryFallback, "build-fallback");
-    _get(maxBuildJobs, "build-max-jobs");
-    _get(buildCores, "build-cores");
-    _get(thisSystem, "system");
-    _get(multiplexedBuildOutput, "multiplexed-build-output");
-    _get(maxSilentTime, "build-max-silent-time");
-    _get(buildTimeout, "build-timeout");
-    _get(reservedSize, "gc-reserved-space");
-    _get(fsyncMetadata, "fsync-metadata");
-    _get(useSQLiteWAL, "use-sqlite-wal");
-    _get(syncBeforeRegistering, "sync-before-registering");
-    _get(useSubstitutes, "build-use-substitutes");
-    _get(buildUsersGroup, "build-users-group");
-    _get(useChroot, "build-use-chroot");
-    _get(impersonateLinux26, "build-impersonate-linux-26");
-    _get(keepLog, "build-keep-log");
-    // _get(logCompression, "build-log-compression");
-    _get(maxLogSize, "build-max-log-size");
-    _get(cacheFailure, "build-cache-failure");
-    _get(pollInterval, "build-poll-interval");
-    _get(checkRootReachability, "gc-check-reachability");
-    _get(gcKeepOutputs, "gc-keep-outputs");
-    _get(gcKeepDerivations, "gc-keep-derivations");
-    _get(autoOptimiseStore, "auto-optimise-store");
-    _get(envKeepDerivations, "env-keep-derivations");
-}
-
-
-void Settings::_get(string & res, const string & name)
+int
+Settings::get (const string &name, int def)
 {
-    SettingsMap::iterator i = settings.find(name);
-    if (i == settings.end()) return;
-    res = i->second;
+  int res = def;
+  _get (res, name);
+  return res;
 }
 
-
-void Settings::_get(bool & res, const string & name)
+void
+Settings::update ()
+{
+  _get (tryFallback, "build-fallback");
+  _get (maxBuildJobs, "build-max-jobs");
+  _get (buildCores, "build-cores");
+  _get (thisSystem, "system");
+  _get (multiplexedBuildOutput, "multiplexed-build-output");
+  _get (maxSilentTime, "build-max-silent-time");
+  _get (buildTimeout, "build-timeout");
+  _get (reservedSize, "gc-reserved-space");
+  _get (fsyncMetadata, "fsync-metadata");
+  _get (useSQLiteWAL, "use-sqlite-wal");
+  _get (syncBeforeRegistering, "sync-before-registering");
+  _get (useSubstitutes, "build-use-substitutes");
+  _get (buildUsersGroup, "build-users-group");
+  _get (useChroot, "build-use-chroot");
+  _get (impersonateLinux26, "build-impersonate-linux-26");
+  _get (keepLog, "build-keep-log");
+  // _get(logCompression, "build-log-compression");
+  _get (maxLogSize, "build-max-log-size");
+  _get (cacheFailure, "build-cache-failure");
+  _get (pollInterval, "build-poll-interval");
+  _get (checkRootReachability, "gc-check-reachability");
+  _get (gcKeepOutputs, "gc-keep-outputs");
+  _get (gcKeepDerivations, "gc-keep-derivations");
+  _get (autoOptimiseStore, "auto-optimise-store");
+  _get (envKeepDerivations, "env-keep-derivations");
+}
+
+void
+Settings::_get (string &res, const string &name)
 {
-    SettingsMap::iterator i = settings.find(name);
-    if (i == settings.end()) return;
-    if (i->second == "true") res = true;
-    else if (i->second == "false") res = false;
-    else throw Error(format("configuration option `%1%' should be either `true' or `false', not `%2%'")
-        % name % i->second);
+  SettingsMap::iterator i = settings.find (name);
+  if (i == settings.end ())
+    return;
+  res = i->second;
 }
 
-
-void Settings::_get(StringSet & res, const string & name)
+void
+Settings::_get (bool &res, const string &name)
+{
+  SettingsMap::iterator i = settings.find (name);
+  if (i == settings.end ())
+    return;
+  if (i->second == "true")
+    res = true;
+  else if (i->second == "false")
+    res = false;
+  else
+    throw Error (format ("configuration option `%1%' should be either `true' "
+                         "or `false', not `%2%'")
+                 % name % i->second);
+}
+
+void
+Settings::_get (StringSet &res, const string &name)
 {
-    SettingsMap::iterator i = settings.find(name);
-    if (i == settings.end()) return;
-    res.clear();
-    Strings ss = tokenizeString<Strings>(i->second);
-    res.insert(ss.begin(), ss.end());
+  SettingsMap::iterator i = settings.find (name);
+  if (i == settings.end ())
+    return;
+  res.clear ();
+  Strings ss = tokenizeString<Strings> (i->second);
+  res.insert (ss.begin (), ss.end ());
 }
 
-void Settings::_get(Strings & res, const string & name)
+void
+Settings::_get (Strings &res, const string &name)
 {
-    SettingsMap::iterator i = settings.find(name);
-    if (i == settings.end()) return;
-    res = tokenizeString<Strings>(i->second);
+  SettingsMap::iterator i = settings.find (name);
+  if (i == settings.end ())
+    return;
+  res = tokenizeString<Strings> (i->second);
 }
 
-
-template<class N> void Settings::_get(N & res, const string & name)
+template <class N>
+void
+Settings::_get (N &res, const string &name)
 {
-    SettingsMap::iterator i = settings.find(name);
-    if (i == settings.end()) return;
-    if (!string2Int(i->second, res))
-        throw Error(format("configuration setting `%1%' should have an integer value") % name);
+  SettingsMap::iterator i = settings.find (name);
+  if (i == settings.end ())
+    return;
+  if (!string2Int (i->second, res))
+    throw Error (
+        format ("configuration setting `%1%' should have an integer value")
+        % name);
 }
 
-
-string Settings::pack()
+string
+Settings::pack ()
 {
-    string s;
-    foreach (SettingsMap::iterator, i, settings) {
-        if (i->first.find('\n') != string::npos ||
-            i->first.find('=') != string::npos ||
-            i->second.find('\n') != string::npos)
-            throw Error("invalid option name/value");
-        s += i->first; s += '='; s += i->second; s += '\n';
+  string s;
+  foreach (SettingsMap::iterator, i, settings)
+    {
+      if (i->first.find ('\n') != string::npos
+          || i->first.find ('=') != string::npos
+          || i->second.find ('\n') != string::npos)
+        throw Error ("invalid option name/value");
+      s += i->first;
+      s += '=';
+      s += i->second;
+      s += '\n';
     }
-    return s;
+  return s;
 }
 
-
-Settings::SettingsMap Settings::getOverrides()
+Settings::SettingsMap
+Settings::getOverrides ()
 {
-    return overrides;
+  return overrides;
 }
 
-
 const string nixVersion = PACKAGE_VERSION;
 
-
-}
+} // namespace nix
diff --git a/nix/libstore/globals.hh b/nix/libstore/globals.hh
index 27616a2283..4ba1d8a67e 100644
--- a/nix/libstore/globals.hh
+++ b/nix/libstore/globals.hh
@@ -6,222 +6,221 @@
 #include <map>
 #include <sys/types.h>
 
-
-namespace nix {
+namespace nix
+{
 
 enum CompressionType
 {
-    COMPRESSION_NONE = 0,
-    COMPRESSION_GZIP = 1
+  COMPRESSION_NONE = 0,
+  COMPRESSION_GZIP = 1
 #if HAVE_BZLIB_H
-    , COMPRESSION_BZIP2 = 2
+  ,
+  COMPRESSION_BZIP2 = 2
 #endif
 };
 
-struct Settings {
+struct Settings
+{
 
-    typedef std::map<string, string> SettingsMap;
+  typedef std::map<string, string> SettingsMap;
 
-    Settings();
+  Settings ();
 
-    void processEnvironment();
+  void processEnvironment ();
 
-    void set(const string & name, const string & value);
+  void set (const string &name, const string &value);
 
-    string get(const string & name, const string & def);
+  string get (const string &name, const string &def);
 
-    Strings get(const string & name, const Strings & def);
+  Strings get (const string &name, const Strings &def);
 
-    bool get(const string & name, bool def);
+  bool get (const string &name, bool def);
 
-    int get(const string & name, int def);
+  int get (const string &name, int def);
 
-    void update();
+  void update ();
 
-    string pack();
+  string pack ();
 
-    SettingsMap getOverrides();
+  SettingsMap getOverrides ();
 
-    /* The directory where we store sources and derived files. */
-    Path nixStore;
+  /* The directory where we store sources and derived files. */
+  Path nixStore;
 
-    /* The directory where we log various operations. */
-    Path nixLogDir;
+  /* The directory where we log various operations. */
+  Path nixLogDir;
 
-    /* The directory where state is stored. */
-    Path nixStateDir;
+  /* The directory where state is stored. */
+  Path nixStateDir;
 
-    /* The directory where we keep the SQLite database. */
-    Path nixDBPath;
+  /* The directory where we keep the SQLite database. */
+  Path nixDBPath;
 
-    /* The directory where configuration files are stored. */
-    Path nixConfDir;
+  /* The directory where configuration files are stored. */
+  Path nixConfDir;
 
-    /* The directory where the main programs are stored. */
-    Path nixBinDir;
+  /* The directory where the main programs are stored. */
+  Path nixBinDir;
 
-    /* File name of the socket the daemon listens to.  */
-    Path nixDaemonSocketFile;
+  /* File name of the socket the daemon listens to.  */
+  Path nixDaemonSocketFile;
 
-    /* Absolute file name of the 'guix' program.  */
-    Path guixProgram;
+  /* Absolute file name of the 'guix' program.  */
+  Path guixProgram;
 
-    /* Whether to keep temporary directories of failed builds. */
-    bool keepFailed;
+  /* Whether to keep temporary directories of failed builds. */
+  bool keepFailed;
 
-    /* Whether to keep building subgoals when a sibling (another
-       subgoal of the same goal) fails. */
-    bool keepGoing;
+  /* Whether to keep building subgoals when a sibling (another
+     subgoal of the same goal) fails. */
+  bool keepGoing;
 
-    /* User and groud id of the client issuing the build request.  Used to set
-       the owner and group of the kept temporary directories of failed
-       builds. */
-    uid_t clientUid;
-    gid_t clientGid;
+  /* User and groud id of the client issuing the build request.  Used to set
+     the owner and group of the kept temporary directories of failed
+     builds. */
+  uid_t clientUid;
+  gid_t clientGid;
 
-    /* Whether, if we cannot realise the known closure corresponding
-       to a derivation, we should try to normalise the derivation
-       instead. */
-    bool tryFallback;
+  /* Whether, if we cannot realise the known closure corresponding
+     to a derivation, we should try to normalise the derivation
+     instead. */
+  bool tryFallback;
 
-    /* Verbosity level for build output. */
-    Verbosity buildVerbosity;
+  /* Verbosity level for build output. */
+  Verbosity buildVerbosity;
 
-    /* Maximum number of parallel build jobs.  0 means unlimited. */
-    unsigned int maxBuildJobs;
+  /* Maximum number of parallel build jobs.  0 means unlimited. */
+  unsigned int maxBuildJobs;
 
-    /* Number of CPU cores to utilize in parallel within a build,
-       i.e. by passing this number to Make via '-j'. 0 means that the
-       number of actual CPU cores on the local host ought to be
-       auto-detected. */
-    unsigned int buildCores;
+  /* Number of CPU cores to utilize in parallel within a build,
+     i.e. by passing this number to Make via '-j'. 0 means that the
+     number of actual CPU cores on the local host ought to be
+     auto-detected. */
+  unsigned int buildCores;
 
-    /* Read-only mode.  Don't copy stuff to the store, don't change
-       the database. */
-    bool readOnlyMode;
+  /* Read-only mode.  Don't copy stuff to the store, don't change
+     the database. */
+  bool readOnlyMode;
 
-    /* The canonical system name, as returned by config.guess. */
-    string thisSystem;
+  /* The canonical system name, as returned by config.guess. */
+  string thisSystem;
 
-    /* The maximum time in seconds that a builer can go without
-       producing any output on stdout/stderr before it is killed.  0
-       means infinity. */
-    time_t maxSilentTime;
+  /* The maximum time in seconds that a builer can go without
+     producing any output on stdout/stderr before it is killed.  0
+     means infinity. */
+  time_t maxSilentTime;
 
-    /* The maximum duration in seconds that a builder can run.  0
-       means infinity.  */
-    time_t buildTimeout;
+  /* The maximum duration in seconds that a builder can run.  0
+     means infinity.  */
+  time_t buildTimeout;
 
-    /* Whether to use build hooks (for distributed builds).  Sometimes
-       users want to disable this from the command-line. */
-    bool useBuildHook;
+  /* Whether to use build hooks (for distributed builds).  Sometimes
+     users want to disable this from the command-line. */
+  bool useBuildHook;
 
-    /* Whether buildDerivations() should print out lines on stderr in
-       a fixed format to allow its progress to be monitored.  Each
-       line starts with a "@".  The following are defined:
+  /* Whether buildDerivations() should print out lines on stderr in
+     a fixed format to allow its progress to be monitored.  Each
+     line starts with a "@".  The following are defined:
 
-       @ build-started <drvpath> <outpath> <system> <logfile> <pid>
-       @ build-failed <drvpath> <outpath> <exitcode> <error text>
-       @ build-succeeded <drvpath> <outpath>
-       @ substituter-started <outpath> <substituter>
-       @ substituter-failed <outpath> <exitcode> <error text>
-       @ substituter-succeeded <outpath>
+     @ build-started <drvpath> <outpath> <system> <logfile> <pid>
+     @ build-failed <drvpath> <outpath> <exitcode> <error text>
+     @ build-succeeded <drvpath> <outpath>
+     @ substituter-started <outpath> <substituter>
+     @ substituter-failed <outpath> <exitcode> <error text>
+     @ substituter-succeeded <outpath>
 
-       Best combined with --no-build-output, otherwise stderr might
-       conceivably contain lines in this format printed by the
-       builders. */
-    bool printBuildTrace;
+     Best combined with --no-build-output, otherwise stderr might
+     conceivably contain lines in this format printed by the
+     builders. */
+  bool printBuildTrace;
 
-    /* When true, 'buildDerivations' prefixes lines coming from builders so
-       that clients know exactly which line comes from which builder, and
-       which line comes from the daemon itself.  The prefix for data coming
-       from builders is "log:PID:LEN:DATA" where PID uniquely identifies the
-       builder (PID is given in "build-started" traces.)  */
-    bool multiplexedBuildOutput;
+  /* When true, 'buildDerivations' prefixes lines coming from builders so
+     that clients know exactly which line comes from which builder, and
+     which line comes from the daemon itself.  The prefix for data coming
+     from builders is "log:PID:LEN:DATA" where PID uniquely identifies the
+     builder (PID is given in "build-started" traces.)  */
+  bool multiplexedBuildOutput;
 
-    /* Amount of reserved space for the garbage collector
-       (/nix/var/nix/db/reserved). */
-    off_t reservedSize;
+  /* Amount of reserved space for the garbage collector
+     (/nix/var/nix/db/reserved). */
+  off_t reservedSize;
 
-    /* Whether SQLite should use fsync. */
-    bool fsyncMetadata;
+  /* Whether SQLite should use fsync. */
+  bool fsyncMetadata;
 
-    /* Whether SQLite should use WAL mode. */
-    bool useSQLiteWAL;
+  /* Whether SQLite should use WAL mode. */
+  bool useSQLiteWAL;
 
-    /* Whether to call sync() before registering a path as valid. */
-    bool syncBeforeRegistering;
+  /* Whether to call sync() before registering a path as valid. */
+  bool syncBeforeRegistering;
 
-    /* Whether to use substitutes. */
-    bool useSubstitutes;
+  /* Whether to use substitutes. */
+  bool useSubstitutes;
 
-    /* The Unix group that contains the build users. */
-    string buildUsersGroup;
+  /* The Unix group that contains the build users. */
+  string buildUsersGroup;
 
-    /* Whether to build in chroot. */
-    bool useChroot;
+  /* Whether to build in chroot. */
+  bool useChroot;
 
-    /* Whether to impersonate a Linux 2.6 machine on newer kernels. */
-    bool impersonateLinux26;
+  /* Whether to impersonate a Linux 2.6 machine on newer kernels. */
+  bool impersonateLinux26;
 
-    /* Whether to store build logs. */
-    bool keepLog;
+  /* Whether to store build logs. */
+  bool keepLog;
 
-    /* Whether to compress logs. */
-    enum CompressionType logCompression;
+  /* Whether to compress logs. */
+  enum CompressionType logCompression;
 
-    /* Maximum number of bytes a builder can write to stdout/stderr
-       before being killed (0 means no limit). */
-    unsigned long maxLogSize;
+  /* Maximum number of bytes a builder can write to stdout/stderr
+     before being killed (0 means no limit). */
+  unsigned long maxLogSize;
 
-    /* Whether to cache build failures. */
-    bool cacheFailure;
+  /* Whether to cache build failures. */
+  bool cacheFailure;
 
-    /* How often (in seconds) to poll for locks. */
-    unsigned int pollInterval;
+  /* How often (in seconds) to poll for locks. */
+  unsigned int pollInterval;
 
-    /* Whether to check if new GC roots can in fact be found by the
-       garbage collector. */
-    bool checkRootReachability;
+  /* Whether to check if new GC roots can in fact be found by the
+     garbage collector. */
+  bool checkRootReachability;
 
-    /* Whether the garbage collector should keep outputs of live
-       derivations. */
-    bool gcKeepOutputs;
+  /* Whether the garbage collector should keep outputs of live
+     derivations. */
+  bool gcKeepOutputs;
 
-    /* Whether the garbage collector should keep derivers of live
-       paths. */
-    bool gcKeepDerivations;
+  /* Whether the garbage collector should keep derivers of live
+     paths. */
+  bool gcKeepDerivations;
 
-    /* Whether to automatically replace files with identical contents
-       with hard links. */
-    bool autoOptimiseStore;
+  /* Whether to automatically replace files with identical contents
+     with hard links. */
+  bool autoOptimiseStore;
 
-    /* Whether to add derivations as a dependency of user environments
-       (to prevent them from being GCed). */
-    bool envKeepDerivations;
+  /* Whether to add derivations as a dependency of user environments
+     (to prevent them from being GCed). */
+  bool envKeepDerivations;
 
-    /* Whether to lock the Nix client and worker to the same CPU. */
-    bool lockCPU;
+  /* Whether to lock the Nix client and worker to the same CPU. */
+  bool lockCPU;
 
-    /* Whether to show a stack trace if Nix evaluation fails. */
-    bool showTrace;
+  /* Whether to show a stack trace if Nix evaluation fails. */
+  bool showTrace;
 
 private:
-    SettingsMap settings, overrides;
+  SettingsMap settings, overrides;
 
-    void _get(string & res, const string & name);
-    void _get(bool & res, const string & name);
-    void _get(StringSet & res, const string & name);
-    void _get(Strings & res, const string & name);
-    template<class N> void _get(N & res, const string & name);
+  void _get (string &res, const string &name);
+  void _get (bool &res, const string &name);
+  void _get (StringSet &res, const string &name);
+  void _get (Strings &res, const string &name);
+  template <class N> void _get (N &res, const string &name);
 };
 
-
 // FIXME: don't use a global variable.
 extern Settings settings;
 
-
 extern const string nixVersion;
 
-
 }
diff --git a/nix/libstore/local-store.cc b/nix/libstore/local-store.cc
index f6540c2117..07c543b2f4 100644
--- a/nix/libstore/local-store.cc
+++ b/nix/libstore/local-store.cc
@@ -1,1633 +1,1845 @@
-#include "config.h"
 #include "local-store.hh"
-#include "globals.hh"
+#include "affinity.hh"
 #include "archive.hh"
+#include "config.h"
+#include "derivations.hh"
+#include "globals.hh"
 #include "pathlocks.hh"
 #include "worker-protocol.hh"
-#include "derivations.hh"
-#include "affinity.hh"
 
-#include <iostream>
 #include <algorithm>
 #include <cstring>
+#include <iostream>
 
-#include <sys/types.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <grp.h>
+#include <stdio.h>
 #include <sys/stat.h>
 #include <sys/time.h>
+#include <sys/types.h>
+#include <time.h>
 #include <unistd.h>
 #include <utime.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <stdio.h>
-#include <time.h>
-#include <grp.h>
-#include <ctype.h>
 
 #if HAVE_UNSHARE && HAVE_STATVFS && HAVE_SYS_MOUNT_H
 #include <sched.h>
-#include <sys/statvfs.h>
 #include <sys/mount.h>
+#include <sys/statvfs.h>
 #endif
 
-#include <sys/ioctl.h>
 #include <errno.h>
+#include <sys/ioctl.h>
 
 #include <sqlite3.h>
 
+namespace nix
+{
 
-namespace nix {
-
-
-void checkStoreNotSymlink()
+void
+checkStoreNotSymlink ()
 {
-    if (getEnv("NIX_IGNORE_SYMLINK_STORE") == "1") return;
-    Path path = settings.nixStore;
-    struct stat st;
-    while (path != "/") {
-        if (lstat(path.c_str(), &st))
-            throw SysError(format("getting status of `%1%'") % path);
-        if (S_ISLNK(st.st_mode))
-            throw Error(format(
+  if (getEnv ("NIX_IGNORE_SYMLINK_STORE") == "1")
+    return;
+  Path path = settings.nixStore;
+  struct stat st;
+  while (path != "/")
+    {
+      if (lstat (path.c_str (), &st))
+        throw SysError (format ("getting status of `%1%'") % path);
+      if (S_ISLNK (st.st_mode))
+        throw Error (
+            format (
                 "the path `%1%' is a symlink; "
                 "this is not allowed for the store and its parent directories")
-                % path);
-        path = dirOf(path);
+            % path);
+      path = dirOf (path);
     }
 }
 
-
-LocalStore::LocalStore(bool reserveSpace)
+LocalStore::LocalStore (bool reserveSpace)
 {
-    schemaPath = settings.nixDBPath + "/schema";
+  schemaPath = settings.nixDBPath + "/schema";
 
-    if (settings.readOnlyMode) {
-        openDB(false);
-        return;
+  if (settings.readOnlyMode)
+    {
+      openDB (false);
+      return;
     }
 
-    /* Create missing state directories if they don't already exist. */
-    createDirs(settings.nixStore);
-    makeStoreWritable();
-    createDirs(linksDir = settings.nixStore + "/.links");
-    Path profilesDir = settings.nixStateDir + "/profiles";
-    createDirs(profilesDir);
-    createDirs(settings.nixStateDir + "/temproots");
-    createDirs(settings.nixDBPath);
-    Path gcRootsDir = settings.nixStateDir + "/gcroots";
-    if (!pathExists(gcRootsDir)) {
-        createDirs(gcRootsDir);
-        createSymlink(profilesDir, gcRootsDir + "/profiles");
+  /* Create missing state directories if they don't already exist. */
+  createDirs (settings.nixStore);
+  makeStoreWritable ();
+  createDirs (linksDir = settings.nixStore + "/.links");
+  Path profilesDir = settings.nixStateDir + "/profiles";
+  createDirs (profilesDir);
+  createDirs (settings.nixStateDir + "/temproots");
+  createDirs (settings.nixDBPath);
+  Path gcRootsDir = settings.nixStateDir + "/gcroots";
+  if (!pathExists (gcRootsDir))
+    {
+      createDirs (gcRootsDir);
+      createSymlink (profilesDir, gcRootsDir + "/profiles");
     }
 
-    Path perUserDir = profilesDir + "/per-user";
-    createDirs(perUserDir);
-
-    /* Optionally, set permissions for a multi-user install.  */
-    if (getuid() == 0 && settings.buildUsersGroup != "") {
-
-        if (chmod(perUserDir.c_str(), 0755) == -1)
-            throw SysError(format("could not set permissions on '%1%' to 755")
-                           % perUserDir);
+  Path perUserDir = profilesDir + "/per-user";
+  createDirs (perUserDir);
 
-        mode_t perm = 01775;
-
-        struct group * gr = getgrnam(settings.buildUsersGroup.c_str());
-        if (!gr)
-            throw Error(format("the group `%1%' specified in `build-users-group' does not exist")
-                % settings.buildUsersGroup);
-        else {
-            struct stat st;
-            if (stat(settings.nixStore.c_str(), &st))
-                throw SysError(format("getting attributes of path '%1%'") % settings.nixStore);
+  /* Optionally, set permissions for a multi-user install.  */
+  if (getuid () == 0 && settings.buildUsersGroup != "")
+    {
 
-            if (st.st_uid != 0 || st.st_gid != gr->gr_gid || (st.st_mode & ~S_IFMT) != perm) {
-                if (chown(settings.nixStore.c_str(), 0, gr->gr_gid) == -1)
-                    throw SysError(format("changing ownership of path '%1%'") % settings.nixStore);
-                if (chmod(settings.nixStore.c_str(), perm) == -1)
-                    throw SysError(format("changing permissions on path '%1%'") % settings.nixStore);
+      if (chmod (perUserDir.c_str (), 0755) == -1)
+        throw SysError (format ("could not set permissions on '%1%' to 755")
+                        % perUserDir);
+
+      mode_t perm = 01775;
+
+      struct group *gr = getgrnam (settings.buildUsersGroup.c_str ());
+      if (!gr)
+        throw Error (format ("the group `%1%' specified in "
+                             "`build-users-group' does not exist")
+                     % settings.buildUsersGroup);
+      else
+        {
+          struct stat st;
+          if (stat (settings.nixStore.c_str (), &st))
+            throw SysError (format ("getting attributes of path '%1%'")
+                            % settings.nixStore);
+
+          if (st.st_uid != 0 || st.st_gid != gr->gr_gid
+              || (st.st_mode & ~S_IFMT) != perm)
+            {
+              if (chown (settings.nixStore.c_str (), 0, gr->gr_gid) == -1)
+                throw SysError (format ("changing ownership of path '%1%'")
+                                % settings.nixStore);
+              if (chmod (settings.nixStore.c_str (), perm) == -1)
+                throw SysError (format ("changing permissions on path '%1%'")
+                                % settings.nixStore);
             }
         }
     }
 
-    checkStoreNotSymlink();
-
-    /* We can't open a SQLite database if the disk is full.  Since
-       this prevents the garbage collector from running when it's most
-       needed, we reserve some dummy space that we can free just
-       before doing a garbage collection. */
-    try {
-        Path reservedPath = settings.nixDBPath + "/reserved";
-        if (reserveSpace) {
-            struct stat st;
-            if (stat(reservedPath.c_str(), &st) == -1 ||
-                st.st_size != settings.reservedSize)
+  checkStoreNotSymlink ();
+
+  /* We can't open a SQLite database if the disk is full.  Since
+     this prevents the garbage collector from running when it's most
+     needed, we reserve some dummy space that we can free just
+     before doing a garbage collection. */
+  try
+    {
+      Path reservedPath = settings.nixDBPath + "/reserved";
+      if (reserveSpace)
+        {
+          struct stat st;
+          if (stat (reservedPath.c_str (), &st) == -1
+              || st.st_size != settings.reservedSize)
             {
-                AutoCloseFD fd = open(reservedPath.c_str(), O_WRONLY | O_CREAT, 0600);
-                int res = -1;
+              AutoCloseFD fd
+                  = open (reservedPath.c_str (), O_WRONLY | O_CREAT, 0600);
+              int res = -1;
 #if HAVE_POSIX_FALLOCATE
-                res = posix_fallocate(fd, 0, settings.reservedSize);
+              res = posix_fallocate (fd, 0, settings.reservedSize);
 #endif
-                if (res == -1) {
-                    writeFull(fd, string(settings.reservedSize, 'X'));
-                    ftruncate(fd, settings.reservedSize);
+              if (res == -1)
+                {
+                  writeFull (fd, string (settings.reservedSize, 'X'));
+                  ftruncate (fd, settings.reservedSize);
                 }
             }
         }
-        else
-            deletePath(reservedPath);
-    } catch (SysError & e) { /* don't care about errors */
+      else
+        deletePath (reservedPath);
+    }
+  catch (SysError &e)
+    { /* don't care about errors */
     }
 
-    /* Acquire the big fat lock in shared mode to make sure that no
-       schema upgrade is in progress. */
-    try {
-        Path globalLockPath = settings.nixDBPath + "/big-lock";
-        globalLock = openLockFile(globalLockPath.c_str(), true);
-    } catch (SysError & e) {
-        if (e.errNo != EACCES) throw;
-        settings.readOnlyMode = true;
-        openDB(false);
-        return;
+  /* Acquire the big fat lock in shared mode to make sure that no
+     schema upgrade is in progress. */
+  try
+    {
+      Path globalLockPath = settings.nixDBPath + "/big-lock";
+      globalLock = openLockFile (globalLockPath.c_str (), true);
+    }
+  catch (SysError &e)
+    {
+      if (e.errNo != EACCES)
+        throw;
+      settings.readOnlyMode = true;
+      openDB (false);
+      return;
     }
 
-    if (!lockFile(globalLock, ltRead, false)) {
-        printMsg(lvlError, "waiting for the big store lock...");
-        lockFile(globalLock, ltRead, true);
+  if (!lockFile (globalLock, ltRead, false))
+    {
+      printMsg (lvlError, "waiting for the big store lock...");
+      lockFile (globalLock, ltRead, true);
     }
 
-    /* Check the current database schema and if necessary do an
-       upgrade.  */
-    int curSchema = getSchema();
-    if (curSchema > nixSchemaVersion)
-        throw Error(format("current store schema is version %1%, but I only support %2%")
-            % curSchema % nixSchemaVersion);
-
-    else if (curSchema == 0) { /* new store */
-        curSchema = nixSchemaVersion;
-        openDB(true);
-        writeFile(schemaPath, (format("%1%") % nixSchemaVersion).str());
+  /* Check the current database schema and if necessary do an
+     upgrade.  */
+  int curSchema = getSchema ();
+  if (curSchema > nixSchemaVersion)
+    throw Error (
+        format ("current store schema is version %1%, but I only support %2%")
+        % curSchema % nixSchemaVersion);
+
+  else if (curSchema == 0)
+    { /* new store */
+      curSchema = nixSchemaVersion;
+      openDB (true);
+      writeFile (schemaPath, (format ("%1%") % nixSchemaVersion).str ());
     }
 
-    else if (curSchema < nixSchemaVersion) {
-	/* Guix always used version 7 of the schema.  */
-	throw Error(
-	    format("Your store database uses an implausibly old schema, version %1%.")
-	    % curSchema);
+  else if (curSchema < nixSchemaVersion)
+    {
+      /* Guix always used version 7 of the schema.  */
+      throw Error (format ("Your store database uses an implausibly old "
+                           "schema, version %1%.")
+                   % curSchema);
     }
 
-    else openDB(false);
+  else
+    openDB (false);
 }
 
-
-LocalStore::~LocalStore()
+LocalStore::~LocalStore ()
 {
-    try {
-        if (fdTempRoots != -1) {
-            fdTempRoots.close();
-            unlink(fnTempRoots.c_str());
+  try
+    {
+      if (fdTempRoots != -1)
+        {
+          fdTempRoots.close ();
+          unlink (fnTempRoots.c_str ());
         }
-    } catch (...) {
-        ignoreException();
     }
-}
-
-
-int LocalStore::getSchema()
-{
-    int curSchema = 0;
-    if (pathExists(schemaPath)) {
-        string s = readFile(schemaPath);
-        if (!string2Int(s, curSchema))
-            throw Error(format("`%1%' is corrupt") % schemaPath);
+  catch (...)
+    {
+      ignoreException ();
     }
-    return curSchema;
 }
 
-
-void LocalStore::openDB(bool create)
+int
+LocalStore::getSchema ()
 {
-    if (access(settings.nixDBPath.c_str(), R_OK | W_OK))
-        throw SysError(format("store database directory `%1%' is not writable") % settings.nixDBPath);
-
-    /* Open the store database. */
-    string dbPath = settings.nixDBPath + "/db.sqlite";
-    if (sqlite3_open_v2(dbPath.c_str(), &db.db,
-            SQLITE_OPEN_READWRITE | (create ? SQLITE_OPEN_CREATE : 0), 0) != SQLITE_OK)
-        throw Error(format("cannot open store database `%1%'") % dbPath);
-
-    if (sqlite3_busy_timeout(db, 60 * 60 * 1000) != SQLITE_OK)
-        throwSQLiteError(db, "setting timeout");
-
-    if (sqlite3_exec(db, "pragma foreign_keys = 1;", 0, 0, 0) != SQLITE_OK)
-        throwSQLiteError(db, "enabling foreign keys");
-
-    /* !!! check whether sqlite has been built with foreign key
-       support */
-
-    /* Whether SQLite should fsync().  "Normal" synchronous mode
-       should be safe enough.  If the user asks for it, don't sync at
-       all.  This can cause database corruption if the system
-       crashes. */
-    string syncMode = settings.fsyncMetadata ? "normal" : "off";
-    if (sqlite3_exec(db, ("pragma synchronous = " + syncMode + ";").c_str(), 0, 0, 0) != SQLITE_OK)
-        throwSQLiteError(db, "setting synchronous mode");
-
-    /* Set the SQLite journal mode.  WAL mode is fastest, so it's the
-       default. */
-    string mode = settings.useSQLiteWAL ? "wal" : "truncate";
-    string prevMode;
+  int curSchema = 0;
+  if (pathExists (schemaPath))
     {
-        SQLiteStmt stmt;
-        stmt.create(db, "pragma main.journal_mode;");
-        if (sqlite3_step(stmt) != SQLITE_ROW)
-            throwSQLiteError(db, "querying journal mode");
-        prevMode = string((const char *) sqlite3_column_text(stmt, 0));
+      string s = readFile (schemaPath);
+      if (!string2Int (s, curSchema))
+        throw Error (format ("`%1%' is corrupt") % schemaPath);
     }
-    if (prevMode != mode &&
-        sqlite3_exec(db, ("pragma main.journal_mode = " + mode + ";").c_str(), 0, 0, 0) != SQLITE_OK)
-        throwSQLiteError(db, "setting journal mode");
-
-    /* Increase the auto-checkpoint interval to 40000 pages.  This
-       seems enough to ensure that instantiating the NixOS system
-       derivation is done in a single fsync(). */
-    if (mode == "wal" && sqlite3_exec(db, "pragma wal_autocheckpoint = 40000;", 0, 0, 0) != SQLITE_OK)
-        throwSQLiteError(db, "setting autocheckpoint interval");
-
-    /* Initialise the database schema, if necessary. */
-    if (create) {
-        const char * schema =
+  return curSchema;
+}
+
+void
+LocalStore::openDB (bool create)
+{
+  if (access (settings.nixDBPath.c_str (), R_OK | W_OK))
+    throw SysError (format ("store database directory `%1%' is not writable")
+                    % settings.nixDBPath);
+
+  /* Open the store database. */
+  string dbPath = settings.nixDBPath + "/db.sqlite";
+  if (sqlite3_open_v2 (
+          dbPath.c_str (), &db.db,
+          SQLITE_OPEN_READWRITE | (create ? SQLITE_OPEN_CREATE : 0), 0)
+      != SQLITE_OK)
+    throw Error (format ("cannot open store database `%1%'") % dbPath);
+
+  if (sqlite3_busy_timeout (db, 60 * 60 * 1000) != SQLITE_OK)
+    throwSQLiteError (db, "setting timeout");
+
+  if (sqlite3_exec (db, "pragma foreign_keys = 1;", 0, 0, 0) != SQLITE_OK)
+    throwSQLiteError (db, "enabling foreign keys");
+
+  /* !!! check whether sqlite has been built with foreign key
+     support */
+
+  /* Whether SQLite should fsync().  "Normal" synchronous mode
+     should be safe enough.  If the user asks for it, don't sync at
+     all.  This can cause database corruption if the system
+     crashes. */
+  string syncMode = settings.fsyncMetadata ? "normal" : "off";
+  if (sqlite3_exec (db, ("pragma synchronous = " + syncMode + ";").c_str (), 0,
+                    0, 0)
+      != SQLITE_OK)
+    throwSQLiteError (db, "setting synchronous mode");
+
+  /* Set the SQLite journal mode.  WAL mode is fastest, so it's the
+     default. */
+  string mode = settings.useSQLiteWAL ? "wal" : "truncate";
+  string prevMode;
+  {
+    SQLiteStmt stmt;
+    stmt.create (db, "pragma main.journal_mode;");
+    if (sqlite3_step (stmt) != SQLITE_ROW)
+      throwSQLiteError (db, "querying journal mode");
+    prevMode = string ((const char *)sqlite3_column_text (stmt, 0));
+  }
+  if (prevMode != mode
+      && sqlite3_exec (db,
+                       ("pragma main.journal_mode = " + mode + ";").c_str (),
+                       0, 0, 0)
+             != SQLITE_OK)
+    throwSQLiteError (db, "setting journal mode");
+
+  /* Increase the auto-checkpoint interval to 40000 pages.  This
+     seems enough to ensure that instantiating the NixOS system
+     derivation is done in a single fsync(). */
+  if (mode == "wal"
+      && sqlite3_exec (db, "pragma wal_autocheckpoint = 40000;", 0, 0, 0)
+             != SQLITE_OK)
+    throwSQLiteError (db, "setting autocheckpoint interval");
+
+  /* Initialise the database schema, if necessary. */
+  if (create)
+    {
+      const char *schema =
 #include "schema.sql.hh"
-            ;
-        if (sqlite3_exec(db, (const char *) schema, 0, 0, 0) != SQLITE_OK)
-            throwSQLiteError(db, "initialising database schema");
+          ;
+      if (sqlite3_exec (db, (const char *)schema, 0, 0, 0) != SQLITE_OK)
+        throwSQLiteError (db, "initialising database schema");
     }
 
-    /* Prepare SQL statements. */
-    stmtRegisterValidPath.create(db,
-        "insert into ValidPaths (path, hash, registrationTime, deriver, narSize) values (?, ?, ?, ?, ?);");
-    stmtUpdatePathInfo.create(db,
-        "update ValidPaths set narSize = ?, hash = ? where path = ?;");
-    stmtAddReference.create(db,
-        "insert or replace into Refs (referrer, reference) values (?, ?);");
-    stmtQueryPathInfo.create(db,
-        "select id, hash, registrationTime, deriver, narSize from ValidPaths where path = ?;");
-    stmtQueryReferences.create(db,
-        "select path from Refs join ValidPaths on reference = id where referrer = ?;");
-    stmtQueryReferrers.create(db,
-        "select path from Refs join ValidPaths on referrer = id where reference = (select id from ValidPaths where path = ?);");
-    stmtInvalidatePath.create(db,
-        "delete from ValidPaths where path = ?;");
-    stmtRegisterFailedPath.create(db,
-        "insert or ignore into FailedPaths (path, time) values (?, ?);");
-    stmtHasPathFailed.create(db,
-        "select time from FailedPaths where path = ?;");
-    stmtQueryFailedPaths.create(db,
-        "select path from FailedPaths;");
-    // If the path is a derivation, then clear its outputs.
-    stmtClearFailedPath.create(db,
-        "delete from FailedPaths where ?1 = '*' or path = ?1 "
-        "or path in (select d.path from DerivationOutputs d join ValidPaths v on d.drv = v.id where v.path = ?1);");
-    stmtAddDerivationOutput.create(db,
-        "insert or replace into DerivationOutputs (drv, id, path) values (?, ?, ?);");
-    stmtQueryValidDerivers.create(db,
-        "select v.id, v.path from DerivationOutputs d join ValidPaths v on d.drv = v.id where d.path = ?;");
-    stmtQueryDerivationOutputs.create(db,
-        "select id, path from DerivationOutputs where drv = ?;");
-    // Use "path >= ?" with limit 1 rather than "path like '?%'" to
-    // ensure efficient lookup.
-    stmtQueryPathFromHashPart.create(db,
-        "select path from ValidPaths where path >= ? limit 1;");
-    stmtQueryValidPaths.create(db, "select path from ValidPaths");
+  /* Prepare SQL statements. */
+  stmtRegisterValidPath.create (
+      db, "insert into ValidPaths (path, hash, registrationTime, deriver, "
+          "narSize) values (?, ?, ?, ?, ?);");
+  stmtUpdatePathInfo.create (
+      db, "update ValidPaths set narSize = ?, hash = ? where path = ?;");
+  stmtAddReference.create (
+      db, "insert or replace into Refs (referrer, reference) values (?, ?);");
+  stmtQueryPathInfo.create (db, "select id, hash, registrationTime, deriver, "
+                                "narSize from ValidPaths where path = ?;");
+  stmtQueryReferences.create (db, "select path from Refs join ValidPaths on "
+                                  "reference = id where referrer = ?;");
+  stmtQueryReferrers.create (
+      db, "select path from Refs join ValidPaths on referrer = id where "
+          "reference = (select id from ValidPaths where path = ?);");
+  stmtInvalidatePath.create (db, "delete from ValidPaths where path = ?;");
+  stmtRegisterFailedPath.create (
+      db, "insert or ignore into FailedPaths (path, time) values (?, ?);");
+  stmtHasPathFailed.create (db,
+                            "select time from FailedPaths where path = ?;");
+  stmtQueryFailedPaths.create (db, "select path from FailedPaths;");
+  // If the path is a derivation, then clear its outputs.
+  stmtClearFailedPath.create (
+      db, "delete from FailedPaths where ?1 = '*' or path = ?1 "
+          "or path in (select d.path from DerivationOutputs d join ValidPaths "
+          "v on d.drv = v.id where v.path = ?1);");
+  stmtAddDerivationOutput.create (db,
+                                  "insert or replace into DerivationOutputs "
+                                  "(drv, id, path) values (?, ?, ?);");
+  stmtQueryValidDerivers.create (
+      db, "select v.id, v.path from DerivationOutputs d join ValidPaths v on "
+          "d.drv = v.id where d.path = ?;");
+  stmtQueryDerivationOutputs.create (
+      db, "select id, path from DerivationOutputs where drv = ?;");
+  // Use "path >= ?" with limit 1 rather than "path like '?%'" to
+  // ensure efficient lookup.
+  stmtQueryPathFromHashPart.create (
+      db, "select path from ValidPaths where path >= ? limit 1;");
+  stmtQueryValidPaths.create (db, "select path from ValidPaths");
 }
 
-
 /* To improve purity, users may want to make the store a read-only
    bind mount.  So make the store writable for this process. */
-void LocalStore::makeStoreWritable()
-{
-#if HAVE_UNSHARE && HAVE_STATVFS && HAVE_SYS_MOUNT_H && defined(MS_BIND) && defined(MS_REMOUNT)
-    if (getuid() != 0) return;
-    /* Check if /nix/store is on a read-only mount. */
-    struct statvfs stat;
-    if (statvfs(settings.nixStore.c_str(), &stat) != 0)
-        throw SysError("getting info about the store mount point");
-
-    if (stat.f_flag & ST_RDONLY) {
-        if (unshare(CLONE_NEWNS) == -1)
-            throw SysError("setting up a private mount namespace");
-
-        if (mount(0, settings.nixStore.c_str(), "none", MS_REMOUNT | MS_BIND, 0) == -1)
-            throw SysError(format("remounting %1% writable") % settings.nixStore);
+void
+LocalStore::makeStoreWritable ()
+{
+#if HAVE_UNSHARE && HAVE_STATVFS && HAVE_SYS_MOUNT_H && defined(MS_BIND)      \
+    && defined(MS_REMOUNT)
+  if (getuid () != 0)
+    return;
+  /* Check if /nix/store is on a read-only mount. */
+  struct statvfs stat;
+  if (statvfs (settings.nixStore.c_str (), &stat) != 0)
+    throw SysError ("getting info about the store mount point");
+
+  if (stat.f_flag & ST_RDONLY)
+    {
+      if (unshare (CLONE_NEWNS) == -1)
+        throw SysError ("setting up a private mount namespace");
+
+      if (mount (0, settings.nixStore.c_str (), "none", MS_REMOUNT | MS_BIND,
+                 0)
+          == -1)
+        throw SysError (format ("remounting %1% writable")
+                        % settings.nixStore);
     }
 #endif
 }
 
-
 const time_t mtimeStore = 1; /* 1 second into the epoch */
 
-
-static void canonicaliseTimestampAndPermissions(const Path & path, const struct stat & st)
+static void
+canonicaliseTimestampAndPermissions (const Path &path, const struct stat &st)
 {
-    if (!S_ISLNK(st.st_mode)) {
+  if (!S_ISLNK (st.st_mode))
+    {
 
-        /* Mask out all type related bits. */
-        mode_t mode = st.st_mode & ~S_IFMT;
+      /* Mask out all type related bits. */
+      mode_t mode = st.st_mode & ~S_IFMT;
 
-        if (mode != 0444 && mode != 0555) {
-            mode = (st.st_mode & S_IFMT)
-                 | 0444
+      if (mode != 0444 && mode != 0555)
+        {
+          mode = (st.st_mode & S_IFMT) | 0444
                  | (st.st_mode & S_IXUSR ? 0111 : 0);
-            if (chmod(path.c_str(), mode) == -1)
-                throw SysError(format("changing mode of `%1%' to %2$o") % path % mode);
+          if (chmod (path.c_str (), mode) == -1)
+            throw SysError (format ("changing mode of `%1%' to %2$o") % path
+                            % mode);
         }
-
     }
 
-    if (st.st_mtime != mtimeStore) {
-        struct timeval times[2];
-        times[0].tv_sec = st.st_atime;
-        times[0].tv_usec = 0;
-        times[1].tv_sec = mtimeStore;
-        times[1].tv_usec = 0;
+  if (st.st_mtime != mtimeStore)
+    {
+      struct timeval times[2];
+      times[0].tv_sec = st.st_atime;
+      times[0].tv_usec = 0;
+      times[1].tv_sec = mtimeStore;
+      times[1].tv_usec = 0;
 #if HAVE_LUTIMES
-        if (lutimes(path.c_str(), times) == -1)
-            if (errno != ENOSYS ||
-                (!S_ISLNK(st.st_mode) && utimes(path.c_str(), times) == -1))
+      if (lutimes (path.c_str (), times) == -1)
+        if (errno != ENOSYS
+            || (!S_ISLNK (st.st_mode) && utimes (path.c_str (), times) == -1))
 #else
-        if (!S_ISLNK(st.st_mode) && utimes(path.c_str(), times) == -1)
+      if (!S_ISLNK (st.st_mode) && utimes (path.c_str (), times) == -1)
 #endif
-            throw SysError(format("changing modification time of `%1%'") % path);
+          throw SysError (format ("changing modification time of `%1%'")
+                          % path);
     }
 }
 
-
-void canonicaliseTimestampAndPermissions(const Path & path)
+void
+canonicaliseTimestampAndPermissions (const Path &path)
 {
-    struct stat st;
-    if (lstat(path.c_str(), &st))
-        throw SysError(format("getting attributes of path `%1%'") % path);
-    canonicaliseTimestampAndPermissions(path, st);
+  struct stat st;
+  if (lstat (path.c_str (), &st))
+    throw SysError (format ("getting attributes of path `%1%'") % path);
+  canonicaliseTimestampAndPermissions (path, st);
 }
 
-
-static void canonicalisePathMetaData_(const Path & path, uid_t fromUid, InodesSeen & inodesSeen)
+static void
+canonicalisePathMetaData_ (const Path &path, uid_t fromUid,
+                           InodesSeen &inodesSeen)
 {
-    checkInterrupt();
+  checkInterrupt ();
 
-    struct stat st;
-    if (lstat(path.c_str(), &st))
-        throw SysError(format("getting attributes of path `%1%'") % path);
+  struct stat st;
+  if (lstat (path.c_str (), &st))
+    throw SysError (format ("getting attributes of path `%1%'") % path);
 
-    /* Really make sure that the path is of a supported type. */
-    if (!(S_ISREG(st.st_mode) || S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)))
-        throw Error(format("file ‘%1%’ has an unsupported type") % path);
+  /* Really make sure that the path is of a supported type. */
+  if (!(S_ISREG (st.st_mode) || S_ISDIR (st.st_mode) || S_ISLNK (st.st_mode)))
+    throw Error (format ("file ‘%1%’ has an unsupported type") % path);
 
-    /* Fail if the file is not owned by the build user.  This prevents
-       us from messing up the ownership/permissions of files
-       hard-linked into the output (e.g. "ln /etc/shadow $out/foo").
-       However, ignore files that we chown'ed ourselves previously to
-       ensure that we don't fail on hard links within the same build
-       (i.e. "touch $out/foo; ln $out/foo $out/bar"). */
-    if (fromUid != (uid_t) -1 && st.st_uid != fromUid) {
-        assert(!S_ISDIR(st.st_mode));
-        if (inodesSeen.find(Inode(st.st_dev, st.st_ino)) == inodesSeen.end())
-            throw BuildError(format("invalid ownership on file `%1%'") % path);
-        mode_t mode = st.st_mode & ~S_IFMT;
-        assert(S_ISLNK(st.st_mode) || (st.st_uid == geteuid() && (mode == 0444 || mode == 0555) && st.st_mtime == mtimeStore));
-        return;
+  /* Fail if the file is not owned by the build user.  This prevents
+     us from messing up the ownership/permissions of files
+     hard-linked into the output (e.g. "ln /etc/shadow $out/foo").
+     However, ignore files that we chown'ed ourselves previously to
+     ensure that we don't fail on hard links within the same build
+     (i.e. "touch $out/foo; ln $out/foo $out/bar"). */
+  if (fromUid != (uid_t)-1 && st.st_uid != fromUid)
+    {
+      assert (!S_ISDIR (st.st_mode));
+      if (inodesSeen.find (Inode (st.st_dev, st.st_ino)) == inodesSeen.end ())
+        throw BuildError (format ("invalid ownership on file `%1%'") % path);
+      mode_t mode = st.st_mode & ~S_IFMT;
+      assert (S_ISLNK (st.st_mode)
+              || (st.st_uid == geteuid () && (mode == 0444 || mode == 0555)
+                  && st.st_mtime == mtimeStore));
+      return;
     }
 
-    inodesSeen.insert(Inode(st.st_dev, st.st_ino));
+  inodesSeen.insert (Inode (st.st_dev, st.st_ino));
 
-    canonicaliseTimestampAndPermissions(path, st);
+  canonicaliseTimestampAndPermissions (path, st);
 
-    /* Change ownership to the current uid.  If it's a symlink, use
-       lchown if available, otherwise don't bother.  Wrong ownership
-       of a symlink doesn't matter, since the owning user can't change
-       the symlink and can't delete it because the directory is not
-       writable.  The only exception is top-level paths in the
-       store (since that directory is group-writable for the build
-       users group); we check for this case below. */
-    if (st.st_uid != geteuid()) {
+  /* Change ownership to the current uid.  If it's a symlink, use
+     lchown if available, otherwise don't bother.  Wrong ownership
+     of a symlink doesn't matter, since the owning user can't change
+     the symlink and can't delete it because the directory is not
+     writable.  The only exception is top-level paths in the
+     store (since that directory is group-writable for the build
+     users group); we check for this case below. */
+  if (st.st_uid != geteuid ())
+    {
 #if HAVE_LCHOWN
-        if (lchown(path.c_str(), geteuid(), getegid()) == -1)
+      if (lchown (path.c_str (), geteuid (), getegid ()) == -1)
 #else
-        if (!S_ISLNK(st.st_mode) &&
-            chown(path.c_str(), geteuid(), getegid()) == -1)
+      if (!S_ISLNK (st.st_mode)
+          && chown (path.c_str (), geteuid (), getegid ()) == -1)
 #endif
-            throw SysError(format("changing owner of `%1%' to %2%")
-                % path % geteuid());
+        throw SysError (format ("changing owner of `%1%' to %2%") % path
+                        % geteuid ());
     }
 
-    if (S_ISDIR(st.st_mode)) {
-        DirEntries entries = readDirectory(path);
-        for (auto & i : entries)
-            canonicalisePathMetaData_(path + "/" + i.name, fromUid, inodesSeen);
+  if (S_ISDIR (st.st_mode))
+    {
+      DirEntries entries = readDirectory (path);
+      for (auto &i : entries)
+        canonicalisePathMetaData_ (path + "/" + i.name, fromUid, inodesSeen);
     }
 }
 
-
-void canonicalisePathMetaData(const Path & path, uid_t fromUid, InodesSeen & inodesSeen)
+void
+canonicalisePathMetaData (const Path &path, uid_t fromUid,
+                          InodesSeen &inodesSeen)
 {
-    canonicalisePathMetaData_(path, fromUid, inodesSeen);
+  canonicalisePathMetaData_ (path, fromUid, inodesSeen);
 
-    /* On platforms that don't have lchown(), the top-level path can't
-       be a symlink, since we can't change its ownership. */
-    struct stat st;
-    if (lstat(path.c_str(), &st))
-        throw SysError(format("getting attributes of path `%1%'") % path);
+  /* On platforms that don't have lchown(), the top-level path can't
+     be a symlink, since we can't change its ownership. */
+  struct stat st;
+  if (lstat (path.c_str (), &st))
+    throw SysError (format ("getting attributes of path `%1%'") % path);
 
-    if (st.st_uid != geteuid()) {
-        assert(S_ISLNK(st.st_mode));
-        throw Error(format("wrong ownership of top-level store path `%1%'") % path);
+  if (st.st_uid != geteuid ())
+    {
+      assert (S_ISLNK (st.st_mode));
+      throw Error (format ("wrong ownership of top-level store path `%1%'")
+                   % path);
     }
 }
 
-
-void canonicalisePathMetaData(const Path & path, uid_t fromUid)
+void
+canonicalisePathMetaData (const Path &path, uid_t fromUid)
 {
-    InodesSeen inodesSeen;
-    canonicalisePathMetaData(path, fromUid, inodesSeen);
+  InodesSeen inodesSeen;
+  canonicalisePathMetaData (path, fromUid, inodesSeen);
 }
 
-
-void LocalStore::checkDerivationOutputs(const Path & drvPath, const Derivation & drv)
+void
+LocalStore::checkDerivationOutputs (const Path &drvPath, const Derivation &drv)
 {
-    string drvName = storePathToName(drvPath);
-    assert(isDerivation(drvName));
-    drvName = string(drvName, 0, drvName.size() - drvExtension.size());
-
-    if (isFixedOutputDrv(drv)) {
-        DerivationOutputs::const_iterator out = drv.outputs.find("out");
-        if (out == drv.outputs.end())
-            throw Error(format("derivation `%1%' does not have an output named `out'") % drvPath);
+  string drvName = storePathToName (drvPath);
+  assert (isDerivation (drvName));
+  drvName = string (drvName, 0, drvName.size () - drvExtension.size ());
 
-        bool recursive; HashType ht; Hash h;
-        out->second.parseHashInfo(recursive, ht, h);
-        Path outPath = makeFixedOutputPath(recursive, ht, h, drvName);
-
-        StringPairs::const_iterator j = drv.env.find("out");
-        if (out->second.path != outPath || j == drv.env.end() || j->second != outPath)
-            throw Error(format("derivation `%1%' has incorrect output `%2%', should be `%3%'")
-                % drvPath % out->second.path % outPath);
+  if (isFixedOutputDrv (drv))
+    {
+      DerivationOutputs::const_iterator out = drv.outputs.find ("out");
+      if (out == drv.outputs.end ())
+        throw Error (
+            format ("derivation `%1%' does not have an output named `out'")
+            % drvPath);
+
+      bool recursive;
+      HashType ht;
+      Hash h;
+      out->second.parseHashInfo (recursive, ht, h);
+      Path outPath = makeFixedOutputPath (recursive, ht, h, drvName);
+
+      StringPairs::const_iterator j = drv.env.find ("out");
+      if (out->second.path != outPath || j == drv.env.end ()
+          || j->second != outPath)
+        throw Error (
+            format (
+                "derivation `%1%' has incorrect output `%2%', should be `%3%'")
+            % drvPath % out->second.path % outPath);
     }
 
-    else {
-        Derivation drvCopy(drv);
-        foreach (DerivationOutputs::iterator, i, drvCopy.outputs) {
-            i->second.path = "";
-            drvCopy.env[i->first] = "";
+  else
+    {
+      Derivation drvCopy (drv);
+      foreach (DerivationOutputs::iterator, i, drvCopy.outputs)
+        {
+          i->second.path = "";
+          drvCopy.env[i->first] = "";
         }
 
-        Hash h = hashDerivationModulo(*this, drvCopy);
-
-        foreach (DerivationOutputs::const_iterator, i, drv.outputs) {
-            Path outPath = makeOutputPath(i->first, h, drvName);
-            StringPairs::const_iterator j = drv.env.find(i->first);
-            if (i->second.path != outPath || j == drv.env.end() || j->second != outPath)
-                throw Error(format("derivation `%1%' has incorrect output `%2%', should be `%3%'")
-                    % drvPath % i->second.path % outPath);
+      Hash h = hashDerivationModulo (*this, drvCopy);
+
+      foreach (DerivationOutputs::const_iterator, i, drv.outputs)
+        {
+          Path outPath = makeOutputPath (i->first, h, drvName);
+          StringPairs::const_iterator j = drv.env.find (i->first);
+          if (i->second.path != outPath || j == drv.env.end ()
+              || j->second != outPath)
+            throw Error (format ("derivation `%1%' has incorrect output "
+                                 "`%2%', should be `%3%'")
+                         % drvPath % i->second.path % outPath);
         }
     }
 }
 
+uint64_t
+LocalStore::addValidPath (const ValidPathInfo &info, bool checkOutputs)
+{
+  stmtRegisterValidPath
+      .use () (info.path) ("sha256:" + printHash (info.hash)) (
+          info.registrationTime == 0 ? time (0) : info.registrationTime) (
+          info.deriver, info.deriver != "") (info.narSize, info.narSize != 0)
+      .exec ();
+  uint64_t id = sqlite3_last_insert_rowid (db);
 
-uint64_t LocalStore::addValidPath(const ValidPathInfo & info, bool checkOutputs)
-{
-    stmtRegisterValidPath.use()
-        (info.path)
-        ("sha256:" + printHash(info.hash))
-        (info.registrationTime == 0 ? time(0) : info.registrationTime)
-        (info.deriver, info.deriver != "")
-        (info.narSize, info.narSize != 0)
-        .exec();
-    uint64_t id = sqlite3_last_insert_rowid(db);
-
-    /* If this is a derivation, then store the derivation outputs in
-       the database.  This is useful for the garbage collector: it can
-       efficiently query whether a path is an output of some
-       derivation. */
-    if (isDerivation(info.path)) {
-        Derivation drv = readDerivation(info.path);
-
-        /* Verify that the output paths in the derivation are correct
-           (i.e., follow the scheme for computing output paths from
-           derivations).  Note that if this throws an error, then the
-           DB transaction is rolled back, so the path validity
-           registration above is undone. */
-        if (checkOutputs) checkDerivationOutputs(info.path, drv);
-
-        for (auto & i : drv.outputs) {
-            stmtAddDerivationOutput.use()
-                (id)
-                (i.first)
-                (i.second.path)
-                .exec();
+  /* If this is a derivation, then store the derivation outputs in
+     the database.  This is useful for the garbage collector: it can
+     efficiently query whether a path is an output of some
+     derivation. */
+  if (isDerivation (info.path))
+    {
+      Derivation drv = readDerivation (info.path);
+
+      /* Verify that the output paths in the derivation are correct
+         (i.e., follow the scheme for computing output paths from
+         derivations).  Note that if this throws an error, then the
+         DB transaction is rolled back, so the path validity
+         registration above is undone. */
+      if (checkOutputs)
+        checkDerivationOutputs (info.path, drv);
+
+      for (auto &i : drv.outputs)
+        {
+          stmtAddDerivationOutput.use () (id) (i.first) (i.second.path)
+              .exec ();
         }
     }
 
-    return id;
+  return id;
 }
 
-
-void LocalStore::addReference(uint64_t referrer, uint64_t reference)
+void
+LocalStore::addReference (uint64_t referrer, uint64_t reference)
 {
-    stmtAddReference.use()(referrer)(reference).exec();
+  stmtAddReference.use () (referrer) (reference).exec ();
 }
 
-
-void LocalStore::registerFailedPath(const Path & path)
+void
+LocalStore::registerFailedPath (const Path &path)
 {
-    retrySQLite<void>([&]() {
-        stmtRegisterFailedPath.use()(path)(time(0)).step();
-    });
+  retrySQLite<void> (
+      [&] () { stmtRegisterFailedPath.use () (path) (time (0)).step (); });
 }
 
-
-bool LocalStore::hasPathFailed(const Path & path)
+bool
+LocalStore::hasPathFailed (const Path &path)
 {
-    return retrySQLite<bool>([&]() {
-        return stmtHasPathFailed.use()(path).next();
-    });
+  return retrySQLite<bool> (
+      [&] () { return stmtHasPathFailed.use () (path).next (); });
 }
 
-
-PathSet LocalStore::queryFailedPaths()
+PathSet
+LocalStore::queryFailedPaths ()
 {
-    return retrySQLite<PathSet>([&]() {
-        auto useQueryFailedPaths(stmtQueryFailedPaths.use());
+  return retrySQLite<PathSet> ([&] () {
+    auto useQueryFailedPaths (stmtQueryFailedPaths.use ());
 
-        PathSet res;
-        while (useQueryFailedPaths.next())
-            res.insert(useQueryFailedPaths.getStr(0));
+    PathSet res;
+    while (useQueryFailedPaths.next ())
+      res.insert (useQueryFailedPaths.getStr (0));
 
-        return res;
-    });
+    return res;
+  });
 }
 
-
-void LocalStore::clearFailedPaths(const PathSet & paths)
+void
+LocalStore::clearFailedPaths (const PathSet &paths)
 {
-    retrySQLite<void>([&]() {
-        SQLiteTxn txn(db);
+  retrySQLite<void> ([&] () {
+    SQLiteTxn txn (db);
 
-        for (auto & path : paths)
-            stmtClearFailedPath.use()(path).exec();
+    for (auto &path : paths)
+      stmtClearFailedPath.use () (path).exec ();
 
-        txn.commit();
-    });
+    txn.commit ();
+  });
 }
 
-
-Hash parseHashField(const Path & path, const string & s)
+Hash
+parseHashField (const Path &path, const string &s)
 {
-    string::size_type colon = s.find(':');
-    if (colon == string::npos)
-        throw Error(format("corrupt hash `%1%' in valid-path entry for `%2%'")
-            % s % path);
-    HashType ht = parseHashType(string(s, 0, colon));
-    if (ht == htUnknown)
-        throw Error(format("unknown hash type `%1%' in valid-path entry for `%2%'")
-            % string(s, 0, colon) % path);
-    return parseHash(ht, string(s, colon + 1));
+  string::size_type colon = s.find (':');
+  if (colon == string::npos)
+    throw Error (format ("corrupt hash `%1%' in valid-path entry for `%2%'")
+                 % s % path);
+  HashType ht = parseHashType (string (s, 0, colon));
+  if (ht == htUnknown)
+    throw Error (
+        format ("unknown hash type `%1%' in valid-path entry for `%2%'")
+        % string (s, 0, colon) % path);
+  return parseHash (ht, string (s, colon + 1));
 }
 
-
-ValidPathInfo LocalStore::queryPathInfo(const Path & path)
+ValidPathInfo
+LocalStore::queryPathInfo (const Path &path)
 {
-    ValidPathInfo info;
-    info.path = path;
+  ValidPathInfo info;
+  info.path = path;
 
-    assertStorePath(path);
+  assertStorePath (path);
 
-    return retrySQLite<ValidPathInfo>([&]() {
+  return retrySQLite<ValidPathInfo> ([&] () {
+    /* Get the path info. */
+    auto useQueryPathInfo (stmtQueryPathInfo.use () (path));
 
-        /* Get the path info. */
-        auto useQueryPathInfo(stmtQueryPathInfo.use()(path));
+    if (!useQueryPathInfo.next ())
+      throw Error (format ("path `%1%' is not valid") % path);
 
-        if (!useQueryPathInfo.next())
-            throw Error(format("path `%1%' is not valid") % path);
+    info.id = useQueryPathInfo.getInt (0);
 
-        info.id = useQueryPathInfo.getInt(0);
+    info.hash = parseHashField (path, useQueryPathInfo.getStr (1));
 
-        info.hash = parseHashField(path, useQueryPathInfo.getStr(1));
+    info.registrationTime = useQueryPathInfo.getInt (2);
 
-        info.registrationTime = useQueryPathInfo.getInt(2);
+    auto s = (const char *)sqlite3_column_text (stmtQueryPathInfo, 3);
+    if (s)
+      info.deriver = s;
 
-        auto s = (const char *) sqlite3_column_text(stmtQueryPathInfo, 3);
-        if (s) info.deriver = s;
+    /* Note that narSize = NULL yields 0. */
+    info.narSize = useQueryPathInfo.getInt (4);
 
-        /* Note that narSize = NULL yields 0. */
-        info.narSize = useQueryPathInfo.getInt(4);
+    /* Get the references. */
+    auto useQueryReferences (stmtQueryReferences.use () (info.id));
 
-        /* Get the references. */
-        auto useQueryReferences(stmtQueryReferences.use()(info.id));
+    while (useQueryReferences.next ())
+      info.references.insert (useQueryReferences.getStr (0));
 
-        while (useQueryReferences.next())
-            info.references.insert(useQueryReferences.getStr(0));
-
-        return info;
-    });
+    return info;
+  });
 }
 
-
 /* Update path info in the database.  Currently only updates the
    narSize field. */
-void LocalStore::updatePathInfo(const ValidPathInfo & info)
+void
+LocalStore::updatePathInfo (const ValidPathInfo &info)
 {
-    stmtUpdatePathInfo.use()
-        (info.narSize, info.narSize != 0)
-        ("sha256:" + printHash(info.hash))
-        (info.path)
-        .exec();
+  stmtUpdatePathInfo
+      .use () (info.narSize, info.narSize != 0) (
+          "sha256:" + printHash (info.hash)) (info.path)
+      .exec ();
 }
 
-
-uint64_t LocalStore::queryValidPathId(const Path & path)
+uint64_t
+LocalStore::queryValidPathId (const Path &path)
 {
-    auto use(stmtQueryPathInfo.use()(path));
-    if (!use.next())
-        throw Error(format("path ‘%1%’ is not valid") % path);
-    return use.getInt(0);
+  auto use (stmtQueryPathInfo.use () (path));
+  if (!use.next ())
+    throw Error (format ("path ‘%1%’ is not valid") % path);
+  return use.getInt (0);
 }
 
-
-bool LocalStore::isValidPath_(const Path & path)
+bool
+LocalStore::isValidPath_ (const Path &path)
 {
-    return stmtQueryPathInfo.use()(path).next();
+  return stmtQueryPathInfo.use () (path).next ();
 }
 
-
-bool LocalStore::isValidPath(const Path & path)
+bool
+LocalStore::isValidPath (const Path &path)
 {
-    return retrySQLite<bool>([&]() {
-        return isValidPath_(path);
-    });
+  return retrySQLite<bool> ([&] () { return isValidPath_ (path); });
 }
 
-
-PathSet LocalStore::queryValidPaths(const PathSet & paths)
+PathSet
+LocalStore::queryValidPaths (const PathSet &paths)
 {
-    return retrySQLite<PathSet>([&]() {
-        PathSet res;
-        foreach (PathSet::const_iterator, i, paths)
-            if (isValidPath_(*i)) res.insert(*i);
-        return res;
-    });
+  return retrySQLite<PathSet> ([&] () {
+    PathSet res;
+    foreach (PathSet::const_iterator, i, paths)
+      if (isValidPath_ (*i))
+        res.insert (*i);
+    return res;
+  });
 }
 
-
-PathSet LocalStore::queryAllValidPaths()
+PathSet
+LocalStore::queryAllValidPaths ()
 {
-    return retrySQLite<PathSet>([&]() {
-        auto use(stmtQueryValidPaths.use());
-        PathSet res;
-        while (use.next()) res.insert(use.getStr(0));
-        return res;
-    });
+  return retrySQLite<PathSet> ([&] () {
+    auto use (stmtQueryValidPaths.use ());
+    PathSet res;
+    while (use.next ())
+      res.insert (use.getStr (0));
+    return res;
+  });
 }
 
-
-void LocalStore::queryReferences(const Path & path,
-    PathSet & references)
+void
+LocalStore::queryReferences (const Path &path, PathSet &references)
 {
-    ValidPathInfo info = queryPathInfo(path);
-    references.insert(info.references.begin(), info.references.end());
+  ValidPathInfo info = queryPathInfo (path);
+  references.insert (info.references.begin (), info.references.end ());
 }
 
-
-void LocalStore::queryReferrers_(const Path & path, PathSet & referrers)
+void
+LocalStore::queryReferrers_ (const Path &path, PathSet &referrers)
 {
-    auto useQueryReferrers(stmtQueryReferrers.use()(path));
+  auto useQueryReferrers (stmtQueryReferrers.use () (path));
 
-    while (useQueryReferrers.next())
-        referrers.insert(useQueryReferrers.getStr(0));
+  while (useQueryReferrers.next ())
+    referrers.insert (useQueryReferrers.getStr (0));
 }
 
-
-void LocalStore::queryReferrers(const Path & path, PathSet & referrers)
+void
+LocalStore::queryReferrers (const Path &path, PathSet &referrers)
 {
-    assertStorePath(path);
-    return retrySQLite<void>([&]() {
-        queryReferrers_(path, referrers);
-    });
+  assertStorePath (path);
+  return retrySQLite<void> ([&] () { queryReferrers_ (path, referrers); });
 }
 
-
-Path LocalStore::queryDeriver(const Path & path)
+Path
+LocalStore::queryDeriver (const Path &path)
 {
-    return queryPathInfo(path).deriver;
+  return queryPathInfo (path).deriver;
 }
 
-
-PathSet LocalStore::queryValidDerivers(const Path & path)
+PathSet
+LocalStore::queryValidDerivers (const Path &path)
 {
-    assertStorePath(path);
+  assertStorePath (path);
 
-    return retrySQLite<PathSet>([&]() {
-        auto useQueryValidDerivers(stmtQueryValidDerivers.use()(path));
+  return retrySQLite<PathSet> ([&] () {
+    auto useQueryValidDerivers (stmtQueryValidDerivers.use () (path));
 
-        PathSet derivers;
-        while (useQueryValidDerivers.next())
-            derivers.insert(useQueryValidDerivers.getStr(1));
+    PathSet derivers;
+    while (useQueryValidDerivers.next ())
+      derivers.insert (useQueryValidDerivers.getStr (1));
 
-        return derivers;
-    });
+    return derivers;
+  });
 }
 
-
-PathSet LocalStore::queryDerivationOutputs(const Path & path)
+PathSet
+LocalStore::queryDerivationOutputs (const Path &path)
 {
-    return retrySQLite<PathSet>([&]() {
-        auto useQueryDerivationOutputs(stmtQueryDerivationOutputs.use()(queryValidPathId(path)));
+  return retrySQLite<PathSet> ([&] () {
+    auto useQueryDerivationOutputs (
+        stmtQueryDerivationOutputs.use () (queryValidPathId (path)));
 
-        PathSet outputs;
-        while (useQueryDerivationOutputs.next())
-            outputs.insert(useQueryDerivationOutputs.getStr(1));
+    PathSet outputs;
+    while (useQueryDerivationOutputs.next ())
+      outputs.insert (useQueryDerivationOutputs.getStr (1));
 
-        return outputs;
-    });
+    return outputs;
+  });
 }
 
-
-StringSet LocalStore::queryDerivationOutputNames(const Path & path)
+StringSet
+LocalStore::queryDerivationOutputNames (const Path &path)
 {
-    return retrySQLite<StringSet>([&]() {
-        auto useQueryDerivationOutputs(stmtQueryDerivationOutputs.use()(queryValidPathId(path)));
+  return retrySQLite<StringSet> ([&] () {
+    auto useQueryDerivationOutputs (
+        stmtQueryDerivationOutputs.use () (queryValidPathId (path)));
 
-        StringSet outputNames;
-        while (useQueryDerivationOutputs.next())
-            outputNames.insert(useQueryDerivationOutputs.getStr(0));
+    StringSet outputNames;
+    while (useQueryDerivationOutputs.next ())
+      outputNames.insert (useQueryDerivationOutputs.getStr (0));
 
-        return outputNames;
-    });
+    return outputNames;
+  });
 }
 
-
-Path LocalStore::queryPathFromHashPart(const string & hashPart)
+Path
+LocalStore::queryPathFromHashPart (const string &hashPart)
 {
-    if (hashPart.size() != 32) throw Error("invalid hash part");
+  if (hashPart.size () != 32)
+    throw Error ("invalid hash part");
 
-    Path prefix = settings.nixStore + "/" + hashPart;
+  Path prefix = settings.nixStore + "/" + hashPart;
 
-    return retrySQLite<Path>([&]() -> Path {
-        auto useQueryPathFromHashPart(stmtQueryPathFromHashPart.use()(prefix));
+  return retrySQLite<Path> ([&] () -> Path {
+    auto useQueryPathFromHashPart (stmtQueryPathFromHashPart.use () (prefix));
 
-        if (!useQueryPathFromHashPart.next()) return "";
+    if (!useQueryPathFromHashPart.next ())
+      return "";
 
-        const char * s = (const char *) sqlite3_column_text(stmtQueryPathFromHashPart, 0);
-        return s && prefix.compare(0, prefix.size(), s, prefix.size()) == 0 ? s : "";
-    });
+    const char *s
+        = (const char *)sqlite3_column_text (stmtQueryPathFromHashPart, 0);
+    return s && prefix.compare (0, prefix.size (), s, prefix.size ()) == 0
+               ? s
+               : "";
+  });
 }
 
 /* Read a line from the substituter's reply file descriptor, while also
    processing its stderr. */
-string LocalStore::getLineFromSubstituter(Agent & run)
+string
+LocalStore::getLineFromSubstituter (Agent &run)
 {
-    string res, err;
-
-    while (1) {
-        checkInterrupt();
+  string res, err;
 
-        fd_set fds;
-        FD_ZERO(&fds);
-        FD_SET(run.fromAgent.readSide, &fds);
-        FD_SET(run.builderOut.readSide, &fds);
-
-        /* Wait for data to appear on the substituter's stdout or
-           stderr. */
-        if (select(std::max(run.fromAgent.readSide, run.builderOut.readSide) + 1, &fds, 0, 0, 0) == -1) {
-            if (errno == EINTR) continue;
-            throw SysError("waiting for input from the substituter");
+  while (1)
+    {
+      checkInterrupt ();
+
+      fd_set fds;
+      FD_ZERO (&fds);
+      FD_SET (run.fromAgent.readSide, &fds);
+      FD_SET (run.builderOut.readSide, &fds);
+
+      /* Wait for data to appear on the substituter's stdout or
+         stderr. */
+      if (select (std::max (run.fromAgent.readSide, run.builderOut.readSide)
+                      + 1,
+                  &fds, 0, 0, 0)
+          == -1)
+        {
+          if (errno == EINTR)
+            continue;
+          throw SysError ("waiting for input from the substituter");
         }
 
-        /* Completely drain stderr before dealing with stdout. */
-        if (FD_ISSET(run.fromAgent.readSide, &fds)) {
-            char buf[4096];
-            ssize_t n = read(run.fromAgent.readSide, (unsigned char *) buf, sizeof(buf));
-            if (n == -1) {
-                if (errno == EINTR) continue;
-                throw SysError("reading from substituter's stderr");
+      /* Completely drain stderr before dealing with stdout. */
+      if (FD_ISSET (run.fromAgent.readSide, &fds))
+        {
+          char buf[4096];
+          ssize_t n = read (run.fromAgent.readSide, (unsigned char *)buf,
+                            sizeof (buf));
+          if (n == -1)
+            {
+              if (errno == EINTR)
+                continue;
+              throw SysError ("reading from substituter's stderr");
             }
-            if (n == 0) throw EndOfFile(format("`%1% substitute' died unexpectedly")
-					% settings.guixProgram);
-            err.append(buf, n);
-            string::size_type p;
-            while (((p = err.find('\n')) != string::npos)
-		   || ((p = err.find('\r')) != string::npos)) {
-	        string thing(err, 0, p + 1);
-	        writeToStderr("substitute: " + thing);
-                err = string(err, p + 1);
+          if (n == 0)
+            throw EndOfFile (format ("`%1% substitute' died unexpectedly")
+                             % settings.guixProgram);
+          err.append (buf, n);
+          string::size_type p;
+          while (((p = err.find ('\n')) != string::npos)
+                 || ((p = err.find ('\r')) != string::npos))
+            {
+              string thing (err, 0, p + 1);
+              writeToStderr ("substitute: " + thing);
+              err = string (err, p + 1);
             }
         }
 
-        /* Read from stdout until we get a newline or the buffer is empty. */
-        else if (FD_ISSET(run.builderOut.readSide, &fds)) {
-	    unsigned char c;
-	    readFull(run.builderOut.readSide, (unsigned char *) &c, 1);
-	    if (c == '\n') {
-		if (!err.empty()) printMsg(lvlError, "substitute: " + err);
-		return res;
-	    }
-	    res += c;
+      /* Read from stdout until we get a newline or the buffer is empty. */
+      else if (FD_ISSET (run.builderOut.readSide, &fds))
+        {
+          unsigned char c;
+          readFull (run.builderOut.readSide, (unsigned char *)&c, 1);
+          if (c == '\n')
+            {
+              if (!err.empty ())
+                printMsg (lvlError, "substitute: " + err);
+              return res;
+            }
+          res += c;
         }
     }
 }
 
-
-template<class T> T LocalStore::getIntLineFromSubstituter(Agent & run)
+template <class T>
+T
+LocalStore::getIntLineFromSubstituter (Agent &run)
 {
-    string s = getLineFromSubstituter(run);
-    T res;
-    if (!string2Int(s, res))
-        throw Error(format("integer expected from stream: %1%") % s);
-    return res;
+  string s = getLineFromSubstituter (run);
+  T res;
+  if (!string2Int (s, res))
+    throw Error (format ("integer expected from stream: %1%") % s);
+  return res;
 }
 
-
-PathSet LocalStore::querySubstitutablePaths(const PathSet & paths)
+PathSet
+LocalStore::querySubstitutablePaths (const PathSet &paths)
 {
-    PathSet res;
+  PathSet res;
 
-    if (!settings.useSubstitutes || paths.empty()) return res;
-
-    Agent & run = *substituter();
-
-    string s = "have ";
-    foreach (PathSet::const_iterator, j, paths)
-	if (res.find(*j) == res.end()) { s += *j; s += " "; }
-    writeLine(run.toAgent.writeSide, s);
-    while (true) {
-	/* FIXME: we only read stderr when an error occurs, so
-	   substituters should only write (short) messages to
-	   stderr when they fail.  I.e. they shouldn't write debug
-	   output. */
-	Path path = getLineFromSubstituter(run);
-	if (path == "") break;
-	res.insert(path);
+  if (!settings.useSubstitutes || paths.empty ())
+    return res;
+
+  Agent &run = *substituter ();
+
+  string s = "have ";
+  foreach (PathSet::const_iterator, j, paths)
+    if (res.find (*j) == res.end ())
+      {
+        s += *j;
+        s += " ";
+      }
+  writeLine (run.toAgent.writeSide, s);
+  while (true)
+    {
+      /* FIXME: we only read stderr when an error occurs, so
+         substituters should only write (short) messages to
+         stderr when they fail.  I.e. they shouldn't write debug
+         output. */
+      Path path = getLineFromSubstituter (run);
+      if (path == "")
+        break;
+      res.insert (path);
     }
 
-    return res;
+  return res;
 }
 
-
-std::shared_ptr<Agent> LocalStore::substituter()
+std::shared_ptr<Agent>
+LocalStore::substituter ()
 {
-    if (!runningSubstituter) {
-	const Strings args = { "substitute", "--query" };
-	const std::map<string, string> env = { { "_NIX_OPTIONS", settings.pack() } };
-	runningSubstituter = std::make_shared<Agent>(settings.guixProgram, args, env);
+  if (!runningSubstituter)
+    {
+      const Strings args = { "substitute", "--query" };
+      const std::map<string, string> env
+          = { { "_NIX_OPTIONS", settings.pack () } };
+      runningSubstituter
+          = std::make_shared<Agent> (settings.guixProgram, args, env);
     }
 
-    return runningSubstituter;
+  return runningSubstituter;
 }
 
-void LocalStore::querySubstitutablePathInfos(PathSet & paths, SubstitutablePathInfos & infos)
+void
+LocalStore::querySubstitutablePathInfos (PathSet &paths,
+                                         SubstitutablePathInfos &infos)
 {
-    if (!settings.useSubstitutes) return;
+  if (!settings.useSubstitutes)
+    return;
 
-    Agent & run = *substituter();
+  Agent &run = *substituter ();
 
-    string s = "info ";
-    foreach (PathSet::const_iterator, i, paths)
-        if (infos.find(*i) == infos.end()) { s += *i; s += " "; }
-    writeLine(run.toAgent.writeSide, s);
-
-    while (true) {
-        Path path = getLineFromSubstituter(run);
-        if (path == "") break;
-        if (paths.find(path) == paths.end())
-            throw Error(format("got unexpected path `%1%' from substituter") % path);
-        paths.erase(path);
-        SubstitutablePathInfo & info(infos[path]);
-        info.deriver = getLineFromSubstituter(run);
-        if (info.deriver != "") assertStorePath(info.deriver);
-        int nrRefs = getIntLineFromSubstituter<int>(run);
-        while (nrRefs--) {
-            Path p = getLineFromSubstituter(run);
-            assertStorePath(p);
-            info.references.insert(p);
+  string s = "info ";
+  foreach (PathSet::const_iterator, i, paths)
+    if (infos.find (*i) == infos.end ())
+      {
+        s += *i;
+        s += " ";
+      }
+  writeLine (run.toAgent.writeSide, s);
+
+  while (true)
+    {
+      Path path = getLineFromSubstituter (run);
+      if (path == "")
+        break;
+      if (paths.find (path) == paths.end ())
+        throw Error (format ("got unexpected path `%1%' from substituter")
+                     % path);
+      paths.erase (path);
+      SubstitutablePathInfo &info (infos[path]);
+      info.deriver = getLineFromSubstituter (run);
+      if (info.deriver != "")
+        assertStorePath (info.deriver);
+      int nrRefs = getIntLineFromSubstituter<int> (run);
+      while (nrRefs--)
+        {
+          Path p = getLineFromSubstituter (run);
+          assertStorePath (p);
+          info.references.insert (p);
         }
-        info.downloadSize = getIntLineFromSubstituter<unsigned long long>(run);
-        info.narSize = getIntLineFromSubstituter<unsigned long long>(run);
+      info.downloadSize = getIntLineFromSubstituter<unsigned long long> (run);
+      info.narSize = getIntLineFromSubstituter<unsigned long long> (run);
     }
 }
 
-
-void LocalStore::querySubstitutablePathInfos(const PathSet & paths,
-    SubstitutablePathInfos & infos)
+void
+LocalStore::querySubstitutablePathInfos (const PathSet &paths,
+                                         SubstitutablePathInfos &infos)
 {
-    if (!paths.empty()) {
-	PathSet todo = paths;
-	querySubstitutablePathInfos(todo, infos);
+  if (!paths.empty ())
+    {
+      PathSet todo = paths;
+      querySubstitutablePathInfos (todo, infos);
     }
 }
 
-
-Hash LocalStore::queryPathHash(const Path & path)
+Hash
+LocalStore::queryPathHash (const Path &path)
 {
-    return queryPathInfo(path).hash;
+  return queryPathInfo (path).hash;
 }
 
-
-void LocalStore::registerValidPath(const ValidPathInfo & info)
+void
+LocalStore::registerValidPath (const ValidPathInfo &info)
 {
-    ValidPathInfos infos;
-    infos.push_back(info);
-    registerValidPaths(infos);
+  ValidPathInfos infos;
+  infos.push_back (info);
+  registerValidPaths (infos);
 }
 
-
-void LocalStore::registerValidPaths(const ValidPathInfos & infos)
+void
+LocalStore::registerValidPaths (const ValidPathInfos &infos)
 {
-    /* SQLite will fsync by default, but the new valid paths may not be fsync-ed.
-     * So some may want to fsync them before registering the validity, at the
-     * expense of some speed of the path registering operation. */
-    if (settings.syncBeforeRegistering) sync();
-
-    return retrySQLite<void>([&]() {
-        SQLiteTxn txn(db);
-        PathSet paths;
+  /* SQLite will fsync by default, but the new valid paths may not be fsync-ed.
+   * So some may want to fsync them before registering the validity, at the
+   * expense of some speed of the path registering operation. */
+  if (settings.syncBeforeRegistering)
+    sync ();
 
-        foreach (ValidPathInfos::const_iterator, i, infos) {
-            assert(i->hash.type == htSHA256);
-            if (isValidPath_(i->path))
-                updatePathInfo(*i);
-            else
-                addValidPath(*i, false);
-            paths.insert(i->path);
-        }
+  return retrySQLite<void> ([&] () {
+    SQLiteTxn txn (db);
+    PathSet paths;
 
-        for (auto & i : infos) {
-            auto referrer = queryValidPathId(i.path);
-            for (auto & j : i.references)
-                addReference(referrer, queryValidPathId(j));
+    foreach (ValidPathInfos::const_iterator, i, infos)
+      {
+        assert (i->hash.type == htSHA256);
+        if (isValidPath_ (i->path))
+          updatePathInfo (*i);
+        else
+          addValidPath (*i, false);
+        paths.insert (i->path);
+      }
+
+    for (auto &i : infos)
+      {
+        auto referrer = queryValidPathId (i.path);
+        for (auto &j : i.references)
+          addReference (referrer, queryValidPathId (j));
+      }
+
+    /* Check that the derivation outputs are correct.  We can't do
+       this in addValidPath() above, because the references might
+       not be valid yet. */
+    foreach (ValidPathInfos::const_iterator, i, infos)
+      if (isDerivation (i->path))
+        {
+          // FIXME: inefficient; we already loaded the
+          // derivation in addValidPath().
+          Derivation drv = readDerivation (i->path);
+          checkDerivationOutputs (i->path, drv);
         }
 
-        /* Check that the derivation outputs are correct.  We can't do
-           this in addValidPath() above, because the references might
-           not be valid yet. */
-        foreach (ValidPathInfos::const_iterator, i, infos)
-            if (isDerivation(i->path)) {
-                // FIXME: inefficient; we already loaded the
-                // derivation in addValidPath().
-                Derivation drv = readDerivation(i->path);
-                checkDerivationOutputs(i->path, drv);
-            }
-
-        /* Do a topological sort of the paths.  This will throw an
-           error if a cycle is detected and roll back the
-           transaction.  Cycles can only occur when a derivation
-           has multiple outputs. */
-        topoSortPaths(*this, paths);
+    /* Do a topological sort of the paths.  This will throw an
+       error if a cycle is detected and roll back the
+       transaction.  Cycles can only occur when a derivation
+       has multiple outputs. */
+    topoSortPaths (*this, paths);
 
-        txn.commit();
-    });
+    txn.commit ();
+  });
 }
 
-
 /* Invalidate a path.  The caller is responsible for checking that
    there are no referrers. */
-void LocalStore::invalidatePath(const Path & path)
+void
+LocalStore::invalidatePath (const Path &path)
 {
-    debug(format("invalidating path `%1%'") % path);
+  debug (format ("invalidating path `%1%'") % path);
 
-    drvHashes.erase(path);
+  drvHashes.erase (path);
 
-    stmtInvalidatePath.use()(path).exec();
+  stmtInvalidatePath.use () (path).exec ();
 
-    /* Note that the foreign key constraints on the Refs table take
-       care of deleting the references entries for `path'. */
+  /* Note that the foreign key constraints on the Refs table take
+     care of deleting the references entries for `path'. */
 }
 
-
-Path LocalStore::addToStoreFromDump(const string & dump, const string & name,
-    bool recursive, HashType hashAlgo, bool repair)
+Path
+LocalStore::addToStoreFromDump (const string &dump, const string &name,
+                                bool recursive, HashType hashAlgo, bool repair)
 {
-    Hash h = hashString(hashAlgo, dump);
+  Hash h = hashString (hashAlgo, dump);
 
-    Path dstPath = makeFixedOutputPath(recursive, hashAlgo, h, name);
+  Path dstPath = makeFixedOutputPath (recursive, hashAlgo, h, name);
 
-    addTempRoot(dstPath);
+  addTempRoot (dstPath);
 
-    if (repair || !isValidPath(dstPath)) {
+  if (repair || !isValidPath (dstPath))
+    {
 
-        /* The first check above is an optimisation to prevent
-           unnecessary lock acquisition. */
+      /* The first check above is an optimisation to prevent
+         unnecessary lock acquisition. */
 
-        PathLocks outputLock(singleton<PathSet, Path>(dstPath));
+      PathLocks outputLock (singleton<PathSet, Path> (dstPath));
 
-        if (repair || !isValidPath(dstPath)) {
+      if (repair || !isValidPath (dstPath))
+        {
 
-            if (pathExists(dstPath)) deletePath(dstPath);
+          if (pathExists (dstPath))
+            deletePath (dstPath);
 
-            if (recursive) {
-                StringSource source(dump);
-                restorePath(dstPath, source);
-            } else
-                writeFile(dstPath, dump);
+          if (recursive)
+            {
+              StringSource source (dump);
+              restorePath (dstPath, source);
+            }
+          else
+            writeFile (dstPath, dump);
 
-            canonicalisePathMetaData(dstPath, -1);
+          canonicalisePathMetaData (dstPath, -1);
 
-            /* Register the SHA-256 hash of the NAR serialisation of
-               the path in the database.  We may just have computed it
-               above (if called with recursive == true and hashAlgo ==
-               sha256); otherwise, compute it here. */
-            HashResult hash;
-            if (recursive) {
-                hash.first = hashAlgo == htSHA256 ? h : hashString(htSHA256, dump);
-                hash.second = dump.size();
-            } else
-                hash = hashPath(htSHA256, dstPath);
+          /* Register the SHA-256 hash of the NAR serialisation of
+             the path in the database.  We may just have computed it
+             above (if called with recursive == true and hashAlgo ==
+             sha256); otherwise, compute it here. */
+          HashResult hash;
+          if (recursive)
+            {
+              hash.first
+                  = hashAlgo == htSHA256 ? h : hashString (htSHA256, dump);
+              hash.second = dump.size ();
+            }
+          else
+            hash = hashPath (htSHA256, dstPath);
 
-            optimisePath(dstPath); // FIXME: combine with hashPath()
+          optimisePath (dstPath); // FIXME: combine with hashPath()
 
-            ValidPathInfo info;
-            info.path = dstPath;
-            info.hash = hash.first;
-            info.narSize = hash.second;
-            registerValidPath(info);
+          ValidPathInfo info;
+          info.path = dstPath;
+          info.hash = hash.first;
+          info.narSize = hash.second;
+          registerValidPath (info);
         }
 
-        outputLock.setDeletion(true);
+      outputLock.setDeletion (true);
     }
 
-    return dstPath;
+  return dstPath;
 }
 
-
-Path LocalStore::addToStore(const string & name, const Path & _srcPath,
-    bool recursive, HashType hashAlgo, PathFilter & filter, bool repair)
+Path
+LocalStore::addToStore (const string &name, const Path &_srcPath,
+                        bool recursive, HashType hashAlgo, PathFilter &filter,
+                        bool repair)
 {
-    Path srcPath(absPath(_srcPath));
-    debug(format("adding `%1%' to the store") % srcPath);
+  Path srcPath (absPath (_srcPath));
+  debug (format ("adding `%1%' to the store") % srcPath);
 
-    /* Read the whole path into memory. This is not a very scalable
-       method for very large paths, but `copyPath' is mainly used for
-       small files. */
-    StringSink sink;
-    if (recursive)
-        dumpPath(srcPath, sink, filter);
-    else
-        sink.s = readFile(srcPath);
+  /* Read the whole path into memory. This is not a very scalable
+     method for very large paths, but `copyPath' is mainly used for
+     small files. */
+  StringSink sink;
+  if (recursive)
+    dumpPath (srcPath, sink, filter);
+  else
+    sink.s = readFile (srcPath);
 
-    return addToStoreFromDump(sink.s, name, recursive, hashAlgo, repair);
+  return addToStoreFromDump (sink.s, name, recursive, hashAlgo, repair);
 }
 
-
-Path LocalStore::addTextToStore(const string & name, const string & s,
-    const PathSet & references, bool repair)
+Path
+LocalStore::addTextToStore (const string &name, const string &s,
+                            const PathSet &references, bool repair)
 {
-    Path dstPath = computeStorePathForText(name, s, references);
+  Path dstPath = computeStorePathForText (name, s, references);
 
-    addTempRoot(dstPath);
+  addTempRoot (dstPath);
 
-    if (repair || !isValidPath(dstPath)) {
+  if (repair || !isValidPath (dstPath))
+    {
 
-        PathLocks outputLock(singleton<PathSet, Path>(dstPath));
+      PathLocks outputLock (singleton<PathSet, Path> (dstPath));
 
-        if (repair || !isValidPath(dstPath)) {
+      if (repair || !isValidPath (dstPath))
+        {
 
-            if (pathExists(dstPath)) deletePath(dstPath);
+          if (pathExists (dstPath))
+            deletePath (dstPath);
 
-            writeFile(dstPath, s);
+          writeFile (dstPath, s);
 
-            canonicalisePathMetaData(dstPath, -1);
+          canonicalisePathMetaData (dstPath, -1);
 
-            HashResult hash = hashPath(htSHA256, dstPath);
+          HashResult hash = hashPath (htSHA256, dstPath);
 
-            optimisePath(dstPath);
+          optimisePath (dstPath);
 
-            ValidPathInfo info;
-            info.path = dstPath;
-            info.hash = hash.first;
-            info.narSize = hash.second;
-            info.references = references;
-            registerValidPath(info);
+          ValidPathInfo info;
+          info.path = dstPath;
+          info.hash = hash.first;
+          info.narSize = hash.second;
+          info.references = references;
+          registerValidPath (info);
         }
 
-        outputLock.setDeletion(true);
+      outputLock.setDeletion (true);
     }
 
-    return dstPath;
+  return dstPath;
 }
 
-
 struct HashAndWriteSink : Sink
 {
-    Sink & writeSink;
-    HashSink hashSink;
-    HashAndWriteSink(Sink & writeSink) : writeSink(writeSink), hashSink(htSHA256)
-    {
-    }
-    virtual void operator () (const unsigned char * data, size_t len)
-    {
-        writeSink(data, len);
-        hashSink(data, len);
-    }
-    Hash currentHash()
-    {
-        return hashSink.currentHash().first;
-    }
+  Sink &writeSink;
+  HashSink hashSink;
+  HashAndWriteSink (Sink &writeSink)
+      : writeSink (writeSink), hashSink (htSHA256)
+  {
+  }
+  virtual void
+  operator() (const unsigned char *data, size_t len)
+  {
+    writeSink (data, len);
+    hashSink (data, len);
+  }
+  Hash
+  currentHash ()
+  {
+    return hashSink.currentHash ().first;
+  }
 };
 
-
 #define EXPORT_MAGIC 0x4558494e
 
-
-static void checkSecrecy(const Path & path)
+static void
+checkSecrecy (const Path &path)
 {
-    struct stat st;
-    if (stat(path.c_str(), &st))
-        throw SysError(format("getting status of `%1%'") % path);
-    if ((st.st_mode & (S_IRWXG | S_IRWXO)) != 0)
-        throw Error(format("file `%1%' should be secret (inaccessible to everybody else)!") % path);
+  struct stat st;
+  if (stat (path.c_str (), &st))
+    throw SysError (format ("getting status of `%1%'") % path);
+  if ((st.st_mode & (S_IRWXG | S_IRWXO)) != 0)
+    throw Error (
+        format (
+            "file `%1%' should be secret (inaccessible to everybody else)!")
+        % path);
 }
 
-
 /* Return the authentication agent, a "guix authenticate" process started
    lazily.  */
-static std::shared_ptr<Agent> authenticationAgent()
+static std::shared_ptr<Agent>
+authenticationAgent ()
 {
-    static std::shared_ptr<Agent> agent;
+  static std::shared_ptr<Agent> agent;
 
-    if (!agent) {
-	Strings args = { "authenticate" };
-	agent = std::make_shared<Agent>(settings.guixProgram, args);
+  if (!agent)
+    {
+      Strings args = { "authenticate" };
+      agent = std::make_shared<Agent> (settings.guixProgram, args);
     }
 
-    return agent;
+  return agent;
 }
 
 /* Read an integer and the byte that immediately follows it from FD.  Return
    the integer.  */
-static int readInteger(int fd)
-{
-    string str;
-
-    while (1) {
-        char ch;
-        ssize_t rd = read(fd, &ch, 1);
-        if (rd == -1) {
-            if (errno != EINTR)
-                throw SysError("reading an integer");
-        } else if (rd == 0)
-            throw EndOfFile("unexpected EOF reading an integer");
-        else {
-	    if (isdigit(ch)) {
-		str += ch;
-	    } else {
-		break;
-	    }
+static int
+readInteger (int fd)
+{
+  string str;
+
+  while (1)
+    {
+      char ch;
+      ssize_t rd = read (fd, &ch, 1);
+      if (rd == -1)
+        {
+          if (errno != EINTR)
+            throw SysError ("reading an integer");
+        }
+      else if (rd == 0)
+        throw EndOfFile ("unexpected EOF reading an integer");
+      else
+        {
+          if (isdigit (ch))
+            {
+              str += ch;
+            }
+          else
+            {
+              break;
+            }
         }
     }
 
-    return stoi(str);
+  return stoi (str);
 }
 
 /* Read from FD a reply coming from 'guix authenticate'.  The reply has the
    form "CODE LEN:STR".  CODE is an integer, where zero indicates success.
    LEN specifies the length in bytes of the string that immediately
    follows.  */
-static std::string readAuthenticateReply(int fd)
+static std::string
+readAuthenticateReply (int fd)
 {
-    int code = readInteger(fd);
-    int len = readInteger(fd);
+  int code = readInteger (fd);
+  int len = readInteger (fd);
 
-    string str;
-    str.resize(len);
-    readFull(fd, (unsigned char *) &str[0], len);
+  string str;
+  str.resize (len);
+  readFull (fd, (unsigned char *)&str[0], len);
 
-    if (code == 0)
-	return str;
-    else
-	throw Error(str);
+  if (code == 0)
+    return str;
+  else
+    throw Error (str);
 }
 
 /* Sign HASH with the key stored in file SECRETKEY.  Return the signature as a
    string, or raise an exception upon error.  */
-static std::string signHash(const string &secretKey, const Hash &hash)
+static std::string
+signHash (const string &secretKey, const Hash &hash)
 {
-    auto agent = authenticationAgent();
-    auto hexHash = printHash(hash);
+  auto agent = authenticationAgent ();
+  auto hexHash = printHash (hash);
 
-    writeLine(agent->toAgent.writeSide,
-	      (format("sign %1%:%2% %3%:%4%")
-	       % secretKey.size() % secretKey
-	       % hexHash.size() % hexHash).str());
+  writeLine (agent->toAgent.writeSide,
+             (format ("sign %1%:%2% %3%:%4%") % secretKey.size () % secretKey
+              % hexHash.size () % hexHash)
+                 .str ());
 
-    return readAuthenticateReply(agent->fromAgent.readSide);
+  return readAuthenticateReply (agent->fromAgent.readSide);
 }
 
 /* Verify SIGNATURE and return the base16-encoded hash over which it was
    computed.  */
-static std::string verifySignature(const string &signature)
+static std::string
+verifySignature (const string &signature)
 {
-    auto agent = authenticationAgent();
+  auto agent = authenticationAgent ();
 
-    writeLine(agent->toAgent.writeSide,
-	      (format("verify %1%:%2%")
-	       % signature.size() % signature).str());
+  writeLine (
+      agent->toAgent.writeSide,
+      (format ("verify %1%:%2%") % signature.size () % signature).str ());
 
-    return readAuthenticateReply(agent->fromAgent.readSide);
+  return readAuthenticateReply (agent->fromAgent.readSide);
 }
 
-void LocalStore::exportPath(const Path & path, bool sign,
-    Sink & sink)
+void
+LocalStore::exportPath (const Path &path, bool sign, Sink &sink)
 {
-    assertStorePath(path);
+  assertStorePath (path);
 
-    printMsg(lvlInfo, format("exporting path `%1%'") % path);
+  printMsg (lvlInfo, format ("exporting path `%1%'") % path);
 
-    if (!isValidPath(path))
-        throw Error(format("path `%1%' is not valid") % path);
+  if (!isValidPath (path))
+    throw Error (format ("path `%1%' is not valid") % path);
 
-    HashAndWriteSink hashAndWriteSink(sink);
+  HashAndWriteSink hashAndWriteSink (sink);
 
-    dumpPath(path, hashAndWriteSink);
+  dumpPath (path, hashAndWriteSink);
 
-    /* Refuse to export paths that have changed.  This prevents
-       filesystem corruption from spreading to other machines.
-       Don't complain if the stored hash is zero (unknown). */
-    Hash hash = hashAndWriteSink.currentHash();
-    Hash storedHash = queryPathHash(path);
-    if (hash != storedHash && storedHash != Hash(storedHash.type))
-        throw Error(format("hash of path `%1%' has changed from `%2%' to `%3%'!") % path
-            % printHash(storedHash) % printHash(hash));
+  /* Refuse to export paths that have changed.  This prevents
+     filesystem corruption from spreading to other machines.
+     Don't complain if the stored hash is zero (unknown). */
+  Hash hash = hashAndWriteSink.currentHash ();
+  Hash storedHash = queryPathHash (path);
+  if (hash != storedHash && storedHash != Hash (storedHash.type))
+    throw Error (format ("hash of path `%1%' has changed from `%2%' to `%3%'!")
+                 % path % printHash (storedHash) % printHash (hash));
 
-    writeInt(EXPORT_MAGIC, hashAndWriteSink);
+  writeInt (EXPORT_MAGIC, hashAndWriteSink);
 
-    writeString(path, hashAndWriteSink);
+  writeString (path, hashAndWriteSink);
 
-    PathSet references;
-    queryReferences(path, references);
-    writeStrings(references, hashAndWriteSink);
+  PathSet references;
+  queryReferences (path, references);
+  writeStrings (references, hashAndWriteSink);
 
-    Path deriver = queryDeriver(path);
-    writeString(deriver, hashAndWriteSink);
+  Path deriver = queryDeriver (path);
+  writeString (deriver, hashAndWriteSink);
 
-    if (sign) {
-        Hash hash = hashAndWriteSink.currentHash();
-
-        writeInt(1, hashAndWriteSink);
+  if (sign)
+    {
+      Hash hash = hashAndWriteSink.currentHash ();
 
-        Path secretKey = settings.nixConfDir + "/signing-key.sec";
-        checkSecrecy(secretKey);
+      writeInt (1, hashAndWriteSink);
 
-	string signature = signHash(secretKey, hash);
+      Path secretKey = settings.nixConfDir + "/signing-key.sec";
+      checkSecrecy (secretKey);
 
-        writeString(signature, hashAndWriteSink);
+      string signature = signHash (secretKey, hash);
 
-    } else
-        writeInt(0, hashAndWriteSink);
+      writeString (signature, hashAndWriteSink);
+    }
+  else
+    writeInt (0, hashAndWriteSink);
 }
 
-
 struct HashAndReadSource : Source
 {
-    Source & readSource;
-    HashSink hashSink;
-    bool hashing;
-    HashAndReadSource(Source & readSource) : readSource(readSource), hashSink(htSHA256)
-    {
-        hashing = true;
-    }
-    size_t read(unsigned char * data, size_t len)
-    {
-        size_t n = readSource.read(data, len);
-        if (hashing) hashSink(data, n);
-        return n;
-    }
+  Source &readSource;
+  HashSink hashSink;
+  bool hashing;
+  HashAndReadSource (Source &readSource)
+      : readSource (readSource), hashSink (htSHA256)
+  {
+    hashing = true;
+  }
+  size_t
+  read (unsigned char *data, size_t len)
+  {
+    size_t n = readSource.read (data, len);
+    if (hashing)
+      hashSink (data, n);
+    return n;
+  }
 };
 
-
 /* Create a temporary directory in the store that won't be
    garbage-collected. */
-Path LocalStore::createTempDirInStore()
+Path
+LocalStore::createTempDirInStore ()
 {
-    Path tmpDir;
-    do {
-        /* There is a slight possibility that `tmpDir' gets deleted by
-           the GC between createTempDir() and addTempRoot(), so repeat
-           until `tmpDir' exists. */
-        tmpDir = createTempDir(settings.nixStore);
-        addTempRoot(tmpDir);
-    } while (!pathExists(tmpDir));
-    return tmpDir;
+  Path tmpDir;
+  do
+    {
+      /* There is a slight possibility that `tmpDir' gets deleted by
+         the GC between createTempDir() and addTempRoot(), so repeat
+         until `tmpDir' exists. */
+      tmpDir = createTempDir (settings.nixStore);
+      addTempRoot (tmpDir);
+    }
+  while (!pathExists (tmpDir));
+  return tmpDir;
 }
 
-
-Path LocalStore::importPath(bool requireSignature, Source & source)
+Path
+LocalStore::importPath (bool requireSignature, Source &source)
 {
-    HashAndReadSource hashAndReadSource(source);
+  HashAndReadSource hashAndReadSource (source);
 
-    /* We don't yet know what store path this archive contains (the
-       store path follows the archive data proper), and besides, we
-       don't know yet whether the signature is valid. */
-    Path tmpDir = createTempDirInStore();
-    AutoDelete delTmp(tmpDir);
-    Path unpacked = tmpDir + "/unpacked";
+  /* We don't yet know what store path this archive contains (the
+     store path follows the archive data proper), and besides, we
+     don't know yet whether the signature is valid. */
+  Path tmpDir = createTempDirInStore ();
+  AutoDelete delTmp (tmpDir);
+  Path unpacked = tmpDir + "/unpacked";
 
-    restorePath(unpacked, hashAndReadSource);
+  restorePath (unpacked, hashAndReadSource);
 
-    unsigned int magic = readInt(hashAndReadSource);
-    if (magic != EXPORT_MAGIC)
-        throw Error("normalized archive cannot be imported; wrong format");
+  unsigned int magic = readInt (hashAndReadSource);
+  if (magic != EXPORT_MAGIC)
+    throw Error ("normalized archive cannot be imported; wrong format");
 
-    Path dstPath = readStorePath(hashAndReadSource);
+  Path dstPath = readStorePath (hashAndReadSource);
 
-    PathSet references = readStorePaths<PathSet>(hashAndReadSource);
+  PathSet references = readStorePaths<PathSet> (hashAndReadSource);
 
-    Path deriver = readString(hashAndReadSource);
-    if (deriver != "") assertStorePath(deriver);
+  Path deriver = readString (hashAndReadSource);
+  if (deriver != "")
+    assertStorePath (deriver);
 
-    Hash hash = hashAndReadSource.hashSink.finish().first;
-    hashAndReadSource.hashing = false;
+  Hash hash = hashAndReadSource.hashSink.finish ().first;
+  hashAndReadSource.hashing = false;
 
-    bool haveSignature = readInt(hashAndReadSource) == 1;
+  bool haveSignature = readInt (hashAndReadSource) == 1;
 
-    if (requireSignature && !haveSignature)
-        throw Error(format("imported archive of `%1%' lacks a signature") % dstPath);
+  if (requireSignature && !haveSignature)
+    throw Error (format ("imported archive of `%1%' lacks a signature")
+                 % dstPath);
 
-    if (haveSignature) {
-        string signature = readString(hashAndReadSource);
+  if (haveSignature)
+    {
+      string signature = readString (hashAndReadSource);
 
-        if (requireSignature) {
-	    string hash2 = verifySignature(signature);
+      if (requireSignature)
+        {
+          string hash2 = verifySignature (signature);
 
-            /* Note: runProgram() throws an exception if the signature
-               is invalid. */
+          /* Note: runProgram() throws an exception if the signature
+             is invalid. */
 
-            if (printHash(hash) != hash2)
-                throw Error(
-                    "signed hash doesn't match actual contents of imported "
-                    "archive; archive could be corrupt, or someone is trying "
-                    "to import a Trojan horse");
+          if (printHash (hash) != hash2)
+            throw Error (
+                "signed hash doesn't match actual contents of imported "
+                "archive; archive could be corrupt, or someone is trying "
+                "to import a Trojan horse");
         }
     }
 
-    /* Do the actual import. */
+  /* Do the actual import. */
 
-    /* !!! way too much code duplication with addTextToStore() etc. */
-    addTempRoot(dstPath);
+  /* !!! way too much code duplication with addTextToStore() etc. */
+  addTempRoot (dstPath);
 
-    if (!isValidPath(dstPath)) {
+  if (!isValidPath (dstPath))
+    {
 
-        PathLocks outputLock;
+      PathLocks outputLock;
 
-        /* Lock the output path.  But don't lock if we're being called
-           from a build hook (whose parent process already acquired a
-           lock on this path). */
-        Strings locksHeld = tokenizeString<Strings>(getEnv("NIX_HELD_LOCKS"));
-        if (find(locksHeld.begin(), locksHeld.end(), dstPath) == locksHeld.end())
-            outputLock.lockPaths(singleton<PathSet, Path>(dstPath));
+      /* Lock the output path.  But don't lock if we're being called
+         from a build hook (whose parent process already acquired a
+         lock on this path). */
+      Strings locksHeld = tokenizeString<Strings> (getEnv ("NIX_HELD_LOCKS"));
+      if (find (locksHeld.begin (), locksHeld.end (), dstPath)
+          == locksHeld.end ())
+        outputLock.lockPaths (singleton<PathSet, Path> (dstPath));
 
-        if (!isValidPath(dstPath)) {
+      if (!isValidPath (dstPath))
+        {
 
-            if (pathExists(dstPath)) deletePath(dstPath);
+          if (pathExists (dstPath))
+            deletePath (dstPath);
 
-            if (rename(unpacked.c_str(), dstPath.c_str()) == -1)
-                throw SysError(format("cannot move `%1%' to `%2%'")
-                    % unpacked % dstPath);
+          if (rename (unpacked.c_str (), dstPath.c_str ()) == -1)
+            throw SysError (format ("cannot move `%1%' to `%2%'") % unpacked
+                            % dstPath);
 
-            canonicalisePathMetaData(dstPath, -1);
+          canonicalisePathMetaData (dstPath, -1);
 
-            /* !!! if we were clever, we could prevent the hashPath()
-               here. */
-            HashResult hash = hashPath(htSHA256, dstPath);
+          /* !!! if we were clever, we could prevent the hashPath()
+             here. */
+          HashResult hash = hashPath (htSHA256, dstPath);
 
-            optimisePath(dstPath); // FIXME: combine with hashPath()
+          optimisePath (dstPath); // FIXME: combine with hashPath()
 
-            ValidPathInfo info;
-            info.path = dstPath;
-            info.hash = hash.first;
-            info.narSize = hash.second;
-            info.references = references;
-            info.deriver = deriver != "" && isValidPath(deriver) ? deriver : "";
-            registerValidPath(info);
+          ValidPathInfo info;
+          info.path = dstPath;
+          info.hash = hash.first;
+          info.narSize = hash.second;
+          info.references = references;
+          info.deriver = deriver != "" && isValidPath (deriver) ? deriver : "";
+          registerValidPath (info);
         }
 
-        outputLock.setDeletion(true);
+      outputLock.setDeletion (true);
     }
 
-    return dstPath;
+  return dstPath;
 }
 
-
-Paths LocalStore::importPaths(bool requireSignature, Source & source)
+Paths
+LocalStore::importPaths (bool requireSignature, Source &source)
 {
-    Paths res;
-    while (true) {
-        unsigned long long n = readLongLong(source);
-        if (n == 0) break;
-        if (n != 1) throw Error("input doesn't look like something created by `nix-store --export'");
-        res.push_back(importPath(requireSignature, source));
+  Paths res;
+  while (true)
+    {
+      unsigned long long n = readLongLong (source);
+      if (n == 0)
+        break;
+      if (n != 1)
+        throw Error ("input doesn't look like something created by `nix-store "
+                     "--export'");
+      res.push_back (importPath (requireSignature, source));
     }
-    return res;
+  return res;
 }
 
-
-void LocalStore::invalidatePathChecked(const Path & path)
+void
+LocalStore::invalidatePathChecked (const Path &path)
 {
-    assertStorePath(path);
+  assertStorePath (path);
 
-    retrySQLite<void>([&]() {
-        SQLiteTxn txn(db);
+  retrySQLite<void> ([&] () {
+    SQLiteTxn txn (db);
 
-        if (isValidPath_(path)) {
-            PathSet referrers; queryReferrers_(path, referrers);
-            referrers.erase(path); /* ignore self-references */
-            if (!referrers.empty())
-                throw PathInUse(format("cannot delete path `%1%' because it is in use by %2%")
-                    % path % showPaths(referrers));
-            invalidatePath(path);
-        }
+    if (isValidPath_ (path))
+      {
+        PathSet referrers;
+        queryReferrers_ (path, referrers);
+        referrers.erase (path); /* ignore self-references */
+        if (!referrers.empty ())
+          throw PathInUse (
+              format ("cannot delete path `%1%' because it is in use by %2%")
+              % path % showPaths (referrers));
+        invalidatePath (path);
+      }
 
-        txn.commit();
-    });
+    txn.commit ();
+  });
 }
 
-
-bool LocalStore::verifyStore(bool checkContents, bool repair)
+bool
+LocalStore::verifyStore (bool checkContents, bool repair)
 {
-    printMsg(lvlError, format("reading the store..."));
-
-    bool errors = false;
-
-    /* Acquire the global GC lock to prevent a garbage collection. */
-    AutoCloseFD fdGCLock = openGCLock(ltWrite);
+  printMsg (lvlError, format ("reading the store..."));
 
-    PathSet store;
-    for (auto & i : readDirectory(settings.nixStore)) store.insert(i.name);
+  bool errors = false;
 
-    /* Check whether all valid paths actually exist. */
-    printMsg(lvlInfo, "checking path existence...");
+  /* Acquire the global GC lock to prevent a garbage collection. */
+  AutoCloseFD fdGCLock = openGCLock (ltWrite);
 
-    PathSet validPaths2 = queryAllValidPaths(), validPaths, done;
+  PathSet store;
+  for (auto &i : readDirectory (settings.nixStore))
+    store.insert (i.name);
 
-    foreach (PathSet::iterator, i, validPaths2)
-        verifyPath(*i, store, done, validPaths, repair, errors);
+  /* Check whether all valid paths actually exist. */
+  printMsg (lvlInfo, "checking path existence...");
 
-    /* Release the GC lock so that checking content hashes (which can
-       take ages) doesn't block the GC or builds. */
-    fdGCLock.close();
+  PathSet validPaths2 = queryAllValidPaths (), validPaths, done;
 
-    /* Optionally, check the content hashes (slow). */
-    if (checkContents) {
-        printMsg(lvlInfo, "checking hashes...");
+  foreach (PathSet::iterator, i, validPaths2)
+    verifyPath (*i, store, done, validPaths, repair, errors);
 
-        Hash nullHash(htSHA256);
+  /* Release the GC lock so that checking content hashes (which can
+     take ages) doesn't block the GC or builds. */
+  fdGCLock.close ();
 
-        foreach (PathSet::iterator, i, validPaths) {
-            try {
-                ValidPathInfo info = queryPathInfo(*i);
-
-                /* Check the content hash (optionally - slow). */
-                printMsg(lvlTalkative, format("checking contents of `%1%'") % *i);
-                HashResult current = hashPath(info.hash.type, *i);
-
-                if (info.hash != nullHash && info.hash != current.first) {
-                    printMsg(lvlError, format("path `%1%' was modified! "
-                            "expected hash `%2%', got `%3%'")
-                        % *i % printHash(info.hash) % printHash(current.first));
-                    if (repair) repairPath(*i); else errors = true;
-                } else {
+  /* Optionally, check the content hashes (slow). */
+  if (checkContents)
+    {
+      printMsg (lvlInfo, "checking hashes...");
 
-                    bool update = false;
+      Hash nullHash (htSHA256);
 
-                    /* Fill in missing hashes. */
-                    if (info.hash == nullHash) {
-                        printMsg(lvlError, format("fixing missing hash on `%1%'") % *i);
-                        info.hash = current.first;
-                        update = true;
+      foreach (PathSet::iterator, i, validPaths)
+        {
+          try
+            {
+              ValidPathInfo info = queryPathInfo (*i);
+
+              /* Check the content hash (optionally - slow). */
+              printMsg (lvlTalkative,
+                        format ("checking contents of `%1%'") % *i);
+              HashResult current = hashPath (info.hash.type, *i);
+
+              if (info.hash != nullHash && info.hash != current.first)
+                {
+                  printMsg (lvlError, format ("path `%1%' was modified! "
+                                              "expected hash `%2%', got `%3%'")
+                                          % *i % printHash (info.hash)
+                                          % printHash (current.first));
+                  if (repair)
+                    repairPath (*i);
+                  else
+                    errors = true;
+                }
+              else
+                {
+
+                  bool update = false;
+
+                  /* Fill in missing hashes. */
+                  if (info.hash == nullHash)
+                    {
+                      printMsg (lvlError,
+                                format ("fixing missing hash on `%1%'") % *i);
+                      info.hash = current.first;
+                      update = true;
                     }
 
-                    /* Fill in missing narSize fields (from old stores). */
-                    if (info.narSize == 0) {
-                        printMsg(lvlError, format("updating size field on `%1%' to %2%") % *i % current.second);
-                        info.narSize = current.second;
-                        update = true;
+                  /* Fill in missing narSize fields (from old stores). */
+                  if (info.narSize == 0)
+                    {
+                      printMsg (lvlError,
+                                format ("updating size field on `%1%' to %2%")
+                                    % *i % current.second);
+                      info.narSize = current.second;
+                      update = true;
                     }
 
-                    if (update) updatePathInfo(info);
-
+                  if (update)
+                    updatePathInfo (info);
                 }
-
-            } catch (Error & e) {
-                /* It's possible that the path got GC'ed, so ignore
-                   errors on invalid paths. */
-                if (isValidPath(*i))
-                    printMsg(lvlError, format("error: %1%") % e.msg());
-                else
-                    printMsg(lvlError, format("warning: %1%") % e.msg());
-                errors = true;
+            }
+          catch (Error &e)
+            {
+              /* It's possible that the path got GC'ed, so ignore
+                 errors on invalid paths. */
+              if (isValidPath (*i))
+                printMsg (lvlError, format ("error: %1%") % e.msg ());
+              else
+                printMsg (lvlError, format ("warning: %1%") % e.msg ());
+              errors = true;
             }
         }
     }
 
-    return errors;
+  return errors;
 }
 
-
-void LocalStore::verifyPath(const Path & path, const PathSet & store,
-    PathSet & done, PathSet & validPaths, bool repair, bool & errors)
+void
+LocalStore::verifyPath (const Path &path, const PathSet &store, PathSet &done,
+                        PathSet &validPaths, bool repair, bool &errors)
 {
-    checkInterrupt();
+  checkInterrupt ();
 
-    if (done.find(path) != done.end()) return;
-    done.insert(path);
+  if (done.find (path) != done.end ())
+    return;
+  done.insert (path);
 
-    if (!isStorePath(path)) {
-        printMsg(lvlError, format("path `%1%' is not in the store") % path);
-        invalidatePath(path);
-        return;
+  if (!isStorePath (path))
+    {
+      printMsg (lvlError, format ("path `%1%' is not in the store") % path);
+      invalidatePath (path);
+      return;
     }
 
-    if (store.find(baseNameOf(path)) == store.end()) {
-        /* Check any referrers first.  If we can invalidate them
-           first, then we can invalidate this path as well. */
-        bool canInvalidate = true;
-        PathSet referrers; queryReferrers(path, referrers);
-        foreach (PathSet::iterator, i, referrers)
-            if (*i != path) {
-                verifyPath(*i, store, done, validPaths, repair, errors);
-                if (validPaths.find(*i) != validPaths.end())
-                    canInvalidate = false;
-            }
-
-        if (canInvalidate) {
-            printMsg(lvlError, format("path `%1%' disappeared, removing from database...") % path);
-            invalidatePath(path);
-        } else {
-            printMsg(lvlError, format("path `%1%' disappeared, but it still has valid referrers!") % path);
-            if (repair)
-                try {
-                    repairPath(path);
-                } catch (Error & e) {
-                    printMsg(lvlError, format("warning: %1%") % e.msg());
-                    errors = true;
-                }
-            else errors = true;
+  if (store.find (baseNameOf (path)) == store.end ())
+    {
+      /* Check any referrers first.  If we can invalidate them
+         first, then we can invalidate this path as well. */
+      bool canInvalidate = true;
+      PathSet referrers;
+      queryReferrers (path, referrers);
+      foreach (PathSet::iterator, i, referrers)
+        if (*i != path)
+          {
+            verifyPath (*i, store, done, validPaths, repair, errors);
+            if (validPaths.find (*i) != validPaths.end ())
+              canInvalidate = false;
+          }
+
+      if (canInvalidate)
+        {
+          printMsg (
+              lvlError,
+              format ("path `%1%' disappeared, removing from database...")
+                  % path);
+          invalidatePath (path);
+        }
+      else
+        {
+          printMsg (
+              lvlError,
+              format (
+                  "path `%1%' disappeared, but it still has valid referrers!")
+                  % path);
+          if (repair)
+            try
+              {
+                repairPath (path);
+              }
+            catch (Error &e)
+              {
+                printMsg (lvlError, format ("warning: %1%") % e.msg ());
+                errors = true;
+              }
+          else
+            errors = true;
         }
 
-        return;
+      return;
     }
 
-    validPaths.insert(path);
+  validPaths.insert (path);
 }
 
-
-bool LocalStore::pathContentsGood(const Path & path)
+bool
+LocalStore::pathContentsGood (const Path &path)
 {
-    std::map<Path, bool>::iterator i = pathContentsGoodCache.find(path);
-    if (i != pathContentsGoodCache.end()) return i->second;
-    printMsg(lvlInfo, format("checking path `%1%'...") % path);
-    ValidPathInfo info = queryPathInfo(path);
-    bool res;
-    if (!pathExists(path))
-        res = false;
-    else {
-        HashResult current = hashPath(info.hash.type, path);
-        Hash nullHash(htSHA256);
-        res = info.hash == nullHash || info.hash == current.first;
+  std::map<Path, bool>::iterator i = pathContentsGoodCache.find (path);
+  if (i != pathContentsGoodCache.end ())
+    return i->second;
+  printMsg (lvlInfo, format ("checking path `%1%'...") % path);
+  ValidPathInfo info = queryPathInfo (path);
+  bool res;
+  if (!pathExists (path))
+    res = false;
+  else
+    {
+      HashResult current = hashPath (info.hash.type, path);
+      Hash nullHash (htSHA256);
+      res = info.hash == nullHash || info.hash == current.first;
     }
-    pathContentsGoodCache[path] = res;
-    if (!res) printMsg(lvlError, format("path `%1%' is corrupted or missing!") % path);
-    return res;
+  pathContentsGoodCache[path] = res;
+  if (!res)
+    printMsg (lvlError, format ("path `%1%' is corrupted or missing!") % path);
+  return res;
 }
 
-
-void LocalStore::markContentsGood(const Path & path)
+void
+LocalStore::markContentsGood (const Path &path)
 {
-    pathContentsGoodCache[path] = true;
+  pathContentsGoodCache[path] = true;
 }
 
-
-void LocalStore::vacuumDB()
+void
+LocalStore::vacuumDB ()
 {
-    if (sqlite3_exec(db, "vacuum;", 0, 0, 0) != SQLITE_OK)
-        throwSQLiteError(db, "vacuuming SQLite database");
+  if (sqlite3_exec (db, "vacuum;", 0, 0, 0) != SQLITE_OK)
+    throwSQLiteError (db, "vacuuming SQLite database");
 }
 
-
-void LocalStore::createUser(const std::string & userName, uid_t userId)
+void
+LocalStore::createUser (const std::string &userName, uid_t userId)
 {
-    auto dir = settings.nixStateDir + "/profiles/per-user/" + userName;
+  auto dir = settings.nixStateDir + "/profiles/per-user/" + userName;
 
-    auto created = createDirs(dir);
-    if (!created.empty()) {
-	if (chmod(dir.c_str(), 0755) == -1)
-	    throw SysError(format("changing permissions of directory '%s'") % dir);
-
-	/* The following operation requires CAP_CHOWN or can be handled
-	   manually by a user with CAP_CHOWN.  */
-	if (chown(dir.c_str(), userId, -1) == -1) {
-	    rmdir(dir.c_str());
-	    string message = strerror(errno);
-	    printMsg(lvlInfo, format("failed to change owner of directory '%1%' to %2%: %3%") % dir % userId % message);
-	}
+  auto created = createDirs (dir);
+  if (!created.empty ())
+    {
+      if (chmod (dir.c_str (), 0755) == -1)
+        throw SysError (format ("changing permissions of directory '%s'")
+                        % dir);
+
+      /* The following operation requires CAP_CHOWN or can be handled
+         manually by a user with CAP_CHOWN.  */
+      if (chown (dir.c_str (), userId, -1) == -1)
+        {
+          rmdir (dir.c_str ());
+          string message = strerror (errno);
+          printMsg (
+              lvlInfo,
+              format ("failed to change owner of directory '%1%' to %2%: %3%")
+                  % dir % userId % message);
+        }
     }
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libstore/local-store.hh b/nix/libstore/local-store.hh
index 20d3c3c893..7b48bfe4aa 100644
--- a/nix/libstore/local-store.hh
+++ b/nix/libstore/local-store.hh
@@ -8,9 +8,8 @@
 #include "store-api.hh"
 #include "util.hh"
 
-
-namespace nix {
-
+namespace nix
+{
 
 /* Nix store and database schema version.  Version 1 (or 0) was Nix <=
    0.7.  Version 2 was Nix 0.8 and 0.9.  Version 3 is Nix 0.10.
@@ -18,265 +17,260 @@ namespace nix {
    Nix 1.0.  Version 7 is Nix 1.3.  Guix has always used version 7.  */
 const int nixSchemaVersion = 7;
 
-
 extern string drvsLogDir;
 
-
 struct Derivation;
 
-
 struct OptimiseStats
 {
-    unsigned long filesLinked;
-    unsigned long long bytesFreed;
-    unsigned long long blocksFreed;
-    OptimiseStats()
-    {
-        filesLinked = 0;
-        bytesFreed = blocksFreed = 0;
-    }
+  unsigned long filesLinked;
+  unsigned long long bytesFreed;
+  unsigned long long blocksFreed;
+  OptimiseStats ()
+  {
+    filesLinked = 0;
+    bytesFreed = blocksFreed = 0;
+  }
 };
 
-
 class LocalStore : public StoreAPI
 {
 private:
-    /* The currently running substituter or empty.  */
-    std::shared_ptr<Agent> runningSubstituter;
+  /* The currently running substituter or empty.  */
+  std::shared_ptr<Agent> runningSubstituter;
 
-    /* Ensure the substituter is running and return it.  */
-    std::shared_ptr<Agent> substituter();
+  /* Ensure the substituter is running and return it.  */
+  std::shared_ptr<Agent> substituter ();
 
-    Path linksDir;
+  Path linksDir;
 
 public:
+  /* Initialise the local store, upgrading the schema if
+     necessary. */
+  LocalStore (bool reserveSpace = true);
 
-    /* Initialise the local store, upgrading the schema if
-       necessary. */
-    LocalStore(bool reserveSpace = true);
-
-    ~LocalStore();
+  ~LocalStore ();
 
-    /* Implementations of abstract store API methods. */
+  /* Implementations of abstract store API methods. */
 
-    bool isValidPath(const Path & path);
+  bool isValidPath (const Path &path);
 
-    PathSet queryValidPaths(const PathSet & paths);
+  PathSet queryValidPaths (const PathSet &paths);
 
-    PathSet queryAllValidPaths();
+  PathSet queryAllValidPaths ();
 
-    ValidPathInfo queryPathInfo(const Path & path);
+  ValidPathInfo queryPathInfo (const Path &path);
 
-    Hash queryPathHash(const Path & path);
+  Hash queryPathHash (const Path &path);
 
-    void queryReferences(const Path & path, PathSet & references);
+  void queryReferences (const Path &path, PathSet &references);
 
-    void queryReferrers(const Path & path, PathSet & referrers);
+  void queryReferrers (const Path &path, PathSet &referrers);
 
-    Path queryDeriver(const Path & path);
+  Path queryDeriver (const Path &path);
 
-    PathSet queryValidDerivers(const Path & path);
+  PathSet queryValidDerivers (const Path &path);
 
-    PathSet queryDerivationOutputs(const Path & path);
+  PathSet queryDerivationOutputs (const Path &path);
 
-    StringSet queryDerivationOutputNames(const Path & path);
+  StringSet queryDerivationOutputNames (const Path &path);
 
-    Path queryPathFromHashPart(const string & hashPart);
+  Path queryPathFromHashPart (const string &hashPart);
 
-    PathSet querySubstitutablePaths(const PathSet & paths);
+  PathSet querySubstitutablePaths (const PathSet &paths);
 
-    void querySubstitutablePathInfos(PathSet & paths,
-        SubstitutablePathInfos & infos);
+  void querySubstitutablePathInfos (PathSet &paths,
+                                    SubstitutablePathInfos &infos);
 
-    void querySubstitutablePathInfos(const PathSet & paths,
-        SubstitutablePathInfos & infos);
+  void querySubstitutablePathInfos (const PathSet &paths,
+                                    SubstitutablePathInfos &infos);
 
-    Path addToStore(const string & name, const Path & srcPath,
-        bool recursive = true, HashType hashAlgo = htSHA256,
-        PathFilter & filter = defaultPathFilter, bool repair = false);
+  Path addToStore (const string &name, const Path &srcPath,
+                   bool recursive = true, HashType hashAlgo = htSHA256,
+                   PathFilter &filter = defaultPathFilter,
+                   bool repair = false);
 
-    /* Like addToStore(), but the contents of the path are contained
-       in `dump', which is either a NAR serialisation (if recursive ==
-       true) or simply the contents of a regular file (if recursive ==
-       false). */
-    Path addToStoreFromDump(const string & dump, const string & name,
-        bool recursive = true, HashType hashAlgo = htSHA256, bool repair = false);
+  /* Like addToStore(), but the contents of the path are contained
+     in `dump', which is either a NAR serialisation (if recursive ==
+     true) or simply the contents of a regular file (if recursive ==
+     false). */
+  Path addToStoreFromDump (const string &dump, const string &name,
+                           bool recursive = true, HashType hashAlgo = htSHA256,
+                           bool repair = false);
 
-    Path addTextToStore(const string & name, const string & s,
-        const PathSet & references, bool repair = false);
+  Path addTextToStore (const string &name, const string &s,
+                       const PathSet &references, bool repair = false);
 
-    void exportPath(const Path & path, bool sign,
-        Sink & sink);
+  void exportPath (const Path &path, bool sign, Sink &sink);
 
-    Paths importPaths(bool requireSignature, Source & source);
+  Paths importPaths (bool requireSignature, Source &source);
 
-    void buildPaths(const PathSet & paths, BuildMode buildMode);
+  void buildPaths (const PathSet &paths, BuildMode buildMode);
 
-    void ensurePath(const Path & path);
+  void ensurePath (const Path &path);
 
-    void addTempRoot(const Path & path);
+  void addTempRoot (const Path &path);
 
-    void addIndirectRoot(const Path & path);
+  void addIndirectRoot (const Path &path);
 
-    void syncWithGC();
+  void syncWithGC ();
 
-    Roots findRoots();
+  Roots findRoots ();
 
-    void collectGarbage(const GCOptions & options, GCResults & results);
+  void collectGarbage (const GCOptions &options, GCResults &results);
 
-    /* Optimise the disk space usage of the Nix store by hard-linking
-       files with the same contents. */
-    void optimiseStore(OptimiseStats & stats);
+  /* Optimise the disk space usage of the Nix store by hard-linking
+     files with the same contents. */
+  void optimiseStore (OptimiseStats &stats);
 
-    /* Generic variant of the above method.  */
-    void optimiseStore();
+  /* Generic variant of the above method.  */
+  void optimiseStore ();
 
-    /* Optimise a single store path. */
-    void optimisePath(const Path & path);
+  /* Optimise a single store path. */
+  void optimisePath (const Path &path);
 
-    /* Check the integrity of the Nix store.  Returns true if errors
-       remain. */
-    bool verifyStore(bool checkContents, bool repair);
+  /* Check the integrity of the Nix store.  Returns true if errors
+     remain. */
+  bool verifyStore (bool checkContents, bool repair);
 
-    /* Register the validity of a path, i.e., that `path' exists, that
-       the paths referenced by it exists, and in the case of an output
-       path of a derivation, that it has been produced by a successful
-       execution of the derivation (or something equivalent).  Also
-       register the hash of the file system contents of the path.  The
-       hash must be a SHA-256 hash. */
-    void registerValidPath(const ValidPathInfo & info);
+  /* Register the validity of a path, i.e., that `path' exists, that
+     the paths referenced by it exists, and in the case of an output
+     path of a derivation, that it has been produced by a successful
+     execution of the derivation (or something equivalent).  Also
+     register the hash of the file system contents of the path.  The
+     hash must be a SHA-256 hash. */
+  void registerValidPath (const ValidPathInfo &info);
 
-    void registerValidPaths(const ValidPathInfos & infos);
+  void registerValidPaths (const ValidPathInfos &infos);
 
-    /* Register that the build of a derivation with output `path' has
-       failed. */
-    void registerFailedPath(const Path & path);
+  /* Register that the build of a derivation with output `path' has
+     failed. */
+  void registerFailedPath (const Path &path);
 
-    /* Query whether `path' previously failed to build. */
-    bool hasPathFailed(const Path & path);
+  /* Query whether `path' previously failed to build. */
+  bool hasPathFailed (const Path &path);
 
-    PathSet queryFailedPaths();
+  PathSet queryFailedPaths ();
 
-    void clearFailedPaths(const PathSet & paths);
+  void clearFailedPaths (const PathSet &paths);
 
-    void vacuumDB();
+  void vacuumDB ();
 
-    /* Repair the contents of the given path by redownloading it using
-       a substituter (if available). */
-    void repairPath(const Path & path);
+  /* Repair the contents of the given path by redownloading it using
+     a substituter (if available). */
+  void repairPath (const Path &path);
 
-    /* Check whether the given valid path exists and has the right
-       contents. */
-    bool pathContentsGood(const Path & path);
+  /* Check whether the given valid path exists and has the right
+     contents. */
+  bool pathContentsGood (const Path &path);
 
-    void markContentsGood(const Path & path);
+  void markContentsGood (const Path &path);
 
-    void createUser(const std::string & userName, uid_t userId);
+  void createUser (const std::string &userName, uid_t userId);
 
 private:
+  Path schemaPath;
 
-    Path schemaPath;
-
-    /* Lock file used for upgrading. */
-    AutoCloseFD globalLock;
+  /* Lock file used for upgrading. */
+  AutoCloseFD globalLock;
 
-    /* The SQLite database object. */
-    SQLite db;
+  /* The SQLite database object. */
+  SQLite db;
 
-    /* Some precompiled SQLite statements. */
-    SQLiteStmt stmtRegisterValidPath;
-    SQLiteStmt stmtUpdatePathInfo;
-    SQLiteStmt stmtAddReference;
-    SQLiteStmt stmtQueryPathInfo;
-    SQLiteStmt stmtQueryReferences;
-    SQLiteStmt stmtQueryReferrers;
-    SQLiteStmt stmtInvalidatePath;
-    SQLiteStmt stmtRegisterFailedPath;
-    SQLiteStmt stmtHasPathFailed;
-    SQLiteStmt stmtQueryFailedPaths;
-    SQLiteStmt stmtClearFailedPath;
-    SQLiteStmt stmtAddDerivationOutput;
-    SQLiteStmt stmtQueryValidDerivers;
-    SQLiteStmt stmtQueryDerivationOutputs;
-    SQLiteStmt stmtQueryPathFromHashPart;
-    SQLiteStmt stmtQueryValidPaths;
+  /* Some precompiled SQLite statements. */
+  SQLiteStmt stmtRegisterValidPath;
+  SQLiteStmt stmtUpdatePathInfo;
+  SQLiteStmt stmtAddReference;
+  SQLiteStmt stmtQueryPathInfo;
+  SQLiteStmt stmtQueryReferences;
+  SQLiteStmt stmtQueryReferrers;
+  SQLiteStmt stmtInvalidatePath;
+  SQLiteStmt stmtRegisterFailedPath;
+  SQLiteStmt stmtHasPathFailed;
+  SQLiteStmt stmtQueryFailedPaths;
+  SQLiteStmt stmtClearFailedPath;
+  SQLiteStmt stmtAddDerivationOutput;
+  SQLiteStmt stmtQueryValidDerivers;
+  SQLiteStmt stmtQueryDerivationOutputs;
+  SQLiteStmt stmtQueryPathFromHashPart;
+  SQLiteStmt stmtQueryValidPaths;
 
-    /* Cache for pathContentsGood(). */
-    std::map<Path, bool> pathContentsGoodCache;
+  /* Cache for pathContentsGood(). */
+  std::map<Path, bool> pathContentsGoodCache;
 
-    /* The file to which we write our temporary roots. */
-    Path fnTempRoots;
-    AutoCloseFD fdTempRoots;
+  /* The file to which we write our temporary roots. */
+  Path fnTempRoots;
+  AutoCloseFD fdTempRoots;
 
-    int getSchema();
+  int getSchema ();
 
-    void openDB(bool create);
+  void openDB (bool create);
 
-    void makeStoreWritable();
+  void makeStoreWritable ();
 
-    uint64_t queryValidPathId(const Path & path);
+  uint64_t queryValidPathId (const Path &path);
 
-    uint64_t addValidPath(const ValidPathInfo & info, bool checkOutputs = true);
+  uint64_t addValidPath (const ValidPathInfo &info, bool checkOutputs = true);
 
-    void addReference(uint64_t referrer, uint64_t reference);
+  void addReference (uint64_t referrer, uint64_t reference);
 
-    void appendReferrer(const Path & from, const Path & to, bool lock);
+  void appendReferrer (const Path &from, const Path &to, bool lock);
 
-    void rewriteReferrers(const Path & path, bool purge, PathSet referrers);
+  void rewriteReferrers (const Path &path, bool purge, PathSet referrers);
 
-    void invalidatePath(const Path & path);
+  void invalidatePath (const Path &path);
 
-    /* Delete a path from the Nix store. */
-    void invalidatePathChecked(const Path & path);
+  /* Delete a path from the Nix store. */
+  void invalidatePathChecked (const Path &path);
 
-    void verifyPath(const Path & path, const PathSet & store,
-        PathSet & done, PathSet & validPaths, bool repair, bool & errors);
+  void verifyPath (const Path &path, const PathSet &store, PathSet &done,
+                   PathSet &validPaths, bool repair, bool &errors);
 
-    void updatePathInfo(const ValidPathInfo & info);
+  void updatePathInfo (const ValidPathInfo &info);
 
-    struct GCState;
+  struct GCState;
 
-    void deleteGarbage(GCState & state, const Path & path);
+  void deleteGarbage (GCState &state, const Path &path);
 
-    void tryToDelete(GCState & state, const Path & path);
+  void tryToDelete (GCState &state, const Path &path);
 
-    bool canReachRoot(GCState & state, PathSet & visited, const Path & path);
+  bool canReachRoot (GCState &state, PathSet &visited, const Path &path);
 
-    void deletePathRecursive(GCState & state, const Path & path);
+  void deletePathRecursive (GCState &state, const Path &path);
 
-    bool isActiveTempFile(const GCState & state,
-        const Path & path, const string & suffix);
+  bool isActiveTempFile (const GCState &state, const Path &path,
+                         const string &suffix);
 
-    int openGCLock(LockType lockType);
+  int openGCLock (LockType lockType);
 
-    void removeUnusedLinks(const GCState & state);
+  void removeUnusedLinks (const GCState &state);
 
-    string getLineFromSubstituter(Agent & run);
+  string getLineFromSubstituter (Agent &run);
 
-    template<class T> T getIntLineFromSubstituter(Agent & run);
+  template <class T> T getIntLineFromSubstituter (Agent &run);
 
-    Path createTempDirInStore();
+  Path createTempDirInStore ();
 
-    Path importPath(bool requireSignature, Source & source);
+  Path importPath (bool requireSignature, Source &source);
 
-    void checkDerivationOutputs(const Path & drvPath, const Derivation & drv);
+  void checkDerivationOutputs (const Path &drvPath, const Derivation &drv);
 
-    typedef std::unordered_set<ino_t> InodeHash;
+  typedef std::unordered_set<ino_t> InodeHash;
 
-    InodeHash loadInodeHash();
-    Strings readDirectoryIgnoringInodes(const Path & path, const InodeHash & inodeHash);
-    void optimisePath_(OptimiseStats & stats, const Path & path, InodeHash & inodeHash);
+  InodeHash loadInodeHash ();
+  Strings readDirectoryIgnoringInodes (const Path &path,
+                                       const InodeHash &inodeHash);
+  void optimisePath_ (OptimiseStats &stats, const Path &path,
+                      InodeHash &inodeHash);
 
-    // Internal versions that are not wrapped in retry_sqlite.
-    bool isValidPath_(const Path & path);
-    void queryReferrers_(const Path & path, PathSet & referrers);
+  // Internal versions that are not wrapped in retry_sqlite.
+  bool isValidPath_ (const Path &path);
+  void queryReferrers_ (const Path &path, PathSet &referrers);
 };
 
-
 typedef std::pair<dev_t, ino_t> Inode;
 typedef set<Inode> InodesSeen;
 
-
 /* "Fix", or canonicalise, the meta-data of the files in a store path
    after it has been built.  In particular:
    - the last modification date on each file is set to 1 (i.e.,
@@ -285,12 +279,13 @@ typedef set<Inode> InodesSeen;
      without execute permission; setuid bits etc. are cleared)
    - the owner and group are set to the Nix user and group, if we're
      running as root. */
-void canonicalisePathMetaData(const Path & path, uid_t fromUid, InodesSeen & inodesSeen);
-void canonicalisePathMetaData(const Path & path, uid_t fromUid);
+void canonicalisePathMetaData (const Path &path, uid_t fromUid,
+                               InodesSeen &inodesSeen);
+void canonicalisePathMetaData (const Path &path, uid_t fromUid);
 
-void canonicaliseTimestampAndPermissions(const Path & path);
+void canonicaliseTimestampAndPermissions (const Path &path);
 
-MakeError(PathInUse, Error);
+MakeError (PathInUse, Error);
 
 /* Size below which a file is not considered for deduplication.  */
 extern const size_t deduplicationMinSize;
diff --git a/nix/libstore/misc.cc b/nix/libstore/misc.cc
index d4e6d1b4af..9a01739d8f 100644
--- a/nix/libstore/misc.cc
+++ b/nix/libstore/misc.cc
@@ -1,98 +1,109 @@
 #include "misc.hh"
-#include "store-api.hh"
-#include "local-store.hh"
 #include "globals.hh"
+#include "local-store.hh"
+#include "store-api.hh"
 
+namespace nix
+{
 
-namespace nix {
-
-
-Derivation derivationFromPath(StoreAPI & store, const Path & drvPath)
+Derivation
+derivationFromPath (StoreAPI &store, const Path &drvPath)
 {
-    assertStorePath(drvPath);
-    store.ensurePath(drvPath);
-    return readDerivation(drvPath);
+  assertStorePath (drvPath);
+  store.ensurePath (drvPath);
+  return readDerivation (drvPath);
 }
 
-
-void computeFSClosure(StoreAPI & store, const Path & path,
-    PathSet & paths, bool flipDirection, bool includeOutputs, bool includeDerivers)
+void
+computeFSClosure (StoreAPI &store, const Path &path, PathSet &paths,
+                  bool flipDirection, bool includeOutputs,
+                  bool includeDerivers)
 {
-    if (paths.find(path) != paths.end()) return;
-    paths.insert(path);
+  if (paths.find (path) != paths.end ())
+    return;
+  paths.insert (path);
 
-    PathSet edges;
+  PathSet edges;
 
-    if (flipDirection) {
-        store.queryReferrers(path, edges);
+  if (flipDirection)
+    {
+      store.queryReferrers (path, edges);
 
-        if (includeOutputs) {
-            PathSet derivers = store.queryValidDerivers(path);
-            foreach (PathSet::iterator, i, derivers)
-                edges.insert(*i);
+      if (includeOutputs)
+        {
+          PathSet derivers = store.queryValidDerivers (path);
+          foreach (PathSet::iterator, i, derivers)
+            edges.insert (*i);
         }
 
-        if (includeDerivers && isDerivation(path)) {
-            PathSet outputs = store.queryDerivationOutputs(path);
-            foreach (PathSet::iterator, i, outputs)
-                if (store.isValidPath(*i) && store.queryDeriver(*i) == path)
-                    edges.insert(*i);
+      if (includeDerivers && isDerivation (path))
+        {
+          PathSet outputs = store.queryDerivationOutputs (path);
+          foreach (PathSet::iterator, i, outputs)
+            if (store.isValidPath (*i) && store.queryDeriver (*i) == path)
+              edges.insert (*i);
         }
-
-    } else {
-        store.queryReferences(path, edges);
-
-        if (includeOutputs && isDerivation(path)) {
-            PathSet outputs = store.queryDerivationOutputs(path);
-            foreach (PathSet::iterator, i, outputs)
-                if (store.isValidPath(*i)) edges.insert(*i);
+    }
+  else
+    {
+      store.queryReferences (path, edges);
+
+      if (includeOutputs && isDerivation (path))
+        {
+          PathSet outputs = store.queryDerivationOutputs (path);
+          foreach (PathSet::iterator, i, outputs)
+            if (store.isValidPath (*i))
+              edges.insert (*i);
         }
 
-        if (includeDerivers) {
-            Path deriver = store.queryDeriver(path);
-            if (store.isValidPath(deriver)) edges.insert(deriver);
+      if (includeDerivers)
+        {
+          Path deriver = store.queryDeriver (path);
+          if (store.isValidPath (deriver))
+            edges.insert (deriver);
         }
     }
 
-    foreach (PathSet::iterator, i, edges)
-        computeFSClosure(store, *i, paths, flipDirection, includeOutputs, includeDerivers);
+  foreach (PathSet::iterator, i, edges)
+    computeFSClosure (store, *i, paths, flipDirection, includeOutputs,
+                      includeDerivers);
 }
 
-
-static void dfsVisit(StoreAPI & store, const PathSet & paths,
-    const Path & path, PathSet & visited, Paths & sorted,
-    PathSet & parents)
+static void
+dfsVisit (StoreAPI &store, const PathSet &paths, const Path &path,
+          PathSet &visited, Paths &sorted, PathSet &parents)
 {
-    if (parents.find(path) != parents.end())
-        throw BuildError(format("cycle detected in the references of `%1%'") % path);
-
-    if (visited.find(path) != visited.end()) return;
-    visited.insert(path);
-    parents.insert(path);
-
-    PathSet references;
-    if (store.isValidPath(path))
-        store.queryReferences(path, references);
-
-    foreach (PathSet::iterator, i, references)
-        /* Don't traverse into paths that don't exist.  That can
-           happen due to substitutes for non-existent paths. */
-        if (*i != path && paths.find(*i) != paths.end())
-            dfsVisit(store, paths, *i, visited, sorted, parents);
-
-    sorted.push_front(path);
-    parents.erase(path);
+  if (parents.find (path) != parents.end ())
+    throw BuildError (format ("cycle detected in the references of `%1%'")
+                      % path);
+
+  if (visited.find (path) != visited.end ())
+    return;
+  visited.insert (path);
+  parents.insert (path);
+
+  PathSet references;
+  if (store.isValidPath (path))
+    store.queryReferences (path, references);
+
+  foreach (PathSet::iterator, i, references)
+    /* Don't traverse into paths that don't exist.  That can
+       happen due to substitutes for non-existent paths. */
+    if (*i != path && paths.find (*i) != paths.end ())
+      dfsVisit (store, paths, *i, visited, sorted, parents);
+
+  sorted.push_front (path);
+  parents.erase (path);
 }
 
-
-Paths topoSortPaths(StoreAPI & store, const PathSet & paths)
+Paths
+topoSortPaths (StoreAPI &store, const PathSet &paths)
 {
-    Paths sorted;
-    PathSet visited, parents;
-    foreach (PathSet::const_iterator, i, paths)
-        dfsVisit(store, paths, *i, visited, sorted, parents);
-    return sorted;
+  Paths sorted;
+  PathSet visited, parents;
+  foreach (PathSet::const_iterator, i, paths)
+    dfsVisit (store, paths, *i, visited, sorted, parents);
+  return sorted;
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libstore/misc.hh b/nix/libstore/misc.hh
index f70cda9fd0..2706df8bba 100644
--- a/nix/libstore/misc.hh
+++ b/nix/libstore/misc.hh
@@ -2,13 +2,12 @@
 
 #include "derivations.hh"
 
-
-namespace nix {
-
+namespace nix
+{
 
 /* Read a derivation, after ensuring its existence through
    ensurePath(). */
-Derivation derivationFromPath(StoreAPI & store, const Path & drvPath);
+Derivation derivationFromPath (StoreAPI &store, const Path &drvPath);
 
 /* Place in `paths' the set of all store paths in the file system
    closure of `storePath'; that is, all paths than can be directly or
@@ -17,13 +16,12 @@ Derivation derivationFromPath(StoreAPI & store, const Path & drvPath);
    `storePath' is returned; that is, the closures under the
    `referrers' relation instead of the `references' relation is
    returned. */
-void computeFSClosure(StoreAPI & store, const Path & path,
-    PathSet & paths, bool flipDirection = false,
-    bool includeOutputs = false, bool includeDerivers = false);
-
-bool willBuildLocally(const Derivation & drv);
+void computeFSClosure (StoreAPI &store, const Path &path, PathSet &paths,
+                       bool flipDirection = false, bool includeOutputs = false,
+                       bool includeDerivers = false);
 
-bool substitutesAllowed(const Derivation & drv);
+bool willBuildLocally (const Derivation &drv);
 
+bool substitutesAllowed (const Derivation &drv);
 
 }
diff --git a/nix/libstore/optimise-store.cc b/nix/libstore/optimise-store.cc
index 9fd6f3cb35..811639efea 100644
--- a/nix/libstore/optimise-store.cc
+++ b/nix/libstore/optimise-store.cc
@@ -1,281 +1,324 @@
 #include "config.h"
 
-#include "util.hh"
-#include "local-store.hh"
 #include "globals.hh"
+#include "local-store.hh"
+#include "util.hh"
 
 #include <cstdlib>
 #include <cstring>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 #include <errno.h>
 #include <stdio.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
 
-
-namespace nix {
+namespace nix
+{
 
 /* Any file smaller than this is not considered for deduplication.
    Keep in sync with (guix store deduplication).  */
 const size_t deduplicationMinSize = 8192;
 
-static void makeWritable(const Path & path)
+static void
+makeWritable (const Path &path)
 {
-    struct stat st;
-    if (lstat(path.c_str(), &st))
-        throw SysError(format("getting attributes of path `%1%'") % path);
-    if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1)
-        throw SysError(format("changing writability of `%1%'") % path);
+  struct stat st;
+  if (lstat (path.c_str (), &st))
+    throw SysError (format ("getting attributes of path `%1%'") % path);
+  if (chmod (path.c_str (), st.st_mode | S_IWUSR) == -1)
+    throw SysError (format ("changing writability of `%1%'") % path);
 }
 
-
 struct MakeReadOnly
 {
-    Path path;
-    MakeReadOnly(const Path & path) : path(path) { }
-    ~MakeReadOnly()
-    {
-        try {
-            /* This will make the path read-only. */
-            if (path != "") canonicaliseTimestampAndPermissions(path);
-        } catch (...) {
-            ignoreException();
-        }
-    }
+  Path path;
+  MakeReadOnly (const Path &path) : path (path) {}
+  ~MakeReadOnly ()
+  {
+    try
+      {
+        /* This will make the path read-only. */
+        if (path != "")
+          canonicaliseTimestampAndPermissions (path);
+      }
+    catch (...)
+      {
+        ignoreException ();
+      }
+  }
 };
 
-
-LocalStore::InodeHash LocalStore::loadInodeHash()
+LocalStore::InodeHash
+LocalStore::loadInodeHash ()
 {
-    printMsg(lvlDebug, "loading hash inodes in memory");
-    InodeHash inodeHash;
-
-    AutoCloseDir dir = opendir(linksDir.c_str());
-    if (!dir) throw SysError(format("opening directory `%1%'") % linksDir);
-
-    struct dirent * dirent;
-    while (errno = 0, dirent = readdir(dir)) { /* sic */
-        checkInterrupt();
-        // We don't care if we hit non-hash files, anything goes
-        inodeHash.insert(dirent->d_ino);
+  printMsg (lvlDebug, "loading hash inodes in memory");
+  InodeHash inodeHash;
+
+  AutoCloseDir dir = opendir (linksDir.c_str ());
+  if (!dir)
+    throw SysError (format ("opening directory `%1%'") % linksDir);
+
+  struct dirent *dirent;
+  while (errno = 0, dirent = readdir (dir))
+    { /* sic */
+      checkInterrupt ();
+      // We don't care if we hit non-hash files, anything goes
+      inodeHash.insert (dirent->d_ino);
     }
-    if (errno) throw SysError(format("reading directory `%1%'") % linksDir);
+  if (errno)
+    throw SysError (format ("reading directory `%1%'") % linksDir);
 
-    printMsg(lvlTalkative, format("loaded %1% hash inodes") % inodeHash.size());
+  printMsg (lvlTalkative,
+            format ("loaded %1% hash inodes") % inodeHash.size ());
 
-    return inodeHash;
+  return inodeHash;
 }
 
-
-Strings LocalStore::readDirectoryIgnoringInodes(const Path & path, const InodeHash & inodeHash)
+Strings
+LocalStore::readDirectoryIgnoringInodes (const Path &path,
+                                         const InodeHash &inodeHash)
 {
-    Strings names;
-
-    AutoCloseDir dir = opendir(path.c_str());
-    if (!dir) throw SysError(format("opening directory `%1%'") % path);
-
-    struct dirent * dirent;
-    while (errno = 0, dirent = readdir(dir)) { /* sic */
-        checkInterrupt();
-
-        if (inodeHash.count(dirent->d_ino)) {
-            printMsg(lvlDebug, format("`%1%' is already linked") % dirent->d_name);
-            continue;
+  Strings names;
+
+  AutoCloseDir dir = opendir (path.c_str ());
+  if (!dir)
+    throw SysError (format ("opening directory `%1%'") % path);
+
+  struct dirent *dirent;
+  while (errno = 0, dirent = readdir (dir))
+    { /* sic */
+      checkInterrupt ();
+
+      if (inodeHash.count (dirent->d_ino))
+        {
+          printMsg (lvlDebug,
+                    format ("`%1%' is already linked") % dirent->d_name);
+          continue;
         }
 
-        string name = dirent->d_name;
-        if (name == "." || name == "..") continue;
-        names.push_back(name);
+      string name = dirent->d_name;
+      if (name == "." || name == "..")
+        continue;
+      names.push_back (name);
     }
-    if (errno) throw SysError(format("reading directory `%1%'") % path);
+  if (errno)
+    throw SysError (format ("reading directory `%1%'") % path);
 
-    return names;
+  return names;
 }
 
-
-void LocalStore::optimisePath_(OptimiseStats & stats, const Path & path, InodeHash & inodeHash)
+void
+LocalStore::optimisePath_ (OptimiseStats &stats, const Path &path,
+                           InodeHash &inodeHash)
 {
-    checkInterrupt();
+  checkInterrupt ();
 
-    struct stat st;
-    if (lstat(path.c_str(), &st))
-        throw SysError(format("getting attributes of path `%1%'") % path);
+  struct stat st;
+  if (lstat (path.c_str (), &st))
+    throw SysError (format ("getting attributes of path `%1%'") % path);
 
-    if (S_ISDIR(st.st_mode)) {
-        Strings names = readDirectoryIgnoringInodes(path, inodeHash);
-        foreach (Strings::iterator, i, names)
-            optimisePath_(stats, path + "/" + *i, inodeHash);
-        return;
+  if (S_ISDIR (st.st_mode))
+    {
+      Strings names = readDirectoryIgnoringInodes (path, inodeHash);
+      foreach (Strings::iterator, i, names)
+        optimisePath_ (stats, path + "/" + *i, inodeHash);
+      return;
     }
 
-    /* We can hard link regular files (and maybe symlinks), but do that only
-       for files larger than some threshold.  This avoids adding too many
-       entries to '.links', which would slow down 'removeUnusedLinks' while
-       saving little space.  */
-    if (!S_ISREG(st.st_mode) || ((size_t) st.st_size) < deduplicationMinSize)
-	return;
-
-    /* Sometimes SNAFUs can cause files in the store to be
-       modified, in particular when running programs as root under
-       Guix System (example: $fontconfig/var/cache being modified).  Skip
-       those files.  FIXME: check the modification time. */
-    if (S_ISREG(st.st_mode) && (st.st_mode & S_IWUSR)) {
-        printMsg(lvlError, format("skipping suspicious writable file `%1%'") % path);
-        return;
+  /* We can hard link regular files (and maybe symlinks), but do that only
+     for files larger than some threshold.  This avoids adding too many
+     entries to '.links', which would slow down 'removeUnusedLinks' while
+     saving little space.  */
+  if (!S_ISREG (st.st_mode) || ((size_t)st.st_size) < deduplicationMinSize)
+    return;
+
+  /* Sometimes SNAFUs can cause files in the store to be
+     modified, in particular when running programs as root under
+     Guix System (example: $fontconfig/var/cache being modified).  Skip
+     those files.  FIXME: check the modification time. */
+  if (S_ISREG (st.st_mode) && (st.st_mode & S_IWUSR))
+    {
+      printMsg (lvlError,
+                format ("skipping suspicious writable file `%1%'") % path);
+      return;
     }
 
-    /* This can still happen on top-level files. */
-    if (st.st_nlink > 1 && inodeHash.count(st.st_ino)) {
-        printMsg(lvlDebug, format("`%1%' is already linked, with %2% other file(s).") % path % (st.st_nlink - 2));
-        return;
+  /* This can still happen on top-level files. */
+  if (st.st_nlink > 1 && inodeHash.count (st.st_ino))
+    {
+      printMsg (lvlDebug,
+                format ("`%1%' is already linked, with %2% other file(s).")
+                    % path % (st.st_nlink - 2));
+      return;
     }
 
-    /* Hash the file.  Note that hashPath() returns the hash over the
-       NAR serialisation, which includes the execute bit on the file.
-       Thus, executable and non-executable files with the same
-       contents *won't* be linked (which is good because otherwise the
-       permissions would be screwed up).
-
-       Also note that if `path' is a symlink, then we're hashing the
-       contents of the symlink (i.e. the result of readlink()), not
-       the contents of the target (which may not even exist). */
-    Hash hash = hashPath(htSHA256, path).first;
-    printMsg(lvlDebug, format("`%1%' has hash `%2%'") % path % printHash(hash));
-
-    /* Check if this is a known hash. */
-    Path linkPath = linksDir + "/" + printHash32(hash);
-
- retry:
-    if (!pathExists(linkPath)) {
-        /* Nope, create a hard link in the links directory. */
-        if (link(path.c_str(), linkPath.c_str()) == 0) {
-            inodeHash.insert(st.st_ino);
-            return;
+  /* Hash the file.  Note that hashPath() returns the hash over the
+     NAR serialisation, which includes the execute bit on the file.
+     Thus, executable and non-executable files with the same
+     contents *won't* be linked (which is good because otherwise the
+     permissions would be screwed up).
+
+     Also note that if `path' is a symlink, then we're hashing the
+     contents of the symlink (i.e. the result of readlink()), not
+     the contents of the target (which may not even exist). */
+  Hash hash = hashPath (htSHA256, path).first;
+  printMsg (lvlDebug,
+            format ("`%1%' has hash `%2%'") % path % printHash (hash));
+
+  /* Check if this is a known hash. */
+  Path linkPath = linksDir + "/" + printHash32 (hash);
+
+retry:
+  if (!pathExists (linkPath))
+    {
+      /* Nope, create a hard link in the links directory. */
+      if (link (path.c_str (), linkPath.c_str ()) == 0)
+        {
+          inodeHash.insert (st.st_ino);
+          return;
         }
 
-	switch (errno) {
-	case EEXIST:
-	    /* Fall through if another process created ‘linkPath’ before
-	       we did. */
-	    break;
-
-	case ENOSPC:
-	    /* On ext4, that probably means the directory index is full.  When
-	       that happens, it's fine to ignore it: we just effectively
-	       disable deduplication of this file.  */
-	    printMsg(lvlInfo, format("cannot link `%1%' to `%2%': %3%")
-		     % linkPath % path % strerror(ENOSPC));
-	    return;
-
-	default:
-            throw SysError(format("cannot link `%1%' to `%2%'") % linkPath % path);
-	}
+      switch (errno)
+        {
+        case EEXIST:
+          /* Fall through if another process created ‘linkPath’ before
+             we did. */
+          break;
+
+        case ENOSPC:
+          /* On ext4, that probably means the directory index is full.  When
+             that happens, it's fine to ignore it: we just effectively
+             disable deduplication of this file.  */
+          printMsg (lvlInfo, format ("cannot link `%1%' to `%2%': %3%")
+                                 % linkPath % path % strerror (ENOSPC));
+          return;
+
+        default:
+          throw SysError (format ("cannot link `%1%' to `%2%'") % linkPath
+                          % path);
+        }
     }
 
-    /* Yes!  We've seen a file with the same contents.  Replace the
-       current file with a hard link to that file. */
-    struct stat stLink;
-    if (lstat(linkPath.c_str(), &stLink))
-        throw SysError(format("getting attributes of path `%1%'") % linkPath);
+  /* Yes!  We've seen a file with the same contents.  Replace the
+     current file with a hard link to that file. */
+  struct stat stLink;
+  if (lstat (linkPath.c_str (), &stLink))
+    throw SysError (format ("getting attributes of path `%1%'") % linkPath);
 
-    if (st.st_ino == stLink.st_ino) {
-        printMsg(lvlDebug, format("`%1%' is already linked to `%2%'") % path % linkPath);
-        return;
+  if (st.st_ino == stLink.st_ino)
+    {
+      printMsg (lvlDebug,
+                format ("`%1%' is already linked to `%2%'") % path % linkPath);
+      return;
     }
 
-    if (st.st_size != stLink.st_size) {
-        printMsg(lvlError, format("removing corrupted link ‘%1%’") % linkPath);
-        unlink(linkPath.c_str());
-        goto retry;
+  if (st.st_size != stLink.st_size)
+    {
+      printMsg (lvlError, format ("removing corrupted link ‘%1%’") % linkPath);
+      unlink (linkPath.c_str ());
+      goto retry;
     }
 
-    printMsg(lvlTalkative, format("linking ‘%1%’ to ‘%2%’") % path % linkPath);
-
-    /* Make the containing directory writable, but only if it's not
-       the store itself (we don't want or need to mess with its
-       permissions). */
-    bool mustToggle = !isStorePath(path);
-    if (mustToggle) makeWritable(dirOf(path));
-
-    /* When we're done, make the directory read-only again and reset
-       its timestamp back to 0. */
-    MakeReadOnly makeReadOnly(mustToggle ? dirOf(path) : "");
-
-    Path tempLink = (format("%1%/.tmp-link-%2%-%3%")
-        % settings.nixStore % getpid() % rand()).str();
-
-    if (link(linkPath.c_str(), tempLink.c_str()) == -1) {
-        if (errno == EMLINK) {
-            /* Too many links to the same file (>= 32000 on most file
-               systems).  This is likely to happen with empty files.
-               Just shrug and ignore. */
-            if (st.st_size)
-                printMsg(lvlInfo, format("`%1%' has maximum number of links") % linkPath);
-            return;
+  printMsg (lvlTalkative, format ("linking ‘%1%’ to ‘%2%’") % path % linkPath);
+
+  /* Make the containing directory writable, but only if it's not
+     the store itself (we don't want or need to mess with its
+     permissions). */
+  bool mustToggle = !isStorePath (path);
+  if (mustToggle)
+    makeWritable (dirOf (path));
+
+  /* When we're done, make the directory read-only again and reset
+     its timestamp back to 0. */
+  MakeReadOnly makeReadOnly (mustToggle ? dirOf (path) : "");
+
+  Path tempLink = (format ("%1%/.tmp-link-%2%-%3%") % settings.nixStore
+                   % getpid () % rand ())
+                      .str ();
+
+  if (link (linkPath.c_str (), tempLink.c_str ()) == -1)
+    {
+      if (errno == EMLINK)
+        {
+          /* Too many links to the same file (>= 32000 on most file
+             systems).  This is likely to happen with empty files.
+             Just shrug and ignore. */
+          if (st.st_size)
+            printMsg (lvlInfo,
+                      format ("`%1%' has maximum number of links") % linkPath);
+          return;
         }
-	    throw SysError(format("cannot link `%1%' to `%2%'") % tempLink % linkPath);
-	}
-
-    /* Atomically replace the old file with the new hard link. */
-    if (rename(tempLink.c_str(), path.c_str()) == -1) {
-	int renameErrno = errno;
-        if (unlink(tempLink.c_str()) == -1)
-            printMsg(lvlError, format("unable to unlink `%1%'") % tempLink);
-        if (renameErrno == EMLINK) {
-            /* Some filesystems generate too many links on the rename,
-               rather than on the original link.  (Probably it
-               temporarily increases the st_nlink field before
-               decreasing it again.) */
-            if (st.st_size)
-                printMsg(lvlInfo, format("`%1%' has maximum number of links") % linkPath);
-            return;
+      throw SysError (format ("cannot link `%1%' to `%2%'") % tempLink
+                      % linkPath);
+    }
+
+  /* Atomically replace the old file with the new hard link. */
+  if (rename (tempLink.c_str (), path.c_str ()) == -1)
+    {
+      int renameErrno = errno;
+      if (unlink (tempLink.c_str ()) == -1)
+        printMsg (lvlError, format ("unable to unlink `%1%'") % tempLink);
+      if (renameErrno == EMLINK)
+        {
+          /* Some filesystems generate too many links on the rename,
+             rather than on the original link.  (Probably it
+             temporarily increases the st_nlink field before
+             decreasing it again.) */
+          if (st.st_size)
+            printMsg (lvlInfo,
+                      format ("`%1%' has maximum number of links") % linkPath);
+          return;
         }
-        throw SysError(format("cannot rename `%1%' to `%2%'") % tempLink % path);
+      throw SysError (format ("cannot rename `%1%' to `%2%'") % tempLink
+                      % path);
     }
 
-    stats.filesLinked++;
-    stats.bytesFreed += st.st_size;
-    stats.blocksFreed += st.st_blocks;
+  stats.filesLinked++;
+  stats.bytesFreed += st.st_size;
+  stats.blocksFreed += st.st_blocks;
 }
 
-
-void LocalStore::optimiseStore(OptimiseStats & stats)
+void
+LocalStore::optimiseStore (OptimiseStats &stats)
 {
-    PathSet paths = queryAllValidPaths();
-    InodeHash inodeHash = loadInodeHash();
-
-    foreach (PathSet::iterator, i, paths) {
-        addTempRoot(*i);
-        if (!isValidPath(*i)) continue; /* path was GC'ed, probably */
-        startNest(nest, lvlChatty, format("hashing files in `%1%'") % *i);
-        optimisePath_(stats, *i, inodeHash);
+  PathSet paths = queryAllValidPaths ();
+  InodeHash inodeHash = loadInodeHash ();
+
+  foreach (PathSet::iterator, i, paths)
+    {
+      addTempRoot (*i);
+      if (!isValidPath (*i))
+        continue; /* path was GC'ed, probably */
+      startNest (nest, lvlChatty, format ("hashing files in `%1%'") % *i);
+      optimisePath_ (stats, *i, inodeHash);
     }
 }
 
-static string showBytes(unsigned long long bytes)
+static string
+showBytes (unsigned long long bytes)
 {
-    return (format("%.2f MiB") % (bytes / (1024.0 * 1024.0))).str();
+  return (format ("%.2f MiB") % (bytes / (1024.0 * 1024.0))).str ();
 }
 
-void LocalStore::optimiseStore()
+void
+LocalStore::optimiseStore ()
 {
-    OptimiseStats stats;
+  OptimiseStats stats;
 
-    optimiseStore(stats);
+  optimiseStore (stats);
 
-    printMsg(lvlError,
-        format("%1% freed by hard-linking %2% files")
-        % showBytes(stats.bytesFreed)
-        % stats.filesLinked);
+  printMsg (lvlError, format ("%1% freed by hard-linking %2% files")
+                          % showBytes (stats.bytesFreed) % stats.filesLinked);
 }
 
-void LocalStore::optimisePath(const Path & path)
+void
+LocalStore::optimisePath (const Path &path)
 {
-    OptimiseStats stats;
-    InodeHash inodeHash;
+  OptimiseStats stats;
+  InodeHash inodeHash;
 
-    if (settings.autoOptimiseStore) optimisePath_(stats, path, inodeHash);
+  if (settings.autoOptimiseStore)
+    optimisePath_ (stats, path, inodeHash);
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libstore/pathlocks.cc b/nix/libstore/pathlocks.cc
index 9797ddd7ab..c7c6233bc3 100644
--- a/nix/libstore/pathlocks.cc
+++ b/nix/libstore/pathlocks.cc
@@ -4,71 +4,80 @@
 #include <cerrno>
 #include <cstdlib>
 
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
+namespace nix
+{
 
-namespace nix {
-
-
-int openLockFile(const Path & path, bool create)
+int
+openLockFile (const Path &path, bool create)
 {
-    AutoCloseFD fd;
+  AutoCloseFD fd;
 
-    fd = open(path.c_str(), O_RDWR | (create ? O_CREAT : 0), 0600);
-    if (fd == -1 && (create || errno != ENOENT))
-        throw SysError(format("opening lock file `%1%'") % path);
+  fd = open (path.c_str (), O_RDWR | (create ? O_CREAT : 0), 0600);
+  if (fd == -1 && (create || errno != ENOENT))
+    throw SysError (format ("opening lock file `%1%'") % path);
 
-    closeOnExec(fd);
+  closeOnExec (fd);
 
-    return fd.borrow();
+  return fd.borrow ();
 }
 
-
-void deleteLockFile(const Path & path, int fd)
+void
+deleteLockFile (const Path &path, int fd)
 {
-    /* Get rid of the lock file.  Have to be careful not to introduce
-       races.  Write a (meaningless) token to the file to indicate to
-       other processes waiting on this lock that the lock is stale
-       (deleted). */
-    unlink(path.c_str());
-    writeFull(fd, "d");
-    /* Note that the result of unlink() is ignored; removing the lock
-       file is an optimisation, not a necessity. */
+  /* Get rid of the lock file.  Have to be careful not to introduce
+     races.  Write a (meaningless) token to the file to indicate to
+     other processes waiting on this lock that the lock is stale
+     (deleted). */
+  unlink (path.c_str ());
+  writeFull (fd, "d");
+  /* Note that the result of unlink() is ignored; removing the lock
+     file is an optimisation, not a necessity. */
 }
 
-
-bool lockFile(int fd, LockType lockType, bool wait)
+bool
+lockFile (int fd, LockType lockType, bool wait)
 {
-    struct flock lock;
-    if (lockType == ltRead) lock.l_type = F_RDLCK;
-    else if (lockType == ltWrite) lock.l_type = F_WRLCK;
-    else if (lockType == ltNone) lock.l_type = F_UNLCK;
-    else abort();
-    lock.l_whence = SEEK_SET;
-    lock.l_start = 0;
-    lock.l_len = 0; /* entire file */
-
-    if (wait) {
-        while (fcntl(fd, F_SETLKW, &lock) != 0) {
-            checkInterrupt();
-            if (errno != EINTR)
-                throw SysError(format("acquiring/releasing lock"));
+  struct flock lock;
+  if (lockType == ltRead)
+    lock.l_type = F_RDLCK;
+  else if (lockType == ltWrite)
+    lock.l_type = F_WRLCK;
+  else if (lockType == ltNone)
+    lock.l_type = F_UNLCK;
+  else
+    abort ();
+  lock.l_whence = SEEK_SET;
+  lock.l_start = 0;
+  lock.l_len = 0; /* entire file */
+
+  if (wait)
+    {
+      while (fcntl (fd, F_SETLKW, &lock) != 0)
+        {
+          checkInterrupt ();
+          if (errno != EINTR)
+            throw SysError (format ("acquiring/releasing lock"));
         }
-    } else {
-        while (fcntl(fd, F_SETLK, &lock) != 0) {
-            checkInterrupt();
-            if (errno == EACCES || errno == EAGAIN) return false;
-            if (errno != EINTR) 
-                throw SysError(format("acquiring/releasing lock"));
+    }
+  else
+    {
+      while (fcntl (fd, F_SETLK, &lock) != 0)
+        {
+          checkInterrupt ();
+          if (errno == EACCES || errno == EAGAIN)
+            return false;
+          if (errno != EINTR)
+            throw SysError (format ("acquiring/releasing lock"));
         }
     }
 
-    return true;
+  return true;
 }
 
-
 /* This enables us to check whether are not already holding a lock on
    a file ourselves.  POSIX locks (fcntl) suck in this respect: if we
    close a descriptor, the previous lock will be closed as well.  And
@@ -76,128 +85,134 @@ bool lockFile(int fd, LockType lockType, bool wait)
    only works on locks held by other processes). */
 static StringSet lockedPaths; /* !!! not thread-safe */
 
+PathLocks::PathLocks () : deletePaths (false) {}
 
-PathLocks::PathLocks()
-    : deletePaths(false)
+PathLocks::PathLocks (const PathSet &paths, const string &waitMsg)
+    : deletePaths (false)
 {
+  lockPaths (paths, waitMsg);
 }
 
-
-PathLocks::PathLocks(const PathSet & paths, const string & waitMsg)
-    : deletePaths(false)
+bool
+PathLocks::lockPaths (const PathSet &_paths, const string &waitMsg, bool wait)
 {
-    lockPaths(paths, waitMsg);
-}
+  assert (fds.empty ());
 
+  /* Note that `fds' is built incrementally so that the destructor
+     will only release those locks that we have already acquired. */
 
-bool PathLocks::lockPaths(const PathSet & _paths,
-    const string & waitMsg, bool wait)
-{
-    assert(fds.empty());
-    
-    /* Note that `fds' is built incrementally so that the destructor
-       will only release those locks that we have already acquired. */
-
-    /* Sort the paths.  This assures that locks are always acquired in
-       the same order, thus preventing deadlocks. */
-    Paths paths(_paths.begin(), _paths.end());
-    paths.sort();
-    
-    /* Acquire the lock for each path. */
-    foreach (Paths::iterator, i, paths) {
-        checkInterrupt();
-        Path path = *i;
-        Path lockPath = path + ".lock";
-
-        debug(format("locking path `%1%'") % path);
-
-        if (lockedPaths.find(lockPath) != lockedPaths.end())
-            throw Error("deadlock: trying to re-acquire self-held lock");
-
-        AutoCloseFD fd;
-        
-        while (1) {
-
-            /* Open/create the lock file. */
-	    fd = openLockFile(lockPath, true);
-
-            /* Acquire an exclusive lock. */
-            if (!lockFile(fd, ltWrite, false)) {
-                if (wait) {
-                    if (waitMsg != "") printMsg(lvlError, waitMsg);
-                    lockFile(fd, ltWrite, true);
-                } else {
-                    /* Failed to lock this path; release all other
-                       locks. */
-                    unlock();
-                    return false;
+  /* Sort the paths.  This assures that locks are always acquired in
+     the same order, thus preventing deadlocks. */
+  Paths paths (_paths.begin (), _paths.end ());
+  paths.sort ();
+
+  /* Acquire the lock for each path. */
+  foreach (Paths::iterator, i, paths)
+    {
+      checkInterrupt ();
+      Path path = *i;
+      Path lockPath = path + ".lock";
+
+      debug (format ("locking path `%1%'") % path);
+
+      if (lockedPaths.find (lockPath) != lockedPaths.end ())
+        throw Error ("deadlock: trying to re-acquire self-held lock");
+
+      AutoCloseFD fd;
+
+      while (1)
+        {
+
+          /* Open/create the lock file. */
+          fd = openLockFile (lockPath, true);
+
+          /* Acquire an exclusive lock. */
+          if (!lockFile (fd, ltWrite, false))
+            {
+              if (wait)
+                {
+                  if (waitMsg != "")
+                    printMsg (lvlError, waitMsg);
+                  lockFile (fd, ltWrite, true);
+                }
+              else
+                {
+                  /* Failed to lock this path; release all other
+                     locks. */
+                  unlock ();
+                  return false;
                 }
             }
 
-            debug(format("lock acquired on `%1%'") % lockPath);
-
-            /* Check that the lock file hasn't become stale (i.e.,
-               hasn't been unlinked). */
-            struct stat st;
-            if (fstat(fd, &st) == -1)
-                throw SysError(format("statting lock file `%1%'") % lockPath);
-            if (st.st_size != 0)
-                /* This lock file has been unlinked, so we're holding
-                   a lock on a deleted file.  This means that other
-                   processes may create and acquire a lock on
-                   `lockPath', and proceed.  So we must retry. */
-                debug(format("open lock file `%1%' has become stale") % lockPath);
-            else
-                break;
+          debug (format ("lock acquired on `%1%'") % lockPath);
+
+          /* Check that the lock file hasn't become stale (i.e.,
+             hasn't been unlinked). */
+          struct stat st;
+          if (fstat (fd, &st) == -1)
+            throw SysError (format ("statting lock file `%1%'") % lockPath);
+          if (st.st_size != 0)
+            /* This lock file has been unlinked, so we're holding
+               a lock on a deleted file.  This means that other
+               processes may create and acquire a lock on
+               `lockPath', and proceed.  So we must retry. */
+            debug (format ("open lock file `%1%' has become stale")
+                   % lockPath);
+          else
+            break;
         }
 
-        /* Use borrow so that the descriptor isn't closed. */
-        fds.push_back(FDPair(fd.borrow(), lockPath));
-        lockedPaths.insert(lockPath);
+      /* Use borrow so that the descriptor isn't closed. */
+      fds.push_back (FDPair (fd.borrow (), lockPath));
+      lockedPaths.insert (lockPath);
     }
 
-    return true;
+  return true;
 }
 
-
-PathLocks::~PathLocks()
+PathLocks::~PathLocks ()
 {
-    try {
-        unlock();
-    } catch (...) {
-        ignoreException();
+  try
+    {
+      unlock ();
+    }
+  catch (...)
+    {
+      ignoreException ();
     }
 }
 
-
-void PathLocks::unlock()
+void
+PathLocks::unlock ()
 {
-    foreach (list<FDPair>::iterator, i, fds) {
-        if (deletePaths) deleteLockFile(i->second, i->first);
-
-        lockedPaths.erase(i->second);
-        if (close(i->first) == -1)
-            printMsg(lvlError,
-                format("error (ignored): cannot close lock file on `%1%'") % i->second);
-
-        debug(format("lock released on `%1%'") % i->second);
+  foreach (list<FDPair>::iterator, i, fds)
+    {
+      if (deletePaths)
+        deleteLockFile (i->second, i->first);
+
+      lockedPaths.erase (i->second);
+      if (close (i->first) == -1)
+        printMsg (lvlError,
+                  format ("error (ignored): cannot close lock file on `%1%'")
+                      % i->second);
+
+      debug (format ("lock released on `%1%'") % i->second);
     }
 
-    fds.clear();
+  fds.clear ();
 }
 
-
-void PathLocks::setDeletion(bool deletePaths)
+void
+PathLocks::setDeletion (bool deletePaths)
 {
-    this->deletePaths = deletePaths;
+  this->deletePaths = deletePaths;
 }
 
-
-bool pathIsLockedByMe(const Path & path)
+bool
+pathIsLockedByMe (const Path &path)
 {
-    Path lockPath = path + ".lock";
-    return lockedPaths.find(lockPath) != lockedPaths.end();
+  Path lockPath = path + ".lock";
+  return lockedPaths.find (lockPath) != lockedPaths.end ();
 }
 
- 
-}
+} // namespace nix
diff --git a/nix/libstore/pathlocks.hh b/nix/libstore/pathlocks.hh
index 8a6b1450da..c1bbb649fa 100644
--- a/nix/libstore/pathlocks.hh
+++ b/nix/libstore/pathlocks.hh
@@ -2,44 +2,43 @@
 
 #include "types.hh"
 
-
-namespace nix {
-
+namespace nix
+{
 
 /* Open (possibly create) a lock file and return the file descriptor.
    -1 is returned if create is false and the lock could not be opened
    because it doesn't exist.  Any other error throws an exception. */
-int openLockFile(const Path & path, bool create);
+int openLockFile (const Path &path, bool create);
 
 /* Delete an open lock file. */
-void deleteLockFile(const Path & path, int fd);
-
-enum LockType { ltRead, ltWrite, ltNone };
+void deleteLockFile (const Path &path, int fd);
 
-bool lockFile(int fd, LockType lockType, bool wait);
+enum LockType
+{
+  ltRead,
+  ltWrite,
+  ltNone
+};
 
+bool lockFile (int fd, LockType lockType, bool wait);
 
-class PathLocks 
+class PathLocks
 {
 private:
-    typedef std::pair<int, Path> FDPair;
-    list<FDPair> fds;
-    bool deletePaths;
+  typedef std::pair<int, Path> FDPair;
+  list<FDPair> fds;
+  bool deletePaths;
 
 public:
-    PathLocks();
-    PathLocks(const PathSet & paths,
-        const string & waitMsg = "");
-    bool lockPaths(const PathSet & _paths,
-        const string & waitMsg = "",
-        bool wait = true);
-    ~PathLocks();
-    void unlock();
-    void setDeletion(bool deletePaths);
+  PathLocks ();
+  PathLocks (const PathSet &paths, const string &waitMsg = "");
+  bool lockPaths (const PathSet &_paths, const string &waitMsg = "",
+                  bool wait = true);
+  ~PathLocks ();
+  void unlock ();
+  void setDeletion (bool deletePaths);
 };
 
-
-bool pathIsLockedByMe(const Path & path);
-
+bool pathIsLockedByMe (const Path &path);
 
 }
diff --git a/nix/libstore/references.cc b/nix/libstore/references.cc
index 282b848938..39986ef276 100644
--- a/nix/libstore/references.cc
+++ b/nix/libstore/references.cc
@@ -1,122 +1,129 @@
 #include "references.hh"
+#include "archive.hh"
 #include "hash.hh"
 #include "util.hh"
-#include "archive.hh"
 
-#include <map>
 #include <cstdlib>
+#include <map>
 
-
-namespace nix {
-
+namespace nix
+{
 
 static unsigned int refLength = 32; /* characters */
 
-
-static void search(const unsigned char * s, unsigned int len, 
-    StringSet & hashes, StringSet & seen)
+static void
+search (const unsigned char *s, unsigned int len, StringSet &hashes,
+        StringSet &seen)
 {
-    static bool initialised = false;
-    static bool isBase32[256];
-    if (!initialised) {
-        for (unsigned int i = 0; i < 256; ++i) isBase32[i] = false;
-        for (unsigned int i = 0; i < base32Chars.size(); ++i)
-            isBase32[(unsigned char) base32Chars[i]] = true;
-        initialised = true;
+  static bool initialised = false;
+  static bool isBase32[256];
+  if (!initialised)
+    {
+      for (unsigned int i = 0; i < 256; ++i)
+        isBase32[i] = false;
+      for (unsigned int i = 0; i < base32Chars.size (); ++i)
+        isBase32[(unsigned char)base32Chars[i]] = true;
+      initialised = true;
     }
-    
-    for (unsigned int i = 0; i + refLength <= len; ) {
-        int j;
-        bool match = true;
-        for (j = refLength - 1; j >= 0; --j)
-            if (!isBase32[(unsigned char) s[i + j]]) {
-                i += j + 1;
-                match = false;
-                break;
-            }
-        if (!match) continue;
-        string ref((const char *) s + i, refLength);
-        if (hashes.find(ref) != hashes.end()) {
-            debug(format("found reference to `%1%' at offset `%2%'")
-                  % ref % i);
-            seen.insert(ref);
-            hashes.erase(ref);
+
+  for (unsigned int i = 0; i + refLength <= len;)
+    {
+      int j;
+      bool match = true;
+      for (j = refLength - 1; j >= 0; --j)
+        if (!isBase32[(unsigned char)s[i + j]])
+          {
+            i += j + 1;
+            match = false;
+            break;
+          }
+      if (!match)
+        continue;
+      string ref ((const char *)s + i, refLength);
+      if (hashes.find (ref) != hashes.end ())
+        {
+          debug (format ("found reference to `%1%' at offset `%2%'") % ref
+                 % i);
+          seen.insert (ref);
+          hashes.erase (ref);
         }
-        ++i;
+      ++i;
     }
 }
 
-
 struct RefScanSink : Sink
 {
-    HashSink hashSink;
-    StringSet hashes;
-    StringSet seen;
+  HashSink hashSink;
+  StringSet hashes;
+  StringSet seen;
 
-    string tail;
+  string tail;
 
-    RefScanSink() : hashSink(htSHA256) { }
-    
-    void operator () (const unsigned char * data, size_t len);
-};
+  RefScanSink () : hashSink (htSHA256) {}
 
+  void operator() (const unsigned char *data, size_t len);
+};
 
-void RefScanSink::operator () (const unsigned char * data, size_t len)
+void
+RefScanSink::operator() (const unsigned char *data, size_t len)
 {
-    hashSink(data, len);
-
-    /* It's possible that a reference spans the previous and current
-       fragment, so search in the concatenation of the tail of the
-       previous fragment and the start of the current fragment. */
-    string s = tail + string((const char *) data, len > refLength ? refLength : len);
-    search((const unsigned char *) s.data(), s.size(), hashes, seen);
-
-    search(data, len, hashes, seen);
-
-    unsigned int tailLen = len <= refLength ? len : refLength;
-    tail =
-        string(tail, tail.size() < refLength - tailLen ? 0 : tail.size() - (refLength - tailLen)) +
-        string((const char *) data + len - tailLen, tailLen);
+  hashSink (data, len);
+
+  /* It's possible that a reference spans the previous and current
+     fragment, so search in the concatenation of the tail of the
+     previous fragment and the start of the current fragment. */
+  string s
+      = tail + string ((const char *)data, len > refLength ? refLength : len);
+  search ((const unsigned char *)s.data (), s.size (), hashes, seen);
+
+  search (data, len, hashes, seen);
+
+  unsigned int tailLen = len <= refLength ? len : refLength;
+  tail = string (tail, tail.size () < refLength - tailLen
+                           ? 0
+                           : tail.size () - (refLength - tailLen))
+         + string ((const char *)data + len - tailLen, tailLen);
 }
 
-
-PathSet scanForReferences(const string & path,
-    const PathSet & refs, HashResult & hash)
+PathSet
+scanForReferences (const string &path, const PathSet &refs, HashResult &hash)
 {
-    RefScanSink sink;
-    std::map<string, Path> backMap;
-
-    /* For efficiency (and a higher hit rate), just search for the
-       hash part of the file name.  (This assumes that all references
-       have the form `HASH-bla'). */
-    foreach (PathSet::const_iterator, i, refs) {
-        string baseName = baseNameOf(*i);
-        string::size_type pos = baseName.find('-');
-        if (pos == string::npos)
-            throw Error(format("bad reference `%1%'") % *i);
-        string s = string(baseName, 0, pos);
-        assert(s.size() == refLength);
-        assert(backMap.find(s) == backMap.end());
-        // parseHash(htSHA256, s);
-        sink.hashes.insert(s);
-        backMap[s] = *i;
+  RefScanSink sink;
+  std::map<string, Path> backMap;
+
+  /* For efficiency (and a higher hit rate), just search for the
+     hash part of the file name.  (This assumes that all references
+     have the form `HASH-bla'). */
+  foreach (PathSet::const_iterator, i, refs)
+    {
+      string baseName = baseNameOf (*i);
+      string::size_type pos = baseName.find ('-');
+      if (pos == string::npos)
+        throw Error (format ("bad reference `%1%'") % *i);
+      string s = string (baseName, 0, pos);
+      assert (s.size () == refLength);
+      assert (backMap.find (s) == backMap.end ());
+      // parseHash(htSHA256, s);
+      sink.hashes.insert (s);
+      backMap[s] = *i;
     }
 
-    /* Look for the hashes in the NAR dump of the path. */
-    dumpPath(path, sink);
-
-    /* Map the hashes found back to their store paths. */
-    PathSet found;
-    foreach (StringSet::iterator, i, sink.seen) {
-        std::map<string, Path>::iterator j;
-        if ((j = backMap.find(*i)) == backMap.end()) abort();
-        found.insert(j->second);
+  /* Look for the hashes in the NAR dump of the path. */
+  dumpPath (path, sink);
+
+  /* Map the hashes found back to their store paths. */
+  PathSet found;
+  foreach (StringSet::iterator, i, sink.seen)
+    {
+      std::map<string, Path>::iterator j;
+      if ((j = backMap.find (*i)) == backMap.end ())
+        abort ();
+      found.insert (j->second);
     }
 
-    hash = sink.hashSink.finish();
-        
-    return found;
-}
-
+  hash = sink.hashSink.finish ();
 
+  return found;
 }
+
+} // namespace nix
diff --git a/nix/libstore/references.hh b/nix/libstore/references.hh
index 013809d122..2ce121ee87 100644
--- a/nix/libstore/references.hh
+++ b/nix/libstore/references.hh
@@ -1,11 +1,12 @@
 #pragma once
 
-#include "types.hh"
 #include "hash.hh"
+#include "types.hh"
+
+namespace nix
+{
 
-namespace nix {
+PathSet scanForReferences (const Path &path, const PathSet &refs,
+                           HashResult &hash);
 
-PathSet scanForReferences(const Path & path, const PathSet & refs,
-    HashResult & hash);
-    
 }
diff --git a/nix/libstore/sqlite.cc b/nix/libstore/sqlite.cc
index e08c67f40e..fcd660a0f7 100644
--- a/nix/libstore/sqlite.cc
+++ b/nix/libstore/sqlite.cc
@@ -3,164 +3,191 @@
 
 #include <sqlite3.h>
 
-namespace nix {
-
-[[noreturn]] void throwSQLiteError(sqlite3 * db, const format & f)
-{
-    int err = sqlite3_errcode(db);
-    if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL) {
-        if (err == SQLITE_PROTOCOL)
-            printMsg(lvlError, "warning: SQLite database is busy (SQLITE_PROTOCOL)");
-        else {
-            static bool warned = false;
-            if (!warned) {
-                printMsg(lvlError, "warning: SQLite database is busy");
-                warned = true;
+namespace nix
+{
+
+[[noreturn]] void
+throwSQLiteError (sqlite3 *db, const format &f)
+{
+  int err = sqlite3_errcode (db);
+  if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL)
+    {
+      if (err == SQLITE_PROTOCOL)
+        printMsg (lvlError,
+                  "warning: SQLite database is busy (SQLITE_PROTOCOL)");
+      else
+        {
+          static bool warned = false;
+          if (!warned)
+            {
+              printMsg (lvlError, "warning: SQLite database is busy");
+              warned = true;
             }
         }
         /* Sleep for a while since retrying the transaction right away
            is likely to fail again. */
 #if HAVE_NANOSLEEP
-        struct timespec t;
-        t.tv_sec = 0;
-        t.tv_nsec = (random() % 100) * 1000 * 1000; /* <= 0.1s */
-        nanosleep(&t, 0);
+      struct timespec t;
+      t.tv_sec = 0;
+      t.tv_nsec = (random () % 100) * 1000 * 1000; /* <= 0.1s */
+      nanosleep (&t, 0);
 #else
-        sleep(1);
+      sleep (1);
 #endif
-        throw SQLiteBusy(format("%1%: %2%") % f.str() % sqlite3_errmsg(db));
+      throw SQLiteBusy (format ("%1%: %2%") % f.str () % sqlite3_errmsg (db));
     }
-    else
-        throw SQLiteError(format("%1%: %2%") % f.str() % sqlite3_errmsg(db));
+  else
+    throw SQLiteError (format ("%1%: %2%") % f.str () % sqlite3_errmsg (db));
 }
 
-SQLite::~SQLite()
+SQLite::~SQLite ()
 {
-    try {
-        if (db && sqlite3_close(db) != SQLITE_OK)
-            throwSQLiteError(db, "closing database");
-    } catch (...) {
-        ignoreException();
+  try
+    {
+      if (db && sqlite3_close (db) != SQLITE_OK)
+        throwSQLiteError (db, "closing database");
+    }
+  catch (...)
+    {
+      ignoreException ();
     }
 }
 
-void SQLiteStmt::create(sqlite3 * db, const string & s)
+void
+SQLiteStmt::create (sqlite3 *db, const string &s)
 {
-    checkInterrupt();
-    assert(!stmt);
-    if (sqlite3_prepare_v2(db, s.c_str(), -1, &stmt, 0) != SQLITE_OK)
-        throwSQLiteError(db, "creating statement");
-    this->db = db;
+  checkInterrupt ();
+  assert (!stmt);
+  if (sqlite3_prepare_v2 (db, s.c_str (), -1, &stmt, 0) != SQLITE_OK)
+    throwSQLiteError (db, "creating statement");
+  this->db = db;
 }
 
-SQLiteStmt::~SQLiteStmt()
+SQLiteStmt::~SQLiteStmt ()
 {
-    try {
-        if (stmt && sqlite3_finalize(stmt) != SQLITE_OK)
-            throwSQLiteError(db, "finalizing statement");
-    } catch (...) {
-        ignoreException();
+  try
+    {
+      if (stmt && sqlite3_finalize (stmt) != SQLITE_OK)
+        throwSQLiteError (db, "finalizing statement");
+    }
+  catch (...)
+    {
+      ignoreException ();
     }
 }
 
-SQLiteStmt::Use::Use(SQLiteStmt & stmt)
-    : stmt(stmt)
+SQLiteStmt::Use::Use (SQLiteStmt &stmt) : stmt (stmt)
 {
-    assert(stmt.stmt);
-    /* Note: sqlite3_reset() returns the error code for the most
-       recent call to sqlite3_step().  So ignore it. */
-    sqlite3_reset(stmt);
+  assert (stmt.stmt);
+  /* Note: sqlite3_reset() returns the error code for the most
+     recent call to sqlite3_step().  So ignore it. */
+  sqlite3_reset (stmt);
 }
 
-SQLiteStmt::Use::~Use()
-{
-    sqlite3_reset(stmt);
-}
+SQLiteStmt::Use::~Use () { sqlite3_reset (stmt); }
 
-SQLiteStmt::Use & SQLiteStmt::Use::operator () (const std::string & value, bool notNull)
+SQLiteStmt::Use &
+SQLiteStmt::Use::operator() (const std::string &value, bool notNull)
 {
-    if (notNull) {
-        if (sqlite3_bind_text(stmt, curArg++, value.c_str(), -1, SQLITE_TRANSIENT) != SQLITE_OK)
-            throwSQLiteError(stmt.db, "binding argument");
-    } else
-        bind();
-    return *this;
+  if (notNull)
+    {
+      if (sqlite3_bind_text (stmt, curArg++, value.c_str (), -1,
+                             SQLITE_TRANSIENT)
+          != SQLITE_OK)
+        throwSQLiteError (stmt.db, "binding argument");
+    }
+  else
+    bind ();
+  return *this;
 }
 
-SQLiteStmt::Use & SQLiteStmt::Use::operator () (int64_t value, bool notNull)
+SQLiteStmt::Use &
+SQLiteStmt::Use::operator() (int64_t value, bool notNull)
 {
-    if (notNull) {
-        if (sqlite3_bind_int64(stmt, curArg++, value) != SQLITE_OK)
-            throwSQLiteError(stmt.db, "binding argument");
-    } else
-        bind();
-    return *this;
+  if (notNull)
+    {
+      if (sqlite3_bind_int64 (stmt, curArg++, value) != SQLITE_OK)
+        throwSQLiteError (stmt.db, "binding argument");
+    }
+  else
+    bind ();
+  return *this;
 }
 
-SQLiteStmt::Use & SQLiteStmt::Use::bind()
+SQLiteStmt::Use &
+SQLiteStmt::Use::bind ()
 {
-    if (sqlite3_bind_null(stmt, curArg++) != SQLITE_OK)
-        throwSQLiteError(stmt.db, "binding argument");
-    return *this;
+  if (sqlite3_bind_null (stmt, curArg++) != SQLITE_OK)
+    throwSQLiteError (stmt.db, "binding argument");
+  return *this;
 }
 
-int SQLiteStmt::Use::step()
+int
+SQLiteStmt::Use::step ()
 {
-    return sqlite3_step(stmt);
+  return sqlite3_step (stmt);
 }
 
-void SQLiteStmt::Use::exec()
+void
+SQLiteStmt::Use::exec ()
 {
-    int r = step();
-    assert(r != SQLITE_ROW);
-    if (r != SQLITE_DONE)
-        throwSQLiteError(stmt.db, "executing SQLite statement");
+  int r = step ();
+  assert (r != SQLITE_ROW);
+  if (r != SQLITE_DONE)
+    throwSQLiteError (stmt.db, "executing SQLite statement");
 }
 
-bool SQLiteStmt::Use::next()
+bool
+SQLiteStmt::Use::next ()
 {
-    int r = step();
-    if (r != SQLITE_DONE && r != SQLITE_ROW)
-        throwSQLiteError(stmt.db, "executing SQLite query");
-    return r == SQLITE_ROW;
+  int r = step ();
+  if (r != SQLITE_DONE && r != SQLITE_ROW)
+    throwSQLiteError (stmt.db, "executing SQLite query");
+  return r == SQLITE_ROW;
 }
 
-std::string SQLiteStmt::Use::getStr(int col)
+std::string
+SQLiteStmt::Use::getStr (int col)
 {
-    auto s = (const char *) sqlite3_column_text(stmt, col);
-    assert(s);
-    return s;
+  auto s = (const char *)sqlite3_column_text (stmt, col);
+  assert (s);
+  return s;
 }
 
-int64_t SQLiteStmt::Use::getInt(int col)
+int64_t
+SQLiteStmt::Use::getInt (int col)
 {
-    // FIXME: detect nulls?
-    return sqlite3_column_int64(stmt, col);
+  // FIXME: detect nulls?
+  return sqlite3_column_int64 (stmt, col);
 }
 
-SQLiteTxn::SQLiteTxn(sqlite3 * db)
+SQLiteTxn::SQLiteTxn (sqlite3 *db)
 {
-    this->db = db;
-    if (sqlite3_exec(db, "begin;", 0, 0, 0) != SQLITE_OK)
-        throwSQLiteError(db, "starting transaction");
-    active = true;
+  this->db = db;
+  if (sqlite3_exec (db, "begin;", 0, 0, 0) != SQLITE_OK)
+    throwSQLiteError (db, "starting transaction");
+  active = true;
 }
 
-void SQLiteTxn::commit()
+void
+SQLiteTxn::commit ()
 {
-    if (sqlite3_exec(db, "commit;", 0, 0, 0) != SQLITE_OK)
-        throwSQLiteError(db, "committing transaction");
-    active = false;
+  if (sqlite3_exec (db, "commit;", 0, 0, 0) != SQLITE_OK)
+    throwSQLiteError (db, "committing transaction");
+  active = false;
 }
 
-SQLiteTxn::~SQLiteTxn()
+SQLiteTxn::~SQLiteTxn ()
 {
-    try {
-        if (active && sqlite3_exec(db, "rollback;", 0, 0, 0) != SQLITE_OK)
-            throwSQLiteError(db, "aborting transaction");
-    } catch (...) {
-        ignoreException();
+  try
+    {
+      if (active && sqlite3_exec (db, "rollback;", 0, 0, 0) != SQLITE_OK)
+        throwSQLiteError (db, "aborting transaction");
+    }
+  catch (...)
+    {
+      ignoreException ();
     }
 }
 
-}
+} // namespace nix
diff --git a/nix/libstore/sqlite.hh b/nix/libstore/sqlite.hh
index 6cadba6849..eda4e15a9c 100644
--- a/nix/libstore/sqlite.hh
+++ b/nix/libstore/sqlite.hh
@@ -1,101 +1,107 @@
 #pragma once
 
+#include <cstdint>
 #include <functional>
 #include <string>
-#include <cstdint>
 
 #include "types.hh"
 
-class sqlite3;
-class sqlite3_stmt;
+struct sqlite3;
+struct sqlite3_stmt;
 
-namespace nix {
+namespace nix
+{
 
 /* RAII wrapper to close a SQLite database automatically. */
 struct SQLite
 {
-    sqlite3 * db;
-    SQLite() { db = 0; }
-    ~SQLite();
-    operator sqlite3 * () { return db; }
+  sqlite3 *db;
+  SQLite () { db = 0; }
+  ~SQLite ();
+  operator sqlite3 * () { return db; }
 };
 
 /* RAII wrapper to create and destroy SQLite prepared statements. */
 struct SQLiteStmt
 {
-    sqlite3 * db = 0;
-    sqlite3_stmt * stmt = 0;
-    SQLiteStmt() { }
-    void create(sqlite3 * db, const std::string & s);
-    ~SQLiteStmt();
-    operator sqlite3_stmt * () { return stmt; }
-
-    /* Helper for binding / executing statements. */
-    class Use
-    {
-        friend struct SQLiteStmt;
-    private:
-        SQLiteStmt & stmt;
-        unsigned int curArg = 1;
-        Use(SQLiteStmt & stmt);
-
-    public:
-
-        ~Use();
-
-        /* Bind the next parameter. */
-        Use & operator () (const std::string & value, bool notNull = true);
-        Use & operator () (int64_t value, bool notNull = true);
-        Use & bind(); // null
-
-        int step();
-
-        /* Execute a statement that does not return rows. */
-        void exec();
-
-        /* For statements that return 0 or more rows. Returns true iff
-           a row is available. */
-        bool next();
-
-        std::string getStr(int col);
-        int64_t getInt(int col);
-    };
-
-    Use use()
-    {
-        return Use(*this);
-    }
+  sqlite3 *db = 0;
+  sqlite3_stmt *stmt = 0;
+  SQLiteStmt () {}
+  void create (sqlite3 *db, const std::string &s);
+  ~SQLiteStmt ();
+  operator sqlite3_stmt * () { return stmt; }
+
+  /* Helper for binding / executing statements. */
+  class Use
+  {
+    friend struct SQLiteStmt;
+
+  private:
+    SQLiteStmt &stmt;
+    unsigned int curArg = 1;
+    Use (SQLiteStmt &stmt);
+
+  public:
+    ~Use ();
+
+    /* Bind the next parameter. */
+    Use &operator() (const std::string &value, bool notNull = true);
+    Use &operator() (int64_t value, bool notNull = true);
+    Use &bind (); // null
+
+    int step ();
+
+    /* Execute a statement that does not return rows. */
+    void exec ();
+
+    /* For statements that return 0 or more rows. Returns true iff
+       a row is available. */
+    bool next ();
+
+    std::string getStr (int col);
+    int64_t getInt (int col);
+  };
+
+  Use
+  use ()
+  {
+    return Use (*this);
+  }
 };
 
 /* RAII helper that ensures transactions are aborted unless explicitly
    committed. */
 struct SQLiteTxn
 {
-    bool active = false;
-    sqlite3 * db;
+  bool active = false;
+  sqlite3 *db;
 
-    SQLiteTxn(sqlite3 * db);
+  SQLiteTxn (sqlite3 *db);
 
-    void commit();
+  void commit ();
 
-    ~SQLiteTxn();
+  ~SQLiteTxn ();
 };
 
+MakeError (SQLiteError, Error);
+MakeError (SQLiteBusy, SQLiteError);
 
-MakeError(SQLiteError, Error);
-MakeError(SQLiteBusy, SQLiteError);
-
-[[noreturn]] void throwSQLiteError(sqlite3 * db, const format & f);
+[[noreturn]] void throwSQLiteError (sqlite3 *db, const format &f);
 
 /* Convenience function for retrying a SQLite transaction when the
    database is busy. */
-template<typename T>
-T retrySQLite(std::function<T()> fun)
+template <typename T>
+T
+retrySQLite (std::function<T ()> fun)
 {
-    while (true) {
-        try {
-            return fun();
-        } catch (SQLiteBusy & e) {
+  while (true)
+    {
+      try
+        {
+          return fun ();
+        }
+      catch (SQLiteBusy &e)
+        {
         }
     }
 }
diff --git a/nix/libstore/store-api.cc b/nix/libstore/store-api.cc
index 38a1403a71..b3f1ca17fd 100644
--- a/nix/libstore/store-api.cc
+++ b/nix/libstore/store-api.cc
@@ -4,76 +4,72 @@
 
 #include <climits>
 
+namespace nix
+{
 
-namespace nix {
-
-
-GCOptions::GCOptions()
+GCOptions::GCOptions ()
 {
-    action = gcDeleteDead;
-    ignoreLiveness = false;
-    maxFreed = ULLONG_MAX;
+  action = gcDeleteDead;
+  ignoreLiveness = false;
+  maxFreed = ULLONG_MAX;
 }
 
-
-bool isInStore(const Path & path)
+bool
+isInStore (const Path &path)
 {
-    return isInDir(path, settings.nixStore);
+  return isInDir (path, settings.nixStore);
 }
 
-
-bool isStorePath(const Path & path)
+bool
+isStorePath (const Path &path)
 {
-    return isInStore(path)
-        && path.find('/', settings.nixStore.size() + 1) == Path::npos;
+  return isInStore (path)
+         && path.find ('/', settings.nixStore.size () + 1) == Path::npos;
 }
 
-
-void assertStorePath(const Path & path)
+void
+assertStorePath (const Path &path)
 {
-    if (!isStorePath(path))
-        throw Error(format("path `%1%' is not in the store") % path);
+  if (!isStorePath (path))
+    throw Error (format ("path `%1%' is not in the store") % path);
 }
 
-
-Path toStorePath(const Path & path)
+Path
+toStorePath (const Path &path)
 {
-    if (!isInStore(path))
-        throw Error(format("path `%1%' is not in the store") % path);
-    Path::size_type slash = path.find('/', settings.nixStore.size() + 1);
-    if (slash == Path::npos)
-        return path;
-    else
-        return Path(path, 0, slash);
+  if (!isInStore (path))
+    throw Error (format ("path `%1%' is not in the store") % path);
+  Path::size_type slash = path.find ('/', settings.nixStore.size () + 1);
+  if (slash == Path::npos)
+    return path;
+  else
+    return Path (path, 0, slash);
 }
 
-
-string storePathToName(const Path & path)
+string
+storePathToName (const Path &path)
 {
-    assertStorePath(path);
-    return string(path, settings.nixStore.size() + 34);
+  assertStorePath (path);
+  return string (path, settings.nixStore.size () + 34);
 }
 
-
-void checkStoreName(const string & name)
+void
+checkStoreName (const string &name)
 {
-    string validChars = "+-._?=";
-    /* Disallow names starting with a dot for possible security
-       reasons (e.g., "." and ".."). */
-    if (string(name, 0, 1) == ".")
-        throw Error(format("invalid name: `%1%' (can't begin with dot)") % name);
-    foreach (string::const_iterator, i, name)
-        if (!((*i >= 'A' && *i <= 'Z') ||
-              (*i >= 'a' && *i <= 'z') ||
-              (*i >= '0' && *i <= '9') ||
-              validChars.find(*i) != string::npos))
-        {
-            throw Error(format("invalid character `%1%' in name `%2%'")
-                % *i % name);
-        }
+  string validChars = "+-._?=";
+  /* Disallow names starting with a dot for possible security
+     reasons (e.g., "." and ".."). */
+  if (string (name, 0, 1) == ".")
+    throw Error (format ("invalid name: `%1%' (can't begin with dot)") % name);
+  foreach (string::const_iterator, i, name)
+    if (!((*i >= 'A' && *i <= 'Z') || (*i >= 'a' && *i <= 'z')
+          || (*i >= '0' && *i <= '9') || validChars.find (*i) != string::npos))
+      {
+        throw Error (format ("invalid character `%1%' in name `%2%'") % *i
+                     % name);
+      }
 }
 
-
 /* Store paths have the following form:
 
    <store>/<h>-<name>
@@ -81,22 +77,22 @@ void checkStoreName(const string & name)
    where
 
    <store> = the location of the store, usually /gnu/store
-   
+
    <name> = a human readable name for the path, typically obtained
      from the name attribute of the derivation, or the name of the
      source file from which the store path is created.  For derivation
      outputs other than the default "out" output, the string "-<id>"
      is suffixed to <name>.
-     
+
    <h> = base-32 representation of the first 160 bits of a SHA-256
      hash of <s>; the hash part of the store name
-     
+
    <s> = the string "<type>:sha256:<h2>:<store>:<name>";
      note that it includes the location of the store as well as the
      name to make sure that changes to either of those are reflected
      in the hash (e.g. you won't get /nix/store/<h>-name1 and
      /nix/store/<h>-name2 with equal hash parts).
-     
+
    <type> = one of:
      "text:<r1>:<r2>:...<rN>"
        for plain text files written to the store using
@@ -144,126 +140,132 @@ void checkStoreName(const string & name)
    "source:".
 */
 
-
-Path makeStorePath(const string & type,
-    const Hash & hash, const string & name)
+Path
+makeStorePath (const string &type, const Hash &hash, const string &name)
 {
-    /* e.g., "source:sha256:1abc...:/nix/store:foo.tar.gz" */
-    string s = type + ":sha256:" + printHash(hash) + ":"
-        + settings.nixStore + ":" + name;
+  /* e.g., "source:sha256:1abc...:/nix/store:foo.tar.gz" */
+  string s = type + ":sha256:" + printHash (hash) + ":" + settings.nixStore
+             + ":" + name;
 
-    checkStoreName(name);
+  checkStoreName (name);
 
-    return settings.nixStore + "/"
-        + printHash32(compressHash(hashString(htSHA256, s), 20))
-        + "-" + name;
+  return settings.nixStore + "/"
+         + printHash32 (compressHash (hashString (htSHA256, s), 20)) + "-"
+         + name;
 }
 
-
-Path makeOutputPath(const string & id,
-    const Hash & hash, const string & name)
+Path
+makeOutputPath (const string &id, const Hash &hash, const string &name)
 {
-    return makeStorePath("output:" + id, hash,
-        name + (id == "out" ? "" : "-" + id));
+  return makeStorePath ("output:" + id, hash,
+                        name + (id == "out" ? "" : "-" + id));
 }
 
-
-Path makeFixedOutputPath(bool recursive,
-    HashType hashAlgo, Hash hash, string name)
+Path
+makeFixedOutputPath (bool recursive, HashType hashAlgo, Hash hash, string name)
 {
-    return hashAlgo == htSHA256 && recursive
-        ? makeStorePath("source", hash, name)
-        : makeStorePath("output:out", hashString(htSHA256,
-                "fixed:out:" + (recursive ? (string) "r:" : "") +
-                printHashType(hashAlgo) + ":" + printHash(hash) + ":"),
-            name);
+  return hashAlgo == htSHA256 && recursive
+             ? makeStorePath ("source", hash, name)
+             : makeStorePath (
+                   "output:out",
+                   hashString (htSHA256,
+                               "fixed:out:" + (recursive ? (string) "r:" : "")
+                                   + printHashType (hashAlgo) + ":"
+                                   + printHash (hash) + ":"),
+                   name);
 }
 
-
-Path computeStorePathForText(const string & name, const string & s,
-    const PathSet & references)
+Path
+computeStorePathForText (const string &name, const string &s,
+                         const PathSet &references)
 {
-    Hash hash = hashString(htSHA256, s);
-    /* Stuff the references (if any) into the type.  This is a bit
-       hacky, but we can't put them in `s' since that would be
-       ambiguous. */
-    string type = "text";
-    foreach (PathSet::const_iterator, i, references) {
-        type += ":";
-        type += *i;
+  Hash hash = hashString (htSHA256, s);
+  /* Stuff the references (if any) into the type.  This is a bit
+     hacky, but we can't put them in `s' since that would be
+     ambiguous. */
+  string type = "text";
+  foreach (PathSet::const_iterator, i, references)
+    {
+      type += ":";
+      type += *i;
     }
-    return makeStorePath(type, hash, name);
+  return makeStorePath (type, hash, name);
 }
 
-
 /* Return a string accepted by decodeValidPathInfo() that
    registers the specified paths as valid.  Note: it's the
    responsibility of the caller to provide a closure. */
-string StoreAPI::makeValidityRegistration(const PathSet & paths,
-    bool showDerivers, bool showHash)
+string
+StoreAPI::makeValidityRegistration (const PathSet &paths, bool showDerivers,
+                                    bool showHash)
 {
-    string s = "";
-    
-    foreach (PathSet::iterator, i, paths) {
-        s += *i + "\n";
+  string s = "";
 
-        ValidPathInfo info = queryPathInfo(*i);
+  foreach (PathSet::iterator, i, paths)
+    {
+      s += *i + "\n";
 
-        if (showHash) {
-            s += printHash(info.hash) + "\n";
-            s += (format("%1%\n") % info.narSize).str();
+      ValidPathInfo info = queryPathInfo (*i);
+
+      if (showHash)
+        {
+          s += printHash (info.hash) + "\n";
+          s += (format ("%1%\n") % info.narSize).str ();
         }
 
-        Path deriver = showDerivers ? info.deriver : "";
-        s += deriver + "\n";
+      Path deriver = showDerivers ? info.deriver : "";
+      s += deriver + "\n";
 
-        s += (format("%1%\n") % info.references.size()).str();
+      s += (format ("%1%\n") % info.references.size ()).str ();
 
-        foreach (PathSet::iterator, j, info.references)
-            s += *j + "\n";
+      foreach (PathSet::iterator, j, info.references)
+        s += *j + "\n";
     }
 
-    return s;
+  return s;
 }
 
-string showPaths(const PathSet & paths)
+string
+showPaths (const PathSet &paths)
 {
-    string s;
-    foreach (PathSet::const_iterator, i, paths) {
-        if (s.size() != 0) s += ", ";
-        s += "`" + *i + "'";
+  string s;
+  foreach (PathSet::const_iterator, i, paths)
+    {
+      if (s.size () != 0)
+        s += ", ";
+      s += "`" + *i + "'";
     }
-    return s;
+  return s;
 }
 
-Path readStorePath(Source & from)
+Path
+readStorePath (Source &from)
 {
-    Path path = readString(from);
-    assertStorePath(path);
-    return path;
+  Path path = readString (from);
+  assertStorePath (path);
+  return path;
 }
 
-
-template<class T> T readStorePaths(Source & from)
+template <class T>
+T
+readStorePaths (Source &from)
 {
-    T paths = readStrings<T>(from);
-    foreach (typename T::iterator, i, paths) assertStorePath(*i);
-    return paths;
+  T paths = readStrings<T> (from);
+  foreach (typename T::iterator, i, paths)
+    assertStorePath (*i);
+  return paths;
 }
 
-template PathSet readStorePaths(Source & from);
-
-}
+template PathSet readStorePaths (Source &from);
 
+} // namespace nix
 
 #include "local-store.hh"
 #include "serialise.hh"
 
-
-namespace nix {
-
+namespace nix
+{
 
 std::shared_ptr<StoreAPI> store;
 
-
 }
diff --git a/nix/libstore/store-api.hh b/nix/libstore/store-api.hh
index 82a79e50e2..08d01985b2 100644
--- a/nix/libstore/store-api.hh
+++ b/nix/libstore/store-api.hh
@@ -3,328 +3,323 @@
 #include "hash.hh"
 #include "serialise.hh"
 
-#include <string>
 #include <map>
 #include <memory>
+#include <string>
 
-
-namespace nix {
-
+namespace nix
+{
 
 typedef std::map<Path, Path> Roots;
 
-
 struct GCOptions
 {
-    /* Garbage collector operation:
+  /* Garbage collector operation:
 
-       - `gcReturnLive': return the set of paths reachable from
-         (i.e. in the closure of) the roots.
+     - `gcReturnLive': return the set of paths reachable from
+       (i.e. in the closure of) the roots.
 
-       - `gcReturnDead': return the set of paths not reachable from
-         the roots.
+     - `gcReturnDead': return the set of paths not reachable from
+       the roots.
 
-       - `gcDeleteDead': actually delete the latter set.
+     - `gcDeleteDead': actually delete the latter set.
 
-       - `gcDeleteSpecific': delete the paths listed in
-          `pathsToDelete', insofar as they are not reachable.
-    */
-    typedef enum {
-        gcReturnLive,
-        gcReturnDead,
-        gcDeleteDead,
-        gcDeleteSpecific,
-    } GCAction;
+     - `gcDeleteSpecific': delete the paths listed in
+        `pathsToDelete', insofar as they are not reachable.
+  */
+  typedef enum
+  {
+    gcReturnLive,
+    gcReturnDead,
+    gcDeleteDead,
+    gcDeleteSpecific,
+  } GCAction;
 
-    GCAction action;
+  GCAction action;
 
-    /* If `ignoreLiveness' is set, then reachability from the roots is
-       ignored (dangerous!).  However, the paths must still be
-       unreferenced *within* the store (i.e., there can be no other
-       store paths that depend on them). */
-    bool ignoreLiveness;
+  /* If `ignoreLiveness' is set, then reachability from the roots is
+     ignored (dangerous!).  However, the paths must still be
+     unreferenced *within* the store (i.e., there can be no other
+     store paths that depend on them). */
+  bool ignoreLiveness;
 
-    /* For `gcDeleteSpecific', the paths to delete. */
-    PathSet pathsToDelete;
+  /* For `gcDeleteSpecific', the paths to delete. */
+  PathSet pathsToDelete;
 
-    /* Stop after at least `maxFreed' bytes have been freed. */
-    unsigned long long maxFreed;
+  /* Stop after at least `maxFreed' bytes have been freed. */
+  unsigned long long maxFreed;
 
-    GCOptions();
+  GCOptions ();
 };
 
-
 struct GCResults
 {
-    /* Depending on the action, the GC roots, or the paths that would
-       be or have been deleted. */
-    PathSet paths;
-
-    /* For `gcReturnDead', `gcDeleteDead' and `gcDeleteSpecific', the
-       number of bytes that would be or was freed. */
-    unsigned long long bytesFreed;
-
-    GCResults()
-    {
-        bytesFreed = 0;
-    }
-};
+  /* Depending on the action, the GC roots, or the paths that would
+     be or have been deleted. */
+  PathSet paths;
+
+  /* For `gcReturnDead', `gcDeleteDead' and `gcDeleteSpecific', the
+     number of bytes that would be or was freed. */
+  unsigned long long bytesFreed;
 
+  GCResults () { bytesFreed = 0; }
+};
 
 struct SubstitutablePathInfo
 {
-    Path deriver;
-    PathSet references;
-    unsigned long long downloadSize; /* 0 = unknown or inapplicable */
-    unsigned long long narSize; /* 0 = unknown */
+  Path deriver;
+  PathSet references;
+  unsigned long long downloadSize; /* 0 = unknown or inapplicable */
+  unsigned long long narSize;      /* 0 = unknown */
 };
 
 typedef std::map<Path, SubstitutablePathInfo> SubstitutablePathInfos;
 
-
 struct ValidPathInfo
 {
-    Path path;
-    Path deriver;
-    Hash hash;
-    PathSet references;
-    time_t registrationTime = 0;
-    uint64_t narSize = 0; // 0 = unknown
-    uint64_t id; // internal use only
-
-    bool operator == (const ValidPathInfo & i) const
-    {
-        return
-            path == i.path
-            && hash == i.hash
-            && references == i.references;
-    }
+  Path path;
+  Path deriver;
+  Hash hash;
+  PathSet references;
+  time_t registrationTime = 0;
+  uint64_t narSize = 0; // 0 = unknown
+  uint64_t id;          // internal use only
+
+  bool
+  operator== (const ValidPathInfo &i) const
+  {
+    return path == i.path && hash == i.hash && references == i.references;
+  }
 };
 
 typedef list<ValidPathInfo> ValidPathInfos;
 
-
-enum BuildMode { bmNormal, bmRepair, bmCheck };
+enum BuildMode
+{
+  bmNormal,
+  bmRepair,
+  bmCheck
+};
 
 struct BuildResult
 {
-    enum Status {
-        Built = 0,
-        Substituted,
-        AlreadyValid,
-        PermanentFailure,
-        InputRejected,
-        OutputRejected,
-        TransientFailure, // possibly transient
-        CachedFailure,
-        TimedOut,
-        MiscFailure,
-        DependencyFailed,
-        LogLimitExceeded,
-        NotDeterministic,
-    } status = MiscFailure;
-    std::string errorMsg;
-    //time_t startTime = 0, stopTime = 0;
-    bool success() {
-        return status == Built || status == Substituted || status == AlreadyValid;
-    }
+  enum Status
+  {
+    Built = 0,
+    Substituted,
+    AlreadyValid,
+    PermanentFailure,
+    InputRejected,
+    OutputRejected,
+    TransientFailure, // possibly transient
+    CachedFailure,
+    TimedOut,
+    MiscFailure,
+    DependencyFailed,
+    LogLimitExceeded,
+    NotDeterministic,
+  } status
+      = MiscFailure;
+  std::string errorMsg;
+  // time_t startTime = 0, stopTime = 0;
+  bool
+  success ()
+  {
+    return status == Built || status == Substituted || status == AlreadyValid;
+  }
 };
 
-
 class StoreAPI
 {
 public:
-
-    virtual ~StoreAPI() { }
-
-    /* Check whether a path is valid. */
-    virtual bool isValidPath(const Path & path) = 0;
-
-    /* Query which of the given paths is valid. */
-    virtual PathSet queryValidPaths(const PathSet & paths) = 0;
-
-    /* Query the set of all valid paths. */
-    virtual PathSet queryAllValidPaths() = 0;
-
-    /* Query information about a valid path. */
-    virtual ValidPathInfo queryPathInfo(const Path & path) = 0;
-
-    /* Query the hash of a valid path. */
-    virtual Hash queryPathHash(const Path & path) = 0;
-
-    /* Query the set of outgoing FS references for a store path.  The
-       result is not cleared. */
-    virtual void queryReferences(const Path & path,
-        PathSet & references) = 0;
-
-    /* Queries the set of incoming FS references for a store path.
-       The result is not cleared. */
-    virtual void queryReferrers(const Path & path,
-        PathSet & referrers) = 0;
-
-    /* Query the deriver of a store path.  Return the empty string if
-       no deriver has been set. */
-    virtual Path queryDeriver(const Path & path) = 0;
-
-    /* Return all currently valid derivations that have `path' as an
-       output.  (Note that the result of `queryDeriver()' is the
-       derivation that was actually used to produce `path', which may
-       not exist anymore.) */
-    virtual PathSet queryValidDerivers(const Path & path) = 0;
-
-    /* Query the outputs of the derivation denoted by `path'. */
-    virtual PathSet queryDerivationOutputs(const Path & path) = 0;
-
-    /* Query the output names of the derivation denoted by `path'. */
-    virtual StringSet queryDerivationOutputNames(const Path & path) = 0;
-
-    /* Query the full store path given the hash part of a valid store
-       path, or "" if the path doesn't exist. */
-    virtual Path queryPathFromHashPart(const string & hashPart) = 0;
-
-    /* Query which of the given paths have substitutes. */
-    virtual PathSet querySubstitutablePaths(const PathSet & paths) = 0;
-
-    /* Query substitute info (i.e. references, derivers and download
-       sizes) of a set of paths.  If a path does not have substitute
-       info, it's omitted from the resulting ‘infos’ map. */
-    virtual void querySubstitutablePathInfos(const PathSet & paths,
-        SubstitutablePathInfos & infos) = 0;
-
-    /* Copy the contents of a path to the store and register the
-       validity the resulting path.  The resulting path is returned.
-       The function object `filter' can be used to exclude files (see
-       libutil/archive.hh). */
-    virtual Path addToStore(const string & name, const Path & srcPath,
-        bool recursive = true, HashType hashAlgo = htSHA256,
-        PathFilter & filter = defaultPathFilter, bool repair = false) = 0;
-
-    /* Like addToStore, but the contents written to the output path is
-       a regular file containing the given string. */
-    virtual Path addTextToStore(const string & name, const string & s,
-        const PathSet & references, bool repair = false) = 0;
-
-    /* Export a store path, that is, create a NAR dump of the store
-       path and append its references and its deriver.  Optionally, a
-       cryptographic signature (created by OpenSSL) of the preceding
-       data is attached. */
-    virtual void exportPath(const Path & path, bool sign,
-        Sink & sink) = 0;
-
-    /* Import a sequence of NAR dumps created by exportPaths() into
-       the Nix store. */
-    virtual Paths importPaths(bool requireSignature, Source & source) = 0;
-
-    /* For each path, if it's a derivation, build it.  Building a
-       derivation means ensuring that the output paths are valid.  If
-       they are already valid, this is a no-op.  Otherwise, validity
-       can be reached in two ways.  First, if the output paths is
-       substitutable, then build the path that way.  Second, the
-       output paths can be created by running the builder, after
-       recursively building any sub-derivations. For inputs that are
-       not derivations, substitute them. */
-    virtual void buildPaths(const PathSet & paths, BuildMode buildMode = bmNormal) = 0;
-
-    /* Ensure that a path is valid.  If it is not currently valid, it
-       may be made valid by running a substitute (if defined for the
-       path). */
-    virtual void ensurePath(const Path & path) = 0;
-
-    /* Add a store path as a temporary root of the garbage collector.
-       The root disappears as soon as we exit. */
-    virtual void addTempRoot(const Path & path) = 0;
-
-    /* Add an indirect root, which is merely a symlink to `path' from
-       /nix/var/nix/gcroots/auto/<hash of `path'>.  `path' is supposed
-       to be a symlink to a store path.  The garbage collector will
-       automatically remove the indirect root when it finds that
-       `path' has disappeared. */
-    virtual void addIndirectRoot(const Path & path) = 0;
-
-    /* Acquire the global GC lock, then immediately release it.  This
-       function must be called after registering a new permanent root,
-       but before exiting.  Otherwise, it is possible that a running
-       garbage collector doesn't see the new root and deletes the
-       stuff we've just built.  By acquiring the lock briefly, we
-       ensure that either:
-
-       - The collector is already running, and so we block until the
-         collector is finished.  The collector will know about our
-         *temporary* locks, which should include whatever it is we
-         want to register as a permanent lock.
-
-       - The collector isn't running, or it's just started but hasn't
-         acquired the GC lock yet.  In that case we get and release
-         the lock right away, then exit.  The collector scans the
-         permanent root and sees our's.
-
-       In either case the permanent root is seen by the collector. */
-    virtual void syncWithGC() = 0;
-
-    /* Find the roots of the garbage collector.  Each root is a pair
-       (link, storepath) where `link' is the path of the symlink
-       outside of the Nix store that point to `storePath'.  */
-    virtual Roots findRoots() = 0;
-
-    /* Perform a garbage collection. */
-    virtual void collectGarbage(const GCOptions & options, GCResults & results) = 0;
-
-    /* Return the set of paths that have failed to build.*/
-    virtual PathSet queryFailedPaths() = 0;
-
-    /* Clear the "failed" status of the given paths.  The special
-       value `*' causes all failed paths to be cleared. */
-    virtual void clearFailedPaths(const PathSet & paths) = 0;
-
-    /* Return a string representing information about the path that
-       can be loaded into the database using `nix-store --load-db' or
-       `nix-store --register-validity'. */
-    string makeValidityRegistration(const PathSet & paths,
-        bool showDerivers, bool showHash);
-
-    /* Optimise the disk space usage of the Nix store by hard-linking files
-       with the same contents. */
-    virtual void optimiseStore() = 0;
-
-    /* Check the integrity of the Nix store.  Returns true if errors
-       remain. */
-    virtual bool verifyStore(bool checkContents, bool repair) = 0;
-
-    /* Create a profile for the given user.  This is done by the daemon
-       because the 'profiles/per-user' directory is not writable by users.  */
-    virtual void createUser(const std::string & userName, uid_t userId) = 0;
+  virtual ~StoreAPI () {}
+
+  /* Check whether a path is valid. */
+  virtual bool isValidPath (const Path &path) = 0;
+
+  /* Query which of the given paths is valid. */
+  virtual PathSet queryValidPaths (const PathSet &paths) = 0;
+
+  /* Query the set of all valid paths. */
+  virtual PathSet queryAllValidPaths () = 0;
+
+  /* Query information about a valid path. */
+  virtual ValidPathInfo queryPathInfo (const Path &path) = 0;
+
+  /* Query the hash of a valid path. */
+  virtual Hash queryPathHash (const Path &path) = 0;
+
+  /* Query the set of outgoing FS references for a store path.  The
+     result is not cleared. */
+  virtual void queryReferences (const Path &path, PathSet &references) = 0;
+
+  /* Queries the set of incoming FS references for a store path.
+     The result is not cleared. */
+  virtual void queryReferrers (const Path &path, PathSet &referrers) = 0;
+
+  /* Query the deriver of a store path.  Return the empty string if
+     no deriver has been set. */
+  virtual Path queryDeriver (const Path &path) = 0;
+
+  /* Return all currently valid derivations that have `path' as an
+     output.  (Note that the result of `queryDeriver()' is the
+     derivation that was actually used to produce `path', which may
+     not exist anymore.) */
+  virtual PathSet queryValidDerivers (const Path &path) = 0;
+
+  /* Query the outputs of the derivation denoted by `path'. */
+  virtual PathSet queryDerivationOutputs (const Path &path) = 0;
+
+  /* Query the output names of the derivation denoted by `path'. */
+  virtual StringSet queryDerivationOutputNames (const Path &path) = 0;
+
+  /* Query the full store path given the hash part of a valid store
+     path, or "" if the path doesn't exist. */
+  virtual Path queryPathFromHashPart (const string &hashPart) = 0;
+
+  /* Query which of the given paths have substitutes. */
+  virtual PathSet querySubstitutablePaths (const PathSet &paths) = 0;
+
+  /* Query substitute info (i.e. references, derivers and download
+     sizes) of a set of paths.  If a path does not have substitute
+     info, it's omitted from the resulting ‘infos’ map. */
+  virtual void querySubstitutablePathInfos (const PathSet &paths,
+                                            SubstitutablePathInfos &infos)
+      = 0;
+
+  /* Copy the contents of a path to the store and register the
+     validity the resulting path.  The resulting path is returned.
+     The function object `filter' can be used to exclude files (see
+     libutil/archive.hh). */
+  virtual Path addToStore (const string &name, const Path &srcPath,
+                           bool recursive = true, HashType hashAlgo = htSHA256,
+                           PathFilter &filter = defaultPathFilter,
+                           bool repair = false)
+      = 0;
+
+  /* Like addToStore, but the contents written to the output path is
+     a regular file containing the given string. */
+  virtual Path addTextToStore (const string &name, const string &s,
+                               const PathSet &references, bool repair = false)
+      = 0;
+
+  /* Export a store path, that is, create a NAR dump of the store
+     path and append its references and its deriver.  Optionally, a
+     cryptographic signature (created by OpenSSL) of the preceding
+     data is attached. */
+  virtual void exportPath (const Path &path, bool sign, Sink &sink) = 0;
+
+  /* Import a sequence of NAR dumps created by exportPaths() into
+     the Nix store. */
+  virtual Paths importPaths (bool requireSignature, Source &source) = 0;
+
+  /* For each path, if it's a derivation, build it.  Building a
+     derivation means ensuring that the output paths are valid.  If
+     they are already valid, this is a no-op.  Otherwise, validity
+     can be reached in two ways.  First, if the output paths is
+     substitutable, then build the path that way.  Second, the
+     output paths can be created by running the builder, after
+     recursively building any sub-derivations. For inputs that are
+     not derivations, substitute them. */
+  virtual void buildPaths (const PathSet &paths,
+                           BuildMode buildMode = bmNormal)
+      = 0;
+
+  /* Ensure that a path is valid.  If it is not currently valid, it
+     may be made valid by running a substitute (if defined for the
+     path). */
+  virtual void ensurePath (const Path &path) = 0;
+
+  /* Add a store path as a temporary root of the garbage collector.
+     The root disappears as soon as we exit. */
+  virtual void addTempRoot (const Path &path) = 0;
+
+  /* Add an indirect root, which is merely a symlink to `path' from
+     /nix/var/nix/gcroots/auto/<hash of `path'>.  `path' is supposed
+     to be a symlink to a store path.  The garbage collector will
+     automatically remove the indirect root when it finds that
+     `path' has disappeared. */
+  virtual void addIndirectRoot (const Path &path) = 0;
+
+  /* Acquire the global GC lock, then immediately release it.  This
+     function must be called after registering a new permanent root,
+     but before exiting.  Otherwise, it is possible that a running
+     garbage collector doesn't see the new root and deletes the
+     stuff we've just built.  By acquiring the lock briefly, we
+     ensure that either:
+
+     - The collector is already running, and so we block until the
+       collector is finished.  The collector will know about our
+       *temporary* locks, which should include whatever it is we
+       want to register as a permanent lock.
+
+     - The collector isn't running, or it's just started but hasn't
+       acquired the GC lock yet.  In that case we get and release
+       the lock right away, then exit.  The collector scans the
+       permanent root and sees our's.
+
+     In either case the permanent root is seen by the collector. */
+  virtual void syncWithGC () = 0;
+
+  /* Find the roots of the garbage collector.  Each root is a pair
+     (link, storepath) where `link' is the path of the symlink
+     outside of the Nix store that point to `storePath'.  */
+  virtual Roots findRoots () = 0;
+
+  /* Perform a garbage collection. */
+  virtual void collectGarbage (const GCOptions &options, GCResults &results)
+      = 0;
+
+  /* Return the set of paths that have failed to build.*/
+  virtual PathSet queryFailedPaths () = 0;
+
+  /* Clear the "failed" status of the given paths.  The special
+     value `*' causes all failed paths to be cleared. */
+  virtual void clearFailedPaths (const PathSet &paths) = 0;
+
+  /* Return a string representing information about the path that
+     can be loaded into the database using `nix-store --load-db' or
+     `nix-store --register-validity'. */
+  string makeValidityRegistration (const PathSet &paths, bool showDerivers,
+                                   bool showHash);
+
+  /* Optimise the disk space usage of the Nix store by hard-linking files
+     with the same contents. */
+  virtual void optimiseStore () = 0;
+
+  /* Check the integrity of the Nix store.  Returns true if errors
+     remain. */
+  virtual bool verifyStore (bool checkContents, bool repair) = 0;
+
+  /* Create a profile for the given user.  This is done by the daemon
+     because the 'profiles/per-user' directory is not writable by users.  */
+  virtual void createUser (const std::string &userName, uid_t userId) = 0;
 };
 
-
 /* !!! These should be part of the store API, I guess. */
 
 /* Throw an exception if `path' is not directly in the Nix store. */
-void assertStorePath(const Path & path);
+void assertStorePath (const Path &path);
 
-bool isInStore(const Path & path);
-bool isStorePath(const Path & path);
+bool isInStore (const Path &path);
+bool isStorePath (const Path &path);
 
 /* Extract the name part of the given store path. */
-string storePathToName(const Path & path);
-
-void checkStoreName(const string & name);
+string storePathToName (const Path &path);
 
+void checkStoreName (const string &name);
 
 /* Chop off the parts after the top-level store name, e.g.,
    /nix/store/abcd-foo/bar => /nix/store/abcd-foo. */
-Path toStorePath(const Path & path);
-
+Path toStorePath (const Path &path);
 
 /* Constructs a unique store path name. */
-Path makeStorePath(const string & type,
-    const Hash & hash, const string & name);
-
-Path makeOutputPath(const string & id,
-    const Hash & hash, const string & name);
+Path makeStorePath (const string &type, const Hash &hash, const string &name);
 
-Path makeFixedOutputPath(bool recursive,
-    HashType hashAlgo, Hash hash, string name);
+Path makeOutputPath (const string &id, const Hash &hash, const string &name);
 
+Path makeFixedOutputPath (bool recursive, HashType hashAlgo, Hash hash,
+                          string name);
 
 /* Preparatory part of addTextToStore().
 
@@ -340,37 +335,31 @@ Path makeFixedOutputPath(bool recursive,
    simply yield a different store path, so other users wouldn't be
    affected), but it has some backwards compatibility issues (the
    hashing scheme changes), so I'm not doing that for now. */
-Path computeStorePathForText(const string & name, const string & s,
-    const PathSet & references);
-
+Path computeStorePathForText (const string &name, const string &s,
+                              const PathSet &references);
 
 /* Remove the temporary roots file for this process.  Any temporary
    root becomes garbage after this point unless it has been registered
    as a (permanent) root. */
-void removeTempRoots();
-
+void removeTempRoots ();
 
 /* Register a permanent GC root. */
-Path addPermRoot(StoreAPI & store, const Path & storePath,
-    const Path & gcRoot, bool indirect, bool allowOutsideRootsDir = false);
-
+Path addPermRoot (StoreAPI &store, const Path &storePath, const Path &gcRoot,
+                  bool indirect, bool allowOutsideRootsDir = false);
 
 /* Sort a set of paths topologically under the references relation.
    If p refers to q, then p preceeds q in this list. */
-Paths topoSortPaths(StoreAPI & store, const PathSet & paths);
-
+Paths topoSortPaths (StoreAPI &store, const PathSet &paths);
 
 /* For now, there is a single global store API object, but we'll
    purify that in the future. */
 extern std::shared_ptr<StoreAPI> store;
 
-
 /* Display a set of paths in human-readable form (i.e., between quotes
    and separated by commas). */
-string showPaths(const PathSet & paths);
-
-MakeError(SubstError, Error)
-MakeError(BuildError, Error) /* denotes a permanent build failure */
+string showPaths (const PathSet &paths);
 
+MakeError (SubstError, Error)
+    MakeError (BuildError, Error) /* denotes a permanent build failure */
 
 }
diff --git a/nix/libstore/worker-protocol.hh b/nix/libstore/worker-protocol.hh
index ef259db2a0..488ab9a5c4 100644
--- a/nix/libstore/worker-protocol.hh
+++ b/nix/libstore/worker-protocol.hh
@@ -1,7 +1,7 @@
 #pragma once
 
-namespace nix {
-
+namespace nix
+{
 
 #define WORKER_MAGIC_1 0x6e697863
 #define WORKER_MAGIC_2 0x6478696f
@@ -10,54 +10,51 @@ namespace nix {
 #define GET_PROTOCOL_MAJOR(x) ((x) & 0xff00)
 #define GET_PROTOCOL_MINOR(x) ((x) & 0x00ff)
 
-
-typedef enum {
-    wopIsValidPath = 1,
-    wopHasSubstitutes = 3,
-    wopQueryPathHash = 4,
-    wopQueryReferences = 5,
-    wopQueryReferrers = 6,
-    wopAddToStore = 7,
-    wopAddTextToStore = 8,
-    wopBuildPaths = 9,
-    wopEnsurePath = 10,
-    wopAddTempRoot = 11,
-    wopAddIndirectRoot = 12,
-    wopSyncWithGC = 13,
-    wopFindRoots = 14,
-    wopExportPath = 16,
-    wopQueryDeriver = 18,
-    wopSetOptions = 19,
-    wopCollectGarbage = 20,
-    wopQuerySubstitutablePathInfo = 21,
-    wopQueryDerivationOutputs = 22,
-    wopQueryAllValidPaths = 23,
-    wopQueryFailedPaths = 24,
-    wopClearFailedPaths = 25,
-    wopQueryPathInfo = 26,
-    wopImportPaths = 27,
-    wopQueryDerivationOutputNames = 28,
-    wopQueryPathFromHashPart = 29,
-    wopQuerySubstitutablePathInfos = 30,
-    wopQueryValidPaths = 31,
-    wopQuerySubstitutablePaths = 32,
-    wopQueryValidDerivers = 33,
-    wopOptimiseStore = 34,
-    wopVerifyStore = 35,
-    wopBuiltinBuilders = 80,
-    wopSubstituteURLs = 81
+typedef enum
+{
+  wopIsValidPath = 1,
+  wopHasSubstitutes = 3,
+  wopQueryPathHash = 4,
+  wopQueryReferences = 5,
+  wopQueryReferrers = 6,
+  wopAddToStore = 7,
+  wopAddTextToStore = 8,
+  wopBuildPaths = 9,
+  wopEnsurePath = 10,
+  wopAddTempRoot = 11,
+  wopAddIndirectRoot = 12,
+  wopSyncWithGC = 13,
+  wopFindRoots = 14,
+  wopExportPath = 16,
+  wopQueryDeriver = 18,
+  wopSetOptions = 19,
+  wopCollectGarbage = 20,
+  wopQuerySubstitutablePathInfo = 21,
+  wopQueryDerivationOutputs = 22,
+  wopQueryAllValidPaths = 23,
+  wopQueryFailedPaths = 24,
+  wopClearFailedPaths = 25,
+  wopQueryPathInfo = 26,
+  wopImportPaths = 27,
+  wopQueryDerivationOutputNames = 28,
+  wopQueryPathFromHashPart = 29,
+  wopQuerySubstitutablePathInfos = 30,
+  wopQueryValidPaths = 31,
+  wopQuerySubstitutablePaths = 32,
+  wopQueryValidDerivers = 33,
+  wopOptimiseStore = 34,
+  wopVerifyStore = 35,
+  wopBuiltinBuilders = 80,
+  wopSubstituteURLs = 81
 } WorkerOp;
 
-
-#define STDERR_NEXT  0x6f6c6d67
-#define STDERR_READ  0x64617461 // data needed from source
+#define STDERR_NEXT 0x6f6c6d67
+#define STDERR_READ 0x64617461  // data needed from source
 #define STDERR_WRITE 0x64617416 // data for sink
-#define STDERR_LAST  0x616c7473
+#define STDERR_LAST 0x616c7473
 #define STDERR_ERROR 0x63787470
 
-
-Path readStorePath(Source & from);
-template<class T> T readStorePaths(Source & from);
-
+Path readStorePath (Source &from);
+template <class T> T readStorePaths (Source &from);
 
 }
diff --git a/nix/libutil/affinity.cc b/nix/libutil/affinity.cc
index 3e21f43a2e..c8366083e0 100644
--- a/nix/libutil/affinity.cc
+++ b/nix/libutil/affinity.cc
@@ -1,55 +1,57 @@
+#include "affinity.hh"
 #include "types.hh"
 #include "util.hh"
-#include "affinity.hh"
 
 #if HAVE_SCHED_H
 #include <sched.h>
 #endif
 
-namespace nix {
-
+namespace nix
+{
 
 #if HAVE_SCHED_SETAFFINITY
 static bool didSaveAffinity = false;
 static cpu_set_t savedAffinity;
 #endif
 
-
-void setAffinityTo(int cpu)
+void
+setAffinityTo (int cpu)
 {
 #if HAVE_SCHED_SETAFFINITY
-    if (sched_getaffinity(0, sizeof(cpu_set_t), &savedAffinity) == -1) return;
-    didSaveAffinity = true;
-    printMsg(lvlDebug, format("locking this thread to CPU %1%") % cpu);
-    cpu_set_t newAffinity;
-    CPU_ZERO(&newAffinity);
-    CPU_SET(cpu, &newAffinity);
-    if (sched_setaffinity(0, sizeof(cpu_set_t), &newAffinity) == -1)
-        printMsg(lvlError, format("failed to lock thread to CPU %1%") % cpu);
+  if (sched_getaffinity (0, sizeof (cpu_set_t), &savedAffinity) == -1)
+    return;
+  didSaveAffinity = true;
+  printMsg (lvlDebug, format ("locking this thread to CPU %1%") % cpu);
+  cpu_set_t newAffinity;
+  CPU_ZERO (&newAffinity);
+  CPU_SET (cpu, &newAffinity);
+  if (sched_setaffinity (0, sizeof (cpu_set_t), &newAffinity) == -1)
+    printMsg (lvlError, format ("failed to lock thread to CPU %1%") % cpu);
 #endif
 }
 
-
-int lockToCurrentCPU()
+int
+lockToCurrentCPU ()
 {
 #if HAVE_SCHED_SETAFFINITY
-    int cpu = sched_getcpu();
-    if (cpu != -1) setAffinityTo(cpu);
-    return cpu;
+  int cpu = sched_getcpu ();
+  if (cpu != -1)
+    setAffinityTo (cpu);
+  return cpu;
 #else
-    return -1;
+  return -1;
 #endif
 }
 
-
-void restoreAffinity()
+void
+restoreAffinity ()
 {
 #if HAVE_SCHED_SETAFFINITY
-    if (!didSaveAffinity) return;
-    if (sched_setaffinity(0, sizeof(cpu_set_t), &savedAffinity) == -1)
-        printMsg(lvlError, "failed to restore affinity %1%");
+  if (!didSaveAffinity)
+    return;
+  if (sched_setaffinity (0, sizeof (cpu_set_t), &savedAffinity) == -1)
+    printMsg (lvlError, "failed to restore affinity %1%");
 #endif
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libutil/affinity.hh b/nix/libutil/affinity.hh
index c1bd28e136..f98602550e 100644
--- a/nix/libutil/affinity.hh
+++ b/nix/libutil/affinity.hh
@@ -1,9 +1,10 @@
 #pragma once
 
-namespace nix {
+namespace nix
+{
 
-void setAffinityTo(int cpu);
-int lockToCurrentCPU();
-void restoreAffinity();
+void setAffinityTo (int cpu);
+int lockToCurrentCPU ();
+void restoreAffinity ();
 
 }
diff --git a/nix/libutil/archive.cc b/nix/libutil/archive.cc
index 54bcd21f93..0cf0fd4cdd 100644
--- a/nix/libutil/archive.cc
+++ b/nix/libutil/archive.cc
@@ -2,24 +2,24 @@
 
 #include "config.h"
 
-#include <cerrno>
 #include <algorithm>
-#include <vector>
+#include <cerrno>
 #include <map>
+#include <vector>
 
 #include <strings.h> // for strcasecmp
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 #include <dirent.h>
 #include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include "archive.hh"
 #include "util.hh"
 
-
-namespace nix {
+namespace nix
+{
 
 static string archiveVersion1 = "nix-archive-1";
 
@@ -27,96 +27,102 @@ static string caseHackSuffix = "~nix~case~hack~";
 
 PathFilter defaultPathFilter;
 
-
-static void dumpContents(const Path & path, size_t size,
-    Sink & sink)
+static void
+dumpContents (const Path &path, size_t size, Sink &sink)
 {
-    writeString("contents", sink);
-    writeLongLong(size, sink);
+  writeString ("contents", sink);
+  writeLongLong (size, sink);
 
-    AutoCloseFD fd = open(path.c_str(), O_RDONLY);
-    if (fd == -1) throw SysError(format("opening file `%1%'") % path);
+  AutoCloseFD fd = open (path.c_str (), O_RDONLY);
+  if (fd == -1)
+    throw SysError (format ("opening file `%1%'") % path);
 
-    unsigned char buf[65536];
-    size_t left = size;
+  unsigned char buf[65536];
+  size_t left = size;
 
-    while (left > 0) {
-        size_t n = left > sizeof(buf) ? sizeof(buf) : left;
-        readFull(fd, buf, n);
-        left -= n;
-        sink(buf, n);
+  while (left > 0)
+    {
+      size_t n = left > sizeof (buf) ? sizeof (buf) : left;
+      readFull (fd, buf, n);
+      left -= n;
+      sink (buf, n);
     }
 
-    writePadding(size, sink);
+  writePadding (size, sink);
 }
 
-
-static void dump(const Path & path, Sink & sink, PathFilter & filter)
+static void
+dump (const Path &path, Sink &sink, PathFilter &filter)
 {
-    struct stat st;
-    if (lstat(path.c_str(), &st))
-        throw SysError(format("getting attributes of path `%1%'") % path);
+  struct stat st;
+  if (lstat (path.c_str (), &st))
+    throw SysError (format ("getting attributes of path `%1%'") % path);
 
-    writeString("(", sink);
+  writeString ("(", sink);
 
-    if (S_ISREG(st.st_mode)) {
-        writeString("type", sink);
-        writeString("regular", sink);
-        if (st.st_mode & S_IXUSR) {
-            writeString("executable", sink);
-            writeString("", sink);
+  if (S_ISREG (st.st_mode))
+    {
+      writeString ("type", sink);
+      writeString ("regular", sink);
+      if (st.st_mode & S_IXUSR)
+        {
+          writeString ("executable", sink);
+          writeString ("", sink);
         }
-        dumpContents(path, (size_t) st.st_size, sink);
+      dumpContents (path, (size_t)st.st_size, sink);
     }
 
-    else if (S_ISDIR(st.st_mode)) {
-        writeString("type", sink);
-        writeString("directory", sink);
-
-        /* If we're on a case-insensitive system like Mac OS X, undo
-           the case hack applied by restorePath(). */
-        std::map<string, string> unhacked;
-        for (auto & i : readDirectory(path))
-	    unhacked[i.name] = i.name;
-
-        for (auto & i : unhacked)
-            if (filter(path + "/" + i.first)) {
-                writeString("entry", sink);
-                writeString("(", sink);
-                writeString("name", sink);
-                writeString(i.first, sink);
-                writeString("node", sink);
-                dump(path + "/" + i.second, sink, filter);
-                writeString(")", sink);
-            }
+  else if (S_ISDIR (st.st_mode))
+    {
+      writeString ("type", sink);
+      writeString ("directory", sink);
+
+      /* If we're on a case-insensitive system like Mac OS X, undo
+         the case hack applied by restorePath(). */
+      std::map<string, string> unhacked;
+      for (auto &i : readDirectory (path))
+        unhacked[i.name] = i.name;
+
+      for (auto &i : unhacked)
+        if (filter (path + "/" + i.first))
+          {
+            writeString ("entry", sink);
+            writeString ("(", sink);
+            writeString ("name", sink);
+            writeString (i.first, sink);
+            writeString ("node", sink);
+            dump (path + "/" + i.second, sink, filter);
+            writeString (")", sink);
+          }
     }
 
-    else if (S_ISLNK(st.st_mode)) {
-        writeString("type", sink);
-        writeString("symlink", sink);
-        writeString("target", sink);
-        writeString(readLink(path), sink);
+  else if (S_ISLNK (st.st_mode))
+    {
+      writeString ("type", sink);
+      writeString ("symlink", sink);
+      writeString ("target", sink);
+      writeString (readLink (path), sink);
     }
 
-    else throw Error(format("file `%1%' has an unsupported type") % path);
+  else
+    throw Error (format ("file `%1%' has an unsupported type") % path);
 
-    writeString(")", sink);
+  writeString (")", sink);
 }
 
-
-void dumpPath(const Path & path, Sink & sink, PathFilter & filter)
+void
+dumpPath (const Path &path, Sink &sink, PathFilter &filter)
 {
-    writeString(archiveVersion1, sink);
-    dump(path, sink, filter);
+  writeString (archiveVersion1, sink);
+  dump (path, sink, filter);
 }
 
-
-static SerialisationError badArchive(string s)
+static SerialisationError
+badArchive (string s)
 {
-    return SerialisationError("bad archive: " + s);
+  return SerialisationError ("bad archive: " + s);
 }
 
-
 #if 0
 static void skipGeneric(Source & source)
 {
@@ -127,207 +133,249 @@ static void skipGeneric(Source & source)
 }
 #endif
 
-
-static void parseContents(ParseSink & sink, Source & source, const Path & path)
+static void
+parseContents (ParseSink &sink, Source &source, const Path &path)
 {
-    unsigned long long size = readLongLong(source);
+  unsigned long long size = readLongLong (source);
 
-    sink.preallocateContents(size);
+  sink.preallocateContents (size);
 
-    unsigned long long left = size;
-    unsigned char buf[65536];
+  unsigned long long left = size;
+  unsigned char buf[65536];
 
-    while (left) {
-        checkInterrupt();
-        unsigned int n = sizeof(buf);
-        if ((unsigned long long) n > left) n = left;
-        source(buf, n);
-        sink.receiveContents(buf, n);
-        left -= n;
+  while (left)
+    {
+      checkInterrupt ();
+      unsigned int n = sizeof (buf);
+      if ((unsigned long long)n > left)
+        n = left;
+      source (buf, n);
+      sink.receiveContents (buf, n);
+      left -= n;
     }
 
-    readPadding(size, source);
+  readPadding (size, source);
 }
 
-
 struct CaseInsensitiveCompare
 {
-    bool operator() (const string & a, const string & b) const
-    {
-        return strcasecmp(a.c_str(), b.c_str()) < 0;
-    }
+  bool
+  operator() (const string &a, const string &b) const
+  {
+    return strcasecmp (a.c_str (), b.c_str ()) < 0;
+  }
 };
 
-
-static void parse(ParseSink & sink, Source & source, const Path & path)
+static void
+parse (ParseSink &sink, Source &source, const Path &path)
 {
-    string s;
+  string s;
 
-    s = readString(source);
-    if (s != "(") throw badArchive("expected open tag");
+  s = readString (source);
+  if (s != "(")
+    throw badArchive ("expected open tag");
 
-    enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown;
+  enum
+  {
+    tpUnknown,
+    tpRegular,
+    tpDirectory,
+    tpSymlink
+  } type
+      = tpUnknown;
 
-    std::map<Path, int, CaseInsensitiveCompare> names;
+  std::map<Path, int, CaseInsensitiveCompare> names;
 
-    while (1) {
-        checkInterrupt();
+  while (1)
+    {
+      checkInterrupt ();
 
-        s = readString(source);
+      s = readString (source);
 
-        if (s == ")") {
-            break;
+      if (s == ")")
+        {
+          break;
         }
 
-        else if (s == "type") {
-            if (type != tpUnknown)
-                throw badArchive("multiple type fields");
-            string t = readString(source);
+      else if (s == "type")
+        {
+          if (type != tpUnknown)
+            throw badArchive ("multiple type fields");
+          string t = readString (source);
 
-            if (t == "regular") {
-                type = tpRegular;
-                sink.createRegularFile(path);
+          if (t == "regular")
+            {
+              type = tpRegular;
+              sink.createRegularFile (path);
             }
 
-            else if (t == "directory") {
-                sink.createDirectory(path);
-                type = tpDirectory;
+          else if (t == "directory")
+            {
+              sink.createDirectory (path);
+              type = tpDirectory;
             }
 
-            else if (t == "symlink") {
-                type = tpSymlink;
+          else if (t == "symlink")
+            {
+              type = tpSymlink;
             }
 
-            else throw badArchive("unknown file type " + t);
-
+          else
+            throw badArchive ("unknown file type " + t);
         }
 
-        else if (s == "contents" && type == tpRegular) {
-            parseContents(sink, source, path);
+      else if (s == "contents" && type == tpRegular)
+        {
+          parseContents (sink, source, path);
         }
 
-        else if (s == "executable" && type == tpRegular) {
-            readString(source);
-            sink.isExecutable();
+      else if (s == "executable" && type == tpRegular)
+        {
+          readString (source);
+          sink.isExecutable ();
         }
 
-        else if (s == "entry" && type == tpDirectory) {
-            string name, prevName;
-
-            s = readString(source);
-            if (s != "(") throw badArchive("expected open tag");
-
-            while (1) {
-                checkInterrupt();
-
-                s = readString(source);
-
-                if (s == ")") {
-                    break;
-                } else if (s == "name") {
-                    name = readString(source);
-                    if (name.empty() || name == "." || name == ".." || name.find('/') != string::npos || name.find((char) 0) != string::npos)
-                        throw Error(format("NAR contains invalid file name `%1%'") % name);
-                    if (name <= prevName)
-                        throw Error("NAR directory is not sorted");
-                    prevName = name;
-                } else if (s == "node") {
-                    if (s.empty()) throw badArchive("entry name missing");
-                    parse(sink, source, path + "/" + name);
-                } else
-                    throw badArchive("unknown field " + s);
+      else if (s == "entry" && type == tpDirectory)
+        {
+          string name, prevName;
+
+          s = readString (source);
+          if (s != "(")
+            throw badArchive ("expected open tag");
+
+          while (1)
+            {
+              checkInterrupt ();
+
+              s = readString (source);
+
+              if (s == ")")
+                {
+                  break;
+                }
+              else if (s == "name")
+                {
+                  name = readString (source);
+                  if (name.empty () || name == "." || name == ".."
+                      || name.find ('/') != string::npos
+                      || name.find ((char)0) != string::npos)
+                    throw Error (
+                        format ("NAR contains invalid file name `%1%'")
+                        % name);
+                  if (name <= prevName)
+                    throw Error ("NAR directory is not sorted");
+                  prevName = name;
+                }
+              else if (s == "node")
+                {
+                  if (s.empty ())
+                    throw badArchive ("entry name missing");
+                  parse (sink, source, path + "/" + name);
+                }
+              else
+                throw badArchive ("unknown field " + s);
             }
         }
 
-        else if (s == "target" && type == tpSymlink) {
-            string target = readString(source);
-            sink.createSymlink(path, target);
+      else if (s == "target" && type == tpSymlink)
+        {
+          string target = readString (source);
+          sink.createSymlink (path, target);
         }
 
-        else
-            throw badArchive("unknown field " + s);
+      else
+        throw badArchive ("unknown field " + s);
     }
 }
 
-
-void parseDump(ParseSink & sink, Source & source)
+void
+parseDump (ParseSink &sink, Source &source)
 {
-    string version;
-    try {
-        version = readString(source);
-    } catch (SerialisationError & e) {
-        /* This generally means the integer at the start couldn't be
-           decoded.  Ignore and throw the exception below. */
-    }
-    if (version != archiveVersion1)
-        throw badArchive("input doesn't look like a normalized archive");
-    parse(sink, source, "");
-}
-
-
-struct RestoreSink : ParseSink
-{
-    Path dstPath;
-    AutoCloseFD fd;
-
-    void createDirectory(const Path & path)
-    {
-        Path p = dstPath + path;
-        if (mkdir(p.c_str(), 0777) == -1)
-            throw SysError(format("creating directory `%1%'") % p);
-    };
-
-    void createRegularFile(const Path & path)
+  string version;
+  try
     {
-        Path p = dstPath + path;
-        fd.close();
-        fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666);
-        if (fd == -1) throw SysError(format("creating file `%1%'") % p);
+      version = readString (source);
     }
-
-    void isExecutable()
+  catch (SerialisationError &e)
     {
-        struct stat st;
-        if (fstat(fd, &st) == -1)
-            throw SysError("fstat");
-        if (fchmod(fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1)
-            throw SysError("fchmod");
+      /* This generally means the integer at the start couldn't be
+         decoded.  Ignore and throw the exception below. */
     }
+  if (version != archiveVersion1)
+    throw badArchive ("input doesn't look like a normalized archive");
+  parse (sink, source, "");
+}
 
-    void preallocateContents(unsigned long long len)
-    {
+struct RestoreSink : ParseSink
+{
+  Path dstPath;
+  AutoCloseFD fd;
+
+  void
+  createDirectory (const Path &path)
+  {
+    Path p = dstPath + path;
+    if (mkdir (p.c_str (), 0777) == -1)
+      throw SysError (format ("creating directory `%1%'") % p);
+  };
+
+  void
+  createRegularFile (const Path &path)
+  {
+    Path p = dstPath + path;
+    fd.close ();
+    fd = open (p.c_str (), O_CREAT | O_EXCL | O_WRONLY, 0666);
+    if (fd == -1)
+      throw SysError (format ("creating file `%1%'") % p);
+  }
+
+  void
+  isExecutable ()
+  {
+    struct stat st;
+    if (fstat (fd, &st) == -1)
+      throw SysError ("fstat");
+    if (fchmod (fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1)
+      throw SysError ("fchmod");
+  }
+
+  void
+  preallocateContents (unsigned long long len)
+  {
 #if HAVE_POSIX_FALLOCATE
-        if (len) {
-            errno = posix_fallocate(fd, 0, len);
-            /* Note that EINVAL may indicate that the underlying
-               filesystem doesn't support preallocation (e.g. on
-               OpenSolaris).  Since preallocation is just an
-               optimisation, ignore it. */
-            if (errno && errno != EINVAL)
-                throw SysError(format("preallocating file of %1% bytes") % len);
-        }
+    if (len)
+      {
+        errno = posix_fallocate (fd, 0, len);
+        /* Note that EINVAL may indicate that the underlying
+           filesystem doesn't support preallocation (e.g. on
+           OpenSolaris).  Since preallocation is just an
+           optimisation, ignore it. */
+        if (errno && errno != EINVAL)
+          throw SysError (format ("preallocating file of %1% bytes") % len);
+      }
 #endif
-    }
-
-    void receiveContents(unsigned char * data, unsigned int len)
-    {
-        writeFull(fd, data, len);
-    }
-
-    void createSymlink(const Path & path, const string & target)
-    {
-        Path p = dstPath + path;
-        nix::createSymlink(target, p);
-    }
+  }
+
+  void
+  receiveContents (unsigned char *data, unsigned int len)
+  {
+    writeFull (fd, data, len);
+  }
+
+  void
+  createSymlink (const Path &path, const string &target)
+  {
+    Path p = dstPath + path;
+    nix::createSymlink (target, p);
+  }
 };
 
-
-void restorePath(const Path & path, Source & source)
+void
+restorePath (const Path &path, Source &source)
 {
-    RestoreSink sink;
-    sink.dstPath = path;
-    parseDump(sink, source);
+  RestoreSink sink;
+  sink.dstPath = path;
+  parseDump (sink, source);
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libutil/archive.hh b/nix/libutil/archive.hh
index 9b83a5f288..39fec972a9 100644
--- a/nix/libutil/archive.hh
+++ b/nix/libutil/archive.hh
@@ -1,11 +1,10 @@
 #pragma once
 
-#include "types.hh"
 #include "serialise.hh"
+#include "types.hh"
 
-
-namespace nix {
-
+namespace nix
+{
 
 /* dumpPath creates a Nix archive of the specified path.  The format
    is as follows:
@@ -46,29 +45,33 @@ namespace nix {
 
 struct PathFilter
 {
-    virtual ~PathFilter() { }
-    virtual bool operator () (const Path & path) { return true; }
+  virtual ~PathFilter () {}
+  virtual bool
+  operator() (const Path &path)
+  {
+    return true;
+  }
 };
 
 extern PathFilter defaultPathFilter;
 
-void dumpPath(const Path & path, Sink & sink,
-    PathFilter & filter = defaultPathFilter);
+void dumpPath (const Path &path, Sink &sink,
+               PathFilter &filter = defaultPathFilter);
 
 struct ParseSink
 {
-    virtual void createDirectory(const Path & path) { };
+  virtual void createDirectory (const Path &path) {};
 
-    virtual void createRegularFile(const Path & path) { };
-    virtual void isExecutable() { };
-    virtual void preallocateContents(unsigned long long size) { };
-    virtual void receiveContents(unsigned char * data, unsigned int len) { };
+  virtual void createRegularFile (const Path &path) {};
+  virtual void isExecutable () {};
+  virtual void preallocateContents (unsigned long long size) {};
+  virtual void receiveContents (unsigned char *data, unsigned int len) {};
 
-    virtual void createSymlink(const Path & path, const string & target) { };
+  virtual void createSymlink (const Path &path, const string &target) {};
 };
 
-void parseDump(ParseSink & sink, Source & source);
+void parseDump (ParseSink &sink, Source &source);
 
-void restorePath(const Path & path, Source & source);
+void restorePath (const Path &path, Source &source);
 
 }
diff --git a/nix/libutil/hash.cc b/nix/libutil/hash.cc
index 9b83ffcdd9..cdc5fec227 100644
--- a/nix/libutil/hash.cc
+++ b/nix/libutil/hash.cc
@@ -1,187 +1,197 @@
 #include "config.h"
 
-#include <iostream>
 #include <cstring>
+#include <iostream>
 
-#include "hash.hh"
 #include "archive.hh"
+#include "hash.hh"
 #include "util.hh"
 
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
+namespace nix
+{
 
-namespace nix {
-
-
-Hash::Hash()
+Hash::Hash ()
 {
-    type = htUnknown;
-    hashSize = 0;
-    memset(hash, 0, maxHashSize);
+  type = htUnknown;
+  hashSize = 0;
+  memset (hash, 0, maxHashSize);
 }
 
-
-Hash::Hash(HashType type)
+Hash::Hash (HashType type)
 {
-    this->type = type;
-    hashSize = gcry_md_get_algo_dlen(type);
+  this->type = type;
+  hashSize = gcry_md_get_algo_dlen (type);
 
-    if (hashSize == 0) throw Error("unknown hash type");
-    assert(hashSize <= maxHashSize);
-    memset(hash, 0, maxHashSize);
+  if (hashSize == 0)
+    throw Error ("unknown hash type");
+  assert (hashSize <= maxHashSize);
+  memset (hash, 0, maxHashSize);
 }
 
-
-bool Hash::operator == (const Hash & h2) const
+bool
+Hash::operator== (const Hash &h2) const
 {
-    if (hashSize != h2.hashSize) return false;
-    for (unsigned int i = 0; i < hashSize; i++)
-        if (hash[i] != h2.hash[i]) return false;
-    return true;
+  if (hashSize != h2.hashSize)
+    return false;
+  for (unsigned int i = 0; i < hashSize; i++)
+    if (hash[i] != h2.hash[i])
+      return false;
+  return true;
 }
 
-
-bool Hash::operator != (const Hash & h2) const
+bool
+Hash::operator!= (const Hash &h2) const
 {
-    return !(*this == h2);
+  return !(*this == h2);
 }
 
-
-bool Hash::operator < (const Hash & h) const
+bool
+Hash::operator< (const Hash &h) const
 {
-    for (unsigned int i = 0; i < hashSize; i++) {
-        if (hash[i] < h.hash[i]) return true;
-        if (hash[i] > h.hash[i]) return false;
+  for (unsigned int i = 0; i < hashSize; i++)
+    {
+      if (hash[i] < h.hash[i])
+        return true;
+      if (hash[i] > h.hash[i])
+        return false;
     }
-    return false;
+  return false;
 }
 
-
 const string base16Chars = "0123456789abcdef";
 
-
-string printHash(const Hash & hash)
+string
+printHash (const Hash &hash)
 {
-    char buf[hash.hashSize * 2];
-    for (unsigned int i = 0; i < hash.hashSize; i++) {
-        buf[i * 2] = base16Chars[hash.hash[i] >> 4];
-        buf[i * 2 + 1] = base16Chars[hash.hash[i] & 0x0f];
+  char buf[hash.hashSize * 2];
+  for (unsigned int i = 0; i < hash.hashSize; i++)
+    {
+      buf[i * 2] = base16Chars[hash.hash[i] >> 4];
+      buf[i * 2 + 1] = base16Chars[hash.hash[i] & 0x0f];
     }
-    return string(buf, hash.hashSize * 2);
+  return string (buf, hash.hashSize * 2);
 }
 
-
-Hash parseHash(HashType ht, const string & s)
+Hash
+parseHash (HashType ht, const string &s)
 {
-    Hash hash(ht);
-    if (s.length() != hash.hashSize * 2) {
-	string algo = gcry_md_algo_name(ht);
-        throw Error(format("invalid %1% hash '%2%' (%3% bytes but expected %4%)")
-		    % algo % s % (s.length() / 2) % hash.hashSize);
+  Hash hash (ht);
+  if (s.length () != hash.hashSize * 2)
+    {
+      string algo = gcry_md_algo_name (ht);
+      throw Error (
+          format ("invalid %1% hash '%2%' (%3% bytes but expected %4%)") % algo
+          % s % (s.length () / 2) % hash.hashSize);
     }
-    for (unsigned int i = 0; i < hash.hashSize; i++) {
-        string s2(s, i * 2, 2);
-        if (!isxdigit(s2[0]) || !isxdigit(s2[1]))
-            throw Error(format("invalid hash `%1%'") % s);
-        std::istringstream str(s2);
-        int n;
-        str >> std::hex >> n;
-        hash.hash[i] = n;
+  for (unsigned int i = 0; i < hash.hashSize; i++)
+    {
+      string s2 (s, i * 2, 2);
+      if (!isxdigit (s2[0]) || !isxdigit (s2[1]))
+        throw Error (format ("invalid hash `%1%'") % s);
+      std::istringstream str (s2);
+      int n;
+      str >> std::hex >> n;
+      hash.hash[i] = n;
     }
-    return hash;
+  return hash;
 }
 
-
-unsigned int hashLength32(const Hash & hash)
+unsigned int
+hashLength32 (const Hash &hash)
 {
-    return (hash.hashSize * 8 - 1) / 5 + 1;
+  return (hash.hashSize * 8 - 1) / 5 + 1;
 }
 
-
 // omitted: E O U T
 const string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz";
 
-
-string printHash32(const Hash & hash)
+string
+printHash32 (const Hash &hash)
 {
-    Hash hash2(hash);
-    unsigned int len = hashLength32(hash);
-
-    string s;
-    s.reserve(len);
-
-    for (int n = len - 1; n >= 0; n--) {
-        unsigned int b = n * 5;
-        unsigned int i = b / 8;
-        unsigned int j = b % 8;
-        unsigned char c =
-            (hash.hash[i] >> j)
+  unsigned int len = hashLength32 (hash);
+
+  string s;
+  s.reserve (len);
+
+  for (int n = len - 1; n >= 0; n--)
+    {
+      unsigned int b = n * 5;
+      unsigned int i = b / 8;
+      unsigned int j = b % 8;
+      unsigned char c
+          = (hash.hash[i] >> j)
             | (i >= hash.hashSize - 1 ? 0 : hash.hash[i + 1] << (8 - j));
-        s.push_back(base32Chars[c & 0x1f]);
+      s.push_back (base32Chars[c & 0x1f]);
     }
 
-    return s;
+  return s;
 }
 
-
-string printHash16or32(const Hash & hash)
+string
+printHash16or32 (const Hash &hash)
 {
-    return hash.type == htMD5 ? printHash(hash) : printHash32(hash);
+  return hash.type == htMD5 ? printHash (hash) : printHash32 (hash);
 }
 
-
-Hash parseHash32(HashType ht, const string & s)
+Hash
+parseHash32 (HashType ht, const string &s)
 {
-    Hash hash(ht);
-    unsigned int len = hashLength32(ht);
-    assert(s.size() == len);
-
-    for (unsigned int n = 0; n < len; ++n) {
-        char c = s[len - n - 1];
-        unsigned char digit;
-        for (digit = 0; digit < base32Chars.size(); ++digit) /* !!! slow */
-            if (base32Chars[digit] == c) break;
-        if (digit >= 32)
-            throw Error(format("invalid base-32 hash '%1%'") % s);
-        unsigned int b = n * 5;
-        unsigned int i = b / 8;
-        unsigned int j = b % 8;
-        hash.hash[i] |= digit << j;
-        if (i < hash.hashSize - 1) hash.hash[i + 1] |= digit >> (8 - j);
+  Hash hash (ht);
+  unsigned int len = hashLength32 (ht);
+  assert (s.size () == len);
+
+  for (unsigned int n = 0; n < len; ++n)
+    {
+      char c = s[len - n - 1];
+      unsigned char digit;
+      for (digit = 0; digit < base32Chars.size (); ++digit) /* !!! slow */
+        if (base32Chars[digit] == c)
+          break;
+      if (digit >= 32)
+        throw Error (format ("invalid base-32 hash '%1%'") % s);
+      unsigned int b = n * 5;
+      unsigned int i = b / 8;
+      unsigned int j = b % 8;
+      hash.hash[i] |= digit << j;
+      if (i < hash.hashSize - 1)
+        hash.hash[i + 1] |= digit >> (8 - j);
     }
 
-    return hash;
+  return hash;
 }
 
-
-Hash parseHash16or32(HashType ht, const string & s)
+Hash
+parseHash16or32 (HashType ht, const string &s)
 {
-    Hash hash(ht);
-    if (s.size() == hash.hashSize * 2)
-        /* hexadecimal representation */
-        hash = parseHash(ht, s);
-    else if (s.size() == hashLength32(hash))
-        /* base-32 representation */
-        hash = parseHash32(ht, s);
-    else
-        throw Error(format("hash `%1%' has wrong length for hash type `%2%'")
-            % s % printHashType(ht));
-    return hash;
+  Hash hash (ht);
+  if (s.size () == hash.hashSize * 2)
+    /* hexadecimal representation */
+    hash = parseHash (ht, s);
+  else if (s.size () == hashLength32 (hash))
+    /* base-32 representation */
+    hash = parseHash32 (ht, s);
+  else
+    throw Error (format ("hash `%1%' has wrong length for hash type `%2%'") % s
+                 % printHashType (ht));
+  return hash;
 }
 
-
-bool isHash(const string & s)
+bool
+isHash (const string &s)
 {
-    if (s.length() != 32) return false;
-    for (int i = 0; i < 32; i++) {
-        char c = s[i];
-        if (!((c >= '0' && c <= '9') ||
-              (c >= 'a' && c <= 'f')))
-            return false;
+  if (s.length () != 32)
+    return false;
+  for (int i = 0; i < 32; i++)
+    {
+      char c = s[i];
+      if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')))
+        return false;
     }
-    return true;
+  return true;
 }
 
 /* The "hash context".  */
@@ -189,7 +199,7 @@ struct Ctx
 {
   /* This copy constructor is needed in 'HashSink::currentHash()' where we
      expect the copy of a 'Ctx' object to yield a truly different context.  */
-  Ctx(Ctx &ref)
+  Ctx (Ctx &ref)
   {
     if (ref.md_handle == NULL)
       md_handle = NULL;
@@ -198,150 +208,167 @@ struct Ctx
   }
 
   /* Make sure 'md_handle' is always initialized.  */
-  Ctx(): md_handle (NULL) { };
+  Ctx () : md_handle (NULL){};
 
   gcry_md_hd_t md_handle;
 };
 
-
-static void start(HashType ht, Ctx & ctx)
+static void
+start (HashType ht, Ctx &ctx)
 {
-    gcry_error_t err;
+  gcry_error_t err;
 
-    err = gcry_md_open (&ctx.md_handle, ht, 0);
-    assert (err == GPG_ERR_NO_ERROR);
+  err = gcry_md_open (&ctx.md_handle, ht, 0);
+  assert (err == GPG_ERR_NO_ERROR);
 }
 
-
-static void update(HashType ht, Ctx & ctx,
-    const unsigned char * bytes, unsigned int len)
+static void
+update (HashType ht, Ctx &ctx, const unsigned char *bytes, unsigned int len)
 {
-    gcry_md_write (ctx.md_handle, bytes, len);
+  gcry_md_write (ctx.md_handle, bytes, len);
 }
 
-
-static void finish(HashType ht, Ctx & ctx, unsigned char * hash)
+static void
+finish (HashType ht, Ctx &ctx, unsigned char *hash)
 {
-    memcpy (hash, gcry_md_read (ctx.md_handle, ht),
-	    gcry_md_get_algo_dlen (ht));
-    gcry_md_close (ctx.md_handle);
-    ctx.md_handle = NULL;
+  memcpy (hash, gcry_md_read (ctx.md_handle, ht), gcry_md_get_algo_dlen (ht));
+  gcry_md_close (ctx.md_handle);
+  ctx.md_handle = NULL;
 }
 
-
-Hash hashString(HashType ht, const string & s)
+Hash
+hashString (HashType ht, const string &s)
 {
-    Ctx ctx;
-    Hash hash(ht);
-    start(ht, ctx);
-    update(ht, ctx, (const unsigned char *) s.data(), s.length());
-    finish(ht, ctx, hash.hash);
-    return hash;
+  Ctx ctx;
+  Hash hash (ht);
+  start (ht, ctx);
+  update (ht, ctx, (const unsigned char *)s.data (), s.length ());
+  finish (ht, ctx, hash.hash);
+  return hash;
 }
 
-
-Hash hashFile(HashType ht, const Path & path)
+Hash
+hashFile (HashType ht, const Path &path)
 {
-    Ctx ctx;
-    Hash hash(ht);
-    start(ht, ctx);
-
-    AutoCloseFD fd = open(path.c_str(), O_RDONLY);
-    if (fd == -1) throw SysError(format("computing hash of file `%1%'") % path);
-
-    unsigned char buf[8192];
-    ssize_t n;
-    while ((n = read(fd, buf, sizeof(buf)))) {
-        checkInterrupt();
-        if (n == -1) throw SysError(format("reading file `%1%'") % path);
-        update(ht, ctx, buf, n);
+  Ctx ctx;
+  Hash hash (ht);
+  start (ht, ctx);
+
+  AutoCloseFD fd = open (path.c_str (), O_RDONLY);
+  if (fd == -1)
+    throw SysError (format ("computing hash of file `%1%'") % path);
+
+  unsigned char buf[8192];
+  ssize_t n;
+  while ((n = read (fd, buf, sizeof (buf))))
+    {
+      checkInterrupt ();
+      if (n == -1)
+        throw SysError (format ("reading file `%1%'") % path);
+      update (ht, ctx, buf, n);
     }
 
-    finish(ht, ctx, hash.hash);
-    return hash;
+  finish (ht, ctx, hash.hash);
+  return hash;
 }
 
-
-HashSink::HashSink(HashType ht) : ht(ht)
+HashSink::HashSink (HashType ht) : ht (ht)
 {
-    ctx = new Ctx;
-    bytes = 0;
-    start(ht, *ctx);
+  ctx = new Ctx;
+  bytes = 0;
+  start (ht, *ctx);
 }
 
-HashSink::~HashSink()
+HashSink::~HashSink ()
 {
-    bufPos = 0;
-    delete ctx;
+  bufPos = 0;
+  delete ctx;
 }
 
-void HashSink::write(const unsigned char * data, size_t len)
+void
+HashSink::write (const unsigned char *data, size_t len)
 {
-    bytes += len;
-    update(ht, *ctx, data, len);
+  bytes += len;
+  update (ht, *ctx, data, len);
 }
 
-HashResult HashSink::finish()
+HashResult
+HashSink::finish ()
 {
-    flush();
-    Hash hash(ht);
-    nix::finish(ht, *ctx, hash.hash);
-    return HashResult(hash, bytes);
+  flush ();
+  Hash hash (ht);
+  nix::finish (ht, *ctx, hash.hash);
+  return HashResult (hash, bytes);
 }
 
-HashResult HashSink::currentHash()
+HashResult
+HashSink::currentHash ()
 {
-    flush();
-    Ctx ctx2 = *ctx;
-    Hash hash(ht);
-    nix::finish(ht, ctx2, hash.hash);
-    return HashResult(hash, bytes);
+  flush ();
+  Ctx ctx2 = *ctx;
+  Hash hash (ht);
+  nix::finish (ht, ctx2, hash.hash);
+  return HashResult (hash, bytes);
 }
 
-
-HashResult hashPath(
-    HashType ht, const Path & path, PathFilter & filter)
+HashResult
+hashPath (HashType ht, const Path &path, PathFilter &filter)
 {
-    HashSink sink(ht);
-    dumpPath(path, sink, filter);
-    return sink.finish();
+  HashSink sink (ht);
+  dumpPath (path, sink, filter);
+  return sink.finish ();
 }
 
-
-Hash compressHash(const Hash & hash, unsigned int newSize)
+Hash
+compressHash (const Hash &hash, unsigned int newSize)
 {
-    Hash h;
-    h.hashSize = newSize;
-    for (unsigned int i = 0; i < hash.hashSize; ++i)
-        h.hash[i % newSize] ^= hash.hash[i];
-    return h;
+  Hash h;
+  h.hashSize = newSize;
+  for (unsigned int i = 0; i < hash.hashSize; ++i)
+    h.hash[i % newSize] ^= hash.hash[i];
+  return h;
 }
 
-
-HashType parseHashType(const string & s)
+HashType
+parseHashType (const string &s)
 {
-    if (s == "md5") return htMD5;
-    else if (s == "sha1") return htSHA1;
-    else if (s == "sha256") return htSHA256;
-    else if (s == "sha512") return htSHA512;
-    else if (s == "sha3-256") return htSHA3_256;
-    else if (s == "sha3-512") return htSHA3_512;
-    else if (s == "blake2s-256") return htBLAKE2s_256;
-    else return htUnknown;
+  if (s == "md5")
+    return htMD5;
+  else if (s == "sha1")
+    return htSHA1;
+  else if (s == "sha256")
+    return htSHA256;
+  else if (s == "sha512")
+    return htSHA512;
+  else if (s == "sha3-256")
+    return htSHA3_256;
+  else if (s == "sha3-512")
+    return htSHA3_512;
+  else if (s == "blake2s-256")
+    return htBLAKE2s_256;
+  else
+    return htUnknown;
 }
 
-
-string printHashType(HashType ht)
+string
+printHashType (HashType ht)
 {
-    if (ht == htMD5) return "md5";
-    else if (ht == htSHA1) return "sha1";
-    else if (ht == htSHA256) return "sha256";
-    else if (ht == htSHA512) return "sha512";
-    else if (ht == htSHA3_256) return "sha3-256";
-    else if (ht == htSHA3_512) return "sha3-512";
-    else if (ht == htBLAKE2s_256) return "blake2s-256";
-    else throw Error("cannot print unknown hash type");
+  if (ht == htMD5)
+    return "md5";
+  else if (ht == htSHA1)
+    return "sha1";
+  else if (ht == htSHA256)
+    return "sha256";
+  else if (ht == htSHA512)
+    return "sha512";
+  else if (ht == htSHA3_256)
+    return "sha3-256";
+  else if (ht == htSHA3_512)
+    return "sha3-512";
+  else if (ht == htBLAKE2s_256)
+    return "blake2s-256";
+  else
+    throw Error ("cannot print unknown hash type");
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libutil/hash.hh b/nix/libutil/hash.hh
index ac58651a02..2110e6a9b6 100644
--- a/nix/libutil/hash.hh
+++ b/nix/libutil/hash.hh
@@ -2,117 +2,114 @@
 
 #include <gcrypt.h>
 
-#include "types.hh"
 #include "serialise.hh"
+#include "types.hh"
 
-
-namespace nix {
-
+namespace nix
+{
 
 extern const string base32Chars;
 
-typedef enum {
-    htUnknown = 0,
-    htMD5 = GCRY_MD_MD5,
-    htSHA1 = GCRY_MD_SHA1,
-    htSHA256 = GCRY_MD_SHA256,
-    htSHA512 = GCRY_MD_SHA512,
-    htSHA3_256 = GCRY_MD_SHA3_256,
-    htSHA3_512 = GCRY_MD_SHA3_512,
-    htBLAKE2s_256 = GCRY_MD_BLAKE2S_256
+typedef enum
+{
+  htUnknown = 0,
+  htMD5 = GCRY_MD_MD5,
+  htSHA1 = GCRY_MD_SHA1,
+  htSHA256 = GCRY_MD_SHA256,
+  htSHA512 = GCRY_MD_SHA512,
+  htSHA3_256 = GCRY_MD_SHA3_256,
+  htSHA3_512 = GCRY_MD_SHA3_512,
+  htBLAKE2s_256 = GCRY_MD_BLAKE2S_256
 } HashType;
 
 struct Hash
 {
-    static const unsigned int maxHashSize = 64;
-    unsigned int hashSize;
-    unsigned char hash[maxHashSize];
+  static const unsigned int maxHashSize = 64;
+  unsigned int hashSize;
+  unsigned char hash[maxHashSize];
 
-    HashType type;
+  HashType type;
 
-    /* Create an unusable hash object. */
-    Hash();
+  /* Create an unusable hash object. */
+  Hash ();
 
-    /* Create a zero-filled hash object. */
-    Hash(HashType type);
+  /* Create a zero-filled hash object. */
+  Hash (HashType type);
 
-    /* Check whether two hash are equal. */
-    bool operator == (const Hash & h2) const;
+  /* Check whether two hash are equal. */
+  bool operator== (const Hash &h2) const;
 
-    /* Check whether two hash are not equal. */
-    bool operator != (const Hash & h2) const;
+  /* Check whether two hash are not equal. */
+  bool operator!= (const Hash &h2) const;
 
-    /* For sorting. */
-    bool operator < (const Hash & h) const;
+  /* For sorting. */
+  bool operator< (const Hash &h) const;
 };
 
-
 /* Convert a hash to a hexadecimal representation. */
-string printHash(const Hash & hash);
+string printHash (const Hash &hash);
 
 /* Parse a hexadecimal representation of a hash code. */
-Hash parseHash(HashType ht, const string & s);
+Hash parseHash (HashType ht, const string &s);
 
 /* Returns the length of a base-32 hash representation. */
-unsigned int hashLength32(const Hash & hash);
+unsigned int hashLength32 (const Hash &hash);
 
 /* Convert a hash to a base-32 representation. */
-string printHash32(const Hash & hash);
+string printHash32 (const Hash &hash);
 
 /* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
-string printHash16or32(const Hash & hash);
+string printHash16or32 (const Hash &hash);
 
 /* Parse a base-32 representation of a hash code. */
-Hash parseHash32(HashType ht, const string & s);
+Hash parseHash32 (HashType ht, const string &s);
 
 /* Parse a base-16 or base-32 representation of a hash code. */
-Hash parseHash16or32(HashType ht, const string & s);
+Hash parseHash16or32 (HashType ht, const string &s);
 
 /* Verify that the given string is a valid hash code. */
-bool isHash(const string & s);
+bool isHash (const string &s);
 
 /* Compute the hash of the given string. */
-Hash hashString(HashType ht, const string & s);
+Hash hashString (HashType ht, const string &s);
 
 /* Compute the hash of the given file. */
-Hash hashFile(HashType ht, const Path & path);
+Hash hashFile (HashType ht, const Path &path);
 
 /* Compute the hash of the given path.  The hash is defined as
    (essentially) hashString(ht, dumpPath(path)). */
 struct PathFilter;
 extern PathFilter defaultPathFilter;
 typedef std::pair<Hash, unsigned long long> HashResult;
-HashResult hashPath(HashType ht, const Path & path,
-    PathFilter & filter = defaultPathFilter);
+HashResult hashPath (HashType ht, const Path &path,
+                     PathFilter &filter = defaultPathFilter);
 
 /* Compress a hash to the specified number of bytes by cyclically
    XORing bytes together. */
-Hash compressHash(const Hash & hash, unsigned int newSize);
+Hash compressHash (const Hash &hash, unsigned int newSize);
 
 /* Parse a string representing a hash type. */
-HashType parseHashType(const string & s);
+HashType parseHashType (const string &s);
 
 /* And the reverse. */
-string printHashType(HashType ht);
-
+string printHashType (HashType ht);
 
 struct Ctx;
 
 class HashSink : public BufferedSink
 {
 private:
-    HashType ht;
-    Ctx * ctx;
-    unsigned long long bytes;
+  HashType ht;
+  Ctx *ctx;
+  unsigned long long bytes;
 
 public:
-    HashSink(HashType ht);
-    HashSink(const HashSink & h);
-    ~HashSink();
-    void write(const unsigned char * data, size_t len);
-    HashResult finish();
-    HashResult currentHash();
+  HashSink (HashType ht);
+  HashSink (const HashSink &h);
+  ~HashSink ();
+  void write (const unsigned char *data, size_t len);
+  HashResult finish ();
+  HashResult currentHash ();
 };
 
-
 }
diff --git a/nix/libutil/serialise.cc b/nix/libutil/serialise.cc
index 9241750750..d6d3a1d190 100644
--- a/nix/libutil/serialise.cc
+++ b/nix/libutil/serialise.cc
@@ -1,286 +1,308 @@
 #include "serialise.hh"
 #include "util.hh"
 
-#include <cstring>
 #include <cerrno>
+#include <cstring>
 
+namespace nix
+{
 
-namespace nix {
-
-
-BufferedSink::~BufferedSink()
+BufferedSink::~BufferedSink ()
 {
-    /* We can't call flush() here, because C++ for some insane reason
-       doesn't allow you to call virtual methods from a destructor. */
-    assert(!bufPos);
-    delete[] buffer;
+  /* We can't call flush() here, because C++ for some insane reason
+     doesn't allow you to call virtual methods from a destructor. */
+  assert (!bufPos);
+  delete[] buffer;
 }
 
-    
-void BufferedSink::operator () (const unsigned char * data, size_t len)
+void
+BufferedSink::operator() (const unsigned char *data, size_t len)
 {
-    if (!buffer) buffer = new unsigned char[bufSize];
-    
-    while (len) {
-        /* Optimisation: bypass the buffer if the data exceeds the
-           buffer size. */
-        if (bufPos + len >= bufSize) {
-            flush();
-            write(data, len);
-            break;
+  if (!buffer)
+    buffer = new unsigned char[bufSize];
+
+  while (len)
+    {
+      /* Optimisation: bypass the buffer if the data exceeds the
+         buffer size. */
+      if (bufPos + len >= bufSize)
+        {
+          flush ();
+          write (data, len);
+          break;
         }
-        /* Otherwise, copy the bytes to the buffer.  Flush the buffer
-           when it's full. */
-        size_t n = bufPos + len > bufSize ? bufSize - bufPos : len;
-        memcpy(buffer + bufPos, data, n);
-        data += n; bufPos += n; len -= n;
-        if (bufPos == bufSize) flush();
+      /* Otherwise, copy the bytes to the buffer.  Flush the buffer
+         when it's full. */
+      size_t n = bufPos + len > bufSize ? bufSize - bufPos : len;
+      memcpy (buffer + bufPos, data, n);
+      data += n;
+      bufPos += n;
+      len -= n;
+      if (bufPos == bufSize)
+        flush ();
     }
 }
 
-
-void BufferedSink::flush()
+void
+BufferedSink::flush ()
 {
-    if (bufPos == 0) return;
-    size_t n = bufPos;
-    bufPos = 0; // don't trigger the assert() in ~BufferedSink()
-    write(buffer, n);
+  if (bufPos == 0)
+    return;
+  size_t n = bufPos;
+  bufPos = 0; // don't trigger the assert() in ~BufferedSink()
+  write (buffer, n);
 }
 
-
-FdSink::~FdSink()
+FdSink::~FdSink ()
 {
-    try { flush(); } catch (...) { ignoreException(); }
+  try
+    {
+      flush ();
+    }
+  catch (...)
+    {
+      ignoreException ();
+    }
 }
 
-
 size_t threshold = 256 * 1024 * 1024;
 
-static void warnLargeDump()
+static void
+warnLargeDump ()
 {
-    printMsg(lvlError, "warning: dumping very large path (> 256 MiB); this may run out of memory");
+  printMsg (lvlError, "warning: dumping very large path (> 256 MiB); this may "
+                      "run out of memory");
 }
 
-
-void FdSink::write(const unsigned char * data, size_t len)
+void
+FdSink::write (const unsigned char *data, size_t len)
 {
-    static bool warned = false;
-    if (warn && !warned) {
-        written += len;
-        if (written > threshold) {
-            warnLargeDump();
-            warned = true;
+  static bool warned = false;
+  if (warn && !warned)
+    {
+      written += len;
+      if (written > threshold)
+        {
+          warnLargeDump ();
+          warned = true;
         }
     }
-    writeFull(fd, data, len);
+  writeFull (fd, data, len);
 }
 
-
-void Source::operator () (unsigned char * data, size_t len)
+void
+Source::operator() (unsigned char *data, size_t len)
 {
-    while (len) {
-        size_t n = read(data, len);
-        data += n; len -= n;
+  while (len)
+    {
+      size_t n = read (data, len);
+      data += n;
+      len -= n;
     }
 }
 
+BufferedSource::~BufferedSource () { delete[] buffer; }
 
-BufferedSource::~BufferedSource()
+size_t
+BufferedSource::read (unsigned char *data, size_t len)
 {
-    delete[] buffer;
+  if (!buffer)
+    buffer = new unsigned char[bufSize];
+
+  if (!bufPosIn)
+    bufPosIn = readUnbuffered (buffer, bufSize);
+
+  /* Copy out the data in the buffer. */
+  size_t n = len > bufPosIn - bufPosOut ? bufPosIn - bufPosOut : len;
+  memcpy (data, buffer + bufPosOut, n);
+  bufPosOut += n;
+  if (bufPosIn == bufPosOut)
+    bufPosIn = bufPosOut = 0;
+  return n;
 }
 
-
-size_t BufferedSource::read(unsigned char * data, size_t len)
+bool
+BufferedSource::hasData ()
 {
-    if (!buffer) buffer = new unsigned char[bufSize];
-
-    if (!bufPosIn) bufPosIn = readUnbuffered(buffer, bufSize);
-            
-    /* Copy out the data in the buffer. */
-    size_t n = len > bufPosIn - bufPosOut ? bufPosIn - bufPosOut : len;
-    memcpy(data, buffer + bufPosOut, n);
-    bufPosOut += n;
-    if (bufPosIn == bufPosOut) bufPosIn = bufPosOut = 0;
-    return n;
+  return bufPosOut < bufPosIn;
 }
 
-
-bool BufferedSource::hasData()
+size_t
+FdSource::readUnbuffered (unsigned char *data, size_t len)
 {
-    return bufPosOut < bufPosIn;
-}
-
-
-size_t FdSource::readUnbuffered(unsigned char * data, size_t len)
-{
-    ssize_t n;
-    do {
-        checkInterrupt();
-        n = ::read(fd, (char *) data, bufSize);
-    } while (n == -1 && errno == EINTR);
-    if (n == -1) throw SysError("reading from file");
-    if (n == 0) throw EndOfFile("unexpected end-of-file");
-    return n;
+  ssize_t n;
+  do
+    {
+      checkInterrupt ();
+      n = ::read (fd, (char *)data, bufSize);
+    }
+  while (n == -1 && errno == EINTR);
+  if (n == -1)
+    throw SysError ("reading from file");
+  if (n == 0)
+    throw EndOfFile ("unexpected end-of-file");
+  return n;
 }
 
-
-size_t StringSource::read(unsigned char * data, size_t len)
+size_t
+StringSource::read (unsigned char *data, size_t len)
 {
-    if (pos == s.size()) throw EndOfFile("end of string reached");
-    size_t n = s.copy((char *) data, len, pos);
-    pos += n;
-    return n;
+  if (pos == s.size ())
+    throw EndOfFile ("end of string reached");
+  size_t n = s.copy ((char *)data, len, pos);
+  pos += n;
+  return n;
 }
 
-
-void writePadding(size_t len, Sink & sink)
+void
+writePadding (size_t len, Sink &sink)
 {
-    if (len % 8) {
-        unsigned char zero[8];
-        memset(zero, 0, sizeof(zero));
-        sink(zero, 8 - (len % 8));
+  if (len % 8)
+    {
+      unsigned char zero[8];
+      memset (zero, 0, sizeof (zero));
+      sink (zero, 8 - (len % 8));
     }
 }
 
-
-void writeInt(unsigned int n, Sink & sink)
+void
+writeInt (unsigned int n, Sink &sink)
 {
-    unsigned char buf[8];
-    memset(buf, 0, sizeof(buf));
-    buf[0] = n & 0xff;
-    buf[1] = (n >> 8) & 0xff;
-    buf[2] = (n >> 16) & 0xff;
-    buf[3] = (n >> 24) & 0xff;
-    sink(buf, sizeof(buf));
+  unsigned char buf[8];
+  memset (buf, 0, sizeof (buf));
+  buf[0] = n & 0xff;
+  buf[1] = (n >> 8) & 0xff;
+  buf[2] = (n >> 16) & 0xff;
+  buf[3] = (n >> 24) & 0xff;
+  sink (buf, sizeof (buf));
 }
 
-
-void writeLongLong(unsigned long long n, Sink & sink)
+void
+writeLongLong (unsigned long long n, Sink &sink)
 {
-    unsigned char buf[8];
-    buf[0] = n & 0xff;
-    buf[1] = (n >> 8) & 0xff;
-    buf[2] = (n >> 16) & 0xff;
-    buf[3] = (n >> 24) & 0xff;
-    buf[4] = (n >> 32) & 0xff;
-    buf[5] = (n >> 40) & 0xff;
-    buf[6] = (n >> 48) & 0xff;
-    buf[7] = (n >> 56) & 0xff;
-    sink(buf, sizeof(buf));
+  unsigned char buf[8];
+  buf[0] = n & 0xff;
+  buf[1] = (n >> 8) & 0xff;
+  buf[2] = (n >> 16) & 0xff;
+  buf[3] = (n >> 24) & 0xff;
+  buf[4] = (n >> 32) & 0xff;
+  buf[5] = (n >> 40) & 0xff;
+  buf[6] = (n >> 48) & 0xff;
+  buf[7] = (n >> 56) & 0xff;
+  sink (buf, sizeof (buf));
 }
 
-
-void writeString(const unsigned char * buf, size_t len, Sink & sink)
+void
+writeString (const unsigned char *buf, size_t len, Sink &sink)
 {
-    writeInt(len, sink);
-    sink(buf, len);
-    writePadding(len, sink);
+  writeInt (len, sink);
+  sink (buf, len);
+  writePadding (len, sink);
 }
 
-
-void writeString(const string & s, Sink & sink)
+void
+writeString (const string &s, Sink &sink)
 {
-    writeString((const unsigned char *) s.data(), s.size(), sink);
+  writeString ((const unsigned char *)s.data (), s.size (), sink);
 }
 
-
-template<class T> void writeStrings(const T & ss, Sink & sink)
+template <class T>
+void
+writeStrings (const T &ss, Sink &sink)
 {
-    writeInt(ss.size(), sink);
-    foreach (typename T::const_iterator, i, ss)
-        writeString(*i, sink);
+  writeInt (ss.size (), sink);
+  foreach (typename T::const_iterator, i, ss)
+    writeString (*i, sink);
 }
 
-template void writeStrings(const Paths & ss, Sink & sink);
-template void writeStrings(const PathSet & ss, Sink & sink);
-
+template void writeStrings (const Paths &ss, Sink &sink);
+template void writeStrings (const PathSet &ss, Sink &sink);
 
-void readPadding(size_t len, Source & source)
+void
+readPadding (size_t len, Source &source)
 {
-    if (len % 8) {
-        unsigned char zero[8];
-        size_t n = 8 - (len % 8);
-        source(zero, n);
-        for (unsigned int i = 0; i < n; i++)
-            if (zero[i]) throw SerialisationError("non-zero padding");
+  if (len % 8)
+    {
+      unsigned char zero[8];
+      size_t n = 8 - (len % 8);
+      source (zero, n);
+      for (unsigned int i = 0; i < n; i++)
+        if (zero[i])
+          throw SerialisationError ("non-zero padding");
     }
 }
 
-
-unsigned int readInt(Source & source)
+unsigned int
+readInt (Source &source)
 {
-    unsigned char buf[8];
-    source(buf, sizeof(buf));
-    if (buf[4] || buf[5] || buf[6] || buf[7])
-        throw SerialisationError("implementation cannot deal with > 32-bit integers");
-    return
-        buf[0] |
-        (buf[1] << 8) |
-        (buf[2] << 16) |
-        (buf[3] << 24);
+  unsigned char buf[8];
+  source (buf, sizeof (buf));
+  if (buf[4] || buf[5] || buf[6] || buf[7])
+    throw SerialisationError (
+        "implementation cannot deal with > 32-bit integers");
+  return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
 }
 
-
-unsigned long long readLongLong(Source & source)
+unsigned long long
+readLongLong (Source &source)
 {
-    unsigned char buf[8];
-    source(buf, sizeof(buf));
-    return
-        ((unsigned long long) buf[0]) |
-        ((unsigned long long) buf[1] << 8) |
-        ((unsigned long long) buf[2] << 16) |
-        ((unsigned long long) buf[3] << 24) |
-        ((unsigned long long) buf[4] << 32) |
-        ((unsigned long long) buf[5] << 40) |
-        ((unsigned long long) buf[6] << 48) |
-        ((unsigned long long) buf[7] << 56);
+  unsigned char buf[8];
+  source (buf, sizeof (buf));
+  return ((unsigned long long)buf[0]) | ((unsigned long long)buf[1] << 8)
+         | ((unsigned long long)buf[2] << 16)
+         | ((unsigned long long)buf[3] << 24)
+         | ((unsigned long long)buf[4] << 32)
+         | ((unsigned long long)buf[5] << 40)
+         | ((unsigned long long)buf[6] << 48)
+         | ((unsigned long long)buf[7] << 56);
 }
 
-
-size_t readString(unsigned char * buf, size_t max, Source & source)
+size_t
+readString (unsigned char *buf, size_t max, Source &source)
 {
-    size_t len = readInt(source);
-    if (len > max) throw Error("string is too long");
-    source(buf, len);
-    readPadding(len, source);
-    return len;
+  size_t len = readInt (source);
+  if (len > max)
+    throw Error ("string is too long");
+  source (buf, len);
+  readPadding (len, source);
+  return len;
 }
 
- 
-string readString(Source & source)
+string
+readString (Source &source)
 {
-    size_t len = readInt(source);
-    unsigned char * buf = new unsigned char[len];
-    AutoDeleteArray<unsigned char> d(buf);
-    source(buf, len);
-    readPadding(len, source);
-    return string((char *) buf, len);
+  size_t len = readInt (source);
+  unsigned char *buf = new unsigned char[len];
+  AutoDeleteArray<unsigned char> d (buf);
+  source (buf, len);
+  readPadding (len, source);
+  return string ((char *)buf, len);
 }
 
- 
-template<class T> T readStrings(Source & source)
+template <class T>
+T
+readStrings (Source &source)
 {
-    unsigned int count = readInt(source);
-    T ss;
-    while (count--)
-        ss.insert(ss.end(), readString(source));
-    return ss;
+  unsigned int count = readInt (source);
+  T ss;
+  while (count--)
+    ss.insert (ss.end (), readString (source));
+  return ss;
 }
 
-template Paths readStrings(Source & source);
-template PathSet readStrings(Source & source);
-
+template Paths readStrings (Source &source);
+template PathSet readStrings (Source &source);
 
-void StringSink::operator () (const unsigned char * data, size_t len)
+void
+StringSink::operator() (const unsigned char *data, size_t len)
 {
-    static bool warned = false;
-    if (!warned && s.size() > threshold) {
-        warnLargeDump();
-        warned = true;
+  static bool warned = false;
+  if (!warned && s.size () > threshold)
+    {
+      warnLargeDump ();
+      warned = true;
     }
-    s.append((const char *) data, len);
+  s.append ((const char *)data, len);
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libutil/serialise.hh b/nix/libutil/serialise.hh
index 6a6f028aa6..f13ae40086 100644
--- a/nix/libutil/serialise.hh
+++ b/nix/libutil/serialise.hh
@@ -2,131 +2,124 @@
 
 #include "types.hh"
 
-
-namespace nix {
-
+namespace nix
+{
 
 /* Abstract destination of binary data. */
-struct Sink 
+struct Sink
 {
-    virtual ~Sink() { }
-    virtual void operator () (const unsigned char * data, size_t len) = 0;
+  virtual ~Sink () {}
+  virtual void operator() (const unsigned char *data, size_t len) = 0;
 };
 
-
 /* A buffered abstract sink. */
 struct BufferedSink : Sink
 {
-    size_t bufSize, bufPos;
-    unsigned char * buffer;
-
-    BufferedSink(size_t bufSize = 32 * 1024)
-        : bufSize(bufSize), bufPos(0), buffer(0) { }
-    ~BufferedSink();
-
-    void operator () (const unsigned char * data, size_t len);
-    
-    void flush();
-    
-    virtual void write(const unsigned char * data, size_t len) = 0;
-};
+  size_t bufSize, bufPos;
+  unsigned char *buffer;
+
+  BufferedSink (size_t bufSize = 32 * 1024)
+      : bufSize (bufSize), bufPos (0), buffer (0)
+  {
+  }
+  ~BufferedSink ();
+
+  void operator() (const unsigned char *data, size_t len);
 
+  void flush ();
+
+  virtual void write (const unsigned char *data, size_t len) = 0;
+};
 
 /* Abstract source of binary data. */
 struct Source
 {
-    virtual ~Source() { }
-    
-    /* Store exactly ‘len’ bytes in the buffer pointed to by ‘data’.
-       It blocks until all the requested data is available, or throws
-       an error if it is not going to be available.   */
-    void operator () (unsigned char * data, size_t len);
-
-    /* Store up to ‘len’ in the buffer pointed to by ‘data’, and
-       return the number of bytes stored.  If blocks until at least
-       one byte is available. */
-    virtual size_t read(unsigned char * data, size_t len) = 0;
-};
+  virtual ~Source () {}
 
+  /* Store exactly ‘len’ bytes in the buffer pointed to by ‘data’.
+     It blocks until all the requested data is available, or throws
+     an error if it is not going to be available.   */
+  void operator() (unsigned char *data, size_t len);
+
+  /* Store up to ‘len’ in the buffer pointed to by ‘data’, and
+     return the number of bytes stored.  If blocks until at least
+     one byte is available. */
+  virtual size_t read (unsigned char *data, size_t len) = 0;
+};
 
 /* A buffered abstract source. */
 struct BufferedSource : Source
 {
-    size_t bufSize, bufPosIn, bufPosOut;
-    unsigned char * buffer;
-
-    BufferedSource(size_t bufSize = 32 * 1024)
-        : bufSize(bufSize), bufPosIn(0), bufPosOut(0), buffer(0) { }
-    ~BufferedSource();
-    
-    size_t read(unsigned char * data, size_t len);
-    
-    /* Underlying read call, to be overridden. */
-    virtual size_t readUnbuffered(unsigned char * data, size_t len) = 0;
-
-    bool hasData();
-};
+  size_t bufSize, bufPosIn, bufPosOut;
+  unsigned char *buffer;
+
+  BufferedSource (size_t bufSize = 32 * 1024)
+      : bufSize (bufSize), bufPosIn (0), bufPosOut (0), buffer (0)
+  {
+  }
+  ~BufferedSource ();
 
+  size_t read (unsigned char *data, size_t len);
+
+  /* Underlying read call, to be overridden. */
+  virtual size_t readUnbuffered (unsigned char *data, size_t len) = 0;
+
+  bool hasData ();
+};
 
 /* A sink that writes data to a file descriptor. */
 struct FdSink : BufferedSink
 {
-    int fd;
-    bool warn;
-    size_t written;
-
-    FdSink() : fd(-1), warn(false), written(0) { }
-    FdSink(int fd) : fd(fd), warn(false), written(0) { }
-    ~FdSink();
-    
-    void write(const unsigned char * data, size_t len);
-};
+  int fd;
+  bool warn;
+  size_t written;
 
+  FdSink () : fd (-1), warn (false), written (0) {}
+  FdSink (int fd) : fd (fd), warn (false), written (0) {}
+  ~FdSink ();
+
+  void write (const unsigned char *data, size_t len);
+};
 
 /* A source that reads data from a file descriptor. */
 struct FdSource : BufferedSource
 {
-    int fd;
-    FdSource() : fd(-1) { }
-    FdSource(int fd) : fd(fd) { }
-    size_t readUnbuffered(unsigned char * data, size_t len);
+  int fd;
+  FdSource () : fd (-1) {}
+  FdSource (int fd) : fd (fd) {}
+  size_t readUnbuffered (unsigned char *data, size_t len);
 };
 
-
 /* A sink that writes data to a string. */
 struct StringSink : Sink
 {
-    string s;
-    void operator () (const unsigned char * data, size_t len);
+  string s;
+  void operator() (const unsigned char *data, size_t len);
 };
 
-
 /* A source that reads data from a string. */
 struct StringSource : Source
 {
-    const string & s;
-    size_t pos;
-    StringSource(const string & _s) : s(_s), pos(0) { }
-    size_t read(unsigned char * data, size_t len);    
+  const string &s;
+  size_t pos;
+  StringSource (const string &_s) : s (_s), pos (0) {}
+  size_t read (unsigned char *data, size_t len);
 };
 
-
-void writePadding(size_t len, Sink & sink);
-void writeInt(unsigned int n, Sink & sink);
-void writeLongLong(unsigned long long n, Sink & sink);
-void writeString(const unsigned char * buf, size_t len, Sink & sink);
-void writeString(const string & s, Sink & sink);
-template<class T> void writeStrings(const T & ss, Sink & sink);
-
-void readPadding(size_t len, Source & source);
-unsigned int readInt(Source & source);
-unsigned long long readLongLong(Source & source);
-size_t readString(unsigned char * buf, size_t max, Source & source);
-string readString(Source & source);
-template<class T> T readStrings(Source & source);
-
-
-MakeError(SerialisationError, Error)
-
+void writePadding (size_t len, Sink &sink);
+void writeInt (unsigned int n, Sink &sink);
+void writeLongLong (unsigned long long n, Sink &sink);
+void writeString (const unsigned char *buf, size_t len, Sink &sink);
+void writeString (const string &s, Sink &sink);
+template <class T> void writeStrings (const T &ss, Sink &sink);
+
+void readPadding (size_t len, Source &source);
+unsigned int readInt (Source &source);
+unsigned long long readLongLong (Source &source);
+size_t readString (unsigned char *buf, size_t max, Source &source);
+string readString (Source &source);
+template <class T> T readStrings (Source &source);
+
+MakeError (SerialisationError, Error)
 
 }
diff --git a/nix/libutil/types.hh b/nix/libutil/types.hh
index 160884ee1a..68ec82a0aa 100644
--- a/nix/libutil/types.hh
+++ b/nix/libutil/types.hh
@@ -2,9 +2,9 @@
 
 #include "config.h"
 
-#include <string>
 #include <list>
 #include <set>
+#include <string>
 
 #include <boost/format.hpp>
 
@@ -17,83 +17,95 @@
 #endif
 #endif
 
-
-namespace nix {
-
+namespace nix
+{
 
 /* Inherit some names from other namespaces for convenience. */
-using std::string;
+using boost::format;
 using std::list;
 using std::set;
+using std::string;
 using std::vector;
-using boost::format;
-
 
 struct FormatOrString
 {
-    string s;
-    FormatOrString(const string & s) : s(s) { };
-    FormatOrString(const format & f) : s(f.str()) { };
-    FormatOrString(const char * s) : s(s) { };
+  string s;
+  FormatOrString (const string &s) : s (s){};
+  FormatOrString (const format &f) : s (f.str ()){};
+  FormatOrString (const char *s) : s (s){};
 };
 
-
 /* BaseError should generally not be caught, as it has Interrupted as
    a subclass. Catch Error instead. */
 class BaseError : public std::exception
 {
 protected:
-    string prefix_; // used for location traces etc.
-    string err;
+  string prefix_; // used for location traces etc.
+  string err;
+
 public:
-    unsigned int status; // exit status
-    BaseError(const FormatOrString & fs, unsigned int status = 1);
+  unsigned int status; // exit status
+  BaseError (const FormatOrString &fs, unsigned int status = 1);
 #ifdef EXCEPTION_NEEDS_THROW_SPEC
-    ~BaseError() throw () { };
-    const char * what() const throw () { return err.c_str(); }
+  ~BaseError () throw (){};
+  const char *
+  what () const throw ()
+  {
+    return err.c_str ();
+  }
 #else
-    const char * what() const noexcept { return err.c_str(); }
+  const char *
+  what () const noexcept
+  {
+    return err.c_str ();
+  }
 #endif
-    const string & msg() const { return err; }
-    const string & prefix() const { return prefix_; }
-    BaseError & addPrefix(const FormatOrString & fs);
+  const string &
+  msg () const
+  {
+    return err;
+  }
+  const string &
+  prefix () const
+  {
+    return prefix_;
+  }
+  BaseError &addPrefix (const FormatOrString &fs);
 };
 
-#define MakeError(newClass, superClass) \
-    class newClass : public superClass                  \
-    {                                                   \
-    public:                                             \
-        newClass(const FormatOrString & fs, unsigned int status = 1) : superClass(fs, status) { }; \
-    };
+#define MakeError(newClass, superClass)                                       \
+  class newClass : public superClass                                          \
+  {                                                                           \
+  public:                                                                     \
+    newClass (const FormatOrString &fs, unsigned int status = 1)              \
+        : superClass (fs, status){};                                          \
+  };
 
-MakeError(Error, BaseError)
+MakeError (Error, BaseError)
 
-class SysError : public Error
+    class SysError : public Error
 {
 public:
-    int errNo;
-    SysError(const FormatOrString & fs);
+  int errNo;
+  SysError (const FormatOrString &fs);
 };
 
-
 typedef list<string> Strings;
 typedef set<string> StringSet;
 
-
 /* Paths are just strings. */
 typedef string Path;
 typedef list<Path> Paths;
 typedef set<Path> PathSet;
 
-
-typedef enum {
-    lvlError = 0,
-    lvlInfo,
-    lvlTalkative,
-    lvlChatty,
-    lvlDebug,
-    lvlVomit
+typedef enum
+{
+  lvlError = 0,
+  lvlInfo,
+  lvlTalkative,
+  lvlChatty,
+  lvlDebug,
+  lvlVomit
 } Verbosity;
 
-
 }
diff --git a/nix/libutil/util.cc b/nix/libutil/util.cc
index 56f116046c..14f0d02441 100644
--- a/nix/libutil/util.cc
+++ b/nix/libutil/util.cc
@@ -1,19 +1,19 @@
 #include "config.h"
 
-#include "util.hh"
 #include "affinity.hh"
+#include "util.hh"
 
-#include <iostream>
 #include <cerrno>
 #include <cstdio>
 #include <cstdlib>
-#include <sstream>
 #include <cstring>
+#include <iostream>
+#include <sstream>
 
-#include <sys/wait.h>
-#include <unistd.h>
 #include <fcntl.h>
 #include <limits.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
 #ifdef __APPLE__
 #include <sys/syscall.h>
@@ -24,1339 +24,1427 @@
 #endif
 
 #ifdef HAVE_LINUX_CLOSE_RANGE_H
-# include <linux/close_range.h>
+#include <linux/close_range.h>
 #endif
 
+extern char **environ;
 
-extern char * * environ;
-
-
-namespace nix {
-
+namespace nix
+{
 
-BaseError::BaseError(const FormatOrString & fs, unsigned int status)
-    : status(status)
+BaseError::BaseError (const FormatOrString &fs, unsigned int status)
+    : status (status)
 {
-    err = fs.s;
+  err = fs.s;
 }
 
-
-BaseError & BaseError::addPrefix(const FormatOrString & fs)
+BaseError &
+BaseError::addPrefix (const FormatOrString &fs)
 {
-    prefix_ = fs.s + prefix_;
-    return *this;
+  prefix_ = fs.s + prefix_;
+  return *this;
 }
 
-
-SysError::SysError(const FormatOrString & fs)
-    : Error(format("%1%: %2%") % fs.s % strerror(errno))
-    , errNo(errno)
+SysError::SysError (const FormatOrString &fs)
+    : Error (format ("%1%: %2%") % fs.s % strerror (errno)), errNo (errno)
 {
 }
 
-
-string getEnv(const string & key, const string & def)
+string
+getEnv (const string &key, const string &def)
 {
-    char * value = getenv(key.c_str());
-    return value ? string(value) : def;
+  char *value = getenv (key.c_str ());
+  return value ? string (value) : def;
 }
 
-
-Path absPath(Path path, Path dir)
+Path
+absPath (Path path, Path dir)
 {
-    if (path[0] != '/') {
-        if (dir == "") {
+  if (path[0] != '/')
+    {
+      if (dir == "")
+        {
 #ifdef __GNU__
-            /* GNU (aka. GNU/Hurd) doesn't have any limitation on path
-               lengths and doesn't define `PATH_MAX'.  */
-            char *buf = getcwd(NULL, 0);
-            if (buf == NULL)
+          /* GNU (aka. GNU/Hurd) doesn't have any limitation on path
+             lengths and doesn't define `PATH_MAX'.  */
+          char *buf = getcwd (NULL, 0);
+          if (buf == NULL)
 #else
-            char buf[PATH_MAX];
-            if (!getcwd(buf, sizeof(buf)))
+          char buf[PATH_MAX];
+          if (!getcwd (buf, sizeof (buf)))
 #endif
-                throw SysError("cannot get cwd");
-            dir = buf;
+            throw SysError ("cannot get cwd");
+          dir = buf;
 #ifdef __GNU__
-            free(buf);
+          free (buf);
 #endif
         }
-        path = dir + "/" + path;
+      path = dir + "/" + path;
     }
-    return canonPath(path);
+  return canonPath (path);
 }
 
-
-Path canonPath(const Path & path, bool resolveSymlinks)
+Path
+canonPath (const Path &path, bool resolveSymlinks)
 {
-    string s;
+  string s;
 
-    if (path[0] != '/')
-        throw Error(format("not an absolute path: `%1%'") % path);
+  if (path[0] != '/')
+    throw Error (format ("not an absolute path: `%1%'") % path);
 
-    string::const_iterator i = path.begin(), end = path.end();
-    string temp;
+  string::const_iterator i = path.begin (), end = path.end ();
+  string temp;
 
-    /* Count the number of times we follow a symlink and stop at some
-       arbitrary (but high) limit to prevent infinite loops. */
-    unsigned int followCount = 0, maxFollow = 1024;
+  /* Count the number of times we follow a symlink and stop at some
+     arbitrary (but high) limit to prevent infinite loops. */
+  unsigned int followCount = 0, maxFollow = 1024;
 
-    while (1) {
+  while (1)
+    {
 
-        /* Skip slashes. */
-        while (i != end && *i == '/') i++;
-        if (i == end) break;
+      /* Skip slashes. */
+      while (i != end && *i == '/')
+        i++;
+      if (i == end)
+        break;
 
-        /* Ignore `.'. */
-        if (*i == '.' && (i + 1 == end || i[1] == '/'))
-            i++;
+      /* Ignore `.'. */
+      if (*i == '.' && (i + 1 == end || i[1] == '/'))
+        i++;
 
-        /* If `..', delete the last component. */
-        else if (*i == '.' && i + 1 < end && i[1] == '.' &&
-            (i + 2 == end || i[2] == '/'))
+      /* If `..', delete the last component. */
+      else if (*i == '.' && i + 1 < end && i[1] == '.'
+               && (i + 2 == end || i[2] == '/'))
         {
-            if (!s.empty()) s.erase(s.rfind('/'));
-            i += 2;
+          if (!s.empty ())
+            s.erase (s.rfind ('/'));
+          i += 2;
         }
 
-        /* Normal component; copy it. */
-        else {
-            s += '/';
-            while (i != end && *i != '/') s += *i++;
-
-            /* If s points to a symlink, resolve it and restart (since
-               the symlink target might contain new symlinks). */
-            if (resolveSymlinks && isLink(s)) {
-                if (++followCount >= maxFollow)
-                    throw Error(format("infinite symlink recursion in path `%1%'") % path);
-                temp = absPath(readLink(s), dirOf(s))
-                    + string(i, end);
-                i = temp.begin(); /* restart */
-                end = temp.end();
-                s = "";
+      /* Normal component; copy it. */
+      else
+        {
+          s += '/';
+          while (i != end && *i != '/')
+            s += *i++;
+
+          /* If s points to a symlink, resolve it and restart (since
+             the symlink target might contain new symlinks). */
+          if (resolveSymlinks && isLink (s))
+            {
+              if (++followCount >= maxFollow)
+                throw Error (
+                    format ("infinite symlink recursion in path `%1%'")
+                    % path);
+              temp = absPath (readLink (s), dirOf (s)) + string (i, end);
+              i = temp.begin (); /* restart */
+              end = temp.end ();
+              s = "";
             }
         }
     }
 
-    return s.empty() ? "/" : s;
+  return s.empty () ? "/" : s;
 }
 
-
-Path dirOf(const Path & path)
+Path
+dirOf (const Path &path)
 {
-    Path::size_type pos = path.rfind('/');
-    if (pos == string::npos)
-        throw Error(format("invalid file name `%1%'") % path);
-    return pos == 0 ? "/" : Path(path, 0, pos);
+  Path::size_type pos = path.rfind ('/');
+  if (pos == string::npos)
+    throw Error (format ("invalid file name `%1%'") % path);
+  return pos == 0 ? "/" : Path (path, 0, pos);
 }
 
-
-string baseNameOf(const Path & path)
+string
+baseNameOf (const Path &path)
 {
-    Path::size_type pos = path.rfind('/');
-    if (pos == string::npos)
-        throw Error(format("invalid file name `%1%'") % path);
-    return string(path, pos + 1);
+  Path::size_type pos = path.rfind ('/');
+  if (pos == string::npos)
+    throw Error (format ("invalid file name `%1%'") % path);
+  return string (path, pos + 1);
 }
 
-
-bool isInDir(const Path & path, const Path & dir)
+bool
+isInDir (const Path &path, const Path &dir)
 {
-    return path[0] == '/'
-        && string(path, 0, dir.size()) == dir
-        && path.size() >= dir.size() + 2
-        && path[dir.size()] == '/';
+  return path[0] == '/' && string (path, 0, dir.size ()) == dir
+         && path.size () >= dir.size () + 2 && path[dir.size ()] == '/';
 }
 
-
-struct stat lstat(const Path & path)
+struct stat
+lstat (const Path &path)
 {
-    struct stat st;
-    if (lstat(path.c_str(), &st))
-        throw SysError(format("getting status of `%1%'") % path);
-    return st;
+  struct stat st;
+  if (lstat (path.c_str (), &st))
+    throw SysError (format ("getting status of `%1%'") % path);
+  return st;
 }
 
-
-bool pathExists(const Path & path)
+bool
+pathExists (const Path &path)
 {
-    int res;
+  int res;
 #ifdef HAVE_STATX
-    struct statx st;
-    res = statx(AT_FDCWD, path.c_str(), AT_SYMLINK_NOFOLLOW, 0, &st);
+  struct statx st;
+  res = statx (AT_FDCWD, path.c_str (), AT_SYMLINK_NOFOLLOW, 0, &st);
 #else
-    struct stat st;
-    res = lstat(path.c_str(), &st);
+  struct stat st;
+  res = lstat (path.c_str (), &st);
 #endif
-    if (!res) return true;
-    if (errno != ENOENT && errno != ENOTDIR)
-        throw SysError(format("getting status of %1%") % path);
-    return false;
-}
-
-
-Path readLink(const Path & path)
-{
-    checkInterrupt();
-    struct stat st = lstat(path);
-    if (!S_ISLNK(st.st_mode))
-        throw Error(format("`%1%' is not a symlink") % path);
-    char buf[st.st_size];
-    ssize_t rlsize = readlink(path.c_str(), buf, st.st_size);
-    if (rlsize == -1)
-        throw SysError(format("reading symbolic link '%1%'") % path);
-    else if (rlsize > st.st_size)
-        throw Error(format("symbolic link ‘%1%’ size overflow %2% > %3%")
-            % path % rlsize % st.st_size);
-    return string(buf, st.st_size);
-}
-
-
-bool isLink(const Path & path)
-{
-    struct stat st = lstat(path);
-    return S_ISLNK(st.st_mode);
-}
-
-
-static DirEntries readDirectory(DIR *dir)
-{
-    DirEntries entries;
-    entries.reserve(64);
-
-    struct dirent * dirent;
-    while (errno = 0, dirent = readdir(dir)) { /* sic */
-        checkInterrupt();
-        string name = dirent->d_name;
-        if (name == "." || name == "..") continue;
-        entries.emplace_back(name, dirent->d_ino, dirent->d_type);
+  if (!res)
+    return true;
+  if (errno != ENOENT && errno != ENOTDIR)
+    throw SysError (format ("getting status of %1%") % path);
+  return false;
+}
+
+Path
+readLink (const Path &path)
+{
+  checkInterrupt ();
+  struct stat st = lstat (path);
+  if (!S_ISLNK (st.st_mode))
+    throw Error (format ("`%1%' is not a symlink") % path);
+  char buf[st.st_size];
+  ssize_t rlsize = readlink (path.c_str (), buf, st.st_size);
+  if (rlsize == -1)
+    throw SysError (format ("reading symbolic link '%1%'") % path);
+  else if (rlsize > st.st_size)
+    throw Error (format ("symbolic link ‘%1%’ size overflow %2% > %3%") % path
+                 % rlsize % st.st_size);
+  return string (buf, st.st_size);
+}
+
+bool
+isLink (const Path &path)
+{
+  struct stat st = lstat (path);
+  return S_ISLNK (st.st_mode);
+}
+
+static DirEntries
+readDirectory (DIR *dir)
+{
+  DirEntries entries;
+  entries.reserve (64);
+
+  struct dirent *dirent;
+  while (errno = 0, dirent = readdir (dir))
+    { /* sic */
+      checkInterrupt ();
+      string name = dirent->d_name;
+      if (name == "." || name == "..")
+        continue;
+      entries.emplace_back (name, dirent->d_ino, dirent->d_type);
     }
-    if (errno) throw SysError(format("reading directory"));
+  if (errno)
+    throw SysError (format ("reading directory"));
 
-    return entries;
+  return entries;
 }
 
-DirEntries readDirectory(const Path & path)
+DirEntries
+readDirectory (const Path &path)
 {
-    AutoCloseDir dir = opendir(path.c_str());
-    if (!dir) throw SysError(format("opening directory `%1%'") % path);
-    return readDirectory(dir);
+  AutoCloseDir dir = opendir (path.c_str ());
+  if (!dir)
+    throw SysError (format ("opening directory `%1%'") % path);
+  return readDirectory (dir);
 }
 
-static DirEntries readDirectory(int fd)
+static DirEntries
+readDirectory (int fd)
 {
-    /* Since 'closedir' closes the underlying file descriptor, duplicate FD
-       beforehand.  */
-    int fdcopy = dup(fd);
-    if (fdcopy < 0) throw SysError("dup");
+  /* Since 'closedir' closes the underlying file descriptor, duplicate FD
+     beforehand.  */
+  int fdcopy = dup (fd);
+  if (fdcopy < 0)
+    throw SysError ("dup");
 
-    AutoCloseDir dir = fdopendir(fdcopy);
-    if (!dir) throw SysError(format("opening directory from file descriptor `%1%'") % fd);
-    return readDirectory(dir);
+  AutoCloseDir dir = fdopendir (fdcopy);
+  if (!dir)
+    throw SysError (format ("opening directory from file descriptor `%1%'")
+                    % fd);
+  return readDirectory (dir);
 }
 
-unsigned char getFileType(const Path & path)
+unsigned char
+getFileType (const Path &path)
 {
-    struct stat st = lstat(path);
-    if (S_ISDIR(st.st_mode)) return DT_DIR;
-    if (S_ISLNK(st.st_mode)) return DT_LNK;
-    if (S_ISREG(st.st_mode)) return DT_REG;
-    return DT_UNKNOWN;
+  struct stat st = lstat (path);
+  if (S_ISDIR (st.st_mode))
+    return DT_DIR;
+  if (S_ISLNK (st.st_mode))
+    return DT_LNK;
+  if (S_ISREG (st.st_mode))
+    return DT_REG;
+  return DT_UNKNOWN;
 }
 
-
-string readFile(int fd)
+string
+readFile (int fd)
 {
-    struct stat st;
-    if (fstat(fd, &st) == -1)
-        throw SysError("statting file");
+  struct stat st;
+  if (fstat (fd, &st) == -1)
+    throw SysError ("statting file");
 
-    unsigned char * buf = new unsigned char[st.st_size];
-    AutoDeleteArray<unsigned char> d(buf);
-    readFull(fd, buf, st.st_size);
+  unsigned char *buf = new unsigned char[st.st_size];
+  AutoDeleteArray<unsigned char> d (buf);
+  readFull (fd, buf, st.st_size);
 
-    return string((char *) buf, st.st_size);
+  return string ((char *)buf, st.st_size);
 }
 
-
-string readFile(const Path & path, bool drain)
+string
+readFile (const Path &path, bool drain)
 {
-    AutoCloseFD fd = open(path.c_str(), O_RDONLY);
-    if (fd == -1)
-        throw SysError(format("reading file `%1%'") % path);
-    return drain ? drainFD(fd) : readFile(fd);
+  AutoCloseFD fd = open (path.c_str (), O_RDONLY);
+  if (fd == -1)
+    throw SysError (format ("reading file `%1%'") % path);
+  return drain ? drainFD (fd) : readFile (fd);
 }
 
-
-void writeFile(const Path & path, const string & s)
+void
+writeFile (const Path &path, const string &s)
 {
-    AutoCloseFD fd = open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0666);
-    if (fd == -1)
-        throw SysError(format("writing file '%1%'") % path);
-    writeFull(fd, s);
+  AutoCloseFD fd = open (path.c_str (), O_WRONLY | O_TRUNC | O_CREAT, 0666);
+  if (fd == -1)
+    throw SysError (format ("writing file '%1%'") % path);
+  writeFull (fd, s);
 }
 
-
-string readLine(int fd)
+string
+readLine (int fd)
 {
-    string s;
-    while (1) {
-        checkInterrupt();
-        char ch;
-        ssize_t rd = read(fd, &ch, 1);
-        if (rd == -1) {
-            if (errno != EINTR)
-                throw SysError("reading a line");
-        } else if (rd == 0)
-            throw EndOfFile("unexpected EOF reading a line");
-        else {
-            if (ch == '\n') return s;
-            s += ch;
+  string s;
+  while (1)
+    {
+      checkInterrupt ();
+      char ch;
+      ssize_t rd = read (fd, &ch, 1);
+      if (rd == -1)
+        {
+          if (errno != EINTR)
+            throw SysError ("reading a line");
+        }
+      else if (rd == 0)
+        throw EndOfFile ("unexpected EOF reading a line");
+      else
+        {
+          if (ch == '\n')
+            return s;
+          s += ch;
         }
     }
 }
 
-
-void writeLine(int fd, string s)
+void
+writeLine (int fd, string s)
 {
-    s += '\n';
-    writeFull(fd, s);
+  s += '\n';
+  writeFull (fd, s);
 }
 
-
-static void _deletePath(const Path & path, unsigned long long & bytesFreed, size_t linkThreshold)
+static void
+_deletePath (const Path &path, unsigned long long &bytesFreed,
+             size_t linkThreshold)
 {
-    checkInterrupt();
+  checkInterrupt ();
 
-    printMsg(lvlVomit, format("%1%") % path);
+  printMsg (lvlVomit, format ("%1%") % path);
 
 #ifdef HAVE_STATX
-# define st_mode stx_mode
-# define st_size stx_size
-# define st_nlink stx_nlink
-    struct statx st;
-    if (statx(AT_FDCWD, path.c_str(),
-	      AT_SYMLINK_NOFOLLOW,
-	      STATX_SIZE | STATX_NLINK | STATX_MODE, &st) == -1)
-	throw SysError(format("getting status of `%1%'") % path);
+#define st_mode stx_mode
+#define st_size stx_size
+#define st_nlink stx_nlink
+  struct statx st;
+  if (statx (AT_FDCWD, path.c_str (), AT_SYMLINK_NOFOLLOW,
+             STATX_SIZE | STATX_NLINK | STATX_MODE, &st)
+      == -1)
+    throw SysError (format ("getting status of `%1%'") % path);
 #else
-    struct stat st = lstat(path);
+  struct stat st = lstat (path);
 #endif
 
-    if (!S_ISDIR(st.st_mode) && st.st_nlink <= linkThreshold)
-	bytesFreed += st.st_size;
+  if (!S_ISDIR (st.st_mode) && st.st_nlink <= linkThreshold)
+    bytesFreed += st.st_size;
 
-    if (S_ISDIR(st.st_mode)) {
-        /* Make the directory writable. */
-        if (!(st.st_mode & S_IWUSR)) {
-            if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1)
-                throw SysError(format("making `%1%' writable") % path);
+  if (S_ISDIR (st.st_mode))
+    {
+      /* Make the directory writable. */
+      if (!(st.st_mode & S_IWUSR))
+        {
+          if (chmod (path.c_str (), st.st_mode | S_IWUSR) == -1)
+            throw SysError (format ("making `%1%' writable") % path);
         }
 
-        for (auto & i : readDirectory(path))
-            _deletePath(path + "/" + i.name, bytesFreed, linkThreshold);
+      for (auto &i : readDirectory (path))
+        _deletePath (path + "/" + i.name, bytesFreed, linkThreshold);
     }
 
-    int ret;
-    ret = S_ISDIR(st.st_mode) ? rmdir(path.c_str()) : unlink(path.c_str());
-    if (ret == -1)
-        throw SysError(format("cannot unlink `%1%'") % path);
+  int ret;
+  ret = S_ISDIR (st.st_mode) ? rmdir (path.c_str ()) : unlink (path.c_str ());
+  if (ret == -1)
+    throw SysError (format ("cannot unlink `%1%'") % path);
 
 #undef st_mode
 #undef st_size
 #undef st_nlink
 }
 
-
-void deletePath(const Path & path)
+void
+deletePath (const Path &path)
 {
-    unsigned long long dummy;
-    deletePath(path, dummy);
+  unsigned long long dummy;
+  deletePath (path, dummy);
 }
 
-
-void deletePath(const Path & path, unsigned long long & bytesFreed, size_t linkThreshold)
+void
+deletePath (const Path &path, unsigned long long &bytesFreed,
+            size_t linkThreshold)
 {
-    startNest(nest, lvlDebug,
-        format("recursively deleting path `%1%'") % path);
-    bytesFreed = 0;
-    _deletePath(path, bytesFreed, linkThreshold);
+  startNest (nest, lvlDebug,
+             format ("recursively deleting path `%1%'") % path);
+  bytesFreed = 0;
+  _deletePath (path, bytesFreed, linkThreshold);
 }
 
-static void copyFile(int sourceFd, int destinationFd)
+static void
+copyFile (int sourceFd, int destinationFd)
 {
-    struct stat st;
-    if (fstat(sourceFd, &st) == -1) throw SysError("statting file");
+  struct stat st;
+  if (fstat (sourceFd, &st) == -1)
+    throw SysError ("statting file");
 
-    ssize_t result = copy_file_range(sourceFd, NULL, destinationFd, NULL, st.st_size, 0);
-    if (result < 0 && errno == ENOSYS) {
-	for (size_t remaining = st.st_size; remaining > 0; ) {
-	    unsigned char buf[8192];
-	    size_t count = std::min(remaining, sizeof buf);
+  ssize_t result
+      = copy_file_range (sourceFd, NULL, destinationFd, NULL, st.st_size, 0);
+  if (result < 0 && errno == ENOSYS)
+    {
+      for (size_t remaining = st.st_size; remaining > 0;)
+        {
+          unsigned char buf[8192];
+          size_t count = std::min (remaining, sizeof buf);
 
-	    readFull(sourceFd, buf, count);
-	    writeFull(destinationFd, buf, count);
-	    remaining -= count;
-	}
-    } else {
-	if (result < 0)
-	    throw SysError(format("copy_file_range `%1%' to `%2%'") % sourceFd % destinationFd);
+          readFull (sourceFd, buf, count);
+          writeFull (destinationFd, buf, count);
+          remaining -= count;
+        }
+    }
+  else
+    {
+      if (result < 0)
+        throw SysError (format ("copy_file_range `%1%' to `%2%'") % sourceFd
+                        % destinationFd);
 
-	/* If 'copy_file_range' copied less than requested, try again.  */
-	for (ssize_t copied = result; copied < st.st_size; copied += result) {
-	    result = copy_file_range(sourceFd, NULL, destinationFd, NULL,
-				     st.st_size - copied, 0);
-	    if (result < 0)
-		throw SysError(format("copy_file_range `%1%' to `%2%'") % sourceFd % destinationFd);
-	}
+      /* If 'copy_file_range' copied less than requested, try again.  */
+      for (ssize_t copied = result; copied < st.st_size; copied += result)
+        {
+          result = copy_file_range (sourceFd, NULL, destinationFd, NULL,
+                                    st.st_size - copied, 0);
+          if (result < 0)
+            throw SysError (format ("copy_file_range `%1%' to `%2%'")
+                            % sourceFd % destinationFd);
+        }
     }
 }
 
-static void copyFileRecursively(int sourceroot, const Path &source,
-				int destinationroot, const Path &destination,
-				bool deleteSource)
-{
-    struct stat st;
-    if (fstatat(sourceroot, source.c_str(), &st, AT_SYMLINK_NOFOLLOW) == -1)
-	throw SysError(format("statting file `%1%'") % source);
-
-    if (S_ISREG(st.st_mode)) {
-	AutoCloseFD sourceFd = openat(sourceroot, source.c_str(),
-				      O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
-	if (sourceFd == -1) throw SysError(format("opening `%1%'") % source);
-
-	AutoCloseFD destinationFd = openat(destinationroot, destination.c_str(),
-					   O_CLOEXEC | O_CREAT | O_WRONLY | O_TRUNC,
-					   st.st_mode);
-	if (destinationFd == -1) throw SysError(format("opening `%1%'") % source);
-
-	copyFile(sourceFd, destinationFd);
-	fchown(destinationFd, st.st_uid, st.st_gid);
-    } else if (S_ISLNK(st.st_mode)) {
-	char target[st.st_size + 1];
-	ssize_t result = readlinkat(sourceroot, source.c_str(), target, st.st_size);
-	if (result != st.st_size) throw SysError("reading symlink target");
-	target[st.st_size] = '\0';
-	int err = symlinkat(target, destinationroot, destination.c_str());
-	if (err != 0)
-	    throw SysError(format("creating symlink `%1%'") % destination);
-	fchownat(destinationroot, destination.c_str(),
-		 st.st_uid, st.st_gid, AT_SYMLINK_NOFOLLOW);
-    } else if (S_ISDIR(st.st_mode)) {
-	int err = mkdirat(destinationroot, destination.c_str(), 0755);
-	if (err != 0)
-	    throw SysError(format("creating directory `%1%'") % destination);
-
-	AutoCloseFD destinationFd = openat(destinationroot, destination.c_str(),
-					   O_CLOEXEC | O_RDONLY | O_DIRECTORY);
-	if (err != 0)
-	    throw SysError(format("opening directory `%1%'") % destination);
-
-	AutoCloseFD sourceFd = openat(sourceroot, source.c_str(),
-				      O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
-	if (sourceFd == -1)
-	    throw SysError(format("opening `%1%'") % source);
-
-        if (deleteSource && !(st.st_mode & S_IWUSR)) {
-	    /* Ensure the directory writable so files within it can be
-	       deleted.  */
-            if (fchmod(sourceFd, st.st_mode | S_IWUSR) == -1)
-                throw SysError(format("making `%1%' directory writable") % source);
+static void
+copyFileRecursively (int sourceroot, const Path &source, int destinationroot,
+                     const Path &destination, bool deleteSource)
+{
+  struct stat st;
+  if (fstatat (sourceroot, source.c_str (), &st, AT_SYMLINK_NOFOLLOW) == -1)
+    throw SysError (format ("statting file `%1%'") % source);
+
+  if (S_ISREG (st.st_mode))
+    {
+      AutoCloseFD sourceFd = openat (sourceroot, source.c_str (),
+                                     O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
+      if (sourceFd == -1)
+        throw SysError (format ("opening `%1%'") % source);
+
+      AutoCloseFD destinationFd
+          = openat (destinationroot, destination.c_str (),
+                    O_CLOEXEC | O_CREAT | O_WRONLY | O_TRUNC, st.st_mode);
+      if (destinationFd == -1)
+        throw SysError (format ("opening `%1%'") % source);
+
+      copyFile (sourceFd, destinationFd);
+      fchown (destinationFd, st.st_uid, st.st_gid);
+    }
+  else if (S_ISLNK (st.st_mode))
+    {
+      char target[st.st_size + 1];
+      ssize_t result
+          = readlinkat (sourceroot, source.c_str (), target, st.st_size);
+      if (result != st.st_size)
+        throw SysError ("reading symlink target");
+      target[st.st_size] = '\0';
+      int err = symlinkat (target, destinationroot, destination.c_str ());
+      if (err != 0)
+        throw SysError (format ("creating symlink `%1%'") % destination);
+      fchownat (destinationroot, destination.c_str (), st.st_uid, st.st_gid,
+                AT_SYMLINK_NOFOLLOW);
+    }
+  else if (S_ISDIR (st.st_mode))
+    {
+      int err = mkdirat (destinationroot, destination.c_str (), 0755);
+      if (err != 0)
+        throw SysError (format ("creating directory `%1%'") % destination);
+
+      AutoCloseFD destinationFd
+          = openat (destinationroot, destination.c_str (),
+                    O_CLOEXEC | O_RDONLY | O_DIRECTORY);
+      if (err != 0)
+        throw SysError (format ("opening directory `%1%'") % destination);
+
+      AutoCloseFD sourceFd = openat (sourceroot, source.c_str (),
+                                     O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
+      if (sourceFd == -1)
+        throw SysError (format ("opening `%1%'") % source);
+
+      if (deleteSource && !(st.st_mode & S_IWUSR))
+        {
+          /* Ensure the directory writable so files within it can be
+             deleted.  */
+          if (fchmod (sourceFd, st.st_mode | S_IWUSR) == -1)
+            throw SysError (format ("making `%1%' directory writable")
+                            % source);
         }
 
-        for (auto & i : readDirectory(sourceFd))
-	    copyFileRecursively((int)sourceFd, i.name, (int)destinationFd, i.name,
-				deleteSource);
-	fchown(destinationFd, st.st_uid, st.st_gid);
-    } else throw Error(format("refusing to copy irregular file `%1%'") % source);
+      for (auto &i : readDirectory (sourceFd))
+        copyFileRecursively ((int)sourceFd, i.name, (int)destinationFd, i.name,
+                             deleteSource);
+      fchown (destinationFd, st.st_uid, st.st_gid);
+    }
+  else
+    throw Error (format ("refusing to copy irregular file `%1%'") % source);
 
-    if (deleteSource)
-	unlinkat(sourceroot, source.c_str(),
-		 S_ISDIR(st.st_mode) ? AT_REMOVEDIR : 0);
+  if (deleteSource)
+    unlinkat (sourceroot, source.c_str (),
+              S_ISDIR (st.st_mode) ? AT_REMOVEDIR : 0);
 }
 
-void copyFileRecursively(const Path &source, const Path &destination, bool deleteSource)
+void
+copyFileRecursively (const Path &source, const Path &destination,
+                     bool deleteSource)
 {
-    copyFileRecursively(AT_FDCWD, source, AT_FDCWD, destination, deleteSource);
+  copyFileRecursively (AT_FDCWD, source, AT_FDCWD, destination, deleteSource);
 }
 
-static Path tempName(Path tmpRoot, const Path & prefix, bool includePid,
-    int & counter)
+static Path
+tempName (Path tmpRoot, const Path &prefix, bool includePid, int &counter)
 {
-    tmpRoot = canonPath(tmpRoot.empty() ? getEnv("TMPDIR", "/tmp") : tmpRoot, true);
-    if (includePid)
-        return (format("%1%/%2%-%3%-%4%") % tmpRoot % prefix % getpid() % counter++).str();
-    else
-        return (format("%1%/%2%-%3%") % tmpRoot % prefix % counter++).str();
-}
-
-
-Path createTempDir(const Path & tmpRoot, const Path & prefix,
-    bool includePid, bool useGlobalCounter, mode_t mode)
-{
-    static int globalCounter = 0;
-    int localCounter = 0;
-    int & counter(useGlobalCounter ? globalCounter : localCounter);
-
-    while (1) {
-        checkInterrupt();
-        Path tmpDir = tempName(tmpRoot, prefix, includePid, counter);
-        if (mkdir(tmpDir.c_str(), mode) == 0) {
-            /* Explicitly set the group of the directory.  This is to
-               work around around problems caused by BSD's group
-               ownership semantics (directories inherit the group of
-               the parent).  For instance, the group of /tmp on
-               FreeBSD is "wheel", so all directories created in /tmp
-               will be owned by "wheel"; but if the user is not in
-               "wheel", then "tar" will fail to unpack archives that
-               have the setgid bit set on directories. */
-            if (chown(tmpDir.c_str(), (uid_t) -1, getegid()) != 0)
-                throw SysError(format("setting group of directory `%1%'") % tmpDir);
-            return tmpDir;
+  tmpRoot = canonPath (tmpRoot.empty () ? getEnv ("TMPDIR", "/tmp") : tmpRoot,
+                       true);
+  if (includePid)
+    return (format ("%1%/%2%-%3%-%4%") % tmpRoot % prefix % getpid ()
+            % counter++)
+        .str ();
+  else
+    return (format ("%1%/%2%-%3%") % tmpRoot % prefix % counter++).str ();
+}
+
+Path
+createTempDir (const Path &tmpRoot, const Path &prefix, bool includePid,
+               bool useGlobalCounter, mode_t mode)
+{
+  static int globalCounter = 0;
+  int localCounter = 0;
+  int &counter (useGlobalCounter ? globalCounter : localCounter);
+
+  while (1)
+    {
+      checkInterrupt ();
+      Path tmpDir = tempName (tmpRoot, prefix, includePid, counter);
+      if (mkdir (tmpDir.c_str (), mode) == 0)
+        {
+          /* Explicitly set the group of the directory.  This is to
+             work around around problems caused by BSD's group
+             ownership semantics (directories inherit the group of
+             the parent).  For instance, the group of /tmp on
+             FreeBSD is "wheel", so all directories created in /tmp
+             will be owned by "wheel"; but if the user is not in
+             "wheel", then "tar" will fail to unpack archives that
+             have the setgid bit set on directories. */
+          if (chown (tmpDir.c_str (), (uid_t)-1, getegid ()) != 0)
+            throw SysError (format ("setting group of directory `%1%'")
+                            % tmpDir);
+          return tmpDir;
         }
-        if (errno != EEXIST)
-            throw SysError(format("creating directory `%1%'") % tmpDir);
+      if (errno != EEXIST)
+        throw SysError (format ("creating directory `%1%'") % tmpDir);
     }
 }
 
-
-Paths createDirs(const Path & path)
+Paths
+createDirs (const Path &path)
 {
-    Paths created;
-    if (path == "/") return created;
+  Paths created;
+  if (path == "/")
+    return created;
 
-    struct stat st;
-    if (lstat(path.c_str(), &st) == -1) {
-        created = createDirs(dirOf(path));
-        if (mkdir(path.c_str(), 0777) == -1 && errno != EEXIST)
-            throw SysError(format("creating directory `%1%'") % path);
-        st = lstat(path);
-        created.push_back(path);
+  struct stat st;
+  if (lstat (path.c_str (), &st) == -1)
+    {
+      created = createDirs (dirOf (path));
+      if (mkdir (path.c_str (), 0777) == -1 && errno != EEXIST)
+        throw SysError (format ("creating directory `%1%'") % path);
+      st = lstat (path);
+      created.push_back (path);
     }
 
-    if (S_ISLNK(st.st_mode) && stat(path.c_str(), &st) == -1)
-        throw SysError(format("statting symlink `%1%'") % path);
+  if (S_ISLNK (st.st_mode) && stat (path.c_str (), &st) == -1)
+    throw SysError (format ("statting symlink `%1%'") % path);
 
-    if (!S_ISDIR(st.st_mode)) throw Error(format("`%1%' is not a directory") % path);
+  if (!S_ISDIR (st.st_mode))
+    throw Error (format ("`%1%' is not a directory") % path);
 
-    return created;
+  return created;
 }
 
-
-void createSymlink(const Path & target, const Path & link)
+void
+createSymlink (const Path &target, const Path &link)
 {
-    if (symlink(target.c_str(), link.c_str()))
-        throw SysError(format("creating symlink from `%1%' to `%2%'") % link % target);
+  if (symlink (target.c_str (), link.c_str ()))
+    throw SysError (format ("creating symlink from `%1%' to `%2%'") % link
+                    % target);
 }
 
-
 LogType logType = ltPretty;
 Verbosity verbosity = lvlInfo;
 
 static int nestingLevel = 0;
 
+Nest::Nest () { nest = false; }
 
-Nest::Nest()
-{
-    nest = false;
-}
+Nest::~Nest () { close (); }
 
-
-Nest::~Nest()
+static string
+escVerbosity (Verbosity level)
 {
-    close();
+  return std::to_string ((int)level);
 }
 
-
-static string escVerbosity(Verbosity level)
+void
+Nest::open (Verbosity level, const FormatOrString &fs)
 {
-    return std::to_string((int) level);
-}
-
-
-void Nest::open(Verbosity level, const FormatOrString & fs)
-{
-    if (level <= verbosity) {
-        if (logType == ltEscapes)
-            std::cerr << "\033[" << escVerbosity(level) << "p"
-                      << fs.s << "\n";
-        else
-            printMsg_(level, fs);
-        nest = true;
-        nestingLevel++;
+  if (level <= verbosity)
+    {
+      if (logType == ltEscapes)
+        std::cerr << "\033[" << escVerbosity (level) << "p" << fs.s << "\n";
+      else
+        printMsg_ (level, fs);
+      nest = true;
+      nestingLevel++;
     }
 }
 
-
-void Nest::close()
+void
+Nest::close ()
 {
-    if (nest) {
-        nestingLevel--;
-        if (logType == ltEscapes)
-            std::cerr << "\033[q";
-        nest = false;
+  if (nest)
+    {
+      nestingLevel--;
+      if (logType == ltEscapes)
+        std::cerr << "\033[q";
+      nest = false;
     }
 }
 
-
-void printMsg_(Verbosity level, const FormatOrString & fs)
+void
+printMsg_ (Verbosity level, const FormatOrString &fs)
 {
-    checkInterrupt();
-    if (level > verbosity) return;
-    string prefix;
-    if (logType == ltPretty)
-        for (int i = 0; i < nestingLevel; i++)
-            prefix += "|   ";
-    else if (logType == ltEscapes && level != lvlInfo)
-        prefix = "\033[" + escVerbosity(level) + "s";
-    string s = (format("%1%%2%\n") % prefix % fs.s).str();
-    writeToStderr(s);
+  checkInterrupt ();
+  if (level > verbosity)
+    return;
+  string prefix;
+  if (logType == ltPretty)
+    for (int i = 0; i < nestingLevel; i++)
+      prefix += "|   ";
+  else if (logType == ltEscapes && level != lvlInfo)
+    prefix = "\033[" + escVerbosity (level) + "s";
+  string s = (format ("%1%%2%\n") % prefix % fs.s).str ();
+  writeToStderr (s);
 }
 
-
-void warnOnce(bool & haveWarned, const FormatOrString & fs)
+void
+warnOnce (bool &haveWarned, const FormatOrString &fs)
 {
-    if (!haveWarned) {
-        printMsg(lvlError, format("warning: %1%") % fs.s);
-        haveWarned = true;
+  if (!haveWarned)
+    {
+      printMsg (lvlError, format ("warning: %1%") % fs.s);
+      haveWarned = true;
     }
 }
 
-
-void writeToStderr(const string & s)
+void
+writeToStderr (const string &s)
 {
-    try {
-        if (_writeToStderr)
-            _writeToStderr((const unsigned char *) s.data(), s.size());
-        else
-            writeFull(STDERR_FILENO, s);
-    } catch (SysError & e) {
-        /* Ignore failing writes to stderr if we're in an exception
-           handler, otherwise throw an exception.  We need to ignore
-           write errors in exception handlers to ensure that cleanup
-           code runs to completion if the other side of stderr has
-           been closed unexpectedly. */
-        if (!std::uncaught_exception()) throw;
+  try
+    {
+      if (_writeToStderr)
+        _writeToStderr ((const unsigned char *)s.data (), s.size ());
+      else
+        writeFull (STDERR_FILENO, s);
+    }
+  catch (SysError &e)
+    {
+      /* Ignore failing writes to stderr if we're in an exception
+         handler, otherwise throw an exception.  We need to ignore
+         write errors in exception handlers to ensure that cleanup
+         code runs to completion if the other side of stderr has
+         been closed unexpectedly. */
+      if (!std::uncaught_exception ())
+        throw;
     }
 }
 
+void (*_writeToStderr) (const unsigned char *buf, size_t count) = 0;
 
-void (*_writeToStderr) (const unsigned char * buf, size_t count) = 0;
-
-
-void readFull(int fd, unsigned char * buf, size_t count)
+void
+readFull (int fd, unsigned char *buf, size_t count)
 {
-    while (count) {
-        checkInterrupt();
-        ssize_t res = read(fd, (char *) buf, count);
-        if (res == -1) {
-            if (errno == EINTR) continue;
-            throw SysError("reading from file");
+  while (count)
+    {
+      checkInterrupt ();
+      ssize_t res = read (fd, (char *)buf, count);
+      if (res == -1)
+        {
+          if (errno == EINTR)
+            continue;
+          throw SysError ("reading from file");
         }
-        if (res == 0) throw EndOfFile("unexpected end-of-file");
-        count -= res;
-        buf += res;
+      if (res == 0)
+        throw EndOfFile ("unexpected end-of-file");
+      count -= res;
+      buf += res;
     }
 }
 
-
-void writeFull(int fd, const unsigned char * buf, size_t count)
+void
+writeFull (int fd, const unsigned char *buf, size_t count)
 {
-    while (count) {
-        checkInterrupt();
-        ssize_t res = write(fd, (char *) buf, count);
-        if (res == -1) {
-            if (errno == EINTR) continue;
-            throw SysError("writing to file");
+  while (count)
+    {
+      checkInterrupt ();
+      ssize_t res = write (fd, (char *)buf, count);
+      if (res == -1)
+        {
+          if (errno == EINTR)
+            continue;
+          throw SysError ("writing to file");
         }
-        count -= res;
-        buf += res;
+      count -= res;
+      buf += res;
     }
 }
 
-
-void writeFull(int fd, const string & s)
+void
+writeFull (int fd, const string &s)
 {
-    writeFull(fd, (const unsigned char *) s.data(), s.size());
+  writeFull (fd, (const unsigned char *)s.data (), s.size ());
 }
 
-
-string drainFD(int fd)
+string
+drainFD (int fd)
 {
-    string result;
-    unsigned char buffer[4096];
-    while (1) {
-        checkInterrupt();
-        ssize_t rd = read(fd, buffer, sizeof buffer);
-        if (rd == -1) {
-            if (errno != EINTR)
-                throw SysError("reading from file");
+  string result;
+  unsigned char buffer[4096];
+  while (1)
+    {
+      checkInterrupt ();
+      ssize_t rd = read (fd, buffer, sizeof buffer);
+      if (rd == -1)
+        {
+          if (errno != EINTR)
+            throw SysError ("reading from file");
         }
-        else if (rd == 0) break;
-        else result.append((char *) buffer, rd);
+      else if (rd == 0)
+        break;
+      else
+        result.append ((char *)buffer, rd);
     }
-    return result;
+  return result;
 }
 
-
-
 //////////////////////////////////////////////////////////////////////
 
-
-AutoDelete::AutoDelete(const string & p, bool recursive) : path(p)
+AutoDelete::AutoDelete (const string &p, bool recursive) : path (p)
 {
-    del = true;
-    this->recursive = recursive;
+  del = true;
+  this->recursive = recursive;
 }
 
-AutoDelete::~AutoDelete()
+AutoDelete::~AutoDelete ()
 {
-    try {
-        if (del) {
-            if (recursive)
-                deletePath(path);
-            else {
-                if (remove(path.c_str()) == -1)
-                    throw SysError(format("cannot unlink `%1%'") % path);
+  try
+    {
+      if (del)
+        {
+          if (recursive)
+            deletePath (path);
+          else
+            {
+              if (remove (path.c_str ()) == -1)
+                throw SysError (format ("cannot unlink `%1%'") % path);
             }
         }
-    } catch (...) {
-        ignoreException();
+    }
+  catch (...)
+    {
+      ignoreException ();
     }
 }
 
-void AutoDelete::cancel()
+void
+AutoDelete::cancel ()
 {
-    del = false;
+  del = false;
 }
 
-
-
 //////////////////////////////////////////////////////////////////////
 
+AutoCloseFD::AutoCloseFD () { fd = -1; }
 
-AutoCloseFD::AutoCloseFD()
-{
-    fd = -1;
-}
-
-
-AutoCloseFD::AutoCloseFD(int fd)
-{
-    this->fd = fd;
-}
-
+AutoCloseFD::AutoCloseFD (int fd) { this->fd = fd; }
 
-AutoCloseFD::AutoCloseFD(const AutoCloseFD & fd)
+AutoCloseFD::AutoCloseFD (const AutoCloseFD &fd)
 {
-    /* Copying an AutoCloseFD isn't allowed (who should get to close
-       it?).  But as an edge case, allow copying of closed
-       AutoCloseFDs.  This is necessary due to tiresome reasons
-       involving copy constructor use on default object values in STL
-       containers (like when you do `map[value]' where value isn't in
-       the map yet). */
-    this->fd = fd.fd;
-    if (this->fd != -1) abort();
+  /* Copying an AutoCloseFD isn't allowed (who should get to close
+     it?).  But as an edge case, allow copying of closed
+     AutoCloseFDs.  This is necessary due to tiresome reasons
+     involving copy constructor use on default object values in STL
+     containers (like when you do `map[value]' where value isn't in
+     the map yet). */
+  this->fd = fd.fd;
+  if (this->fd != -1)
+    abort ();
 }
 
-
-AutoCloseFD::~AutoCloseFD()
+AutoCloseFD::~AutoCloseFD ()
 {
-    try {
-        close();
-    } catch (...) {
-        ignoreException();
+  try
+    {
+      close ();
+    }
+  catch (...)
+    {
+      ignoreException ();
     }
 }
 
-
-void AutoCloseFD::operator =(int fd)
+void
+AutoCloseFD::operator= (int fd)
 {
-    if (this->fd != fd) close();
-    this->fd = fd;
+  if (this->fd != fd)
+    close ();
+  this->fd = fd;
 }
 
+AutoCloseFD::operator int () const { return fd; }
 
-AutoCloseFD::operator int() const
+void
+AutoCloseFD::close ()
 {
-    return fd;
-}
-
-
-void AutoCloseFD::close()
-{
-    if (fd != -1) {
-        if (::close(fd) == -1)
-            /* This should never happen. */
-            throw SysError(format("closing file descriptor %1%") % fd);
-        fd = -1;
+  if (fd != -1)
+    {
+      if (::close (fd) == -1)
+        /* This should never happen. */
+        throw SysError (format ("closing file descriptor %1%") % fd);
+      fd = -1;
     }
 }
 
-
-bool AutoCloseFD::isOpen()
+bool
+AutoCloseFD::isOpen ()
 {
-    return fd != -1;
+  return fd != -1;
 }
 
-
 /* Pass responsibility for closing this fd to the caller. */
-int AutoCloseFD::borrow()
+int
+AutoCloseFD::borrow ()
 {
-    int oldFD = fd;
-    fd = -1;
-    return oldFD;
+  int oldFD = fd;
+  fd = -1;
+  return oldFD;
 }
 
-
-void Pipe::create()
+void
+Pipe::create ()
 {
-    int fds[2];
-    if (pipe(fds) != 0) throw SysError("creating pipe");
-    readSide = fds[0];
-    writeSide = fds[1];
-    closeOnExec(readSide);
-    closeOnExec(writeSide);
+  int fds[2];
+  if (pipe (fds) != 0)
+    throw SysError ("creating pipe");
+  readSide = fds[0];
+  writeSide = fds[1];
+  closeOnExec (readSide);
+  closeOnExec (writeSide);
 }
 
-
-
 //////////////////////////////////////////////////////////////////////
 
+AutoCloseDir::AutoCloseDir () { dir = 0; }
 
-AutoCloseDir::AutoCloseDir()
-{
-    dir = 0;
-}
-
-
-AutoCloseDir::AutoCloseDir(DIR * dir)
-{
-    this->dir = dir;
-}
-
-
-AutoCloseDir::~AutoCloseDir()
-{
-    close();
-}
-
-
-void AutoCloseDir::operator =(DIR * dir)
-{
-    this->dir = dir;
-}
+AutoCloseDir::AutoCloseDir (DIR *dir) { this->dir = dir; }
 
+AutoCloseDir::~AutoCloseDir () { close (); }
 
-AutoCloseDir::operator DIR *()
+void
+AutoCloseDir::operator= (DIR *dir)
 {
-    return dir;
+  this->dir = dir;
 }
 
+AutoCloseDir::operator DIR * () { return dir; }
 
-void AutoCloseDir::close()
+void
+AutoCloseDir::close ()
 {
-    if (dir) {
-        closedir(dir);
-        dir = 0;
+  if (dir)
+    {
+      closedir (dir);
+      dir = 0;
     }
 }
 
-
 //////////////////////////////////////////////////////////////////////
 
+Pid::Pid () : pid (-1), separatePG (false), killSignal (SIGKILL) {}
 
-Pid::Pid()
-    : pid(-1), separatePG(false), killSignal(SIGKILL)
-{
-}
+Pid::Pid (pid_t pid) : pid (pid), separatePG (false), killSignal (SIGKILL) {}
 
+Pid::~Pid () { kill (); }
 
-Pid::Pid(pid_t pid)
-    : pid(pid), separatePG(false), killSignal(SIGKILL)
+void
+Pid::operator= (pid_t pid)
 {
+  if (this->pid != pid)
+    kill ();
+  this->pid = pid;
+  killSignal = SIGKILL; // reset signal to default
 }
 
+Pid::operator pid_t () { return pid; }
 
-Pid::~Pid()
+void
+Pid::kill (bool quiet)
 {
-    kill();
-}
-
+  if (pid == -1 || pid == 0)
+    return;
 
-void Pid::operator =(pid_t pid)
-{
-    if (this->pid != pid) kill();
-    this->pid = pid;
-    killSignal = SIGKILL; // reset signal to default
-}
+  if (!quiet)
+    printMsg (lvlError, format ("killing process %1%") % pid);
 
+  /* Send the requested signal to the child.  If it has its own
+     process group, send the signal to every process in the child
+     process group (which hopefully includes *all* its children). */
+  if (::kill (separatePG ? -pid : pid, killSignal) != 0)
+    printMsg (lvlError,
+              (SysError (format ("killing process %1%") % pid).msg ()));
 
-Pid::operator pid_t()
-{
-    return pid;
-}
-
-
-void Pid::kill(bool quiet)
-{
-    if (pid == -1 || pid == 0) return;
-
-    if (!quiet)
-        printMsg(lvlError, format("killing process %1%") % pid);
-
-    /* Send the requested signal to the child.  If it has its own
-       process group, send the signal to every process in the child
-       process group (which hopefully includes *all* its children). */
-    if (::kill(separatePG ? -pid : pid, killSignal) != 0)
-        printMsg(lvlError, (SysError(format("killing process %1%") % pid).msg()));
-
-    /* Wait until the child dies, disregarding the exit status. */
-    int status;
-    while (waitpid(pid, &status, 0) == -1) {
-        checkInterrupt();
-        if (errno != EINTR) {
-            printMsg(lvlError,
-                (SysError(format("waiting for process %1%") % pid).msg()));
-            break;
+  /* Wait until the child dies, disregarding the exit status. */
+  int status;
+  while (waitpid (pid, &status, 0) == -1)
+    {
+      checkInterrupt ();
+      if (errno != EINTR)
+        {
+          printMsg (
+              lvlError,
+              (SysError (format ("waiting for process %1%") % pid).msg ()));
+          break;
         }
     }
 
-    pid = -1;
+  pid = -1;
 }
 
-
-int Pid::wait(bool block)
+int
+Pid::wait (bool block)
 {
-    assert(pid != -1);
-    while (1) {
-        int status;
-        int res = waitpid(pid, &status, block ? 0 : WNOHANG);
-        if (res == pid) {
-            pid = -1;
-            return status;
+  assert (pid != -1);
+  while (1)
+    {
+      int status;
+      int res = waitpid (pid, &status, block ? 0 : WNOHANG);
+      if (res == pid)
+        {
+          pid = -1;
+          return status;
         }
-        if (res == 0 && !block) return -1;
-        if (errno != EINTR)
-            throw SysError("cannot get child exit status");
-        checkInterrupt();
+      if (res == 0 && !block)
+        return -1;
+      if (errno != EINTR)
+        throw SysError ("cannot get child exit status");
+      checkInterrupt ();
     }
 }
 
-
-void Pid::setSeparatePG(bool separatePG)
+void
+Pid::setSeparatePG (bool separatePG)
 {
-    this->separatePG = separatePG;
+  this->separatePG = separatePG;
 }
 
-
-void Pid::setKillSignal(int signal)
+void
+Pid::setKillSignal (int signal)
 {
-    this->killSignal = signal;
+  this->killSignal = signal;
 }
 
-
-void killUser(uid_t uid)
+void
+killUser (uid_t uid)
 {
-    debug(format("killing all processes running under uid `%1%'") % uid);
+  debug (format ("killing all processes running under uid `%1%'") % uid);
 
-    assert(uid != 0); /* just to be safe... */
+  assert (uid != 0); /* just to be safe... */
 
-    /* The system call kill(-1, sig) sends the signal `sig' to all
-       users to which the current process can send signals.  So we
-       fork a process, switch to uid, and send a mass kill. */
+  /* The system call kill(-1, sig) sends the signal `sig' to all
+     users to which the current process can send signals.  So we
+     fork a process, switch to uid, and send a mass kill. */
 
-    Pid pid = startProcess([&]() {
+  Pid pid = startProcess ([&] () {
+    if (setuid (uid) == -1)
+      throw SysError ("setting uid");
 
-        if (setuid(uid) == -1)
-            throw SysError("setting uid");
-
-        while (true) {
+    while (true)
+      {
 #ifdef __APPLE__
-            /* OSX's kill syscall takes a third parameter that, among
-               other things, determines if kill(-1, signo) affects the
-               calling process. In the OSX libc, it's set to true,
-               which means "follow POSIX", which we don't want here
-                 */
-            if (syscall(SYS_kill, -1, SIGKILL, false) == 0) break;
+        /* OSX's kill syscall takes a third parameter that, among
+           other things, determines if kill(-1, signo) affects the
+           calling process. In the OSX libc, it's set to true,
+           which means "follow POSIX", which we don't want here
+             */
+        if (syscall (SYS_kill, -1, SIGKILL, false) == 0)
+          break;
 #elif __GNU__
-            /* Killing all a user's processes using PID=-1 does currently
-               not work on the Hurd.  */
-            if (kill(getpid(), SIGKILL) == 0) break;
+        /* Killing all a user's processes using PID=-1 does currently
+           not work on the Hurd.  */
+        if (kill (getpid (), SIGKILL) == 0)
+          break;
 #else
-            if (kill(-1, SIGKILL) == 0) break;
+        if (kill (-1, SIGKILL) == 0)
+          break;
 #endif
-            if (errno == ESRCH) break; /* no more processes */
-            if (errno != EINTR)
-                throw SysError(format("cannot kill processes for uid `%1%'") % uid);
-        }
+        if (errno == ESRCH)
+          break; /* no more processes */
+        if (errno != EINTR)
+          throw SysError (format ("cannot kill processes for uid `%1%'")
+                          % uid);
+      }
 
-        _exit(0);
-    });
+    _exit (0);
+  });
 
-    int status = pid.wait(true);
+  int status = pid.wait (true);
 #if __GNU__
-    /* When the child killed itself, status = SIGKILL.  */
-    if (status == SIGKILL) return;
+  /* When the child killed itself, status = SIGKILL.  */
+  if (status == SIGKILL)
+    return;
 #endif
-    if (status != 0)
-        throw Error(format("cannot kill processes for uid `%1%': %2%") % uid % statusToString(status));
+  if (status != 0)
+    throw Error (format ("cannot kill processes for uid `%1%': %2%") % uid
+                 % statusToString (status));
 
-    /* !!! We should really do some check to make sure that there are
-       no processes left running under `uid', but there is no portable
-       way to do so (I think).  The most reliable way may be `ps -eo
-       uid | grep -q $uid'. */
+  /* !!! We should really do some check to make sure that there are
+     no processes left running under `uid', but there is no portable
+     way to do so (I think).  The most reliable way may be `ps -eo
+     uid | grep -q $uid'. */
 }
 
-
 //////////////////////////////////////////////////////////////////////
 
-
-pid_t startProcess(std::function<void()> fun,
-    bool dieWithParent, const string & errorPrefix, bool runExitHandlers)
+pid_t
+startProcess (std::function<void ()> fun, bool dieWithParent,
+              const string &errorPrefix, bool runExitHandlers)
 {
-    pid_t pid = fork();
-    if (pid == -1) throw SysError("unable to fork");
+  pid_t pid = fork ();
+  if (pid == -1)
+    throw SysError ("unable to fork");
 
-    if (pid == 0) {
-        _writeToStderr = 0;
-        try {
+  if (pid == 0)
+    {
+      _writeToStderr = 0;
+      try
+        {
 #if __linux__
-            if (dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)
-                throw SysError("setting death signal");
+          if (dieWithParent && prctl (PR_SET_PDEATHSIG, SIGKILL) == -1)
+            throw SysError ("setting death signal");
 #endif
-            restoreAffinity();
-            fun();
-        } catch (std::exception & e) {
-            try {
-                std::cerr << errorPrefix << e.what() << "\n";
-            } catch (...) { }
-        } catch (...) { }
-        if (runExitHandlers)
-            exit(1);
-        else
-            _exit(1);
+          restoreAffinity ();
+          fun ();
+        }
+      catch (std::exception &e)
+        {
+          try
+            {
+              std::cerr << errorPrefix << e.what () << "\n";
+            }
+          catch (...)
+            {
+            }
+        }
+      catch (...)
+        {
+        }
+      if (runExitHandlers)
+        exit (1);
+      else
+        _exit (1);
     }
 
-    return pid;
+  return pid;
 }
 
-
-std::vector<char *> stringsToCharPtrs(const Strings & ss)
+std::vector<char *>
+stringsToCharPtrs (const Strings &ss)
 {
-    std::vector<char *> res;
-    for (auto & s : ss) res.push_back((char *) s.c_str());
-    res.push_back(0);
-    return res;
+  std::vector<char *> res;
+  for (auto &s : ss)
+    res.push_back ((char *)s.c_str ());
+  res.push_back (0);
+  return res;
 }
 
-
-string runProgram(Path program, bool searchPath, const Strings & args)
+string
+runProgram (Path program, bool searchPath, const Strings &args)
 {
-    checkInterrupt();
+  checkInterrupt ();
 
-    /* Create a pipe. */
-    Pipe pipe;
-    pipe.create();
+  /* Create a pipe. */
+  Pipe pipe;
+  pipe.create ();
 
-    /* Fork. */
-    Pid pid = startProcess([&]() {
-        if (dup2(pipe.writeSide, STDOUT_FILENO) == -1)
-            throw SysError("dupping stdout");
+  /* Fork. */
+  Pid pid = startProcess ([&] () {
+    if (dup2 (pipe.writeSide, STDOUT_FILENO) == -1)
+      throw SysError ("dupping stdout");
 
-        Strings args_(args);
-        args_.push_front(program);
+    Strings args_ (args);
+    args_.push_front (program);
 
-        if (searchPath)
-            execvp(program.c_str(), stringsToCharPtrs(args_).data());
-        else
-            execv(program.c_str(), stringsToCharPtrs(args_).data());
+    if (searchPath)
+      execvp (program.c_str (), stringsToCharPtrs (args_).data ());
+    else
+      execv (program.c_str (), stringsToCharPtrs (args_).data ());
 
-        throw SysError(format("executing `%1%'") % program);
-    });
+    throw SysError (format ("executing `%1%'") % program);
+  });
 
-    pipe.writeSide.close();
+  pipe.writeSide.close ();
 
-    string result = drainFD(pipe.readSide);
+  string result = drainFD (pipe.readSide);
 
-    /* Wait for the child to finish. */
-    int status = pid.wait(true);
-    if (!statusOk(status))
-        throw ExecError(format("program `%1%' %2%")
-            % program % statusToString(status));
+  /* Wait for the child to finish. */
+  int status = pid.wait (true);
+  if (!statusOk (status))
+    throw ExecError (format ("program `%1%' %2%") % program
+                     % statusToString (status));
 
-    return result;
+  return result;
 }
 
-
-void closeMostFDs(const set<int> & exceptions)
+void
+closeMostFDs (const set<int> &exceptions)
 {
 #ifdef HAVE_CLOSE_RANGE
-    if (exceptions.empty())
-	 close_range(3, ~0U, 0);
-    else
+  if (exceptions.empty ())
+    close_range (3, ~0U, 0);
+  else
 #endif
     {
-	 int maxFD = 0;
-	 maxFD = sysconf(_SC_OPEN_MAX);
-	 for (int fd = 0; fd < maxFD; ++fd)
-	      if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO
-		  && exceptions.find(fd) == exceptions.end())
-		   close(fd); /* ignore result */
+      int maxFD = 0;
+      maxFD = sysconf (_SC_OPEN_MAX);
+      for (int fd = 0; fd < maxFD; ++fd)
+        if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO
+            && exceptions.find (fd) == exceptions.end ())
+          close (fd); /* ignore result */
     }
 }
 
-
-void closeOnExec(int fd)
+void
+closeOnExec (int fd)
 {
-    int prev;
-    if ((prev = fcntl(fd, F_GETFD, 0)) == -1 ||
-        fcntl(fd, F_SETFD, prev | FD_CLOEXEC) == -1)
-        throw SysError("setting close-on-exec flag");
+  int prev;
+  if ((prev = fcntl (fd, F_GETFD, 0)) == -1
+      || fcntl (fd, F_SETFD, prev | FD_CLOEXEC) == -1)
+    throw SysError ("setting close-on-exec flag");
 }
 
-
 //////////////////////////////////////////////////////////////////////
 
-
 volatile sig_atomic_t _isInterrupted = 0;
 
-void _interrupted()
+void
+_interrupted ()
 {
-    /* Block user interrupts while an exception is being handled.
-       Throwing an exception while another exception is being handled
-       kills the program! */
-    if (!std::uncaught_exception()) {
-        _isInterrupted = 0;
-        throw Interrupted("interrupted by the user");
+  /* Block user interrupts while an exception is being handled.
+     Throwing an exception while another exception is being handled
+     kills the program! */
+  if (!std::uncaught_exception ())
+    {
+      _isInterrupted = 0;
+      throw Interrupted ("interrupted by the user");
     }
 }
 
-
-
 //////////////////////////////////////////////////////////////////////
 
-
-template<class C> C tokenizeString(const string & s, const string & separators)
+template <class C>
+C
+tokenizeString (const string &s, const string &separators)
 {
-    C result;
-    string::size_type pos = s.find_first_not_of(separators, 0);
-    while (pos != string::npos) {
-        string::size_type end = s.find_first_of(separators, pos + 1);
-        if (end == string::npos) end = s.size();
-        string token(s, pos, end - pos);
-        result.insert(result.end(), token);
-        pos = s.find_first_not_of(separators, end);
+  C result;
+  string::size_type pos = s.find_first_not_of (separators, 0);
+  while (pos != string::npos)
+    {
+      string::size_type end = s.find_first_of (separators, pos + 1);
+      if (end == string::npos)
+        end = s.size ();
+      string token (s, pos, end - pos);
+      result.insert (result.end (), token);
+      pos = s.find_first_not_of (separators, end);
     }
-    return result;
+  return result;
 }
 
-template Strings tokenizeString(const string & s, const string & separators);
-template StringSet tokenizeString(const string & s, const string & separators);
-template vector<string> tokenizeString(const string & s, const string & separators);
-
+template Strings tokenizeString (const string &s, const string &separators);
+template StringSet tokenizeString (const string &s, const string &separators);
+template vector<string> tokenizeString (const string &s,
+                                        const string &separators);
 
-string concatStringsSep(const string & sep, const Strings & ss)
+string
+concatStringsSep (const string &sep, const Strings &ss)
 {
-    string s;
-    foreach (Strings::const_iterator, i, ss) {
-        if (s.size() != 0) s += sep;
-        s += *i;
+  string s;
+  foreach (Strings::const_iterator, i, ss)
+    {
+      if (s.size () != 0)
+        s += sep;
+      s += *i;
     }
-    return s;
+  return s;
 }
 
-
-string concatStringsSep(const string & sep, const StringSet & ss)
+string
+concatStringsSep (const string &sep, const StringSet &ss)
 {
-    string s;
-    foreach (StringSet::const_iterator, i, ss) {
-        if (s.size() != 0) s += sep;
-        s += *i;
+  string s;
+  foreach (StringSet::const_iterator, i, ss)
+    {
+      if (s.size () != 0)
+        s += sep;
+      s += *i;
     }
-    return s;
+  return s;
 }
 
-
-string chomp(const string & s)
+string
+chomp (const string &s)
 {
-    size_t i = s.find_last_not_of(" \n\r\t");
-    return i == string::npos ? "" : string(s, 0, i + 1);
+  size_t i = s.find_last_not_of (" \n\r\t");
+  return i == string::npos ? "" : string (s, 0, i + 1);
 }
 
-
-string statusToString(int status)
+string
+statusToString (int status)
 {
-    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
-        if (WIFEXITED(status))
-            return (format("failed with exit code %1%") % WEXITSTATUS(status)).str();
-        else if (WIFSIGNALED(status)) {
-            int sig = WTERMSIG(status);
+  if (!WIFEXITED (status) || WEXITSTATUS (status) != 0)
+    {
+      if (WIFEXITED (status))
+        return (format ("failed with exit code %1%") % WEXITSTATUS (status))
+            .str ();
+      else if (WIFSIGNALED (status))
+        {
+          int sig = WTERMSIG (status);
 #if HAVE_STRSIGNAL
-            const char * description = strsignal(sig);
-            return (format("failed due to signal %1% (%2%)") % sig % description).str();
+          const char *description = strsignal (sig);
+          return (format ("failed due to signal %1% (%2%)") % sig
+                  % description)
+              .str ();
 #else
-            return (format("failed due to signal %1%") % sig).str();
+          return (format ("failed due to signal %1%") % sig).str ();
 #endif
         }
-        else
-            return "died abnormally";
-    } else return "succeeded";
+      else
+        return "died abnormally";
+    }
+  else
+    return "succeeded";
 }
 
-
-bool statusOk(int status)
+bool
+statusOk (int status)
 {
-    return WIFEXITED(status) && WEXITSTATUS(status) == 0;
+  return WIFEXITED (status) && WEXITSTATUS (status) == 0;
 }
 
-
-bool hasSuffix(const string & s, const string & suffix)
+bool
+hasSuffix (const string &s, const string &suffix)
 {
-    return s.size() >= suffix.size() && string(s, s.size() - suffix.size()) == suffix;
+  return s.size () >= suffix.size ()
+         && string (s, s.size () - suffix.size ()) == suffix;
 }
 
-
-void expect(std::istream & str, const string & s)
+void
+expect (std::istream &str, const string &s)
 {
-    char s2[s.size()];
-    str.read(s2, s.size());
-    if (string(s2, s.size()) != s)
-        throw FormatError(format("expected string `%1%'") % s);
+  char s2[s.size ()];
+  str.read (s2, s.size ());
+  if (string (s2, s.size ()) != s)
+    throw FormatError (format ("expected string `%1%'") % s);
 }
 
-
-string parseString(std::istream & str)
+string
+parseString (std::istream &str)
 {
-    string res;
-    expect(str, "\"");
-    int c;
-    while ((c = str.get()) != '"')
-        if (c == '\\') {
-            c = str.get();
-            if (c == 'n') res += '\n';
-            else if (c == 'r') res += '\r';
-            else if (c == 't') res += '\t';
-            else res += c;
-        }
-        else res += c;
-    return res;
+  string res;
+  expect (str, "\"");
+  int c;
+  while ((c = str.get ()) != '"')
+    if (c == '\\')
+      {
+        c = str.get ();
+        if (c == 'n')
+          res += '\n';
+        else if (c == 'r')
+          res += '\r';
+        else if (c == 't')
+          res += '\t';
+        else
+          res += c;
+      }
+    else
+      res += c;
+  return res;
 }
 
-
-bool endOfList(std::istream & str)
+bool
+endOfList (std::istream &str)
 {
-    if (str.peek() == ',') {
-        str.get();
-        return false;
+  if (str.peek () == ',')
+    {
+      str.get ();
+      return false;
     }
-    if (str.peek() == ']') {
-        str.get();
-        return true;
+  if (str.peek () == ']')
+    {
+      str.get ();
+      return true;
     }
-    return false;
+  return false;
 }
 
-
-void ignoreException()
+void
+ignoreException ()
 {
-    try {
-        throw;
-    } catch (std::exception & e) {
-        printMsg(lvlError, format("error (ignored): %1%") % e.what());
+  try
+    {
+      throw;
+    }
+  catch (std::exception &e)
+    {
+      printMsg (lvlError, format ("error (ignored): %1%") % e.what ());
     }
 }
 
 static const string pathNullDevice = "/dev/null";
 
 /* Common initialisation performed in child processes. */
-void commonChildInit(Pipe & logPipe)
+void
+commonChildInit (Pipe &logPipe)
 {
-    /* Put the child in a separate session (and thus a separate
-       process group) so that it has no controlling terminal (meaning
-       that e.g. ssh cannot open /dev/tty) and it doesn't receive
-       terminal signals. */
-    if (setsid() == -1)
-        throw SysError(format("creating a new session"));
+  /* Put the child in a separate session (and thus a separate
+     process group) so that it has no controlling terminal (meaning
+     that e.g. ssh cannot open /dev/tty) and it doesn't receive
+     terminal signals. */
+  if (setsid () == -1)
+    throw SysError (format ("creating a new session"));
 
-    /* Close the read end so only the parent holds a reference to it.  */
-    logPipe.readSide.close();
+  /* Close the read end so only the parent holds a reference to it.  */
+  logPipe.readSide.close ();
 
-    /* Dup the write side of the logger pipe into stderr. */
-    if (dup2(logPipe.writeSide, STDERR_FILENO) == -1)
-        throw SysError("cannot pipe standard error into log file");
+  /* Dup the write side of the logger pipe into stderr. */
+  if (dup2 (logPipe.writeSide, STDERR_FILENO) == -1)
+    throw SysError ("cannot pipe standard error into log file");
 
-    /* Dup stderr to stdout. */
-    if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
-        throw SysError("cannot dup stderr into stdout");
+  /* Dup stderr to stdout. */
+  if (dup2 (STDERR_FILENO, STDOUT_FILENO) == -1)
+    throw SysError ("cannot dup stderr into stdout");
 
-    /* Reroute stdin to /dev/null. */
-    int fdDevNull = open(pathNullDevice.c_str(), O_RDWR);
-    if (fdDevNull == -1)
-        throw SysError(format("cannot open `%1%'") % pathNullDevice);
-    if (dup2(fdDevNull, STDIN_FILENO) == -1)
-        throw SysError("cannot dup null device into stdin");
-    close(fdDevNull);
+  /* Reroute stdin to /dev/null. */
+  int fdDevNull = open (pathNullDevice.c_str (), O_RDWR);
+  if (fdDevNull == -1)
+    throw SysError (format ("cannot open `%1%'") % pathNullDevice);
+  if (dup2 (fdDevNull, STDIN_FILENO) == -1)
+    throw SysError ("cannot dup null device into stdin");
+  close (fdDevNull);
 }
 
 //////////////////////////////////////////////////////////////////////
 
-Agent::Agent(const string &command, const Strings &args, const std::map<string, string> &env)
+Agent::Agent (const string &command, const Strings &args,
+              const std::map<string, string> &env)
 {
-    debug(format("starting agent '%1%'") % command);
+  debug (format ("starting agent '%1%'") % command);
 
-    /* Create a pipe to get the output of the child. */
-    fromAgent.create();
+  /* Create a pipe to get the output of the child. */
+  fromAgent.create ();
 
-    /* Create the communication pipes. */
-    toAgent.create();
+  /* Create the communication pipes. */
+  toAgent.create ();
 
-    /* Create a pipe to get the output of the builder. */
-    builderOut.create();
+  /* Create a pipe to get the output of the builder. */
+  builderOut.create ();
 
-    /* Fork the hook. */
-    pid = startProcess([&]() {
+  /* Fork the hook. */
+  pid = startProcess ([&] () {
+    commonChildInit (fromAgent);
 
-        commonChildInit(fromAgent);
+    for (auto pair : env)
+      {
+        setenv (pair.first.c_str (), pair.second.c_str (), 1);
+      }
 
-	for (auto pair: env) {
-	    setenv(pair.first.c_str(), pair.second.c_str(), 1);
-	}
+    if (chdir ("/") == -1)
+      throw SysError ("changing into `/");
 
-        if (chdir("/") == -1) throw SysError("changing into `/");
+    /* Dup the communication pipes. */
+    if (dup2 (toAgent.readSide, STDIN_FILENO) == -1)
+      throw SysError ("dupping to-hook read side");
 
-        /* Dup the communication pipes. */
-        if (dup2(toAgent.readSide, STDIN_FILENO) == -1)
-            throw SysError("dupping to-hook read side");
+    /* Use fd 4 for the builder's stdout/stderr. */
+    if (dup2 (builderOut.writeSide, 4) == -1)
+      throw SysError ("dupping builder's stdout/stderr");
 
-        /* Use fd 4 for the builder's stdout/stderr. */
-        if (dup2(builderOut.writeSide, 4) == -1)
-            throw SysError("dupping builder's stdout/stderr");
+    Strings allArgs;
+    allArgs.push_back (command);
+    allArgs.insert (allArgs.end (), args.begin (), args.end ()); // append
 
-	Strings allArgs;
-	allArgs.push_back(command);
-	allArgs.insert(allArgs.end(), args.begin(), args.end()); // append
+    execv (command.c_str (), stringsToCharPtrs (allArgs).data ());
 
-        execv(command.c_str(), stringsToCharPtrs(allArgs).data());
+    throw SysError (format ("executing `%1%'") % command);
+  });
 
-        throw SysError(format("executing `%1%'") % command);
-    });
-
-    pid.setSeparatePG(true);
-    fromAgent.writeSide.close();
-    toAgent.readSide.close();
+  pid.setSeparatePG (true);
+  fromAgent.writeSide.close ();
+  toAgent.readSide.close ();
 }
 
-
-Agent::~Agent()
+Agent::~Agent ()
 {
-    try {
-        toAgent.writeSide.close();
-        pid.kill(true);
-    } catch (...) {
-        ignoreException();
+  try
+    {
+      toAgent.writeSide.close ();
+      pid.kill (true);
+    }
+  catch (...)
+    {
+      ignoreException ();
     }
 }
 
-
-}
+} // namespace nix
diff --git a/nix/libutil/util.hh b/nix/libutil/util.hh
index 377aac0684..46836eba08 100644
--- a/nix/libutil/util.hh
+++ b/nix/libutil/util.hh
@@ -2,141 +2,141 @@
 
 #include "types.hh"
 
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <dirent.h>
-#include <unistd.h>
-#include <signal.h>
-#include <map>
 #include <functional>
+#include <map>
+#include <signal.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include <cstdio>
 
+namespace nix
+{
 
-namespace nix {
-
-
-#define foreach(it_type, it, collection)                                \
-    for (it_type it = (collection).begin(); it != (collection).end(); ++it)
-
-#define foreach_reverse(it_type, it, collection)                                \
-    for (it_type it = (collection).rbegin(); it != (collection).rend(); ++it)
+#define foreach(it_type, it, collection)                                      \
+  for (it_type it = (collection).begin (); it != (collection).end (); ++it)
 
+#define foreach_reverse(it_type, it, collection)                              \
+  for (it_type it = (collection).rbegin (); it != (collection).rend (); ++it)
 
 /* Return an environment variable. */
-string getEnv(const string & key, const string & def = "");
+string getEnv (const string &key, const string &def = "");
 
 /* Return an absolutized path, resolving paths relative to the
    specified directory, or the current directory otherwise.  The path
    is also canonicalised. */
-Path absPath(Path path, Path dir = "");
+Path absPath (Path path, Path dir = "");
 
 /* Canonicalise a path by removing all `.' or `..' components and
    double or trailing slashes.  Optionally resolves all symlink
    components such that each component of the resulting path is *not*
    a symbolic link. */
-Path canonPath(const Path & path, bool resolveSymlinks = false);
+Path canonPath (const Path &path, bool resolveSymlinks = false);
 
 /* Return the directory part of the given canonical path, i.e.,
    everything before the final `/'.  If the path is the root or an
    immediate child thereof (e.g., `/foo'), this means an empty string
    is returned. */
-Path dirOf(const Path & path);
+Path dirOf (const Path &path);
 
 /* Return the base name of the given canonical path, i.e., everything
    following the final `/'. */
-string baseNameOf(const Path & path);
+string baseNameOf (const Path &path);
 
 /* Check whether a given path is a descendant of the given
    directory. */
-bool isInDir(const Path & path, const Path & dir);
+bool isInDir (const Path &path, const Path &dir);
 
 /* Get status of `path'. */
-struct stat lstat(const Path & path);
+struct stat lstat (const Path &path);
 
 /* Return true iff the given path exists. */
-bool pathExists(const Path & path);
+bool pathExists (const Path &path);
 
 /* Read the contents (target) of a symbolic link.  The result is not
    in any way canonicalised. */
-Path readLink(const Path & path);
+Path readLink (const Path &path);
 
-bool isLink(const Path & path);
+bool isLink (const Path &path);
 
 /* Read the contents of a directory.  The entries `.' and `..' are
    removed. */
 struct DirEntry
 {
-    string name;
-    ino_t ino;
-    unsigned char type; // one of DT_*
-    DirEntry(const string & name, ino_t ino, unsigned char type)
-        : name(name), ino(ino), type(type) { }
+  string name;
+  ino_t ino;
+  unsigned char type; // one of DT_*
+  DirEntry (const string &name, ino_t ino, unsigned char type)
+      : name (name), ino (ino), type (type)
+  {
+  }
 };
 
 typedef vector<DirEntry> DirEntries;
 
-DirEntries readDirectory(const Path & path);
+DirEntries readDirectory (const Path &path);
 
-unsigned char getFileType(const Path & path);
+unsigned char getFileType (const Path &path);
 
 /* Read the contents of a file into a string. */
-string readFile(int fd);
-string readFile(const Path & path, bool drain = false);
+string readFile (int fd);
+string readFile (const Path &path, bool drain = false);
 
 /* Write a string to a file. */
-void writeFile(const Path & path, const string & s);
+void writeFile (const Path &path, const string &s);
 
 /* Read a line from a file descriptor. */
-string readLine(int fd);
+string readLine (int fd);
 
 /* Write a line to a file descriptor. */
-void writeLine(int fd, string s);
+void writeLine (int fd, string s);
 
 /* Delete a path; i.e., in the case of a directory, it is deleted
    recursively.  Don't use this at home, kids.  The second variant
    returns the number of bytes and blocks freed, and 'linkThreshold' denotes
    the number of links under which a file is accounted for in 'bytesFreed'.  */
-void deletePath(const Path & path);
+void deletePath (const Path &path);
 
-void deletePath(const Path & path, unsigned long long & bytesFreed,
-    size_t linkThreshold = 1);
+void deletePath (const Path &path, unsigned long long &bytesFreed,
+                 size_t linkThreshold = 1);
 
 /* Copy SOURCE to DESTINATION, recursively, preserving ownership.  Throw if
    SOURCE contains a file that is not a regular file, symlink, or directory.
    When DELETESOURCE is true, delete source files once they have been
    copied.  */
-void copyFileRecursively(const Path &source, const Path &destination,
-    bool deleteSource = false);
+void copyFileRecursively (const Path &source, const Path &destination,
+                          bool deleteSource = false);
 
 /* Create a temporary directory. */
-Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
-    bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
+Path createTempDir (const Path &tmpRoot = "", const Path &prefix = "nix",
+                    bool includePid = true, bool useGlobalCounter = true,
+                    mode_t mode = 0755);
 
 /* Create a directory and all its parents, if necessary.  Returns the
    list of created directories, in order of creation. */
-Paths createDirs(const Path & path);
+Paths createDirs (const Path &path);
 
 /* Create a symlink. */
-void createSymlink(const Path & target, const Path & link);
-
+void createSymlink (const Path &target, const Path &link);
 
-template<class T, class A>
-T singleton(const A & a)
+template <class T, class A>
+T
+singleton (const A &a)
 {
-    T t;
-    t.insert(a);
-    return t;
+  T t;
+  t.insert (a);
+  return t;
 }
 
-
 /* Messages. */
 
-
-typedef enum {
-    ltPretty,   /* nice, nested output */
-    ltEscapes,  /* nesting indicated using escape codes (for log2xml) */
-    ltFlat      /* no nesting */
+typedef enum
+{
+  ltPretty,  /* nice, nested output */
+  ltEscapes, /* nesting indicated using escape codes (for log2xml) */
+  ltFlat     /* no nesting */
 } LogType;
 
 extern LogType logType;
@@ -145,257 +145,243 @@ extern Verbosity verbosity; /* suppress msgs > this */
 class Nest
 {
 private:
-    bool nest;
+  bool nest;
+
 public:
-    Nest();
-    ~Nest();
-    void open(Verbosity level, const FormatOrString & fs);
-    void close();
+  Nest ();
+  ~Nest ();
+  void open (Verbosity level, const FormatOrString &fs);
+  void close ();
 };
 
-void printMsg_(Verbosity level, const FormatOrString & fs);
+void printMsg_ (Verbosity level, const FormatOrString &fs);
 
-#define startNest(varName, level, f) \
-    Nest varName; \
-    if (level <= verbosity) { \
-      varName.open(level, (f)); \
+#define startNest(varName, level, f)                                          \
+  Nest varName;                                                               \
+  if (level <= verbosity)                                                     \
+    {                                                                         \
+      varName.open (level, (f));                                              \
     }
 
-#define printMsg(level, f) \
-    do { \
-        if (level <= nix::verbosity) { \
-            nix::printMsg_(level, (f)); \
-        } \
-    } while (0)
-
-#define debug(f) printMsg(lvlDebug, f)
+#define printMsg(level, f)                                                    \
+  do                                                                          \
+    {                                                                         \
+      if (level <= nix::verbosity)                                            \
+        {                                                                     \
+          nix::printMsg_ (level, (f));                                        \
+        }                                                                     \
+    }                                                                         \
+  while (0)
 
-void warnOnce(bool & haveWarned, const FormatOrString & fs);
+#define debug(f) printMsg (lvlDebug, f)
 
-void writeToStderr(const string & s);
+void warnOnce (bool &haveWarned, const FormatOrString &fs);
 
-extern void (*_writeToStderr) (const unsigned char * buf, size_t count);
+void writeToStderr (const string &s);
 
+extern void (*_writeToStderr) (const unsigned char *buf, size_t count);
 
 /* Wrappers arount read()/write() that read/write exactly the
    requested number of bytes. */
-void readFull(int fd, unsigned char * buf, size_t count);
-void writeFull(int fd, const unsigned char * buf, size_t count);
-void writeFull(int fd, const string & s);
-
-MakeError(EndOfFile, Error)
-
-
-/* Read a file descriptor until EOF occurs. */
-string drainFD(int fd);
+void readFull (int fd, unsigned char *buf, size_t count);
+void writeFull (int fd, const unsigned char *buf, size_t count);
+void writeFull (int fd, const string &s);
 
+MakeError (EndOfFile, Error)
 
+    /* Read a file descriptor until EOF occurs. */
+    string drainFD (int fd);
 
 /* Automatic cleanup of resources. */
 
-
-template <class T>
-struct AutoDeleteArray
+template <class T> struct AutoDeleteArray
 {
-    T * p;
-    AutoDeleteArray(T * p) : p(p) { }
-    ~AutoDeleteArray()
-    {
-        delete [] p;
-    }
+  T *p;
+  AutoDeleteArray (T *p) : p (p) {}
+  ~AutoDeleteArray () { delete[] p; }
 };
 
-
 class AutoDelete
 {
-    Path path;
-    bool del;
-    bool recursive;
+  Path path;
+  bool del;
+  bool recursive;
+
 public:
-    AutoDelete(const Path & p, bool recursive = true);
-    ~AutoDelete();
-    void cancel();
+  AutoDelete (const Path &p, bool recursive = true);
+  ~AutoDelete ();
+  void cancel ();
 };
 
-
 class AutoCloseFD
 {
-    int fd;
+  int fd;
+
 public:
-    AutoCloseFD();
-    AutoCloseFD(int fd);
-    AutoCloseFD(const AutoCloseFD & fd);
-    ~AutoCloseFD();
-    void operator =(int fd);
-    operator int() const;
-    void close();
-    bool isOpen();
-    int borrow();
+  AutoCloseFD ();
+  AutoCloseFD (int fd);
+  AutoCloseFD (const AutoCloseFD &fd);
+  ~AutoCloseFD ();
+  void operator= (int fd);
+  operator int () const;
+  void close ();
+  bool isOpen ();
+  int borrow ();
 };
 
-
 class Pipe
 {
 public:
-    AutoCloseFD readSide, writeSide;
-    void create();
+  AutoCloseFD readSide, writeSide;
+  void create ();
 };
 
-
 class AutoCloseDir
 {
-    DIR * dir;
+  DIR *dir;
+
 public:
-    AutoCloseDir();
-    AutoCloseDir(DIR * dir);
-    ~AutoCloseDir();
-    void operator =(DIR * dir);
-    operator DIR *();
-    void close();
+  AutoCloseDir ();
+  AutoCloseDir (DIR *dir);
+  ~AutoCloseDir ();
+  void operator= (DIR *dir);
+  operator DIR * ();
+  void close ();
 };
 
-
 class Pid
 {
-    pid_t pid;
-    bool separatePG;
-    int killSignal;
+  pid_t pid;
+  bool separatePG;
+  int killSignal;
+
 public:
-    Pid();
-    Pid(pid_t pid);
-    ~Pid();
-    void operator =(pid_t pid);
-    operator pid_t();
-    void kill(bool quiet = false);
-    int wait(bool block);
-    void setSeparatePG(bool separatePG);
-    void setKillSignal(int signal);
+  Pid ();
+  Pid (pid_t pid);
+  ~Pid ();
+  void operator= (pid_t pid);
+  operator pid_t ();
+  void kill (bool quiet = false);
+  int wait (bool block);
+  void setSeparatePG (bool separatePG);
+  void setKillSignal (int signal);
 };
 
 /* An "agent" is a helper program that runs in the background and that we talk
    to over pipes, such as the "guix offload" program.  */
 struct Agent
 {
-    /* Pipes for talking to the agent. */
-    Pipe toAgent;
+  /* Pipes for talking to the agent. */
+  Pipe toAgent;
 
-    /* Pipe for the agent's standard output/error. */
-    Pipe fromAgent;
+  /* Pipe for the agent's standard output/error. */
+  Pipe fromAgent;
 
-    /* Pipe for build standard output/error--e.g., for build processes started
-       by "guix offload".  */
-    Pipe builderOut;
+  /* Pipe for build standard output/error--e.g., for build processes started
+     by "guix offload".  */
+  Pipe builderOut;
 
-    /* The process ID of the agent. */
-    Pid pid;
+  /* The process ID of the agent. */
+  Pid pid;
 
-    /* The command and arguments passed to the agent along with a list of
-       environment variable name/value pairs.  */
-    Agent(const string &command, const Strings &args,
-	  const std::map<string, string> &env = std::map<string, string>());
+  /* The command and arguments passed to the agent along with a list of
+     environment variable name/value pairs.  */
+  Agent (const string &command, const Strings &args,
+         const std::map<string, string> &env = std::map<string, string> ());
 
-    ~Agent();
+  ~Agent ();
 };
 
-
 /* Kill all processes running under the specified uid by sending them
    a SIGKILL. */
-void killUser(uid_t uid);
-
+void killUser (uid_t uid);
 
 /* Fork a process that runs the given function, and return the child
    pid to the caller. */
-pid_t startProcess(std::function<void()> fun, bool dieWithParent = true,
-    const string & errorPrefix = "error: ", bool runExitHandlers = false);
-
+pid_t startProcess (std::function<void ()> fun, bool dieWithParent = true,
+                    const string &errorPrefix = "error: ",
+                    bool runExitHandlers = false);
 
 /* Run a program and return its stdout in a string (i.e., like the
    shell backtick operator). */
-string runProgram(Path program, bool searchPath = false,
-    const Strings & args = Strings());
+string runProgram (Path program, bool searchPath = false,
+                   const Strings &args = Strings ());
 
-MakeError(ExecError, Error)
+MakeError (ExecError, Error)
 
-/* Convert a list of strings to a null-terminated vector of char
-   *'s. The result must not be accessed beyond the lifetime of the
-   list of strings. */
-std::vector<char *> stringsToCharPtrs(const Strings & ss);
+    /* Convert a list of strings to a null-terminated vector of char
+       *'s. The result must not be accessed beyond the lifetime of the
+       list of strings. */
+    std::vector<char *> stringsToCharPtrs (const Strings &ss);
 
 /* Close all file descriptors except stdin, stdout, stderr, and those
    listed in the given set.  Good practice in child processes. */
-void closeMostFDs(const set<int> & exceptions);
+void closeMostFDs (const set<int> &exceptions);
 
 /* Set the close-on-exec flag for the given file descriptor. */
-void closeOnExec(int fd);
+void closeOnExec (int fd);
 
 /* Common initialisation performed in child processes. */
-void commonChildInit(Pipe & logPipe);
+void commonChildInit (Pipe &logPipe);
 
 /* User interruption. */
 
 extern volatile sig_atomic_t _isInterrupted;
 
-void _interrupted();
+void _interrupted ();
 
-void inline checkInterrupt()
+void inline checkInterrupt ()
 {
-    if (_isInterrupted) _interrupted();
+  if (_isInterrupted)
+    _interrupted ();
 }
 
-MakeError(Interrupted, BaseError)
-
-
-/* String tokenizer. */
-template<class C> C tokenizeString(const string & s, const string & separators = " \t\n\r");
+MakeError (Interrupted, BaseError)
 
+    /* String tokenizer. */
+    template <class C>
+    C tokenizeString (const string &s, const string &separators = " \t\n\r");
 
 /* Concatenate the given strings with a separator between the
    elements. */
-string concatStringsSep(const string & sep, const Strings & ss);
-string concatStringsSep(const string & sep, const StringSet & ss);
-
+string concatStringsSep (const string &sep, const Strings &ss);
+string concatStringsSep (const string &sep, const StringSet &ss);
 
 /* Remove trailing whitespace from a string. */
-string chomp(const string & s);
-
+string chomp (const string &s);
 
 /* Convert the exit status of a child as returned by wait() into an
    error string. */
-string statusToString(int status);
-
-bool statusOk(int status);
+string statusToString (int status);
 
+bool statusOk (int status);
 
 /* Parse a string into an integer. */
-template<class N> bool string2Int(const string & s, N & n)
+template <class N>
+bool
+string2Int (const string &s, N &n)
 {
-    std::istringstream str(s);
-    str >> n;
-    return str && str.get() == EOF;
+  std::istringstream str (s);
+  str >> n;
+  return str && str.get () == EOF;
 }
 
-
 /* Return true iff `s' ends in `suffix'. */
-bool hasSuffix(const string & s, const string & suffix);
-
+bool hasSuffix (const string &s, const string &suffix);
 
 /* Read string `s' from stream `str'. */
-void expect(std::istream & str, const string & s);
-
-MakeError(FormatError, Error)
+void expect (std::istream &str, const string &s);
 
+MakeError (FormatError, Error)
 
-/* Read a C-style string from stream `str'. */
-string parseString(std::istream & str);
-
+    /* Read a C-style string from stream `str'. */
+    string parseString (std::istream &str);
 
 /* Utility function used to parse legacy ATerms. */
-bool endOfList(std::istream & str);
-
+bool endOfList (std::istream &str);
 
 /* Exception handling in destructors: print an error message, then
    ignore the exception. */
-void ignoreException();
-
+void ignoreException ();
 
 }
diff --git a/nix/local.mk b/nix/local.mk
index 53938ff52a..daf99afd0d 100644
--- a/nix/local.mk
+++ b/nix/local.mk
@@ -28,7 +28,12 @@ CLEANFILES += %D%/libstore/schema.sql.hh
 noinst_LIBRARIES = libformat.a libutil.a libstore.a
 
 # Use '-std=c++11' for 'std::shared_ptr', 'auto', lambdas, and more.
-AM_CXXFLAGS = -Wall -std=c++11
+# Make all warnings fatal but disable some that are out of our control
+# like e.g. unused code in the boost subdirectory or Nix's usage of
+# variable-length arrays.
+AM_CXXFLAGS = -Wall -Werror -std=c++11  \
+  -Wno-unneeded-internal-declaration    \
+  -Wno-vla-cxx-extension
 
 libformat_a_SOURCES =				\
   %D%/boost/format/free_funcs.cc		\
diff --git a/nix/nix-daemon/guix-daemon.cc b/nix/nix-daemon/guix-daemon.cc
index d7ab9c5e64..9a333fde3a 100644
--- a/nix/nix-daemon/guix-daemon.cc
+++ b/nix/nix-daemon/guix-daemon.cc
@@ -19,24 +19,24 @@
 
 #include <config.h>
 
-#include <types.hh>
 #include "shared.hh"
 #include <globals.hh>
+#include <types.hh>
 #include <util.hh>
 
 #include <gcrypt.h>
 
-#include <stdlib.h>
 #include <argp.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <netdb.h>
-#include <strings.h>
 #include <exception>
 #include <iostream>
+#include <netdb.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <unistd.h>
 
 #include <libintl.h>
 #include <locale.h>
@@ -50,22 +50,19 @@ using namespace nix;
 /* Entry point in `nix-daemon.cc'.  */
 extern void run (const std::vector<int> &);
 
-
 /* Command-line options.  */
 
-#define n_(str)  str
-#define _(str)   gettext (str)
+#define n_(str) str
+#define _(str) gettext (str)
 static const char guix_textdomain[] = "guix";
 
-
-const char *argp_program_version =
-  "guix-daemon (" PACKAGE_NAME ") " PACKAGE_VERSION;
+const char *argp_program_version
+    = "guix-daemon (" PACKAGE_NAME ") " PACKAGE_VERSION;
 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
 
-static char doc[] =
-  n_("guix-daemon -- perform derivation builds and store accesses")
-  "\v\n"
-  n_("This program is a daemon meant to run in the background.  It serves \
+static char doc[] = n_ ("guix-daemon -- perform derivation builds and store "
+                        "accesses") "\v\n" n_ (
+    "This program is a daemon meant to run in the background.  It serves \
 requests sent over a Unix-domain socket.  It accesses the store, and \
 builds derivations on behalf of its clients.");
 
@@ -91,78 +88,71 @@ builds derivations on behalf of its clients.");
 #define GUIX_OPT_LOG_COMPRESSION 20
 #define GUIX_OPT_DISCOVER 21
 
-static const struct argp_option options[] =
-  {
-    { "system", GUIX_OPT_SYSTEM, n_("SYSTEM"), 0,
-      n_("assume SYSTEM as the current system type") },
-    { "cores", 'c', n_("N"), 0,
-      n_("use N CPU cores to build each derivation; 0 means as many as available")
-    },
-    { "max-jobs", 'M', n_("N"), 0,
-      n_("allow at most N build jobs") },
-    { "timeout", GUIX_OPT_TIMEOUT, n_("SECONDS"), 0,
-      n_("mark builds as failed after SECONDS of activity") },
-    { "max-silent-time", GUIX_OPT_MAX_SILENT_TIME, n_("SECONDS"), 0,
-      n_("mark builds as failed after SECONDS of silence") },
-    { "disable-chroot", GUIX_OPT_DISABLE_CHROOT, 0, 0,
-      n_("disable chroot builds") },
-    { "chroot-directory", GUIX_OPT_CHROOT_DIR, n_("DIR"), 0,
-      n_("add DIR to the build chroot") },
-    { "build-users-group", GUIX_OPT_BUILD_USERS_GROUP, n_("GROUP"), 0,
-      n_("perform builds as a user of GROUP") },
-    { "no-substitutes", GUIX_OPT_NO_SUBSTITUTES, 0, 0,
-      n_("do not use substitutes") },
-    { "substitute-urls", GUIX_OPT_SUBSTITUTE_URLS, n_("URLS"), 0,
-      n_("use URLS as the default list of substitute providers") },
-    { "no-offload", GUIX_OPT_NO_BUILD_HOOK, 0, 0,
-      n_("do not attempt to offload builds") },
-    { "no-build-hook", GUIX_OPT_NO_BUILD_HOOK, 0,
-      OPTION_HIDDEN,				  // deprecated
-      n_("do not attempt to offload builds") },
-    { "cache-failures", GUIX_OPT_CACHE_FAILURES, 0, 0,
-      n_("cache build failures") },
-    { "rounds", GUIX_OPT_BUILD_ROUNDS, "N", 0,
-      n_("build each derivation N times in a row") },
-    { "lose-logs", GUIX_OPT_LOSE_LOGS, 0, 0,
-      n_("do not keep build logs") },
-    { "disable-log-compression", GUIX_OPT_DISABLE_LOG_COMPRESSION, 0,
-      OPTION_HIDDEN,				  // deprecated
-      n_("disable compression of the build logs") },
-    { "log-compression", GUIX_OPT_LOG_COMPRESSION, "TYPE", 0,
-      n_("use the specified compression type for build logs") },
-    { "discover", GUIX_OPT_DISCOVER, "yes/no", OPTION_ARG_OPTIONAL,
-      n_("use substitute servers discovered on the local network") },
-
-    /* '--disable-deduplication' was known as '--disable-store-optimization'
-       up to Guix 0.7 included, so keep the alias around.  */
-    { "disable-deduplication", GUIX_OPT_DISABLE_DEDUPLICATION, 0, 0,
-      n_("disable automatic file \"deduplication\" in the store") },
-    { "disable-store-optimization", GUIX_OPT_DISABLE_DEDUPLICATION, 0,
-      OPTION_ALIAS | OPTION_HIDDEN, NULL },
-
-    { "impersonate-linux-2.6", GUIX_OPT_IMPERSONATE_LINUX_26, 0,
+static const struct argp_option options[] = {
+  { "system", GUIX_OPT_SYSTEM, n_ ("SYSTEM"), 0,
+    n_ ("assume SYSTEM as the current system type") },
+  { "cores", 'c', n_ ("N"), 0,
+    n_ ("use N CPU cores to build each derivation; 0 means as many as "
+        "available") },
+  { "max-jobs", 'M', n_ ("N"), 0, n_ ("allow at most N build jobs") },
+  { "timeout", GUIX_OPT_TIMEOUT, n_ ("SECONDS"), 0,
+    n_ ("mark builds as failed after SECONDS of activity") },
+  { "max-silent-time", GUIX_OPT_MAX_SILENT_TIME, n_ ("SECONDS"), 0,
+    n_ ("mark builds as failed after SECONDS of silence") },
+  { "disable-chroot", GUIX_OPT_DISABLE_CHROOT, 0, 0,
+    n_ ("disable chroot builds") },
+  { "chroot-directory", GUIX_OPT_CHROOT_DIR, n_ ("DIR"), 0,
+    n_ ("add DIR to the build chroot") },
+  { "build-users-group", GUIX_OPT_BUILD_USERS_GROUP, n_ ("GROUP"), 0,
+    n_ ("perform builds as a user of GROUP") },
+  { "no-substitutes", GUIX_OPT_NO_SUBSTITUTES, 0, 0,
+    n_ ("do not use substitutes") },
+  { "substitute-urls", GUIX_OPT_SUBSTITUTE_URLS, n_ ("URLS"), 0,
+    n_ ("use URLS as the default list of substitute providers") },
+  { "no-offload", GUIX_OPT_NO_BUILD_HOOK, 0, 0,
+    n_ ("do not attempt to offload builds") },
+  { "no-build-hook", GUIX_OPT_NO_BUILD_HOOK, 0,
+    OPTION_HIDDEN, // deprecated
+    n_ ("do not attempt to offload builds") },
+  { "cache-failures", GUIX_OPT_CACHE_FAILURES, 0, 0,
+    n_ ("cache build failures") },
+  { "rounds", GUIX_OPT_BUILD_ROUNDS, "N", 0,
+    n_ ("build each derivation N times in a row") },
+  { "lose-logs", GUIX_OPT_LOSE_LOGS, 0, 0, n_ ("do not keep build logs") },
+  { "disable-log-compression", GUIX_OPT_DISABLE_LOG_COMPRESSION, 0,
+    OPTION_HIDDEN, // deprecated
+    n_ ("disable compression of the build logs") },
+  { "log-compression", GUIX_OPT_LOG_COMPRESSION, "TYPE", 0,
+    n_ ("use the specified compression type for build logs") },
+  { "discover", GUIX_OPT_DISCOVER, "yes/no", OPTION_ARG_OPTIONAL,
+    n_ ("use substitute servers discovered on the local network") },
+
+  /* '--disable-deduplication' was known as '--disable-store-optimization'
+     up to Guix 0.7 included, so keep the alias around.  */
+  { "disable-deduplication", GUIX_OPT_DISABLE_DEDUPLICATION, 0, 0,
+    n_ ("disable automatic file \"deduplication\" in the store") },
+  { "disable-store-optimization", GUIX_OPT_DISABLE_DEDUPLICATION, 0,
+    OPTION_ALIAS | OPTION_HIDDEN, NULL },
+
+  { "impersonate-linux-2.6", GUIX_OPT_IMPERSONATE_LINUX_26, 0,
 #ifdef HAVE_SYS_PERSONALITY_H
-      0,
+    0,
 #else
-      OPTION_HIDDEN,
+    OPTION_HIDDEN,
 #endif
-      n_("impersonate Linux 2.6")
-    },
-    { "gc-keep-outputs", GUIX_OPT_GC_KEEP_OUTPUTS,
-      "yes/no", OPTION_ARG_OPTIONAL,
-      n_("tell whether the GC must keep outputs of live derivations") },
-    { "gc-keep-derivations", GUIX_OPT_GC_KEEP_DERIVATIONS,
-      "yes/no", OPTION_ARG_OPTIONAL,
-      n_("tell whether the GC must keep derivations corresponding \
+    n_ ("impersonate Linux 2.6") },
+  { "gc-keep-outputs", GUIX_OPT_GC_KEEP_OUTPUTS, "yes/no", OPTION_ARG_OPTIONAL,
+    n_ ("tell whether the GC must keep outputs of live derivations") },
+  { "gc-keep-derivations", GUIX_OPT_GC_KEEP_DERIVATIONS, "yes/no",
+    OPTION_ARG_OPTIONAL,
+    n_ ("tell whether the GC must keep derivations corresponding \
 to live outputs") },
 
-    { "listen", GUIX_OPT_LISTEN, n_("SOCKET"), 0,
-      n_("listen for connections on SOCKET") },
-    { "debug", GUIX_OPT_DEBUG, 0, 0,
-      n_("produce debugging output") },
-    { 0, 0, 0, 0, 0 }
-  };
-
+  { "listen", GUIX_OPT_LISTEN, n_ ("SOCKET"), 0,
+    n_ ("listen for connections on SOCKET") },
+  { "debug", GUIX_OPT_DEBUG, 0, 0, n_ ("produce debugging output") },
+  { 0, 0, 0, 0, 0 }
+};
 
 /* Default port for '--listen' on TCP/IP.  */
 #define DEFAULT_GUIX_PORT "44146"
@@ -198,31 +188,31 @@ parse_opt (int key, char *arg, struct argp_state *state)
       break;
     case GUIX_OPT_CHROOT_DIR:
       {
-	std::string chroot_dirs;
-
-	chroot_dirs = settings.get ("build-extra-chroot-dirs",
-				    (std::string) "");
-	if (chroot_dirs == "")
-	  chroot_dirs = arg;
-	else
-	  chroot_dirs = chroot_dirs + " " + arg;
-	settings.set("build-extra-chroot-dirs", chroot_dirs);
-	break;
+        std::string chroot_dirs;
+
+        chroot_dirs
+            = settings.get ("build-extra-chroot-dirs", (std::string) "");
+        if (chroot_dirs == "")
+          chroot_dirs = arg;
+        else
+          chroot_dirs = chroot_dirs + " " + arg;
+        settings.set ("build-extra-chroot-dirs", chroot_dirs);
+        break;
       }
     case GUIX_OPT_LOG_COMPRESSION:
       if (strcmp (arg, "none") == 0)
-	settings.logCompression = COMPRESSION_NONE;
+        settings.logCompression = COMPRESSION_NONE;
       else if (strcmp (arg, "gzip") == 0)
-	settings.logCompression = COMPRESSION_GZIP;
+        settings.logCompression = COMPRESSION_GZIP;
 #if HAVE_BZLIB_H
       else if (strcmp (arg, "bzip2") == 0)
-	settings.logCompression = COMPRESSION_BZIP2;
+        settings.logCompression = COMPRESSION_BZIP2;
 #endif
       else
-	{
-	  fprintf (stderr, _("error: %s: unknown compression type\n"), arg);
-	  exit (EXIT_FAILURE);
-	}
+        {
+          fprintf (stderr, _ ("error: %s: unknown compression type\n"), arg);
+          exit (EXIT_FAILURE);
+        }
       break;
     case GUIX_OPT_DISABLE_LOG_COMPRESSION:
       settings.logCompression = COMPRESSION_NONE;
@@ -238,15 +228,15 @@ parse_opt (int key, char *arg, struct argp_state *state)
       break;
     case GUIX_OPT_BUILD_ROUNDS:
       {
-	char *end;
-	unsigned long n = strtoul (arg, &end, 10);
-	if (end != arg + strlen (arg))
-	  {
-	    fprintf (stderr, _("error: %s: invalid number of rounds\n"), arg);
-	    exit (EXIT_FAILURE);
-	  }
-	settings.set ("build-repeat", std::to_string (std::max (0UL, n - 1)));
-	break;
+        char *end;
+        unsigned long n = strtoul (arg, &end, 10);
+        if (end != arg + strlen (arg))
+          {
+            fprintf (stderr, _ ("error: %s: invalid number of rounds\n"), arg);
+            exit (EXIT_FAILURE);
+          }
+        settings.set ("build-repeat", std::to_string (n - 1));
+        break;
       }
     case GUIX_OPT_IMPERSONATE_LINUX_26:
       settings.impersonateLinux26 = true;
@@ -295,29 +285,25 @@ parse_opt (int key, char *arg, struct argp_state *state)
       settings.thisSystem = arg;
       break;
     default:
-      return (error_t) ARGP_ERR_UNKNOWN;
+      return (error_t)ARGP_ERR_UNKNOWN;
     }
 
-  return (error_t) 0;
+  return (error_t)0;
 }
 
 /* Argument parsing.  */
-static const struct argp argp =
-  {
-    options, parse_opt,
-    NULL, doc,
-    NULL, NULL,					  // children and help_filter
-    guix_textdomain
-  };
-
-
+static const struct argp argp
+    = { options,        parse_opt, NULL, doc,
+        NULL,           NULL, // children and help_filter
+        guix_textdomain };
+
 static int
 open_unix_domain_socket (const char *file)
 {
   /* Create and bind to a Unix domain socket. */
   AutoCloseFD fdSocket = socket (PF_UNIX, SOCK_STREAM, 0);
   if (fdSocket == -1)
-    throw SysError (_("cannot create Unix domain socket"));
+    throw SysError (_ ("cannot create Unix domain socket"));
 
   createDirs (dirOf (file));
 
@@ -325,13 +311,13 @@ open_unix_domain_socket (const char *file)
      So chdir to the socket directory so that we can pass a
      relative path name. */
   if (chdir (dirOf (file).c_str ()) == -1)
-    throw SysError (_("cannot change current directory"));
+    throw SysError (_ ("cannot change current directory"));
   Path fileRel = "./" + baseNameOf (file);
 
   struct sockaddr_un addr;
   addr.sun_family = AF_UNIX;
   if (fileRel.size () >= sizeof (addr.sun_path))
-    throw Error (format (_("socket file name '%1%' is too long")) % fileRel);
+    throw Error (format (_ ("socket file name '%1%' is too long")) % fileRel);
   strcpy (addr.sun_path, fileRel.c_str ());
 
   unlink (file);
@@ -340,16 +326,16 @@ open_unix_domain_socket (const char *file)
      (everybody can connect --- provided they have access to the
      directory containing the socket). */
   mode_t oldMode = umask (0111);
-  int res = bind (fdSocket, (struct sockaddr *) &addr, sizeof addr);
+  int res = bind (fdSocket, (struct sockaddr *)&addr, sizeof addr);
   umask (oldMode);
   if (res == -1)
-    throw SysError (format (_("cannot bind to socket '%1%'")) % file);
+    throw SysError (format (_ ("cannot bind to socket '%1%'")) % file);
 
   if (chdir ("/") == -1) /* back to the root */
-    throw SysError (_("cannot change current directory"));
+    throw SysError (_ ("cannot change current directory"));
 
   if (listen (fdSocket, 5) == -1)
-    throw SysError (format (_("cannot listen on socket '%1%'")) % file);
+    throw SysError (format (_ ("cannot listen on socket '%1%'")) % file);
 
   return fdSocket.borrow ();
 }
@@ -360,14 +346,14 @@ open_inet_socket (const struct sockaddr *address, socklen_t length)
 {
   AutoCloseFD fd = socket (address->sa_family, SOCK_STREAM, 0);
   if (fd == -1)
-    throw SysError (_("cannot create TCP socket"));
+    throw SysError (_ ("cannot create TCP socket"));
 
   int res = bind (fd, address, length);
   if (res == -1)
-    throw SysError (_("cannot bind TCP socket"));
+    throw SysError (_ ("cannot bind TCP socket"));
 
   if (listen (fd, 5) == -1)
-    throw SysError (format (_("cannot listen on TCP socket")));
+    throw SysError (format (_ ("cannot listen on TCP socket")));
 
   return fd.borrow ();
 }
@@ -381,54 +367,54 @@ listening_sockets (const std::list<std::string> &options)
   if (options.empty ())
     {
       /* Open the default Unix-domain socket.  */
-      auto fd = open_unix_domain_socket (settings.nixDaemonSocketFile.c_str ());
+      auto fd
+          = open_unix_domain_socket (settings.nixDaemonSocketFile.c_str ());
       result.push_back (fd);
       return result;
     }
 
   /* Open the user-specified sockets.  */
-  for (const std::string& option: options)
+  for (const std::string &option : options)
     {
       if (option[0] == '/')
-	{
-	  /* Assume OPTION is the file name of a Unix-domain socket.  */
-	  settings.nixDaemonSocketFile = canonPath (option);
-	  int fd =
-	    open_unix_domain_socket (settings.nixDaemonSocketFile.c_str ());
-	  result.push_back (fd);
-	}
+        {
+          /* Assume OPTION is the file name of a Unix-domain socket.  */
+          settings.nixDaemonSocketFile = canonPath (option);
+          int fd = open_unix_domain_socket (
+              settings.nixDaemonSocketFile.c_str ());
+          result.push_back (fd);
+        }
       else
-	{
-	  /* Assume OPTIONS has the form "HOST" or "HOST:PORT".  */
-	  auto colon = option.find_last_of (":");
-	  auto host = colon == std::string::npos
-	    ? option : option.substr (0, colon);
-	  auto port = colon == std::string::npos
-	    ? DEFAULT_GUIX_PORT
-	    : option.substr (colon + 1, option.size () - colon - 1);
+        {
+          /* Assume OPTIONS has the form "HOST" or "HOST:PORT".  */
+          auto colon = option.find_last_of (":");
+          auto host
+              = colon == std::string::npos ? option : option.substr (0, colon);
+          auto port
+              = colon == std::string::npos
+                    ? DEFAULT_GUIX_PORT
+                    : option.substr (colon + 1, option.size () - colon - 1);
 
-	  struct addrinfo *res, hints;
+          struct addrinfo *res, hints;
 
-	  memset (&hints, '\0', sizeof hints);
-	  hints.ai_socktype = SOCK_STREAM;
-	  hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG;
+          memset (&hints, '\0', sizeof hints);
+          hints.ai_socktype = SOCK_STREAM;
+          hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG;
 
-	  int err = getaddrinfo (host.c_str(), port.c_str (),
-				 &hints, &res);
+          int err = getaddrinfo (host.c_str (), port.c_str (), &hints, &res);
 
-	  if (err != 0)
-	    throw Error(format ("failed to look up '%1%': %2%")
-			% option % gai_strerror (err));
+          if (err != 0)
+            throw Error (format ("failed to look up '%1%': %2%") % option
+                         % gai_strerror (err));
 
-	  printMsg (lvlDebug, format ("listening on '%1%', port '%2%'")
-		    % host % port);
+          printMsg (lvlDebug,
+                    format ("listening on '%1%', port '%2%'") % host % port);
 
-	  /* XXX: Pick the first result, RES.  */
-	  result.push_back (open_inet_socket (res->ai_addr,
-					      res->ai_addrlen));
+          /* XXX: Pick the first result, RES.  */
+          result.push_back (open_inet_socket (res->ai_addr, res->ai_addrlen));
 
-	  freeaddrinfo (res);
-	}
+          freeaddrinfo (res);
+        }
     }
 
   return result;
@@ -450,16 +436,15 @@ systemd_activation_sockets ()
     {
       unsigned int fdCount;
       if (string2Int (getEnv ("LISTEN_FDS"), fdCount))
-	{
-	  for (unsigned int i = 0; i < fdCount; i++)
-	    result.push_back (SD_LISTEN_FDS_START + i);
-	}
+        {
+          for (unsigned int i = 0; i < fdCount; i++)
+            result.push_back (SD_LISTEN_FDS_START + i);
+        }
     }
 
   return result;
 }
 
-
 int
 main (int argc, char *argv[])
 {
@@ -470,7 +455,7 @@ main (int argc, char *argv[])
   /* Initialize libgcrypt.  */
   if (!gcry_check_version (GCRYPT_VERSION))
     {
-      fprintf (stderr, _("error: libgcrypt version mismatch\n"));
+      fprintf (stderr, _ ("error: libgcrypt version mismatch\n"));
       exit (EXIT_FAILURE);
     }
 
@@ -480,11 +465,11 @@ main (int argc, char *argv[])
 
   /* Set the umask so that the daemon does not end up creating group-writable
      files, which would lead to "suspicious ownership or permission" errors.
-     See <http://lists.gnu.org/archive/html/bug-guix/2013-07/msg00033.html>.  */
+     See <http://lists.gnu.org/archive/html/bug-guix/2013-07/msg00033.html>. */
   umask (S_IWGRP | S_IWOTH);
 
 #ifndef HAVE_CHROOT
-# error chroot is assumed to be available
+#error chroot is assumed to be available
 #endif
 
   /* Always use chroots by default.  */
@@ -521,58 +506,58 @@ main (int argc, char *argv[])
 
       auto sockets = systemd_activation_sockets ();
       if (sockets.empty ())
-	/* We were not "socket-activated" so open the sockets specified by
-	   LISTEN_OPTIONS.  */
-	sockets = listening_sockets (listen_options);
+        /* We were not "socket-activated" so open the sockets specified by
+           LISTEN_OPTIONS.  */
+        sockets = listening_sockets (listen_options);
       else
-	printMsg (lvlInfo,
-		  format (ngettext ("socket-activated with %1% socket",
-				    "socket-activated with %1% sockets",
-				    sockets.size ()))
-		  % sockets.size ());
+        printMsg (lvlInfo,
+                  format (ngettext ("socket-activated with %1% socket",
+                                    "socket-activated with %1% sockets",
+                                    sockets.size ()))
+                      % sockets.size ());
 
       /* Effect all the changes made via 'settings.set'.  */
       settings.update ();
-      printMsg(lvlDebug,
-	       format ("build log compression: %1%") % settings.logCompression);
+      printMsg (lvlDebug, format ("build log compression: %1%")
+                              % settings.logCompression);
 
       if (geteuid () == 0 && settings.buildUsersGroup.empty ())
-	fprintf (stderr, _("warning: daemon is running as root, so \
+        fprintf (stderr, _ ("warning: daemon is running as root, so \
 using `--build-users-group' is highly recommended\n"));
 
       if (settings.useChroot)
-	{
-	  std::string chroot_dirs;
+        {
+          std::string chroot_dirs;
 
-	  chroot_dirs = settings.get ("build-extra-chroot-dirs",
-				      (std::string) "");
-	  printMsg (lvlDebug,
-		    format ("extra chroot directories: '%1%'") % chroot_dirs);
-	}
+          chroot_dirs
+              = settings.get ("build-extra-chroot-dirs", (std::string) "");
+          printMsg (lvlDebug,
+                    format ("extra chroot directories: '%1%'") % chroot_dirs);
+        }
 
       if (useDiscover)
-      {
-        Strings args;
+        {
+          Strings args;
 
-        args.push_back("guix");
-        args.push_back("discover");
+          args.push_back ("guix");
+          args.push_back ("discover");
 
-        startProcess([&]() {
-          execv(settings.guixProgram.c_str(), stringsToCharPtrs(args).data());
-        });
-      }
+          startProcess ([&] () {
+            execv (settings.guixProgram.c_str (),
+                   stringsToCharPtrs (args).data ());
+          });
+        }
 
-      printMsg (lvlDebug,
-		format ("automatic deduplication set to %1%")
-		% settings.autoOptimiseStore);
+      printMsg (lvlDebug, format ("automatic deduplication set to %1%")
+                              % settings.autoOptimiseStore);
 
       run (sockets);
     }
   catch (std::exception &e)
     {
-      fprintf (stderr, _("error: %s\n"), e.what ());
+      fprintf (stderr, _ ("error: %s\n"), e.what ());
       return EXIT_FAILURE;
     }
 
-  return EXIT_SUCCESS;				  /* never reached */
+  return EXIT_SUCCESS; /* never reached */
 }
diff --git a/nix/nix-daemon/nix-daemon.cc b/nix/nix-daemon/nix-daemon.cc
index e29237e65d..4387d59c1a 100644
--- a/nix/nix-daemon/nix-daemon.cc
+++ b/nix/nix-daemon/nix-daemon.cc
@@ -1,36 +1,35 @@
+#include "affinity.hh"
+#include "archive.hh"
+#include "builtins.hh"
 #include "config.h"
-#include "shared.hh"
+#include "globals.hh"
 #include "local-store.hh"
-#include "util.hh"
 #include "serialise.hh"
+#include "shared.hh"
+#include "util.hh"
 #include "worker-protocol.hh"
-#include "archive.hh"
-#include "affinity.hh"
-#include "globals.hh"
-#include "builtins.hh"
 
 #include <algorithm>
 
+#include <arpa/inet.h>
 #include <cstring>
-#include <unistd.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
 #include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <sys/un.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
-#include <fcntl.h>
 #include <errno.h>
-#include <pwd.h>
+#include <fcntl.h>
 #include <grp.h>
+#include <pwd.h>
 
 using namespace nix;
 
-
 /* On platforms that have O_ASYNC, we can detect when a client
    disconnects and immediately kill any ongoing builds.  On platforms
    that lack it, we only notice the disconnection the next time we try
@@ -49,9 +48,8 @@ using namespace nix;
 #endif
 #endif
 
-
-static FdSource from(STDIN_FILENO);
-static FdSink to(STDOUT_FILENO);
+static FdSource from (STDIN_FILENO);
+static FdSink to (STDOUT_FILENO);
 
 bool canSendStderr;
 
@@ -63,55 +61,62 @@ static bool isRemoteConnection;
    stderr.  If we're in a state where the protocol allows it (i.e.,
    when canSendStderr), send the message to the client over the
    socket. */
-static void tunnelStderr(const unsigned char * buf, size_t count)
+static void
+tunnelStderr (const unsigned char *buf, size_t count)
 {
-    if (canSendStderr) {
-        try {
-            writeInt(STDERR_NEXT, to);
-            writeString(buf, count, to);
-            to.flush();
-        } catch (...) {
-            /* Write failed; that means that the other side is
-               gone. */
-            canSendStderr = false;
-            throw;
+  if (canSendStderr)
+    {
+      try
+        {
+          writeInt (STDERR_NEXT, to);
+          writeString (buf, count, to);
+          to.flush ();
         }
-    } else
-        writeFull(STDERR_FILENO, buf, count);
+      catch (...)
+        {
+          /* Write failed; that means that the other side is
+             gone. */
+          canSendStderr = false;
+          throw;
+        }
+    }
+  else
+    writeFull (STDERR_FILENO, buf, count);
 }
 
-
 /* Return true if the remote side has closed its end of the
    connection, false otherwise.  Should not be called on any socket on
    which we expect input! */
-static bool isFarSideClosed(int socket)
+static bool
+isFarSideClosed (int socket)
 {
-    struct timeval timeout;
-    timeout.tv_sec = timeout.tv_usec = 0;
-
-    fd_set fds;
-    FD_ZERO(&fds);
-    FD_SET(socket, &fds);
-
-    while (select(socket + 1, &fds, 0, 0, &timeout) == -1)
-        if (errno != EINTR) throw SysError("select()");
-
-    if (!FD_ISSET(socket, &fds)) return false;
-
-    /* Destructive read to determine whether the select() marked the
-       socket as readable because there is actual input or because
-       we've reached EOF (i.e., a read of size 0 is available). */
-    char c;
-    int rd;
-    if ((rd = read(socket, &c, 1)) > 0)
-        throw Error("EOF expected (protocol error?)");
-    else if (rd == -1 && errno != ECONNRESET)
-        throw SysError("expected connection reset or EOF");
-
-    return true;
+  struct timeval timeout;
+  timeout.tv_sec = timeout.tv_usec = 0;
+
+  fd_set fds;
+  FD_ZERO (&fds);
+  FD_SET (socket, &fds);
+
+  while (select (socket + 1, &fds, 0, 0, &timeout) == -1)
+    if (errno != EINTR)
+      throw SysError ("select()");
+
+  if (!FD_ISSET (socket, &fds))
+    return false;
+
+  /* Destructive read to determine whether the select() marked the
+     socket as readable because there is actual input or because
+     we've reached EOF (i.e., a read of size 0 is available). */
+  char c;
+  int rd;
+  if ((rd = read (socket, &c, 1)) > 0)
+    throw Error ("EOF expected (protocol error?)");
+  else if (rd == -1 && errno != ECONNRESET)
+    throw SysError ("expected connection reset or EOF");
+
+  return true;
 }
 
-
 /* A SIGPOLL signal is received when data is available on the client
    communication socket, or when the client has closed its side of the
    socket.  This handler is enabled at precisely those moments in the
@@ -123,696 +128,776 @@ static bool isFarSideClosed(int socket)
    si_code in siginfo_t.  That would make most of the SIGPOLL
    complexity unnecessary, i.e., we could just enable SIGPOLL all the
    time and wouldn't have to worry about races. */
-static void sigPollHandler(int sigNo)
+static void
+sigPollHandler (int sigNo)
 {
-    using namespace std;
-    try {
-        /* Check that the far side actually closed.  We're still
-           getting spurious signals every once in a while.  I.e.,
-           there is no input available, but we get a signal with
-           POLL_IN set.  Maybe it's delayed or something. */
-        if (isFarSideClosed(from.fd)) {
-            if (!blockInt) {
-                _isInterrupted = 1;
-                blockInt = 1;
-                canSendStderr = false;
-                const char * s = "SIGPOLL\n";
-                write(STDERR_FILENO, s, strlen(s));
+  using namespace std;
+  try
+    {
+      /* Check that the far side actually closed.  We're still
+         getting spurious signals every once in a while.  I.e.,
+         there is no input available, but we get a signal with
+         POLL_IN set.  Maybe it's delayed or something. */
+      if (isFarSideClosed (from.fd))
+        {
+          if (!blockInt)
+            {
+              _isInterrupted = 1;
+              blockInt = 1;
+              canSendStderr = false;
+              const char *s = "SIGPOLL\n";
+              write (STDERR_FILENO, s, strlen (s));
             }
-        } else {
-            const char * s = "spurious SIGPOLL\n";
-            write(STDERR_FILENO, s, strlen(s));
+        }
+      else
+        {
+          const char *s = "spurious SIGPOLL\n";
+          write (STDERR_FILENO, s, strlen (s));
         }
     }
-    catch (Error & e) {
-        /* Shouldn't happen. */
-        string s = "impossible: " + e.msg() + '\n';
-        write(STDERR_FILENO, s.data(), s.size());
-        throw;
+  catch (Error &e)
+    {
+      /* Shouldn't happen. */
+      string s = "impossible: " + e.msg () + '\n';
+      write (STDERR_FILENO, s.data (), s.size ());
+      throw;
     }
 }
 
-
-static void setSigPollAction(bool enable)
+static void
+setSigPollAction (bool enable)
 {
 #ifdef HAVE_HUP_NOTIFICATION
-    struct sigaction act, oact;
-    act.sa_handler = enable ? sigPollHandler : SIG_IGN;
-    sigfillset(&act.sa_mask);
-    act.sa_flags = 0;
-    if (sigaction(SIGPOLL, &act, &oact))
-        throw SysError("setting handler for SIGPOLL");
+  struct sigaction act, oact;
+  act.sa_handler = enable ? sigPollHandler : SIG_IGN;
+  sigfillset (&act.sa_mask);
+  act.sa_flags = 0;
+  if (sigaction (SIGPOLL, &act, &oact))
+    throw SysError ("setting handler for SIGPOLL");
 #endif
 }
 
-static void handleSignal(int signum)
+static void
+handleSignal (int signum)
 {
-    string name = program_invocation_short_name;
-    auto message = name + ": PID " + std::to_string(getpid())
-	+ " caught signal " + std::to_string(signum) + "\n";
-    writeFull(STDERR_FILENO, (unsigned char *) message.c_str(), message.length());
-    _isInterrupted = 1;
-    blockInt = 1;
+  string name = program_invocation_short_name;
+  auto message = name + ": PID " + std::to_string (getpid ())
+                 + " caught signal " + std::to_string (signum) + "\n";
+  writeFull (STDERR_FILENO, (unsigned char *)message.c_str (),
+             message.length ());
+  _isInterrupted = 1;
+  blockInt = 1;
 }
 
-static void setTerminationSignalHandler()
+static void
+setTerminationSignalHandler ()
 {
-    auto signals = { SIGINT, SIGTERM, SIGHUP };
-    for (int signum: signals) {
-	signal(signum, handleSignal);
+  auto signals = { SIGINT, SIGTERM, SIGHUP };
+  for (int signum : signals)
+    {
+      signal (signum, handleSignal);
     }
 }
 
-
 /* startWork() means that we're starting an operation for which we
    want to send out stderr to the client. */
-static void startWork()
+static void
+startWork ()
 {
-    canSendStderr = true;
-
-    /* Handle client death asynchronously. */
-    setSigPollAction(true);
-
-    /* Of course, there is a race condition here: the socket could
-       have closed between when we last read from / wrote to it, and
-       between the time we set the handler for SIGPOLL.  In that case
-       we won't get the signal.  So do a non-blocking select() to find
-       out if any input is available on the socket.  If there is, it
-       has to be the 0-byte read that indicates that the socket has
-       closed. */
-    if (isFarSideClosed(from.fd)) {
-        _isInterrupted = 1;
-        checkInterrupt();
+  canSendStderr = true;
+
+  /* Handle client death asynchronously. */
+  setSigPollAction (true);
+
+  /* Of course, there is a race condition here: the socket could
+     have closed between when we last read from / wrote to it, and
+     between the time we set the handler for SIGPOLL.  In that case
+     we won't get the signal.  So do a non-blocking select() to find
+     out if any input is available on the socket.  If there is, it
+     has to be the 0-byte read that indicates that the socket has
+     closed. */
+  if (isFarSideClosed (from.fd))
+    {
+      _isInterrupted = 1;
+      checkInterrupt ();
     }
 }
 
-
 /* stopWork() means that we're done; stop sending stderr to the
    client. */
-static void stopWork(bool success = true, const string & msg = "", unsigned int status = 0)
+static void
+stopWork (bool success = true, const string &msg = "", unsigned int status = 0)
 {
-    /* Stop handling async client death; we're going to a state where
-       we're either sending or receiving from the client, so we'll be
-       notified of client death anyway. */
-    setSigPollAction(false);
+  /* Stop handling async client death; we're going to a state where
+     we're either sending or receiving from the client, so we'll be
+     notified of client death anyway. */
+  setSigPollAction (false);
 
-    canSendStderr = false;
+  canSendStderr = false;
 
-    if (success)
-        writeInt(STDERR_LAST, to);
-    else {
-        writeInt(STDERR_ERROR, to);
-        writeString(msg, to);
-        if (status != 0) writeInt(status, to);
+  if (success)
+    writeInt (STDERR_LAST, to);
+  else
+    {
+      writeInt (STDERR_ERROR, to);
+      writeString (msg, to);
+      if (status != 0)
+        writeInt (status, to);
     }
 }
 
-
 struct TunnelSink : BufferedSink
 {
-    Sink & to;
-    TunnelSink(Sink & to) : BufferedSink(64 * 1024), to(to) { }
-    virtual void write(const unsigned char * data, size_t len)
-    {
-        writeInt(STDERR_WRITE, to);
-        writeString(data, len, to);
-    }
+  Sink &to;
+  TunnelSink (Sink &to) : BufferedSink (64 * 1024), to (to) {}
+  virtual void
+  write (const unsigned char *data, size_t len)
+  {
+    writeInt (STDERR_WRITE, to);
+    writeString (data, len, to);
+  }
 };
 
-
 struct TunnelSource : BufferedSource
 {
-    Source & from;
-    TunnelSource(Source & from) : from(from) { }
-    size_t readUnbuffered(unsigned char * data, size_t len)
-    {
-        /* Careful: we're going to receive data from the client now,
-           so we have to disable the SIGPOLL handler. */
-        setSigPollAction(false);
-        canSendStderr = false;
-
-        writeInt(STDERR_READ, to);
-        writeInt(len, to);
-        to.flush();
-        size_t n = readString(data, len, from);
-
-        startWork();
-        if (n == 0) throw EndOfFile("unexpected end-of-file");
-        return n;
-    }
-};
+  Source &from;
+  TunnelSource (Source &from) : from (from) {}
+  size_t
+  readUnbuffered (unsigned char *data, size_t len)
+  {
+    /* Careful: we're going to receive data from the client now,
+       so we have to disable the SIGPOLL handler. */
+    setSigPollAction (false);
+    canSendStderr = false;
 
+    writeInt (STDERR_READ, to);
+    writeInt (len, to);
+    to.flush ();
+    size_t n = readString (data, len, from);
+
+    startWork ();
+    if (n == 0)
+      throw EndOfFile ("unexpected end-of-file");
+    return n;
+  }
+};
 
 /* If the NAR archive contains a single file at top-level, then save
    the contents of the file to `s'.  Otherwise barf. */
 struct RetrieveRegularNARSink : ParseSink
 {
-    bool regular;
-    string s;
-
-    RetrieveRegularNARSink() : regular(true) { }
-
-    void createDirectory(const Path & path)
-    {
-        regular = false;
-    }
-
-    void receiveContents(unsigned char * data, unsigned int len)
-    {
-        s.append((const char *) data, len);
-    }
-
-    void createSymlink(const Path & path, const string & target)
-    {
-        regular = false;
-    }
+  bool regular;
+  string s;
+
+  RetrieveRegularNARSink () : regular (true) {}
+
+  void
+  createDirectory (const Path &path)
+  {
+    regular = false;
+  }
+
+  void
+  receiveContents (unsigned char *data, unsigned int len)
+  {
+    s.append ((const char *)data, len);
+  }
+
+  void
+  createSymlink (const Path &path, const string &target)
+  {
+    regular = false;
+  }
 };
 
-
 /* Adapter class of a Source that saves all data read to `s'. */
 struct SavingSourceAdapter : Source
 {
-    Source & orig;
-    string s;
-    SavingSourceAdapter(Source & orig) : orig(orig) { }
-    size_t read(unsigned char * data, size_t len)
-    {
-        size_t n = orig.read(data, len);
-        s.append((const char *) data, n);
-        return n;
-    }
+  Source &orig;
+  string s;
+  SavingSourceAdapter (Source &orig) : orig (orig) {}
+  size_t
+  read (unsigned char *data, size_t len)
+  {
+    size_t n = orig.read (data, len);
+    s.append ((const char *)data, n);
+    return n;
+  }
 };
 
-
-static void performOp(bool trusted, unsigned int clientVersion,
-    Source & from, Sink & to, unsigned int op)
+static void
+performOp (bool trusted, unsigned int clientVersion, Source &from, Sink &to,
+           unsigned int op)
 {
-    switch (op) {
+  switch (op)
+    {
 
-    case wopIsValidPath: {
+    case wopIsValidPath:
+      {
         /* 'readStorePath' could raise an error leading to the connection
            being closed.  To be able to recover from an invalid path error,
            call 'startWork' early, and do 'assertStorePath' afterwards so
            that the 'Error' exception handler doesn't close the
            connection.  */
-        Path path = readString(from);
-        startWork();
-        assertStorePath(path);
-        bool result = store->isValidPath(path);
-        stopWork();
-        writeInt(result, to);
+        Path path = readString (from);
+        startWork ();
+        assertStorePath (path);
+        bool result = store->isValidPath (path);
+        stopWork ();
+        writeInt (result, to);
         break;
-    }
-
-    case wopQueryValidPaths: {
-        PathSet paths = readStorePaths<PathSet>(from);
-        startWork();
-        PathSet res = store->queryValidPaths(paths);
-        stopWork();
-        writeStrings(res, to);
+      }
+
+    case wopQueryValidPaths:
+      {
+        PathSet paths = readStorePaths<PathSet> (from);
+        startWork ();
+        PathSet res = store->queryValidPaths (paths);
+        stopWork ();
+        writeStrings (res, to);
         break;
-    }
-
-    case wopHasSubstitutes: {
-        Path path = readStorePath(from);
-        startWork();
-        PathSet res = store->querySubstitutablePaths(singleton<PathSet>(path));
-        stopWork();
-        writeInt(res.find(path) != res.end(), to);
+      }
+
+    case wopHasSubstitutes:
+      {
+        Path path = readStorePath (from);
+        startWork ();
+        PathSet res
+            = store->querySubstitutablePaths (singleton<PathSet> (path));
+        stopWork ();
+        writeInt (res.find (path) != res.end (), to);
         break;
-    }
-
-    case wopQuerySubstitutablePaths: {
-        PathSet paths = readStorePaths<PathSet>(from);
-        startWork();
-        PathSet res = store->querySubstitutablePaths(paths);
-        stopWork();
-        writeStrings(res, to);
+      }
+
+    case wopQuerySubstitutablePaths:
+      {
+        PathSet paths = readStorePaths<PathSet> (from);
+        startWork ();
+        PathSet res = store->querySubstitutablePaths (paths);
+        stopWork ();
+        writeStrings (res, to);
         break;
-    }
-
-    case wopQueryPathHash: {
-        Path path = readStorePath(from);
-        startWork();
-        Hash hash = store->queryPathHash(path);
-        stopWork();
-        writeString(printHash(hash), to);
+      }
+
+    case wopQueryPathHash:
+      {
+        Path path = readStorePath (from);
+        startWork ();
+        Hash hash = store->queryPathHash (path);
+        stopWork ();
+        writeString (printHash (hash), to);
         break;
-    }
+      }
 
     case wopQueryReferences:
     case wopQueryReferrers:
     case wopQueryValidDerivers:
-    case wopQueryDerivationOutputs: {
-        Path path = readStorePath(from);
-        startWork();
+    case wopQueryDerivationOutputs:
+      {
+        Path path = readStorePath (from);
+        startWork ();
         PathSet paths;
         if (op == wopQueryReferences)
-            store->queryReferences(path, paths);
+          store->queryReferences (path, paths);
         else if (op == wopQueryReferrers)
-            store->queryReferrers(path, paths);
+          store->queryReferrers (path, paths);
         else if (op == wopQueryValidDerivers)
-            paths = store->queryValidDerivers(path);
-        else paths = store->queryDerivationOutputs(path);
-        stopWork();
-        writeStrings(paths, to);
+          paths = store->queryValidDerivers (path);
+        else
+          paths = store->queryDerivationOutputs (path);
+        stopWork ();
+        writeStrings (paths, to);
         break;
-    }
+      }
 
-    case wopQueryDerivationOutputNames: {
-        Path path = readStorePath(from);
-        startWork();
+    case wopQueryDerivationOutputNames:
+      {
+        Path path = readStorePath (from);
+        startWork ();
         StringSet names;
-        names = store->queryDerivationOutputNames(path);
-        stopWork();
-        writeStrings(names, to);
+        names = store->queryDerivationOutputNames (path);
+        stopWork ();
+        writeStrings (names, to);
         break;
-    }
-
-    case wopQueryDeriver: {
-        Path path = readStorePath(from);
-        startWork();
-        Path deriver = store->queryDeriver(path);
-        stopWork();
-        writeString(deriver, to);
+      }
+
+    case wopQueryDeriver:
+      {
+        Path path = readStorePath (from);
+        startWork ();
+        Path deriver = store->queryDeriver (path);
+        stopWork ();
+        writeString (deriver, to);
         break;
-    }
-
-    case wopQueryPathFromHashPart: {
-        string hashPart = readString(from);
-        startWork();
-        Path path = store->queryPathFromHashPart(hashPart);
-        stopWork();
-        writeString(path, to);
+      }
+
+    case wopQueryPathFromHashPart:
+      {
+        string hashPart = readString (from);
+        startWork ();
+        Path path = store->queryPathFromHashPart (hashPart);
+        stopWork ();
+        writeString (path, to);
         break;
-    }
-
-    case wopAddToStore: {
-        string baseName = readString(from);
-        bool fixed = readInt(from) == 1; /* obsolete */
-        bool recursive = readInt(from) == 1;
-        string s = readString(from);
+      }
+
+    case wopAddToStore:
+      {
+        string baseName = readString (from);
+        bool fixed = readInt (from) == 1; /* obsolete */
+        bool recursive = readInt (from) == 1;
+        string s = readString (from);
         /* Compatibility hack. */
-        if (!fixed) {
+        if (!fixed)
+          {
             s = "sha256";
             recursive = true;
-        }
-        HashType hashAlgo = parseHashType(s);
+          }
+        HashType hashAlgo = parseHashType (s);
 
-        SavingSourceAdapter savedNAR(from);
+        SavingSourceAdapter savedNAR (from);
         RetrieveRegularNARSink savedRegular;
 
-        if (recursive) {
+        if (recursive)
+          {
             /* Get the entire NAR dump from the client and save it to
                a string so that we can pass it to
                addToStoreFromDump(). */
             ParseSink sink; /* null sink; just parse the NAR */
-            parseDump(sink, savedNAR);
-        } else
-            parseDump(savedRegular, from);
-
-        startWork();
-        if (!savedRegular.regular) throw Error("regular file expected");
-        Path path = dynamic_cast<LocalStore *>(store.get())
-            ->addToStoreFromDump(recursive ? savedNAR.s : savedRegular.s, baseName, recursive, hashAlgo);
-        stopWork();
-
-        writeString(path, to);
+            parseDump (sink, savedNAR);
+          }
+        else
+          parseDump (savedRegular, from);
+
+        startWork ();
+        if (!savedRegular.regular)
+          throw Error ("regular file expected");
+        Path path = dynamic_cast<LocalStore *> (store.get ())
+                        ->addToStoreFromDump (recursive ? savedNAR.s
+                                                        : savedRegular.s,
+                                              baseName, recursive, hashAlgo);
+        stopWork ();
+
+        writeString (path, to);
         break;
-    }
-
-    case wopAddTextToStore: {
-        string suffix = readString(from);
-        string s = readString(from);
-        PathSet refs = readStorePaths<PathSet>(from);
-        startWork();
-        Path path = store->addTextToStore(suffix, s, refs);
-        stopWork();
-        writeString(path, to);
+      }
+
+    case wopAddTextToStore:
+      {
+        string suffix = readString (from);
+        string s = readString (from);
+        PathSet refs = readStorePaths<PathSet> (from);
+        startWork ();
+        Path path = store->addTextToStore (suffix, s, refs);
+        stopWork ();
+        writeString (path, to);
         break;
-    }
-
-    case wopExportPath: {
-        Path path = readStorePath(from);
-        bool sign = readInt(from) == 1;
-        startWork();
-        TunnelSink sink(to);
-	try {
-	    store->exportPath(path, sign, sink);
-	}
-	catch (Error &e) {
-	    /* Flush SINK beforehand or its destructor will rightfully trigger
-	       an assertion failure.  */
-	    sink.flush();
-	    throw e;
-	}
-        sink.flush();
-        stopWork();
-        writeInt(1, to);
+      }
+
+    case wopExportPath:
+      {
+        Path path = readStorePath (from);
+        bool sign = readInt (from) == 1;
+        startWork ();
+        TunnelSink sink (to);
+        try
+          {
+            store->exportPath (path, sign, sink);
+          }
+        catch (Error &e)
+          {
+            /* Flush SINK beforehand or its destructor will rightfully trigger
+               an assertion failure.  */
+            sink.flush ();
+            throw e;
+          }
+        sink.flush ();
+        stopWork ();
+        writeInt (1, to);
         break;
-    }
+      }
 
-    case wopImportPaths: {
-        startWork();
-        TunnelSource source(from);
+    case wopImportPaths:
+      {
+        startWork ();
+        TunnelSource source (from);
 
         /* Unlike Nix, always require a signature, even for "trusted"
            users.  */
-        Paths paths = store->importPaths(true, source);
-        stopWork();
-        writeStrings(paths, to);
+        Paths paths = store->importPaths (true, source);
+        stopWork ();
+        writeStrings (paths, to);
         break;
-    }
+      }
 
-    case wopBuildPaths: {
-        PathSet drvs = readStorePaths<PathSet>(from);
+    case wopBuildPaths:
+      {
+        PathSet drvs = readStorePaths<PathSet> (from);
         BuildMode mode = bmNormal;
-        if (GET_PROTOCOL_MINOR(clientVersion) >= 15) {
-            mode = (BuildMode)readInt(from);
+        if (GET_PROTOCOL_MINOR (clientVersion) >= 15)
+          {
+            mode = (BuildMode)readInt (from);
 
-	    /* Repairing is not atomic, so disallowed for "untrusted"
-	       clients.  */
+            /* Repairing is not atomic, so disallowed for "untrusted"
+               clients.  */
             if (mode == bmRepair && !trusted)
-                throw Error("repairing is a privileged operation");
-        }
-        startWork();
-        store->buildPaths(drvs, mode);
-        stopWork();
-        writeInt(1, to);
+              throw Error ("repairing is a privileged operation");
+          }
+        startWork ();
+        store->buildPaths (drvs, mode);
+        stopWork ();
+        writeInt (1, to);
         break;
-    }
-
-    case wopEnsurePath: {
-        Path path = readStorePath(from);
-        startWork();
-        store->ensurePath(path);
-        stopWork();
-        writeInt(1, to);
+      }
+
+    case wopEnsurePath:
+      {
+        Path path = readStorePath (from);
+        startWork ();
+        store->ensurePath (path);
+        stopWork ();
+        writeInt (1, to);
         break;
-    }
-
-    case wopAddTempRoot: {
-        Path path = readStorePath(from);
-        startWork();
-        store->addTempRoot(path);
-        stopWork();
-        writeInt(1, to);
+      }
+
+    case wopAddTempRoot:
+      {
+        Path path = readStorePath (from);
+        startWork ();
+        store->addTempRoot (path);
+        stopWork ();
+        writeInt (1, to);
         break;
-    }
-
-    case wopAddIndirectRoot: {
-        Path path = absPath(readString(from));
-        startWork();
-        store->addIndirectRoot(path);
-        stopWork();
-        writeInt(1, to);
+      }
+
+    case wopAddIndirectRoot:
+      {
+        Path path = absPath (readString (from));
+        startWork ();
+        store->addIndirectRoot (path);
+        stopWork ();
+        writeInt (1, to);
         break;
-    }
-
-    case wopSyncWithGC: {
-        startWork();
-        store->syncWithGC();
-        stopWork();
-        writeInt(1, to);
+      }
+
+    case wopSyncWithGC:
+      {
+        startWork ();
+        store->syncWithGC ();
+        stopWork ();
+        writeInt (1, to);
         break;
-    }
-
-    case wopFindRoots: {
-        startWork();
-        Roots roots = store->findRoots();
-        stopWork();
-        writeInt(roots.size(), to);
-        for (Roots::iterator i = roots.begin(); i != roots.end(); ++i) {
-            writeString(i->first, to);
-            writeString(i->second, to);
-        }
+      }
+
+    case wopFindRoots:
+      {
+        startWork ();
+        Roots roots = store->findRoots ();
+        stopWork ();
+        writeInt (roots.size (), to);
+        for (Roots::iterator i = roots.begin (); i != roots.end (); ++i)
+          {
+            writeString (i->first, to);
+            writeString (i->second, to);
+          }
         break;
-    }
+      }
 
-    case wopCollectGarbage: {
-        if (isRemoteConnection) {
-            throw Error("Garbage collection is disabled for remote hosts.");
+    case wopCollectGarbage:
+      {
+        if (isRemoteConnection)
+          {
+            throw Error ("Garbage collection is disabled for remote hosts.");
             break;
-        }
+          }
 
         GCOptions options;
-        options.action = (GCOptions::GCAction) readInt(from);
-        options.pathsToDelete = readStorePaths<PathSet>(from);
-        options.ignoreLiveness = readInt(from);
-        options.maxFreed = readLongLong(from);
-        readInt(from); // obsolete field
-        if (GET_PROTOCOL_MINOR(clientVersion) >= 5) {
+        options.action = (GCOptions::GCAction)readInt (from);
+        options.pathsToDelete = readStorePaths<PathSet> (from);
+        options.ignoreLiveness = readInt (from);
+        options.maxFreed = readLongLong (from);
+        readInt (from); // obsolete field
+        if (GET_PROTOCOL_MINOR (clientVersion) >= 5)
+          {
             /* removed options */
-            readInt(from);
-            readInt(from);
-        }
+            readInt (from);
+            readInt (from);
+          }
 
         GCResults results;
 
-        startWork();
+        startWork ();
         if (options.ignoreLiveness)
-            throw Error("you are not allowed to ignore liveness");
-        store->collectGarbage(options, results);
-        stopWork();
+          throw Error ("you are not allowed to ignore liveness");
+        store->collectGarbage (options, results);
+        stopWork ();
 
-        writeStrings(results.paths, to);
-        writeLongLong(results.bytesFreed, to);
-        writeLongLong(0, to); // obsolete
+        writeStrings (results.paths, to);
+        writeLongLong (results.bytesFreed, to);
+        writeLongLong (0, to); // obsolete
 
         break;
-    }
-
-    case wopSetOptions: {
-        settings.keepFailed = readInt(from) != 0;
-	if (isRemoteConnection)
-	    /* When the client is remote, don't keep the failed build tree as
-	       it is presumably inaccessible to the client and could fill up
-	       our disk.  */
-	    settings.keepFailed = 0;
-
-        settings.keepGoing = readInt(from) != 0;
-        settings.set("build-fallback", readInt(from) ? "true" : "false");
-        verbosity = (Verbosity) readInt(from);
-
-        if (GET_PROTOCOL_MINOR(clientVersion) < 0x61) {
-            settings.set("build-max-jobs", std::to_string(readInt(from)));
-            settings.set("build-max-silent-time", std::to_string(readInt(from)));
-        }
-
-        if (GET_PROTOCOL_MINOR(clientVersion) >= 2) {
+      }
+
+    case wopSetOptions:
+      {
+        settings.keepFailed = readInt (from) != 0;
+        if (isRemoteConnection)
+          /* When the client is remote, don't keep the failed build tree as
+             it is presumably inaccessible to the client and could fill up
+             our disk.  */
+          settings.keepFailed = 0;
+
+        settings.keepGoing = readInt (from) != 0;
+        settings.set ("build-fallback", readInt (from) ? "true" : "false");
+        verbosity = (Verbosity)readInt (from);
+
+        if (GET_PROTOCOL_MINOR (clientVersion) < 0x61)
+          {
+            settings.set ("build-max-jobs", std::to_string (readInt (from)));
+            settings.set ("build-max-silent-time",
+                          std::to_string (readInt (from)));
+          }
+
+        if (GET_PROTOCOL_MINOR (clientVersion) >= 2)
+          {
 #ifdef HAVE_DAEMON_OFFLOAD_HOOK
-            settings.useBuildHook = readInt(from) != 0;
+            settings.useBuildHook = readInt (from) != 0;
 #else
-	    readInt(from);			  // ignore the user's setting
+            readInt (from); // ignore the user's setting
 #endif
-	}
-
-        if (GET_PROTOCOL_MINOR(clientVersion) >= 4) {
-            settings.buildVerbosity = (Verbosity) readInt(from);
-            logType = (LogType) readInt(from);
-            settings.printBuildTrace = readInt(from) != 0;
-        }
-        if (GET_PROTOCOL_MINOR(clientVersion) >= 6
-            && GET_PROTOCOL_MINOR(clientVersion) < 0x61)
-            settings.set("build-cores", std::to_string(readInt(from)));
-        if (GET_PROTOCOL_MINOR(clientVersion) >= 10) {
-	    if (settings.useSubstitutes)
-		settings.set("build-use-substitutes", readInt(from) ? "true" : "false");
-	    else
-		readInt(from);			// substitutes remain disabled
-	}
-        if (GET_PROTOCOL_MINOR(clientVersion) >= 12) {
-            unsigned int n = readInt(from);
-            for (unsigned int i = 0; i < n; i++) {
-                string name = readString(from);
-                string value = readString(from);
+          }
+
+        if (GET_PROTOCOL_MINOR (clientVersion) >= 4)
+          {
+            settings.buildVerbosity = (Verbosity)readInt (from);
+            logType = (LogType)readInt (from);
+            settings.printBuildTrace = readInt (from) != 0;
+          }
+        if (GET_PROTOCOL_MINOR (clientVersion) >= 6
+            && GET_PROTOCOL_MINOR (clientVersion) < 0x61)
+          settings.set ("build-cores", std::to_string (readInt (from)));
+        if (GET_PROTOCOL_MINOR (clientVersion) >= 10)
+          {
+            if (settings.useSubstitutes)
+              settings.set ("build-use-substitutes",
+                            readInt (from) ? "true" : "false");
+            else
+              readInt (from); // substitutes remain disabled
+          }
+        if (GET_PROTOCOL_MINOR (clientVersion) >= 12)
+          {
+            unsigned int n = readInt (from);
+            for (unsigned int i = 0; i < n; i++)
+              {
+                string name = readString (from);
+                string value = readString (from);
                 if (name == "build-timeout" || name == "build-max-silent-time"
                     || name == "build-max-jobs" || name == "build-cores"
                     || name == "build-repeat"
                     || name == "multiplexed-build-output")
-                    settings.set(name, value);
-		else if (name == "user-name"
-                         && settings.clientUid == (uid_t) -1) {
+                  settings.set (name, value);
+                else if (name == "user-name"
+                         && settings.clientUid == (uid_t)-1)
+                  {
                     /* Create the user profile.  This is necessary if
                        clientUid = -1, for instance because the client
                        connected over TCP.  */
-                    struct passwd *pw = getpwnam(value.c_str());
+                    struct passwd *pw = getpwnam (value.c_str ());
                     if (pw != NULL)
-                        store->createUser(value, pw->pw_uid);
+                      store->createUser (value, pw->pw_uid);
                     else
-                        printMsg(lvlInfo, format("user name %1% not found") % value);
-		}
+                      printMsg (lvlInfo,
+                                format ("user name %1% not found") % value);
+                  }
                 else
-                    settings.set(trusted ? name : "untrusted-" + name, value);
-            }
-        }
-        settings.update();
-        startWork();
-        stopWork();
+                  settings.set (trusted ? name : "untrusted-" + name, value);
+              }
+          }
+        settings.update ();
+        startWork ();
+        stopWork ();
         break;
-    }
+      }
 
-    case wopQuerySubstitutablePathInfo: {
-        Path path = absPath(readString(from));
-        startWork();
+    case wopQuerySubstitutablePathInfo:
+      {
+        Path path = absPath (readString (from));
+        startWork ();
         SubstitutablePathInfos infos;
-        store->querySubstitutablePathInfos(singleton<PathSet>(path), infos);
-        stopWork();
-        SubstitutablePathInfos::iterator i = infos.find(path);
-        if (i == infos.end())
-            writeInt(0, to);
-        else {
-            writeInt(1, to);
-            writeString(i->second.deriver, to);
-            writeStrings(i->second.references, to);
-            writeLongLong(i->second.downloadSize, to);
-            if (GET_PROTOCOL_MINOR(clientVersion) >= 7)
-                writeLongLong(i->second.narSize, to);
-        }
+        store->querySubstitutablePathInfos (singleton<PathSet> (path), infos);
+        stopWork ();
+        SubstitutablePathInfos::iterator i = infos.find (path);
+        if (i == infos.end ())
+          writeInt (0, to);
+        else
+          {
+            writeInt (1, to);
+            writeString (i->second.deriver, to);
+            writeStrings (i->second.references, to);
+            writeLongLong (i->second.downloadSize, to);
+            if (GET_PROTOCOL_MINOR (clientVersion) >= 7)
+              writeLongLong (i->second.narSize, to);
+          }
         break;
-    }
+      }
 
-    case wopQuerySubstitutablePathInfos: {
-        PathSet paths = readStorePaths<PathSet>(from);
-        startWork();
+    case wopQuerySubstitutablePathInfos:
+      {
+        PathSet paths = readStorePaths<PathSet> (from);
+        startWork ();
         SubstitutablePathInfos infos;
-        store->querySubstitutablePathInfos(paths, infos);
-        stopWork();
-        writeInt(infos.size(), to);
-        foreach (SubstitutablePathInfos::iterator, i, infos) {
-            writeString(i->first, to);
-            writeString(i->second.deriver, to);
-            writeStrings(i->second.references, to);
-            writeLongLong(i->second.downloadSize, to);
-            writeLongLong(i->second.narSize, to);
-        }
+        store->querySubstitutablePathInfos (paths, infos);
+        stopWork ();
+        writeInt (infos.size (), to);
+        foreach (SubstitutablePathInfos::iterator, i, infos)
+          {
+            writeString (i->first, to);
+            writeString (i->second.deriver, to);
+            writeStrings (i->second.references, to);
+            writeLongLong (i->second.downloadSize, to);
+            writeLongLong (i->second.narSize, to);
+          }
         break;
-    }
-
-    case wopQueryAllValidPaths: {
-        startWork();
-        PathSet paths = store->queryAllValidPaths();
-        stopWork();
-        writeStrings(paths, to);
+      }
+
+    case wopQueryAllValidPaths:
+      {
+        startWork ();
+        PathSet paths = store->queryAllValidPaths ();
+        stopWork ();
+        writeStrings (paths, to);
         break;
-    }
-
-    case wopQueryFailedPaths: {
-        startWork();
-        PathSet paths = store->queryFailedPaths();
-        stopWork();
-        writeStrings(paths, to);
+      }
+
+    case wopQueryFailedPaths:
+      {
+        startWork ();
+        PathSet paths = store->queryFailedPaths ();
+        stopWork ();
+        writeStrings (paths, to);
         break;
-    }
-
-    case wopClearFailedPaths: {
-        PathSet paths = readStrings<PathSet>(from);
-        startWork();
-        store->clearFailedPaths(paths);
-        stopWork();
-        writeInt(1, to);
+      }
+
+    case wopClearFailedPaths:
+      {
+        PathSet paths = readStrings<PathSet> (from);
+        startWork ();
+        store->clearFailedPaths (paths);
+        stopWork ();
+        writeInt (1, to);
         break;
-    }
-
-    case wopQueryPathInfo: {
-        Path path = readStorePath(from);
-        startWork();
-        ValidPathInfo info = store->queryPathInfo(path);
-        stopWork();
-        writeString(info.deriver, to);
-        writeString(printHash(info.hash), to);
-        writeStrings(info.references, to);
-        writeInt(info.registrationTime, to);
-        writeLongLong(info.narSize, to);
+      }
+
+    case wopQueryPathInfo:
+      {
+        Path path = readStorePath (from);
+        startWork ();
+        ValidPathInfo info = store->queryPathInfo (path);
+        stopWork ();
+        writeString (info.deriver, to);
+        writeString (printHash (info.hash), to);
+        writeStrings (info.references, to);
+        writeInt (info.registrationTime, to);
+        writeLongLong (info.narSize, to);
         break;
-    }
+      }
 
     case wopOptimiseStore:
-        startWork();
-        store->optimiseStore();
-        stopWork();
-        writeInt(1, to);
-        break;
-
-    case wopVerifyStore: {
-        bool checkContents = readInt(from) != 0;
-        bool repair = readInt(from) != 0;
-        startWork();
+      startWork ();
+      store->optimiseStore ();
+      stopWork ();
+      writeInt (1, to);
+      break;
+
+    case wopVerifyStore:
+      {
+        bool checkContents = readInt (from) != 0;
+        bool repair = readInt (from) != 0;
+        startWork ();
         if (repair && !trusted)
-            throw Error("you are not privileged to repair paths");
-        bool errors = store->verifyStore(checkContents, repair);
-        stopWork();
-        writeInt(errors, to);
+          throw Error ("you are not privileged to repair paths");
+        bool errors = store->verifyStore (checkContents, repair);
+        stopWork ();
+        writeInt (errors, to);
         break;
-    }
-
-    case wopBuiltinBuilders: {
-	startWork();
-	auto names = builtinBuilderNames();
-	stopWork();
-	writeStrings(names, to);
-	break;
-    }
-
-    case wopSubstituteURLs: {
-	startWork();
-	Strings urls;
-	if (settings.get("build-use-substitutes", std::string("false")) == "true") {
-	    /* First check the client-provided substitute URLs, then those
-	       passed to the daemon.  */
-	    auto str = settings.get("untrusted-substitute-urls",  std::string(""));
-	    if (str.empty()) {
-		str = settings.get("substitute-urls",  std::string(""));
-	    }
-	    urls = tokenizeString<Strings>(str);
-	}
-	stopWork();
-	writeStrings(urls, to);
-	break;
-    }
+      }
+
+    case wopBuiltinBuilders:
+      {
+        startWork ();
+        auto names = builtinBuilderNames ();
+        stopWork ();
+        writeStrings (names, to);
+        break;
+      }
+
+    case wopSubstituteURLs:
+      {
+        startWork ();
+        Strings urls;
+        if (settings.get ("build-use-substitutes", std::string ("false"))
+            == "true")
+          {
+            /* First check the client-provided substitute URLs, then those
+               passed to the daemon.  */
+            auto str
+                = settings.get ("untrusted-substitute-urls", std::string (""));
+            if (str.empty ())
+              {
+                str = settings.get ("substitute-urls", std::string (""));
+              }
+            urls = tokenizeString<Strings> (str);
+          }
+        stopWork ();
+        writeStrings (urls, to);
+        break;
+      }
 
     default:
-        throw Error(format("invalid operation %1%") % op);
+      throw Error (format ("invalid operation %1%") % op);
     }
 }
 
-
-static void processConnection(bool trusted, uid_t userId)
+static void
+processConnection (bool trusted, uid_t userId)
 {
-    canSendStderr = false;
-    _writeToStderr = tunnelStderr;
+  canSendStderr = false;
+  _writeToStderr = tunnelStderr;
 
 #ifdef HAVE_HUP_NOTIFICATION
-    /* Allow us to receive SIGPOLL for events on the client socket. */
-    setSigPollAction(false);
-    if (fcntl(from.fd, F_SETOWN, getpid()) == -1)
-        throw SysError("F_SETOWN");
-    if (fcntl(from.fd, F_SETFL, fcntl(from.fd, F_GETFL, 0) | O_ASYNC) == -1)
-        throw SysError("F_SETFL");
+  /* Allow us to receive SIGPOLL for events on the client socket. */
+  setSigPollAction (false);
+  if (fcntl (from.fd, F_SETOWN, getpid ()) == -1)
+    throw SysError ("F_SETOWN");
+  if (fcntl (from.fd, F_SETFL, fcntl (from.fd, F_GETFL, 0) | O_ASYNC) == -1)
+    throw SysError ("F_SETFL");
 #endif
 
-    /* Exchange the greeting. */
-    unsigned int magic = readInt(from);
-    if (magic != WORKER_MAGIC_1) throw Error("protocol mismatch");
-    writeInt(WORKER_MAGIC_2, to);
-    writeInt(PROTOCOL_VERSION, to);
-    to.flush();
-    unsigned int clientVersion = readInt(from);
+  /* Exchange the greeting. */
+  unsigned int magic = readInt (from);
+  if (magic != WORKER_MAGIC_1)
+    throw Error ("protocol mismatch");
+  writeInt (WORKER_MAGIC_2, to);
+  writeInt (PROTOCOL_VERSION, to);
+  to.flush ();
+  unsigned int clientVersion = readInt (from);
 
-    if (GET_PROTOCOL_MINOR(clientVersion) >= 14 && readInt(from))
-        setAffinityTo(readInt(from));
+  if (GET_PROTOCOL_MINOR (clientVersion) >= 14 && readInt (from))
+    setAffinityTo (readInt (from));
 
-    bool reserveSpace = true;
-    if (GET_PROTOCOL_MINOR(clientVersion) >= 11)
-        reserveSpace = readInt(from) != 0;
+  bool reserveSpace = true;
+  if (GET_PROTOCOL_MINOR (clientVersion) >= 11)
+    reserveSpace = readInt (from) != 0;
 
-    /* Send startup error messages to the client. */
-    startWork();
+  /* Send startup error messages to the client. */
+  startWork ();
 
-    try {
+  try
+    {
 
-        /* If we can't accept clientVersion, then throw an error
-           *here* (not above). */
+      /* If we can't accept clientVersion, then throw an error
+       *here* (not above). */
 
 #if 0
         /* Prevent users from doing something very dangerous. */
@@ -821,267 +906,309 @@ static void processConnection(bool trusted, uid_t userId)
             throw Error("if you run `nix-daemon' as root, then you MUST set `build-users-group'!");
 #endif
 
-	/* Catch SIGTERM & co. to ensure proper termination: closing the store
-	   and its database, thereby deleting its WAL file.  */
-	setTerminationSignalHandler();
-
-        /* Open the store. */
-        store = std::shared_ptr<StoreAPI>(new LocalStore(reserveSpace));
-
-	if (userId != (uid_t) -1) {
-            /* Create the user profile.  */
-            struct passwd *pw = getpwuid(userId);
-            if (pw != NULL && pw->pw_name != NULL)
-                store->createUser(pw->pw_name, userId);
-            else
-                printMsg(lvlInfo, format("user with UID %1% not found") % userId);
-	}
-
-        stopWork();
-        to.flush();
+      /* Catch SIGTERM & co. to ensure proper termination: closing the store
+         and its database, thereby deleting its WAL file.  */
+      setTerminationSignalHandler ();
+
+      /* Open the store. */
+      store = std::shared_ptr<StoreAPI> (new LocalStore (reserveSpace));
+
+      if (userId != (uid_t)-1)
+        {
+          /* Create the user profile.  */
+          struct passwd *pw = getpwuid (userId);
+          if (pw != NULL && pw->pw_name != NULL)
+            store->createUser (pw->pw_name, userId);
+          else
+            printMsg (lvlInfo,
+                      format ("user with UID %1% not found") % userId);
+        }
 
-    } catch (Error & e) {
-        stopWork(false, e.msg(), GET_PROTOCOL_MINOR(clientVersion) >= 8 ? 1 : 0);
-        to.flush();
-        return;
+      stopWork ();
+      to.flush ();
+    }
+  catch (Error &e)
+    {
+      stopWork (false, e.msg (),
+                GET_PROTOCOL_MINOR (clientVersion) >= 8 ? 1 : 0);
+      to.flush ();
+      return;
     }
 
-    /* Process client requests. */
-    unsigned int opCount = 0;
+  /* Process client requests. */
+  unsigned int opCount = 0;
 
-    while (true) {
-        WorkerOp op;
-        try {
-            op = (WorkerOp) readInt(from);
-        } catch (EndOfFile & e) {
-            break;
+  while (true)
+    {
+      WorkerOp op;
+      try
+        {
+          op = (WorkerOp)readInt (from);
+        }
+      catch (EndOfFile &e)
+        {
+          break;
         }
 
-        opCount++;
-
-        try {
-            performOp(trusted, clientVersion, from, to, op);
-        } catch (Error & e) {
-            /* If we're not in a state where we can send replies, then
-               something went wrong processing the input of the
-               client.  This can happen especially if I/O errors occur
-               during addTextToStore() / importPath().  If that
-               happens, just send the error message and exit. */
-            bool errorAllowed = canSendStderr;
-            stopWork(false, e.msg(), GET_PROTOCOL_MINOR(clientVersion) >= 8 ? e.status : 0);
-            if (!errorAllowed) throw;
-        } catch (std::bad_alloc & e) {
-            stopWork(false, "build daemon out of memory", GET_PROTOCOL_MINOR(clientVersion) >= 8 ? 1 : 0);
+      opCount++;
+
+      try
+        {
+          performOp (trusted, clientVersion, from, to, op);
+        }
+      catch (Error &e)
+        {
+          /* If we're not in a state where we can send replies, then
+             something went wrong processing the input of the
+             client.  This can happen especially if I/O errors occur
+             during addTextToStore() / importPath().  If that
+             happens, just send the error message and exit. */
+          bool errorAllowed = canSendStderr;
+          stopWork (false, e.msg (),
+                    GET_PROTOCOL_MINOR (clientVersion) >= 8 ? e.status : 0);
+          if (!errorAllowed)
             throw;
         }
+      catch (std::bad_alloc &e)
+        {
+          stopWork (false, "build daemon out of memory",
+                    GET_PROTOCOL_MINOR (clientVersion) >= 8 ? 1 : 0);
+          throw;
+        }
 
-        to.flush();
+      to.flush ();
 
-        assert(!canSendStderr);
+      assert (!canSendStderr);
     };
 
-    canSendStderr = false;
-    _isInterrupted = false;
-    printMsg(lvlDebug, format("%1% operations") % opCount);
+  canSendStderr = false;
+  _isInterrupted = false;
+  printMsg (lvlDebug, format ("%1% operations") % opCount);
 }
 
-
-static void sigChldHandler(int sigNo)
+static void
+sigChldHandler (int sigNo)
 {
-    /* Reap all dead children. */
-    while (waitpid(-1, 0, WNOHANG) > 0) ;
+  /* Reap all dead children. */
+  while (waitpid (-1, 0, WNOHANG) > 0)
+    ;
 }
 
-
-static void setSigChldAction(bool autoReap)
+static void
+setSigChldAction (bool autoReap)
 {
-    struct sigaction act, oact;
-    act.sa_handler = autoReap ? sigChldHandler : SIG_DFL;
-    sigfillset(&act.sa_mask);
-    act.sa_flags = 0;
-    if (sigaction(SIGCHLD, &act, &oact))
-        throw SysError("setting SIGCHLD handler");
+  struct sigaction act, oact;
+  act.sa_handler = autoReap ? sigChldHandler : SIG_DFL;
+  sigfillset (&act.sa_mask);
+  act.sa_flags = 0;
+  if (sigaction (SIGCHLD, &act, &oact))
+    throw SysError ("setting SIGCHLD handler");
 }
 
-
 /* Accept a connection on FDSOCKET and fork a server process to process the
    new connection.  */
-static void acceptConnection(int fdSocket)
+static void
+acceptConnection (int fdSocket)
 {
-    uid_t clientUid = (uid_t) -1;
-    gid_t clientGid = (gid_t) -1;
+  uid_t clientUid = (uid_t)-1;
+  gid_t clientGid = (gid_t)-1;
 
-    try {
-	/* Important: the server process *cannot* open the SQLite
-	   database, because it doesn't like forks very much. */
-	assert(!store);
+  try
+    {
+      /* Important: the server process *cannot* open the SQLite
+         database, because it doesn't like forks very much. */
+      assert (!store);
 
-	/* Accept a connection. */
-	struct sockaddr_storage remoteAddr;
-	socklen_t remoteAddrLen = sizeof(remoteAddr);
+      /* Accept a connection. */
+      struct sockaddr_storage remoteAddr;
+      socklen_t remoteAddrLen = sizeof (remoteAddr);
 
     try_again:
-	AutoCloseFD remote = accept(fdSocket,
-				    (struct sockaddr *) &remoteAddr, &remoteAddrLen);
-	checkInterrupt();
-	if (remote == -1) {
-	    if (errno == EINTR)
-		goto try_again;
-	    else
-		throw SysError("accepting connection");
-	}
-
-	closeOnExec(remote);
-
-	{
-	  int enabled = 1;
-
-	  /* If we're on a TCP connection, disable Nagle's algorithm so that
-	     data is sent as soon as possible.  */
-	  (void) setsockopt(remote, SOL_TCP, TCP_NODELAY,
-			    &enabled, sizeof enabled);
+      AutoCloseFD remote
+          = accept (fdSocket, (struct sockaddr *)&remoteAddr, &remoteAddrLen);
+      checkInterrupt ();
+      if (remote == -1)
+        {
+          if (errno == EINTR)
+            goto try_again;
+          else
+            throw SysError ("accepting connection");
+        }
+
+      closeOnExec (remote);
+
+      {
+        int enabled = 1;
+
+        /* If we're on a TCP connection, disable Nagle's algorithm so that
+           data is sent as soon as possible.  */
+        (void)setsockopt (remote, SOL_TCP, TCP_NODELAY, &enabled,
+                          sizeof enabled);
 
 #if defined(TCP_QUICKACK)
-	  /* Enable TCP quick-ack if applicable; this might help a little.  */
-	  (void) setsockopt(remote, SOL_TCP, TCP_QUICKACK,
-			    &enabled, sizeof enabled);
+        /* Enable TCP quick-ack if applicable; this might help a little.  */
+        (void)setsockopt (remote, SOL_TCP, TCP_QUICKACK, &enabled,
+                          sizeof enabled);
 #endif
-	}
+      }
 
-	pid_t clientPid = -1;
-	bool trusted = false;
+      pid_t clientPid = -1;
+      bool trusted = false;
 
-	/* Get the identity of the caller, if possible. */
-	if (remoteAddr.ss_family == AF_UNIX) {
+      /* Get the identity of the caller, if possible. */
+      if (remoteAddr.ss_family == AF_UNIX)
+        {
 #if defined(SO_PEERCRED)
-	    ucred cred;
-	    socklen_t credLen = sizeof(cred);
-	    if (getsockopt(remote, SOL_SOCKET, SO_PEERCRED,
-			   &cred, &credLen) == -1)
-		throw SysError("getting peer credentials");
-
-	    clientPid = cred.pid;
-	    clientUid = cred.uid;
-	    clientGid = cred.gid;
-	    trusted = clientUid == 0;
-
-            struct passwd * pw = getpwuid(cred.uid);
-            string user = pw ? pw->pw_name : std::to_string(cred.uid);
-
-	    printMsg(lvlInfo,
-		     format((string) "accepted connection from pid %1%, user %2%")
-		     % clientPid % user);
+          ucred cred;
+          socklen_t credLen = sizeof (cred);
+          if (getsockopt (remote, SOL_SOCKET, SO_PEERCRED, &cred, &credLen)
+              == -1)
+            throw SysError ("getting peer credentials");
+
+          clientPid = cred.pid;
+          clientUid = cred.uid;
+          clientGid = cred.gid;
+          trusted = clientUid == 0;
+
+          struct passwd *pw = getpwuid (cred.uid);
+          string user = pw ? pw->pw_name : std::to_string (cred.uid);
+
+          printMsg (
+              lvlInfo,
+              format ((string) "accepted connection from pid %1%, user %2%")
+                  % clientPid % user);
 #endif
-	} else {
-	    char address_str[128];
-	    const char *result;
-
-	    if (remoteAddr.ss_family == AF_INET) {
-		struct sockaddr_in *addr = (struct sockaddr_in *) &remoteAddr;
-		result = inet_ntop(AF_INET, &addr->sin_addr,
-				   address_str, sizeof address_str);
-	    } else if (remoteAddr.ss_family == AF_INET6) {
-		struct sockaddr_in6 *addr = (struct sockaddr_in6 *) &remoteAddr;
-		result = inet_ntop(AF_INET6, &addr->sin6_addr,
-				   address_str, sizeof address_str);
-	    } else {
-		result = NULL;
-	    }
-
-	    if (result != NULL) {
-		printMsg(lvlInfo,
-			 format("accepted connection from %1%")
-			 % address_str);
-	    }
-	}
-
-	/* Fork a child to handle the connection. */
-	startProcess([&]() {
-                close(fdSocket);
-
-                /* Background the daemon. */
-                if (setsid() == -1)
-                    throw SysError(format("creating a new session"));
-
-                /* Restore normal handling of SIGCHLD. */
-                setSigChldAction(false);
-
-                /* For debugging, stuff the pid into argv[1]. */
-                if (clientPid != -1 && argvSaved[1]) {
-                    string processName = std::to_string(clientPid);
-                    strncpy(argvSaved[1], processName.c_str(), strlen(argvSaved[1]));
-                }
-
-                /* Store the client's user and group for this connection. This
-                   has to be done in the forked process since it is per
-                   connection.  Setting these to -1 means: do not change.  */
-                settings.clientUid = clientUid;
-		settings.clientGid = clientGid;
-                isRemoteConnection = (remoteAddr.ss_family != AF_UNIX);
-
-                /* Handle the connection. */
-                from.fd = remote;
-                to.fd = remote;
-                processConnection(trusted, clientUid);
-
-                exit(0);
-            }, false, "unexpected build daemon error: ", true);
-
-    } catch (Interrupted & e) {
-	throw;
-    } catch (Error & e) {
-	printMsg(lvlError, format("error processing connection: %1%") % e.msg());
+        }
+      else
+        {
+          char address_str[128];
+          const char *result;
+
+          if (remoteAddr.ss_family == AF_INET)
+            {
+              struct sockaddr_in *addr = (struct sockaddr_in *)&remoteAddr;
+              result = inet_ntop (AF_INET, &addr->sin_addr, address_str,
+                                  sizeof address_str);
+            }
+          else if (remoteAddr.ss_family == AF_INET6)
+            {
+              struct sockaddr_in6 *addr = (struct sockaddr_in6 *)&remoteAddr;
+              result = inet_ntop (AF_INET6, &addr->sin6_addr, address_str,
+                                  sizeof address_str);
+            }
+          else
+            {
+              result = NULL;
+            }
+
+          if (result != NULL)
+            {
+              printMsg (lvlInfo,
+                        format ("accepted connection from %1%") % address_str);
+            }
+        }
+
+      /* Fork a child to handle the connection. */
+      startProcess (
+          [&] () {
+            close (fdSocket);
+
+            /* Background the daemon. */
+            if (setsid () == -1)
+              throw SysError (format ("creating a new session"));
+
+            /* Restore normal handling of SIGCHLD. */
+            setSigChldAction (false);
+
+            /* For debugging, stuff the pid into argv[1]. */
+            if (clientPid != -1 && argvSaved[1])
+              {
+                string processName = std::to_string (clientPid);
+                strncpy (argvSaved[1], processName.c_str (),
+                         strlen (argvSaved[1]));
+              }
+
+            /* Store the client's user and group for this connection. This
+               has to be done in the forked process since it is per
+               connection.  Setting these to -1 means: do not change.  */
+            settings.clientUid = clientUid;
+            settings.clientGid = clientGid;
+            isRemoteConnection = (remoteAddr.ss_family != AF_UNIX);
+
+            /* Handle the connection. */
+            from.fd = remote;
+            to.fd = remote;
+            processConnection (trusted, clientUid);
+
+            exit (0);
+          },
+          false, "unexpected build daemon error: ", true);
+    }
+  catch (Interrupted &e)
+    {
+      throw;
+    }
+  catch (Error &e)
+    {
+      printMsg (lvlError,
+                format ("error processing connection: %1%") % e.msg ());
     }
 }
 
-static void daemonLoop(const std::vector<int>& sockets)
+static void
+daemonLoop (const std::vector<int> &sockets)
 {
-    if (chdir("/") == -1)
-        throw SysError("cannot change current directory");
+  if (chdir ("/") == -1)
+    throw SysError ("cannot change current directory");
 
-    /* Get rid of children automatically; don't let them become
-       zombies. */
-    setSigChldAction(true);
+  /* Get rid of children automatically; don't let them become
+     zombies. */
+  setSigChldAction (true);
 
-    /* Mark sockets as close-on-exec.  */
-    for(int fd: sockets) {
-	closeOnExec(fd);
+  /* Mark sockets as close-on-exec.  */
+  for (int fd : sockets)
+    {
+      closeOnExec (fd);
     }
 
-    /* Prepare the FD set corresponding to SOCKETS.  */
-    auto initializeFDSet = [&](fd_set *set) {
-	FD_ZERO(set);
-	for (int fd: sockets) {
-	    FD_SET(fd, set);
-	}
-    };
+  /* Prepare the FD set corresponding to SOCKETS.  */
+  auto initializeFDSet = [&] (fd_set *set) {
+    FD_ZERO (set);
+    for (int fd : sockets)
+      {
+        FD_SET (fd, set);
+      }
+  };
+
+  /* Loop accepting connections. */
+  while (1)
+    {
+      fd_set readfds;
+
+      initializeFDSet (&readfds);
+      int count
+          = select (*std::max_element (sockets.begin (), sockets.end ()) + 1,
+                    &readfds, NULL, NULL, NULL);
+      if (count < 0)
+        {
+          int err = errno;
+          if (err == EINTR)
+            continue;
+          throw SysError (format ("select error: %1%") % strerror (err));
+        }
 
-    /* Loop accepting connections. */
-    while (1) {
-	fd_set readfds;
-
-	initializeFDSet(&readfds);
-	int count =
-	    select(*std::max_element(sockets.begin(), sockets.end()) + 1,
-		   &readfds, NULL, NULL,
-		   NULL);
-	if (count < 0) {
-	    int err = errno;
-	    if (err == EINTR)
-		continue;
-	    throw SysError(format("select error: %1%") % strerror(err));
-	}
-
-	for (unsigned int i = 0; i < sockets.size(); i++) {
-	    if (FD_ISSET(sockets[i], &readfds)) {
-		acceptConnection(sockets[i]);
-	    }
-	}
+      for (unsigned int i = 0; i < sockets.size (); i++)
+        {
+          if (FD_ISSET (sockets[i], &readfds))
+            {
+              acceptConnection (sockets[i]);
+            }
+        }
     }
 }
 
-
-void run(const std::vector<int>& sockets)
+void
+run (const std::vector<int> &sockets)
 {
-    daemonLoop(sockets);
+  daemonLoop (sockets);
 }
diff --git a/nix/nix-daemon/shared.hh b/nix/nix-daemon/shared.hh
index 98ec97410b..c0ba42042a 100644
--- a/nix/nix-daemon/shared.hh
+++ b/nix/nix-daemon/shared.hh
@@ -20,8 +20,8 @@
 
 #pragma once
 
-#include <stdlib.h>
 #include <signal.h>
+#include <stdlib.h>
 
 extern volatile ::sig_atomic_t blockInt;
 
