Use the daemon when we don't have write access to the Nix database

This commit is contained in:
Eelco Dolstra 2016-01-31 10:19:14 +01:00
parent 4fa08f3edb
commit 9e7c1a4bbd
7 changed files with 57 additions and 13 deletions

View file

@ -12,6 +12,7 @@
</partintro>
-->
<xi:include href="rl-1.12.xml" />
<xi:include href="rl-1.11.xml" />
<xi:include href="rl-1.10.xml" />
<xi:include href="rl-1.9.xml" />

View file

@ -0,0 +1,24 @@
<section xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"
version="5.0"
xml:id="ssec-relnotes-1.12">
<title>Release 1.12 (TBA)</title>
<para>This release has the following new features:</para>
<itemizedlist>
<listitem>
<para>It is no longer necessary to set the
<envar>NIX_REMOTE</envar> environment variable if you need to use
the Nix daemon. Nix will use the daemon automatically if you dont
have write access to the Nix database.</para>
</listitem>
</itemizedlist>
<para>This release has contributions from TBD.</para>
</section>

View file

@ -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. */

View file

@ -253,6 +253,12 @@ private:
int getSchema();
public:
static bool haveWriteAccess();
private:
void openDB(bool create);
void makeStoreWritable();

View file

@ -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;

View file

@ -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);
}

View file

@ -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