libstore: expose the allowDaemon parameter of openStore()

This allows other functions to parameterize over it themselves. An
enum class is used to avoid API misuse.

Change-Id: I6a6a6964d2b5ad47ae5ea9eb11af9b6373ce2141
This commit is contained in:
Emily
2025-06-29 01:13:48 +01:00
parent 3cfce7b37e
commit b395831510
4 changed files with 25 additions and 30 deletions
+1 -1
View File
@@ -90,7 +90,7 @@ static int main_build_remote(AsyncIoRoot & aio, std::string programName, Strings
// versions of the build hook (where we don't need one hook process per
// build) we should change this to using a daemon connection, ideally a
// daemon connection provided by the parent via file descriptor passing
auto store = aio.blockOn(openNonDaemonStore());
auto store = aio.blockOn(openStore(settings.storeUri, {}, AllowDaemon::Disallow));
/* It would be more appropriate to use $XDG_RUNTIME_DIR, since
that gets cleared on reboot, but it wouldn't work on macOS. */
+5 -17
View File
@@ -1368,11 +1368,11 @@ static bool isNonUriPath(const std::string & spec)
}
static std::optional<ref<Store>>
openFromNonUri(const std::string & uri, const StoreConfig::Params & params, bool allowDaemon)
openFromNonUri(const std::string & uri, const StoreConfig::Params & params, AllowDaemon allowDaemon)
{
if (uri == "" || uri == "auto") {
auto stateDir = getOr(params, "state", settings.nixStateDir);
if (allowDaemon && pathExists(settings.nixDaemonSocketFile)) {
if (allowDaemon == AllowDaemon::Allow && pathExists(settings.nixDaemonSocketFile)) {
return make_ref<UDSRemoteStore>(params);
} else if (access(stateDir.c_str(), R_OK | W_OK) == 0) {
return LocalStore::makeLocalStore(params);
@@ -1403,7 +1403,7 @@ openFromNonUri(const std::string & uri, const StoreConfig::Params & params, bool
else
return LocalStore::makeLocalStore(params);
} else if (uri == "daemon") {
if (!allowDaemon) {
if (allowDaemon == AllowDaemon::Disallow) {
throw Error("tried to open a daemon store in a context that doesn't support this");
}
return make_ref<UDSRemoteStore>(params);
@@ -1446,8 +1446,8 @@ static std::string extractConnStr(const std::string &proto, const std::string &c
return connStr;
}
static kj::Promise<Result<ref<Store>>>
openStore(const std::string & uri_, const StoreConfig::Params & extraParams, bool allowDaemon)
kj::Promise<Result<ref<Store>>>
openStore(const std::string & uri_, const StoreConfig::Params & extraParams, AllowDaemon allowDaemon)
try {
auto params = extraParams;
try {
@@ -1486,18 +1486,6 @@ try {
co_return result::current_exception();
}
kj::Promise<Result<ref<Store>>>
openStore(const std::string & uri, const StoreConfig::Params & extraParams)
{
return openStore(uri, extraParams, true);
}
kj::Promise<Result<ref<Store>>>
openNonDaemonStore(const std::string & uri, const StoreConfig::Params & extraParams)
{
return openStore(uri, extraParams, false);
}
kj::Promise<Result<std::list<ref<Store>>>> getDefaultSubstituters()
try {
static Sync<std::optional<std::list<ref<Store>>>, AsyncMutex> stores;
+18 -11
View File
@@ -968,6 +968,15 @@ void removeTempRoots();
kj::Promise<Result<OutputPathMap>>
resolveDerivedPath(Store &, const DerivedPath::Built &, Store * evalStore = nullptr);
/**
* Whether to allow daemon connections in openStore().
*/
enum class AllowDaemon
{
Disallow,
Allow,
};
/**
* @return a Store object to access the Nix store denoted by
* uri (slight misnomer...).
@@ -983,7 +992,8 @@ resolveDerivedPath(Store &, const DerivedPath::Built &, Store * evalStore = null
* - unix://<path>: The Nix store accessed via a Unix domain socket
* connection to nix-daemon, with the socket located at <path>.
*
* - auto or ‘’: Try `daemon` if the daemon socket exists and `local` otherwise.
* - auto or ‘’: Try `daemon` if the daemon socket exists and
* `allowDaemon` is `AllowDaemon::Allow`, and `local` otherwise.
*
* - file://<path>: A binary cache stored in <path>.
*
@@ -997,18 +1007,15 @@ resolveDerivedPath(Store &, const DerivedPath::Built &, Store * evalStore = null
*
* You can pass parameters to the store implementation by appending
* ?key=value&key=value&... to the URI.
*
* @param allowDaemon Whether to allow connections to the daemon. The
* default should only be overridden with very good reason. When this is
* `AllowDaemon::Disallow`, `""` and `"auto"` URIs will only attempt the
* local method, and `"daemon"` URIs will cause a hard error.
*/
kj::Promise<Result<ref<Store>>> openStore(const std::string & uri = settings.storeUri.get(),
const StoreConfig::Params & extraParams = {});
/**
* Same as `openStore`, but no connections to the daemon are attempted.
* `""` and `"auto"` urls will only attempt the `local` methods, `"daemon"`
* urls will cause a hard error.
*/
kj::Promise<Result<ref<Store>>> openNonDaemonStore(
const std::string & uri = settings.storeUri.get(), const StoreConfig::Params & extraParams = {}
);
const StoreConfig::Params & extraParams = {},
AllowDaemon allowDaemon = AllowDaemon::Allow);
/**
* @return the default substituter stores, defined by the
+1 -1
View File
@@ -230,7 +230,7 @@ try {
StoreConfig::Params params; // FIXME: get params from somewhere
// Disable caching since the client already does that.
params["path-info-cache-size"] = "0";
co_return TRY_AWAIT(openNonDaemonStore(settings.storeUri, params));
co_return TRY_AWAIT(openStore(settings.storeUri, params, AllowDaemon::Disallow));
} catch (...) {
co_return result::current_exception();
}