Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor unix domain socket store config #11109

Merged
merged 27 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f90e771
feat: Refactor unix domain socket store config
fzakaria Jul 15, 2024
1f72267
uds-remote-store is now clang-formatted
fzakaria Jul 15, 2024
660d868
Removed clang-format changes
fzakaria Jul 15, 2024
6585c6e
Reference public variable
fzakaria Jul 15, 2024
10c5c65
Update src/libstore/uds-remote-store.hh
fzakaria Jul 15, 2024
030d91d
Move constant to within Config object
fzakaria Jul 15, 2024
7169905
Lowercase constexpr
fzakaria Jul 15, 2024
4af5092
Update src/libstore/store-api.cc
fzakaria Jul 15, 2024
dd74b72
Revert back returning daemon for URI if path empty
fzakaria Jul 15, 2024
01fbb4b
Added unit test
fzakaria Jul 15, 2024
6eb3303
Added to meson
fzakaria Jul 15, 2024
d3275e8
Changed to protected and added another test
fzakaria Jul 16, 2024
0087d7d
Clang formatted test file
fzakaria Jul 16, 2024
1e97346
Merge remote-tracking branch 'upstream/master' into issue-10766-unix-…
Ericson2314 Jul 16, 2024
e733f6d
Move `UdsRemoteStore{,Config}` defs out of headers, and other cleanups
Ericson2314 Jul 16, 2024
e435f3e
Add two more headers to try to provide missing template instantiations
Ericson2314 Jul 16, 2024
ed2ccdc
Add globals.hh to uds-remote-store.cc
fzakaria Jul 16, 2024
e9c0636
Fix linking issue, convert many more
Ericson2314 Jul 16, 2024
8162dbd
Merge remote-tracking branch 'upstream/master' into issue-10766-unix-…
Ericson2314 Jul 16, 2024
f23d168
`make format`
Ericson2314 Jul 16, 2024
c4c148f
Declare some template specializations in the header
Ericson2314 Jul 16, 2024
1e25c22
Revert "Declare some template specializations in the header"
Ericson2314 Jul 16, 2024
7b135d5
Comment out tests
fzakaria Jul 17, 2024
95b36f4
Merge branch 'issue-10766-unix-socket' of github.com:fzakaria/nix int…
Ericson2314 Jul 17, 2024
0e65638
Merge remote-tracking branch 'upstream/master' into issue-10766-unix-…
Ericson2314 Jul 17, 2024
34f876f
Format, CI hopefully happy now
Ericson2314 Jul 17, 2024
8886d94
Comment out another test
Ericson2314 Jul 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions src/libstore/uds-remote-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
#include "unix-domain-socket.hh"
#include "worker-protocol.hh"

#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

Expand All @@ -27,34 +25,30 @@ std::string UDSRemoteStoreConfig::doc()
}


UDSRemoteStore::UDSRemoteStore(const Params & params)
UDSRemoteStore::UDSRemoteStore(std::string_view scheme, std::string_view authority, const Params & params)
: StoreConfig(params)
, LocalFSStoreConfig(params)
, RemoteStoreConfig(params)
, UDSRemoteStoreConfig(params)
, UDSRemoteStoreConfig(scheme, authority, params)
, Store(params)
, LocalFSStore(params)
, RemoteStore(params)
{
}


UDSRemoteStore::UDSRemoteStore(
std::string_view scheme,
PathView socket_path,
const Params & params)
: UDSRemoteStore(params)
std::string UDSRemoteStore::getPathOrDefault() const
{
if (!socket_path.empty())
path.emplace(socket_path);
return path.value_or(settings.nixDaemonSocketFile);
}


