Skip to content

Commit 5fefd3b

Browse files
committed
daemon: use per-store state directory for socket path
1 parent 8e3f73a commit 5fefd3b

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public:
6464

6565
PathSetting realStoreDir{
6666
this, rootDir.get() ? *rootDir.get() + "/nix/store" : storeDir, "real", "Physical path of the Nix store."};
67+
68+
/**
69+
* The default daemon socket path for this store, based on its state
70+
* directory.
71+
*/
72+
std::filesystem::path getDefaultDaemonSocketPath() const;
6773
};
6874

6975
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
@@ -33,8 +33,8 @@ struct UDSRemoteStoreConfig : std::enable_shared_from_this<UDSRemoteStoreConfig>
3333
/**
3434
* The path to the unix domain socket.
3535
*
36-
* The default is `settings.nixDaemonSocketFile`, but we don't write
37-
* that below, instead putting in the constructor.
36+
* The default is `stateDir / "daemon-socket/socket"`, overridden by
37+
* `NIX_DAEMON_SOCKET_PATH` if set.
3838
*/
3939
Path path;
4040

src/libstore/local-fs-store.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Path LocalFSStoreConfig::getDefaultLogDir()
1818
return settings.getLogFileSettings().nixLogDir.string();
1919
}
2020

21+
std::filesystem::path LocalFSStoreConfig::getDefaultDaemonSocketPath() const
22+
{
23+
return std::filesystem::path{stateDir.get()} / "daemon-socket" / "socket";
24+
}
25+
2126
LocalFSStoreConfig::LocalFSStoreConfig(PathView rootDir, const Params & params)
2227
: StoreConfig(params)
2328
/* Default `?root` from `rootDir` if non set

src/libstore/uds-remote-store.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ UDSRemoteStoreConfig::UDSRemoteStoreConfig(
2424
: Store::Config{params}
2525
, LocalFSStore::Config{params}
2626
, RemoteStore::Config{params}
27-
, path{authority.empty() ? settings.nixDaemonSocketFile.string() : authority}
27+
, path{
28+
!authority.empty() ? std::string{authority}
29+
: getEnvNonEmpty("NIX_DAEMON_SOCKET_PATH").value_or(getDefaultDaemonSocketPath().string())}
2830
{
2931
if (uriSchemes().count(scheme) == 0) {
3032
throw UsageError("Scheme must be 'unix'");
@@ -38,10 +40,8 @@ std::string UDSRemoteStoreConfig::doc()
3840
;
3941
}
4042

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

src/nix/unix/daemon.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,18 @@ static void daemonLoop(ref<StoreConfig> storeConfig, std::optional<TrustedFlag>
267267
}
268268
#endif
269269

270+
auto socketPath = [&]() -> std::filesystem::path {
271+
if (auto envPath = getEnvNonEmpty("NIX_DAEMON_SOCKET_PATH"))
272+
return *envPath;
273+
if (auto * localFSConfig = dynamic_cast<LocalFSStoreConfig *>(&*storeConfig))
274+
return localFSConfig->getDefaultDaemonSocketPath();
275+
return settings.nixDaemonSocketFile;
276+
}();
277+
270278
try {
271279
unix::serveUnixSocket(
272280
{
273-
.socketPath = settings.nixDaemonSocketFile,
281+
.socketPath = socketPath,
274282
.socketMode = 0666,
275283
},
276284
[&](AutoCloseFD remote, std::function<void()> closeListeners) {

0 commit comments

Comments
 (0)