Skip to content

Commit 855c772

Browse files
committed
daemon: use per-store state directory for socket path
1 parent 63b3644 commit 855c772

File tree

8 files changed

+37
-28
lines changed

8 files changed

+37
-28
lines changed

src/libstore/globals.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@
4848

4949
namespace nix {
5050

51-
/* The default location of the daemon socket, relative to nixStateDir.
52-
The socket is in a directory to allow you to control access to the
53-
Nix daemon by setting the mode/ownership of the directory
54-
appropriately. (This wouldn't work on the socket itself since it
55-
must be deleted and recreated on startup.) */
56-
#define DEFAULT_SOCKET_PATH "daemon-socket" / "socket"
57-
5851
LogFileSettings::LogFileSettings()
5952
: nixLogDir(getEnvOsNonEmpty(OS_STR("NIX_LOG_DIR"))
6053
.transform([](auto && s) { return std::filesystem::path(s); })
@@ -92,12 +85,6 @@ Settings::Settings()
9285
})
9386
.transform([](auto && s) { return canonPath(s); })
9487
.value())
95-
, nixDaemonSocketFile(
96-
getEnvOsNonEmpty(OS_STR("NIX_DAEMON_SOCKET_PATH"))
97-
.transform([](auto && s) { return std::filesystem::path(s); })
98-
.or_else([this]() -> std::optional<std::filesystem::path> { return nixStateDir / DEFAULT_SOCKET_PATH; })
99-
.transform([](auto && s) { return canonPath(s); })
100-
.value())
10188
{
10289
#ifndef _WIN32
10390
buildUsersGroup = isRootUser() ? "nixbld" : "";

src/libstore/include/nix/store/globals.hh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,6 @@ public:
174174
*/
175175
std::filesystem::path nixStateDir;
176176

177-
/**
178-
* File name of the socket the daemon listens to.
179-
*/
180-
std::filesystem::path nixDaemonSocketFile;
181-
182177
Setting<StoreReference> storeUri{
183178
this,
184179
StoreReference::parse(getEnv("NIX_REMOTE").value_or("auto")),

src/libstore/include/nix/store/local-fs-store.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public:
7171
"real",
7272
"Physical path of the Nix store.",
7373
};
74+
75+
/**
76+
* The default daemon socket path for this store.
77+
*
78+
* Returns `NIX_DAEMON_SOCKET_PATH` if set, otherwise
79+
* `stateDir / "daemon-socket" / "socket"`.
80+
*/
81+
std::filesystem::path getDefaultDaemonSocketPath() const;
7482
};
7583

7684
struct alignas(8) /* Work around ASAN failures on i686-linux. */

src/libstore/include/nix/store/uds-remote-store.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ struct UDSRemoteStoreConfig : std::enable_shared_from_this<UDSRemoteStoreConfig>
2828
/**
2929
* The path to the unix domain socket.
3030
*
31-
* The default is `settings.nixDaemonSocketFile`, but we don't write
32-
* that below, instead putting in the constructor.
31+
* The default is `stateDir / "daemon-socket/socket"`, overridden by
32+
* `NIX_DAEMON_SOCKET_PATH` if set.
3333
*/
3434
std::filesystem::path path;
3535

src/libstore/local-fs-store.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "nix/util/archive.hh"
2+
#include "nix/util/environment-variables.hh"
23
#include "nix/util/posix-source-accessor.hh"
34
#include "nix/store/store-api.hh"
45
#include "nix/store/local-fs-store.hh"
@@ -18,6 +19,12 @@ std::filesystem::path LocalFSStoreConfig::getDefaultLogDir()
1819
return settings.getLogFileSettings().nixLogDir;
1920
}
2021