std::string UDSRemoteStore::getUri()
{
if (path) {
return std::string("unix://") + *path;
return std::string(scheme) + "://" + *path;
} else {
// FIXME: Not clear why we return daemon here and not default to
// settings.nixDaemonSocketFile
//
// unix:// with no path also works. Change what we return?
return "daemon";
}
Expand All @@ -74,7 +68,7 @@ ref<RemoteStore::Connection> UDSRemoteStore::openConnection()
/* Connect to a daemon that does the privileged work for us. */
conn->fd = createUnixDomainSocket();

nix::connect(toSocket(conn->fd.get()), path ? *path : settings.nixDaemonSocketFile);
nix::connect(toSocket(conn->fd.get()), getPathOrDefault());

conn->from.fd = conn->fd.get();
conn->to.fd = conn->fd.get();
Expand Down
49 changes: 44 additions & 5 deletions src/libstore/uds-remote-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,43 @@ namespace nix {

struct UDSRemoteStoreConfig : virtual LocalFSStoreConfig, virtual RemoteStoreConfig
{
UDSRemoteStoreConfig(const Params & params)

protected:
static constexpr char const * scheme = "unix";

public:
fzakaria marked this conversation as resolved.
Show resolved Hide resolved

// TODO(fzakaria): Delete this constructor once moved over to the factory pattern
// outlined in https://github.com/NixOS/nix/issues/10766
using LocalFSStoreConfig::LocalFSStoreConfig;
using RemoteStoreConfig::RemoteStoreConfig;

UDSRemoteStoreConfig(std::string_view scheme, std::string_view authority, const Params & params)
: StoreConfig(params)
, LocalFSStoreConfig(params)
, RemoteStoreConfig(params)
{
if (scheme != UDSRemoteStoreConfig::scheme) {
throw UsageError("Scheme must be 'unix'");
}

if (!authority.empty()) {
path.emplace(authority);
}
}

const std::string name() override { return "Local Daemon Store"; }

std::string doc() override;

/**
* The path to the unix domain socket.
*
* The default *could be* settings.nixDaemonSocketFile but that
* won't pick up live changes unfortunately. This optional handling is instead
* handled on opening of the connection.
*/
std::optional<std::string> path;
};

class UDSRemoteStore : public virtual UDSRemoteStoreConfig
Expand All @@ -27,16 +54,27 @@ class UDSRemoteStore : public virtual UDSRemoteStoreConfig
{
public:

UDSRemoteStore(const Params & params);
/*
\deprecated This is the old API to construct the store.
A bit gross that we now pass empty string but this is knowing
that empty string will later default to the same nixDaemonSocketFile.
Why don't we just wire it all through?
I believe there are cases where it will live reload so we want to
continue to account for that.
*/
UDSRemoteStore(const Params & params)
: nix::UDSRemoteStore(scheme, "", params) {}

UDSRemoteStore(
std::string_view scheme,
PathView path,
// authority is the socket path
std::string_view authority,
const Params & params);

std::string getUri() override;

static std::set<std::string> uriSchemes()
{ return {"unix"}; }
{ return {scheme}; }

ref<SourceAccessor> getFSAccessor(bool requireValidPath = true) override
{ return LocalFSStore::getFSAccessor(requireValidPath); }
Expand All @@ -63,7 +101,8 @@ private:
};

ref<RemoteStore::Connection> openConnection() override;
std::optional<std::string> path;

std::string getPathOrDefault() const;
};

}
1 change: 1 addition & 0 deletions tests/unit/libstore/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ sources = files(
'serve-protocol.cc',
'store-reference.cc',
'worker-protocol.cc',
'uds-remote-store.cc',
)

include_dirs = [include_directories('.')]
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/libstore/uds-remote-store.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <gtest/gtest.h>

#include "uds-remote-store.hh"

namespace nix {

TEST(UDSRemoteStore, constructConfig)
{
UDSRemoteStoreConfig config{"unix", "/tmp/socket", {}};

EXPECT_EQ(*config.path, "/tmp/socket");
}

TEST(UDSRemoteStore, constructConfigWrongScheme)
{
EXPECT_THROW(UDSRemoteStoreConfig("http", "/tmp/socket", {}), UsageError);
}

} // namespace nix
Loading