From 9e7c1a4bbdbe6129dd9dc385776612c307d3d1bb Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sun, 31 Jan 2016 10:19:14 +0100 Subject: [PATCH] Use the daemon when we don't have write access to the Nix database --- doc/manual/release-notes/release-notes.xml | 1 + doc/manual/release-notes/rl-1.12.xml | 24 ++++++++++++++++++++++ src/libstore/local-store.cc | 8 +++++++- src/libstore/local-store.hh | 6 ++++++ src/libstore/remote-store.cc | 10 ++------- src/libstore/store-api.cc | 20 ++++++++++++++---- tests/dump-db.sh | 1 + 7 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 doc/manual/release-notes/rl-1.12.xml diff --git a/doc/manual/release-notes/release-notes.xml b/doc/manual/release-notes/release-notes.xml index 0aa3e8717..8c2deb394 100644 --- a/doc/manual/release-notes/release-notes.xml +++ b/doc/manual/release-notes/release-notes.xml @@ -12,6 +12,7 @@ --> + diff --git a/doc/manual/release-notes/rl-1.12.xml b/doc/manual/release-notes/rl-1.12.xml new file mode 100644 index 000000000..d6864b3f5 --- /dev/null +++ b/doc/manual/release-notes/rl-1.12.xml @@ -0,0 +1,24 @@ +
+ +Release 1.12 (TBA) + +This release has the following new features: + + + + + It is no longer necessary to set the + NIX_REMOTE environment variable if you need to use + the Nix daemon. Nix will use the daemon automatically if you don’t + have write access to the Nix database. + + + + +This release has contributions from TBD. + +
diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 9199b1206..55bd3e70e 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 ebdf19bf1..5b27f9072 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 262e4650b..679210d4c 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 a73ebd824..f5035d323 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -311,10 +311,22 @@ std::shared_ptr store; std::shared_ptr openStore(bool reserveSpace) { - if (getEnv("NIX_REMOTE") == "") - return std::shared_ptr(new LocalStore(reserveSpace)); - else - return std::shared_ptr(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) std::make_shared() + : std::make_shared(reserveSpace); } diff --git a/tests/dump-db.sh b/tests/dump-db.sh index 234b7ac02..57c8c4016 100644 --- a/tests/dump-db.sh +++ b/tests/dump-db.sh @@ -9,6 +9,7 @@ deps="$(nix-store -qR $TEST_ROOT/result)" nix-store --dump-db > $TEST_ROOT/dump rm -rf $NIX_DB_DIR +mkdir $NIX_DB_DIR nix-store --load-db < $TEST_ROOT/dump