22+
std::filesystem::path LocalFSStoreConfig::getDefaultDaemonSocketPath() const
23+
{
24+
auto env = getEnvNonEmpty("NIX_DAEMON_SOCKET_PATH");
25+
return env ? std::filesystem::path{*env} : std::filesystem::path{stateDir.get()} / "daemon-socket" / "socket";
26+
}
27+
2128
LocalFSStoreConfig::LocalFSStoreConfig(const std::filesystem::path & rootDir, const Params & params)
2229
: StoreConfig(params)
2330
/* Default `?root` from `rootDir` if non set

src/libstore/store-registration.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "nix/store/local-store.hh"
44
#include "nix/store/uds-remote-store.hh"
55
#include "nix/store/globals.hh"
6+
#include "nix/util/environment-variables.hh"
7+
8+
#include <filesystem>
69

710
namespace nix {
811

@@ -33,7 +36,9 @@ ref<StoreConfig> resolveStoreConfig(StoreReference && storeURI)
3336
auto stateDir = getOr(params, "state", settings.nixStateDir.string());
3437
if (access(stateDir.c_str(), R_OK | W_OK) == 0)
3538
return make_ref<LocalStore::Config>(params);
36-
else if (pathExists(settings.nixDaemonSocketFile))
39+
else if (pathExists(
40+
getEnvNonEmpty("NIX_DAEMON_SOCKET_PATH")
41+
.value_or((std::filesystem::path{stateDir} / "daemon-socket" / "socket").string())))
3742
return make_ref<UDSRemoteStore::Config>(params);
3843
#ifdef __linux__
3944
else if (

src/libstore/uds-remote-store.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ UDSRemoteStoreConfig::UDSRemoteStoreConfig(const std::filesystem::path & path, c
2323
: Store::Config{params}
2424
, LocalFSStore::Config{params}
2525
, RemoteStore::Config{params}
26-
, path{path.empty() ? settings.nixDaemonSocketFile : path}
26+
, path{
27+
path.empty() ? getDefaultDaemonSocketPath() : path,
28+
}
2729
{
2830
}
2931

@@ -34,10 +36,8 @@ std::string UDSRemoteStoreConfig::doc()
3436
;
3537
}
3638

37-
// A bit gross that we now pass empty string but this is knowing that
38-
// empty string will later default to the same nixDaemonSocketFile. Why
39-
// don't we just wire it all through? I believe there are cases where it
40-
// will live reload so we want to continue to account for that.
39+
// Empty authority will default to the per-store socket path based on
40+
// stateDir.
4141
UDSRemoteStoreConfig::UDSRemoteStoreConfig(const Params & params)
4242
: UDSRemoteStoreConfig("", params)
4343
{
@@ -56,7 +56,7 @@ StoreReference UDSRemoteStoreConfig::getReference() const
5656
/* We specifically return "daemon" here instead of "unix://" or "unix://${path}"
5757
* to be more compatible with older versions of nix. Some tooling out there
5858
* tries hard to parse store references and it might not be able to handle "unix://". */
59-
if (path == settings.nixDaemonSocketFile)
59+
if (path == getEnvNonEmpty("NIX_DAEMON_SOCKET_PATH").value_or(getDefaultDaemonSocketPath().string()))
6060
return {
6161
.variant = StoreReference::Daemon{},
6262
.params = getQueryParams(),

src/nix/unix/daemon.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "nix/util/unix-domain-socket.hh"
55
#include "nix/cmd/command.hh"
66
#include "nix/main/shared.hh"
7+
#include "nix/store/local-fs-store.hh"
78
#include "nix/store/local-store.hh"
89
#include "nix/store/remote-store.hh"
910
#include "nix/store/remote-store-connection.hh"
@@ -280,10 +281,16 @@ static void daemonLoop(ref<const StoreConfig> storeConfig, std::optional<Trusted
280281
}
281282
#endif
282283

284+
auto socketPath = [&]() -> std::filesystem::path {
285+
if (auto * localFSConfig = dynamic_cast<const LocalFSStoreConfig *>(&*storeConfig))
286+
return localFSConfig->getDefaultDaemonSocketPath();
287+
throw Error("cannot run daemon for non-local-filesystem store");
288+
}();
289+
283290
try {
284291
unix::serveUnixSocket(
285292
{
286-
.socketPath = settings.nixDaemonSocketFile,
293+
.socketPath = socketPath,
287294
.socketMode = 0666,
288295
.auxiliaryFd = sigChldPipe.readSide.get(),
289296
.onAuxiliaryFdPollin =

0 commit comments

Comments
 (0)