Skip to content

Commit

Permalink
Merge branch 'document-store-options' of https://github.com/tweag/nix
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Sep 16, 2020
2 parents 0066ef6 + 77a0e2c commit 5080d4e
Show file tree
Hide file tree
Showing 30 changed files with 442 additions and 227 deletions.
2 changes: 1 addition & 1 deletion src/libexpr/flake/lockfile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace nix {
class Store;
struct StorePath;
class StorePath;
}

namespace nix::flake {
Expand Down
2 changes: 1 addition & 1 deletion src/libfetchers/fetchers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct InputScheme;

struct Input
{
friend class InputScheme;
friend struct InputScheme;

std::shared_ptr<InputScheme> scheme; // note: can be null
Attrs attrs;
Expand Down
3 changes: 2 additions & 1 deletion src/libstore/binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
namespace nix {

BinaryCacheStore::BinaryCacheStore(const Params & params)
: Store(params)
: BinaryCacheStoreConfig(params)
, Store(params)
{
if (secretKeyFile != "")
secretKey = std::unique_ptr<SecretKey>(new SecretKey(readFile(secretKeyFile)));
Expand Down
24 changes: 14 additions & 10 deletions src/libstore/binary-cache-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ namespace nix {

struct NarInfo;

class BinaryCacheStore : public Store
struct BinaryCacheStoreConfig : virtual StoreConfig
{
public:

const Setting<std::string> compression{this, "xz", "compression", "NAR compression method ('xz', 'bzip2', or 'none')"};
const Setting<bool> writeNARListing{this, false, "write-nar-listing", "whether to write a JSON file listing the files in each NAR"};
const Setting<bool> writeDebugInfo{this, false, "index-debug-info", "whether to index DWARF debug info files by build ID"};
const Setting<Path> secretKeyFile{this, "", "secret-key", "path to secret key used to sign the binary cache"};
const Setting<Path> localNarCache{this, "", "local-nar-cache", "path to a local cache of NARs"};
const Setting<bool> parallelCompression{this, false, "parallel-compression",
using StoreConfig::StoreConfig;

const Setting<std::string> compression{(StoreConfig*) this, "xz", "compression", "NAR compression method ('xz', 'bzip2', or 'none')"};
const Setting<bool> writeNARListing{(StoreConfig*) this, false, "write-nar-listing", "whether to write a JSON file listing the files in each NAR"};
const Setting<bool> writeDebugInfo{(StoreConfig*) this, false, "index-debug-info", "whether to index DWARF debug info files by build ID"};
const Setting<Path> secretKeyFile{(StoreConfig*) this, "", "secret-key", "path to secret key used to sign the binary cache"};
const Setting<Path> localNarCache{(StoreConfig*) this, "", "local-nar-cache", "path to a local cache of NARs"};
const Setting<bool> parallelCompression{(StoreConfig*) this, false, "parallel-compression",
"enable multi-threading compression, available for xz only currently"};
};

class BinaryCacheStore : public Store, public virtual BinaryCacheStoreConfig
{

private:

Expand Down Expand Up @@ -58,7 +62,7 @@ public:

public:

virtual void init();
virtual void init() override;

private:

Expand Down
9 changes: 7 additions & 2 deletions src/libstore/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2872,18 +2872,23 @@ void DerivationGoal::writeStructuredAttrs()
chownToBuilder(tmpDir + "/.attrs.sh");
}

struct RestrictedStoreConfig : LocalFSStoreConfig
{
using LocalFSStoreConfig::LocalFSStoreConfig;
const std::string name() { return "Restricted Store"; }
};

/* A wrapper around LocalStore that only allows building/querying of
paths that are in the input closures of the build or were added via
recursive Nix calls. */
struct RestrictedStore : public LocalFSStore
struct RestrictedStore : public LocalFSStore, public virtual RestrictedStoreConfig
{
ref<LocalStore> next;

DerivationGoal & goal;

RestrictedStore(const Params & params, ref<LocalStore> next, DerivationGoal & goal)
: Store(params), LocalFSStore(params), next(next), goal(goal)
: StoreConfig(params), Store(params), LocalFSStore(params), next(next), goal(goal)
{ }

Path getRealStoreDir() override
Expand Down
32 changes: 20 additions & 12 deletions src/libstore/dummy-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@

namespace nix {

static std::string uriScheme = "dummy://";
struct DummyStoreConfig : virtual StoreConfig {
using StoreConfig::StoreConfig;

struct DummyStore : public Store
const std::string name() override { return "Dummy Store"; }
};

struct DummyStore : public Store, public virtual DummyStoreConfig
{
DummyStore(const Params & params)
: Store(params)
DummyStore(const std::string scheme, const std::string uri, const Params & params)
: DummyStore(params)
{ }

DummyStore(const Params & params)
: StoreConfig(params)
, Store(params)
{
}

string getUri() override
{
return uriScheme;
return *uriSchemes().begin();
}

void queryPathInfoUncached(const StorePath & path,
Expand All @@ -21,6 +31,10 @@ struct DummyStore : public Store
callback(nullptr);
}

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

std::optional<StorePath> queryPathFromHashPart(const std::string & hashPart) override
{ unsupported("queryPathFromHashPart"); }

Expand Down Expand Up @@ -48,12 +62,6 @@ struct DummyStore : public Store
{ unsupported("buildDerivation"); }
};

static RegisterStoreImplementation regStore([](
const std::string & uri, const Store::Params & params)
-> std::shared_ptr<Store>
{
if (uri != uriScheme) return nullptr;
return std::make_shared<DummyStore>(params);
});
static RegisterStoreImplementation<DummyStore, DummyStoreConfig> regStore;

}
5 changes: 0 additions & 5 deletions src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@ template<> std::string BaseSetting<SandboxMode>::to_string() const
else abort();
}

template<> nlohmann::json BaseSetting<SandboxMode>::toJSON()
{
return AbstractSetting::toJSON();
}

template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::string & category)
{
args.addFlag({
Expand Down
1 change: 1 addition & 0 deletions src/libstore/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "types.hh"
#include "config.hh"
#include "abstractsettingtojson.hh"
#include "util.hh"

#include <map>
Expand Down
39 changes: 22 additions & 17 deletions src/libstore/http-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ namespace nix {

MakeError(UploadToHTTP, Error);

class HttpBinaryCacheStore : public BinaryCacheStore
struct HttpBinaryCacheStoreConfig : virtual BinaryCacheStoreConfig
{
using BinaryCacheStoreConfig::BinaryCacheStoreConfig;

const std::string name() override { return "Http Binary Cache Store"; }
};

class HttpBinaryCacheStore : public BinaryCacheStore, public HttpBinaryCacheStoreConfig
{
private:

Expand All @@ -24,9 +31,12 @@ class HttpBinaryCacheStore : public BinaryCacheStore
public:

HttpBinaryCacheStore(
const Params & params, const Path & _cacheUri)
: BinaryCacheStore(params)
, cacheUri(_cacheUri)
const std::string & scheme,
const Path & _cacheUri,
const Params & params)
: StoreConfig(params)
, BinaryCacheStore(params)
, cacheUri(scheme + "://" + _cacheUri)
{
if (cacheUri.back() == '/')
cacheUri.pop_back();
Expand Down Expand Up @@ -55,6 +65,13 @@ class HttpBinaryCacheStore : public BinaryCacheStore
}
}

static std::set<std::string> uriSchemes()
{
static bool forceHttp = getEnv("_NIX_FORCE_HTTP") == "1";
auto ret = std::set<std::string>({"http", "https"});
if (forceHttp) ret.insert("file");
return ret;
}
protected:

void maybeDisable()
Expand Down Expand Up @@ -162,18 +179,6 @@ class HttpBinaryCacheStore : public BinaryCacheStore

};

static RegisterStoreImplementation regStore([](
const std::string & uri, const Store::Params & params)
-> std::shared_ptr<Store>
{
static bool forceHttp = getEnv("_NIX_FORCE_HTTP") == "1";
if (std::string(uri, 0, 7) != "http://" &&
std::string(uri, 0, 8) != "https://" &&
(!forceHttp || std::string(uri, 0, 7) != "file://"))
return 0;
auto store = std::make_shared<HttpBinaryCacheStore>(params, uri);
store->init();
return store;
});
static RegisterStoreImplementation<HttpBinaryCacheStore, HttpBinaryCacheStoreConfig> regStore;

}
41 changes: 22 additions & 19 deletions src/libstore/legacy-ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@

namespace nix {

static std::string uriScheme = "ssh://";

struct LegacySSHStore : public Store
struct LegacySSHStoreConfig : virtual StoreConfig
{
const Setting<int> maxConnections{this, 1, "max-connections", "maximum number of concurrent SSH connections"};
const Setting<Path> sshKey{this, "", "ssh-key", "path to an SSH private key"};
const Setting<bool> compress{this, false, "compress", "whether to compress the connection"};
const Setting<Path> remoteProgram{this, "nix-store", "remote-program", "path to the nix-store executable on the remote system"};
const Setting<std::string> remoteStore{this, "", "remote-store", "URI of the store on the remote system"};
using StoreConfig::StoreConfig;
const Setting<int> maxConnections{(StoreConfig*) this, 1, "max-connections", "maximum number of concurrent SSH connections"};
const Setting<Path> sshKey{(StoreConfig*) this, "", "ssh-key", "path to an SSH private key"};
const Setting<bool> compress{(StoreConfig*) this, false, "compress", "whether to compress the connection"};
const Setting<Path> remoteProgram{(StoreConfig*) this, "nix-store", "remote-program", "path to the nix-store executable on the remote system"};
const Setting<std::string> remoteStore{(StoreConfig*) this, "", "remote-store", "URI of the store on the remote system"};

const std::string name() override { return "Legacy SSH Store"; }
};

struct LegacySSHStore : public Store, public virtual LegacySSHStoreConfig
{
// Hack for getting remote build log output.
const Setting<int> logFD{this, -1, "log-fd", "file descriptor to which SSH's stderr is connected"};
// Intentionally not in `LegacySSHStoreConfig` so that it doesn't appear in
// the documentation
const Setting<int> logFD{(StoreConfig*) this, -1, "log-fd", "file descriptor to which SSH's stderr is connected"};

struct Connection
{
Expand All @@ -37,8 +43,11 @@ struct LegacySSHStore : public Store

SSHMaster master;

LegacySSHStore(const string & host, const Params & params)
: Store(params)
static std::set<std::string> uriSchemes() { return {"ssh"}; }

LegacySSHStore(const string & scheme, const string & host, const Params & params)
: StoreConfig(params)
, Store(params)
, host(host)
, connections(make_ref<Pool<Connection>>(
std::max(1, (int) maxConnections),
Expand Down Expand Up @@ -84,7 +93,7 @@ struct LegacySSHStore : public Store

string getUri() override
{
return uriScheme + host;
return *uriSchemes().begin() + "://" + host;
}

void queryPathInfoUncached(const StorePath & path,
Expand Down Expand Up @@ -325,12 +334,6 @@ struct LegacySSHStore : public Store
}
};

static RegisterStoreImplementation regStore([](
const std::string & uri, const Store::Params & params)
-> std::shared_ptr<Store>
{
if (std::string(uri, 0, uriScheme.size()) != uriScheme) return 0;
return std::make_shared<LegacySSHStore>(std::string(uri, uriScheme.size()), params);
});
static RegisterStoreImplementation<LegacySSHStore, LegacySSHStoreConfig> regStore;

}
36 changes: 23 additions & 13 deletions src/libstore/local-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

namespace nix {

class LocalBinaryCacheStore : public BinaryCacheStore
struct LocalBinaryCacheStoreConfig : virtual BinaryCacheStoreConfig
{
using BinaryCacheStoreConfig::BinaryCacheStoreConfig;

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

class LocalBinaryCacheStore : public BinaryCacheStore, public virtual LocalBinaryCacheStoreConfig
{
private:

Expand All @@ -13,8 +20,11 @@ class LocalBinaryCacheStore : public BinaryCacheStore
public:

LocalBinaryCacheStore(
const Params & params, const Path & binaryCacheDir)
: BinaryCacheStore(params)
const std::string scheme,
const Path & binaryCacheDir,
const Params & params)
: StoreConfig(params)
, BinaryCacheStore(params)
, binaryCacheDir(binaryCacheDir)
{
}
Expand All @@ -26,6 +36,8 @@ class LocalBinaryCacheStore : public BinaryCacheStore
return "file://" + binaryCacheDir;
}

static std::set<std::string> uriSchemes();

protected:

bool fileExists(const std::string & path) override;
Expand Down Expand Up @@ -85,16 +97,14 @@ bool LocalBinaryCacheStore::fileExists(const std::string & path)
return pathExists(binaryCacheDir + "/" + path);
}

static RegisterStoreImplementation regStore([](
const std::string & uri, const Store::Params & params)
-> std::shared_ptr<Store>
std::set<std::string> LocalBinaryCacheStore::uriSchemes()
{
if (getEnv("_NIX_FORCE_HTTP_BINARY_CACHE_STORE") == "1" ||
std::string(uri, 0, 7) != "file://")
return 0;
auto store = std::make_shared<LocalBinaryCacheStore>(params, std::string(uri, 7));
store->init();
return store;
});
if (getEnv("_NIX_FORCE_HTTP_BINARY_CACHE_STORE") == "1")
return {};
else
return {"file"};
}

static RegisterStoreImplementation<LocalBinaryCacheStore, LocalBinaryCacheStoreConfig> regStore;

}
3 changes: 2 additions & 1 deletion src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ namespace nix {


LocalStore::LocalStore(const Params & params)
: Store(params)
: StoreConfig(params)
, Store(params)
, LocalFSStore(params)
, realStoreDir_{this, false, rootDir != "" ? rootDir + "/nix/store" : storeDir, "real",
"physical path to the Nix store"}
Expand Down
Loading

0 comments on commit 5080d4e

Please sign in to comment.