Add a Config class to simplify adding configuration settings

The typical use is to inherit Config and add Setting<T> members:

  class MyClass : private Config
  {
    Setting<int> foo{this, 123, "foo", "the number of foos to use"};
    Setting<std::string> bar{this, "blabla", "bar", "the name of the bar"};

    MyClass() : Config(readConfigFile("/etc/my-app.conf"))
    {
      std::cout << foo << "\n"; // will print 123 unless overriden
    }
  };

Currently, this is used by Store and its subclasses for store
parameters. You now get a warning if you specify a non-existant store
parameter in a store URI.
This commit is contained in:
Eelco Dolstra 2017-04-13 15:55:38 +02:00
parent 568a099c88
commit 2040240e23
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
16 changed files with 334 additions and 40 deletions

View file

@ -79,10 +79,7 @@ struct BinaryCacheStoreAccessor : public FSAccessor
BinaryCacheStore::BinaryCacheStore(const Params & params)
: Store(params)
, compression(get(params, "compression", "xz"))
, writeNARListing(get(params, "write-nar-listing", "0") == "1")
{
auto secretKeyFile = get(params, "secret-key", "");
if (secretKeyFile != "")
secretKey = std::unique_ptr<SecretKey>(new SecretKey(readFile(secretKeyFile)));

View file

@ -13,14 +13,16 @@ struct NarInfo;
class BinaryCacheStore : public Store
{
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<Path> secretKeyFile{this, "", "secret-key", "path to secret key used to sign the binary cache"};
private:
std::unique_ptr<SecretKey> secretKey;
std::string compression;
bool writeNARListing;
protected:
BinaryCacheStore(const Params & params);

View file

@ -3051,13 +3051,11 @@ Path DerivationGoal::openLogFile()
string baseName = baseNameOf(drvPath);
/* Create a log file. */
Path dir = (format("%1%/%2%/%3%/") % worker.store.logDir % worker.store.drvsLogDir % string(baseName, 0, 2)).str();
Path dir = fmt("%s/%s/%s/", worker.store.logDir, worker.store.drvsLogDir, string(baseName, 0, 2));
createDirs(dir);
Path logFileName = (format("%1%/%2%%3%")
% dir
% string(baseName, 2)
% (settings.compressLog ? ".bz2" : "")).str();
Path logFileName = fmt("%s/%s%s", dir, string(baseName, 2),
settings.compressLog ? ".bz2" : "");
fdLogFile = open(logFileName.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0666);
if (!fdLogFile) throw SysError(format("creating log file %1%") % logFileName);

View file

@ -12,6 +12,10 @@ static std::string uriScheme = "ssh://";
struct LegacySSHStore : public Store
{
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"};
struct Connection
{
std::unique_ptr<SSHMaster::Connection> sshConn;
@ -29,16 +33,16 @@ struct LegacySSHStore : public Store
: Store(params)
, host(host)
, connections(make_ref<Pool<Connection>>(
std::max(1, std::stoi(get(params, "max-connections", "1"))),
std::max(1, (int) maxConnections),
[this]() { return openConnection(); },
[](const ref<Connection> & r) { return true; }
))
, master(
host,
get(params, "ssh-key", ""),
sshKey,
// Use SSH master only if using more than 1 connection.
connections->capacity() > 1,
get(params, "compress", "") == "true")
compress)
{
}

View file

@ -9,9 +9,6 @@ namespace nix {
LocalFSStore::LocalFSStore(const Params & params)
: Store(params)
, rootDir(get(params, "root"))
, stateDir(canonPath(get(params, "state", rootDir != "" ? rootDir + "/nix/var/nix" : settings.nixStateDir)))
, logDir(canonPath(get(params, "log", rootDir != "" ? rootDir + "/nix/var/log/nix" : settings.nixLogDir)))
{
}
@ -88,6 +85,8 @@ void LocalFSStore::narFromPath(const Path & path, Sink & sink)
const string LocalFSStore::drvsLogDir = "drvs";
std::shared_ptr<std::string> LocalFSStore::getBuildLog(const Path & path_)
{
auto path(path_);
@ -110,8 +109,8 @@ std::shared_ptr<std::string> LocalFSStore::getBuildLog(const Path & path_)
Path logPath =
j == 0
? (format("%1%/%2%/%3%/%4%") % logDir % drvsLogDir % string(baseName, 0, 2) % string(baseName, 2)).str()
: (format("%1%/%2%/%3%") % logDir % drvsLogDir % baseName).str();
? fmt("%s/%s/%s/%s", logDir, drvsLogDir, string(baseName, 0, 2), string(baseName, 2))
: fmt("%s/%s/%s", logDir, drvsLogDir, baseName);
Path logBz2Path = logPath + ".bz2";
if (pathExists(logPath))

View file

@ -38,13 +38,14 @@ namespace nix {
LocalStore::LocalStore(const Params & params)
: Store(params)
, LocalFSStore(params)
, realStoreDir(get(params, "real", rootDir != "" ? rootDir + "/nix/store" : storeDir))
, realStoreDir_{this, false, rootDir != "" ? rootDir + "/nix/store" : storeDir, "real",
"physical path to the Nix store"}
, realStoreDir(realStoreDir_)
, dbDir(stateDir + "/db")
, linksDir(realStoreDir + "/.links")
, reservedPath(dbDir + "/reserved")
, schemaPath(dbDir + "/schema")
, trashDir(realStoreDir + "/trash")
, requireSigs(trim(settings.get("signed-binary-caches", std::string("*"))) != "") // FIXME: rename option
, publicKeys(getDefaultPublicKeys())
{
auto state(_state.lock());

View file

@ -67,6 +67,8 @@ private:
public:
PathSetting realStoreDir_;
const Path realStoreDir;
const Path dbDir;
const Path linksDir;
@ -76,7 +78,9 @@ public:
private:
bool requireSigs;
Setting<bool> requireSigs{(Store*) this,
trim(settings.get("signed-binary-caches", std::string("*"))) != "",
"require-sigs", "whether store paths should have a trusted signature on import"};
PublicKeys publicKeys;

View file

@ -43,7 +43,7 @@ template Paths readStorePaths(Store & store, Source & from);
RemoteStore::RemoteStore(const Params & params)
: Store(params)
, connections(make_ref<Pool<Connection>>(
std::max(1, std::stoi(get(params, "max-connections", "1"))),
std::max(1, (int) maxConnections),
[this]() { return openConnectionWrapper(); },
[](const ref<Connection> & r) { return r->to.good() && r->from.good(); }
))

View file

@ -22,6 +22,9 @@ class RemoteStore : public virtual Store
{
public:
const Setting<int> maxConnections{(Store*) this, 1,
"max-connections", "maximum number of concurrent connections to the Nix daemon"};
RemoteStore(const Params & params);
/* Implementations of abstract store API methods. */

View file

@ -125,22 +125,22 @@ S3Helper::DownloadResult S3Helper::getObject(
struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
{
const Setting<std::string> region{this, Aws::Region::US_EAST_1, "region", {"aws-region"}};
const Setting<std::string> narinfoCompression{this, "", "narinfo-compression", "compression method for .narinfo files"};
const Setting<std::string> lsCompression{this, "", "ls-compression", "compression method for .ls files"};
const Setting<std::string> logCompression{this, "", "log-compression", "compression method for log/* files"};
std::string bucketName;
Stats stats;
S3Helper s3Helper;
std::string narinfoCompression, lsCompression, logCompression;
S3BinaryCacheStoreImpl(
const Params & params, const std::string & bucketName)
: S3BinaryCacheStore(params)
, bucketName(bucketName)
, s3Helper(get(params, "aws-region", Aws::Region::US_EAST_1))
, narinfoCompression(get(params, "narinfo-compression", ""))
, lsCompression(get(params, "ls-compression", ""))
, logCompression(get(params, "log-compression", ""))
, s3Helper(region)
{
diskCache = getNarInfoDiskCache();
}

View file

@ -14,16 +14,19 @@ class SSHStore : public RemoteStore
{
public:
const Setting<Path> sshKey{(Store*) this, "", "ssh-key", "path to an SSH private key"};
const Setting<bool> compress{(Store*) this, false, "compress", "whether to compress the connection"};
SSHStore(const std::string & host, const Params & params)
: Store(params)
, RemoteStore(params)
, host(host)
, master(
host,
get(params, "ssh-key", ""),
sshKey,
// Use SSH master only if using more than 1 connection.
connections->capacity() > 1,
get(params, "compress", "") == "true")
compress)
{
}

View file

@ -241,8 +241,8 @@ Path Store::computeStorePathForText(const string & name, const string & s,
Store::Store(const Params & params)
: storeDir(get(params, "store", settings.nixStore))
, state({std::stoi(get(params, "path-info-cache-size", "65536"))})
: Config(params)
, state({(size_t) pathInfoCacheSize})
{
}
@ -718,7 +718,10 @@ ref<Store> openStore(const std::string & uri, const Store::Params & params)
{
for (auto fun : *RegisterStoreImplementation::implementations) {
auto store = fun(uri, params);
if (store) return ref<Store>(store);
if (store) {
store->warnUnused();
return ref<Store>(store);
}
}
throw Error(format("don't know how to open Nix store %s") % uri);

View file

@ -6,6 +6,7 @@
#include "lru-cache.hh"
#include "sync.hh"
#include "globals.hh"
#include "config.hh"
#include <atomic>
#include <limits>
@ -224,13 +225,17 @@ struct BuildResult
};
class Store : public std::enable_shared_from_this<Store>
class Store : public std::enable_shared_from_this<Store>, public Config
{
public:
typedef std::map<std::string, std::string> Params;
const Path storeDir;
const PathSetting storeDir_{this, false, settings.nixStore,
"store", "path to the Nix store"};
const Path storeDir = storeDir_;
const Setting<int> pathInfoCacheSize{this, 65536, "path-info-cache-size", "size of the in-memory store path information cache"};
protected:
@ -585,9 +590,19 @@ protected:
class LocalFSStore : public virtual Store
{
public:
const Path rootDir;
const Path stateDir;
const Path logDir;
// FIXME: the (Store*) cast works around a bug in gcc that causes
// it to emit the call to the Option constructor. Clang works fine
// either way.
const PathSetting rootDir{(Store*) this, true, "",
"root", "directory prefixed to all other paths"};
const PathSetting stateDir{(Store*) this, false,
rootDir != "" ? rootDir + "/nix/var/nix" : settings.nixStateDir,
"state", "directory where Nix will store state"};
const PathSetting logDir{(Store*) this, false,
rootDir != "" ? rootDir + "/nix/var/log/nix" : settings.nixLogDir,
"log", "directory where Nix will store state"};
const static string drvsLogDir;
LocalFSStore(const Params & params);