Files
lix/lix/libstore/ssh-store.cc
T
Sebastian Widua 82c7e76c9c libstore: allow specifying port for ssh[-ng] stores
This also adds documentation for it in distributed-builds.md as that's
possibly the most common use-case for remote ssh stores

Support for `std::optional<uint16_t>` in Setting is also added since
setting a port is optional.

The line `#include "lix/libutil/strings.hh"` fixes that templates instanciations
in lix/libutil/config-impl.hh were using string utils without including
the header (why are they even there btw)

Change-Id: Id806c117c48cdf158d9d1cb1e639b0df31d9bf11
2025-02-22 20:37:45 +01:00

113 lines
3.1 KiB
C++

#include "lix/libstore/ssh-store.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libstore/remote-store.hh"
#include "lix/libstore/remote-store-connection.hh"
#include "lix/libstore/worker-protocol.hh"
#include "lix/libutil/pool.hh"
#include "lix/libstore/ssh.hh"
#include "lix/libutil/strings.hh"
namespace nix {
struct SSHStoreConfig : virtual RemoteStoreConfig, virtual CommonSSHStoreConfig
{
using RemoteStoreConfig::RemoteStoreConfig;
using CommonSSHStoreConfig::CommonSSHStoreConfig;
const Setting<Path> remoteProgram{this, "nix-daemon", "remote-program",
"Path to the `nix-daemon` executable on the remote machine."};
const std::string name() override { return "Experimental SSH Store"; }
std::string doc() override
{
return
#include "ssh-store.md"
;
}
};
class SSHStore final : public RemoteStore
{
SSHStoreConfig config_;
public:
SSHStore(const std::string & scheme, const std::string & host, SSHStoreConfig config)
: Store(config)
, RemoteStore(config)
, config_(std::move(config))
, host(host)
, master(
host,
config_.port,
config_.sshKey,
config_.sshPublicHostKey,
// Use SSH master only if using more than 1 connection.
connections->capacity() > 1,
config_.compress)
{
}
SSHStoreConfig & config() override { return config_; }
const SSHStoreConfig & config() const override { return config_; }
static std::set<std::string> uriSchemes() { return {"ssh-ng"}; }
std::string getUri() override
{
return *uriSchemes().begin() + "://" + host;
}
// FIXME extend daemon protocol, move implementation to RemoteStore
std::optional<std::string> getBuildLogExact(const StorePath & path) override
{ unsupported("getBuildLogExact"); }
protected:
struct Connection : RemoteStore::Connection
{
std::unique_ptr<SSHMaster::Connection> sshConn;
void closeWrite() override
{
sshConn->in.close();
}
};
ref<RemoteStore::Connection> openConnection() override;
std::string host;
SSHMaster master;
void setOptions(RemoteStore::Connection & conn) override
{
/* TODO Add a way to explicitly ask for some options to be
forwarded. One option: A way to query the daemon for its
settings, and then a series of params to SSHStore like
forward-cores or forward-overridden-cores that only
override the requested settings.
*/
};
};
ref<RemoteStore::Connection> SSHStore::openConnection()
{
auto conn = make_ref<Connection>();
std::string command = config_.remoteProgram + " --stdio";
if (config_.remoteStore.get() != "")
command += " --store " + shellEscape(config_.remoteStore.get());
conn->sshConn = master.startCommand(command);
conn->to = FdSink(conn->sshConn->in.get());
conn->from = FdSource(conn->sshConn->out.get());
return conn;
}
void registerSSHStore() {
StoreImplementations::add<SSHStore, SSHStoreConfig>();
}
}