about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2016-01-31T09·19+0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2016-01-31T09·28+0100
commit9e7c1a4bbdbe6129dd9dc385776612c307d3d1bb (patch)
tree4c213493e9c6fa1045df1b4192061602e31c78eb /src
parent4fa08f3edb413e65816441acfb6c3befee730a06 (diff)
Use the daemon when we don't have write access to the Nix database
Diffstat (limited to 'src')
-rw-r--r--src/libstore/local-store.cc8
-rw-r--r--src/libstore/local-store.hh6
-rw-r--r--src/libstore/remote-store.cc10
-rw-r--r--src/libstore/store-api.cc20
4 files changed, 31 insertions, 13 deletions
diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc
index 9199b12063a0..55bd3e70e7b8 100644
--- a/src/libstore/local-store.cc
+++ b/src/libstore/local-store.cc
@@ -397,9 +397,15 @@ int LocalStore::getSchema()
 }
 
 
+bool LocalStore::haveWriteAccess()
+{
+    return access(settings.nixDBPath.c_str(), R_OK | W_OK) == 0;
+}
+
+
 void LocalStore::openDB(bool create)
 {
-    if (access(settings.nixDBPath.c_str(), R_OK | W_OK))
+    if (!haveWriteAccess())
         throw SysError(format("Nix database directory ‘%1%’ is not writable") % settings.nixDBPath);
 
     /* Open the Nix database. */
diff --git a/src/libstore/local-store.hh b/src/libstore/local-store.hh
index ebdf19bf1359..5b27f907236d 100644
--- a/src/libstore/local-store.hh
+++ b/src/libstore/local-store.hh
@@ -253,6 +253,12 @@ private:
 
     int getSchema();
 
+public:
+
+    static bool haveWriteAccess();
+
+private:
+
     void openDB(bool create);
 
     void makeStoreWritable();
diff --git a/src/libstore/remote-store.cc b/src/libstore/remote-store.cc
index 262e4650bfb5..679210d4cb16 100644
--- a/src/libstore/remote-store.cc
+++ b/src/libstore/remote-store.cc
@@ -50,14 +50,8 @@ void RemoteStore::openConnection(bool reserveSpace)
     if (initialised) return;
     initialised = true;
 
-    string remoteMode = getEnv("NIX_REMOTE");
-
-    if (remoteMode == "daemon")
-        /* Connect to a daemon that does the privileged work for
-           us. */
-        connectToDaemon();
-    else
-        throw Error(format("invalid setting for NIX_REMOTE, ‘%1%’") % remoteMode);
+    /* Connect to a daemon that does the privileged work for us. */
+    connectToDaemon();
 
     from.fd = fdSocket;
     to.fd = fdSocket;
diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc
index a73ebd824264..f5035d3230fd 100644
--- a/src/libstore/store-api.cc
+++ b/src/libstore/store-api.cc
@@ -311,10 +311,22 @@ std::shared_ptr<StoreAPI> store;
 
 std::shared_ptr<StoreAPI> openStore(bool reserveSpace)
 {
-    if (getEnv("NIX_REMOTE") == "")
-        return std::shared_ptr<StoreAPI>(new LocalStore(reserveSpace));
-    else
-        return std::shared_ptr<StoreAPI>(new RemoteStore());
+    enum { mDaemon, mLocal, mAuto } mode;
+
+    mode = getEnv("NIX_REMOTE") == "daemon" ? mDaemon : mAuto;
+
+    if (mode == mAuto) {
+        if (LocalStore::haveWriteAccess())
+            mode = mLocal;
+        else if (pathExists(settings.nixDaemonSocketFile))
+            mode = mDaemon;
+        else
+            mode = mLocal;
+    }
+
+    return mode == mDaemon
+        ? (std::shared_ptr<StoreAPI>) std::make_shared<RemoteStore>()
+        : std::make_shared<LocalStore>(reserveSpace);
 }