Skip to content

Commit

Permalink
Convert LocalStoreConfig to new system
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Jul 18, 2024
1 parent 118b050 commit 307440b
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 86 deletions.
1 change: 1 addition & 0 deletions src/libstore-c/nix_api_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "path.hh"
#include "store-api.hh"
#include "store-open.hh"
#include "build-result.hh"

#include "globals.hh"
Expand Down
6 changes: 5 additions & 1 deletion src/libstore/binary-cache-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ public:

public:

virtual void init() override;
/**
* Perform any necessary effectful operation to make the store up and
* running
*/
virtual void init();

private:

Expand Down
6 changes: 3 additions & 3 deletions src/libstore/dummy-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct DummyStoreConfig : virtual StoreConfig {
return {"dummy"};
}

std::shared_ptr<Store> open() override;
std::shared_ptr<Store> openStore() override;
};


Expand All @@ -50,7 +50,7 @@ struct DummyStore : public virtual DummyStoreConfig, public virtual Store
DummyStore(const Config & config)
: StoreConfig(config)
, DummyStoreConfig(config)
, Store(config)
, Store{static_cast<const Store::Config &>(*this)}
{ }

std::string getUri() override
Expand Down Expand Up @@ -100,7 +100,7 @@ struct DummyStore : public virtual DummyStoreConfig, public virtual Store
{ unsupported("getFSAccessor"); }
};

std::shared_ptr<Store> DummyStore::Config::open()
std::shared_ptr<Store> DummyStore::Config::openStore()
{
return std::make_shared<DummyStore>(*this);
}
Expand Down
3 changes: 2 additions & 1 deletion src/libstore/local-fs-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ LocalFSStore::Config::LocalFSStoreConfig(PathView rootDir, const StoreReference:
}

LocalFSStore::LocalFSStore(const Config & config)
: Store(config)
: LocalFSStore::Config{config}
, Store{static_cast<const Store::Config &>(*this)}
{
}

Expand Down
22 changes: 11 additions & 11 deletions src/libstore/local-fs-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ struct LocalFSStoreConfig :
virtual Store::Config,
LocalFSStoreConfigT<config::JustValue>
{
LocalFSStoreConfig(const StoreReference::Params &);

/**
* Used to override the `root` settings. Can't be done via modifying
* `params` reliably because this parameter is unused except for
* passing to base class constructors.
*
* @todo Make this less error-prone with new store settings system.
*/
LocalFSStoreConfig(PathView path, const StoreReference::Params & params);

struct Descriptions :
virtual Store::Config::Descriptions,
LocalFSStoreConfigT<config::SettingInfo>
Expand All @@ -49,6 +38,17 @@ struct LocalFSStoreConfig :
static LocalFSStoreConfigT<config::JustValue> defaults(
const Store::Config &,
const std::optional<Path> rootDir);

LocalFSStoreConfig(const StoreReference::Params &);

/**
* Used to override the `root` settings. Can't be done via modifying
* `params` reliably because this parameter is unused except for
* passing to base class constructors.
*
* @todo Make this less error-prone with new store settings system.
*/
LocalFSStoreConfig(PathView path, const StoreReference::Params & params);
};

struct LocalFSStore :
Expand Down
79 changes: 58 additions & 21 deletions src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "posix-source-accessor.hh"
#include "keys.hh"
#include "users.hh"
#include "config-parse-impl.hh"
#include "store-open.hh"
#include "store-registration.hh"

#include <iostream>
#include <algorithm>
Expand Down Expand Up @@ -56,12 +59,50 @@

namespace nix {

LocalStoreConfig::LocalStoreConfig(
LocalStore::Config::Descriptions::Descriptions()
: Store::Config::Descriptions{Store::Config::descriptions}
, LocalFSStore::Config::Descriptions{LocalFSStore::Config::descriptions}
, LocalStoreConfigT<config::SettingInfo>{
.requireSigs = {
.name = "require-sigs",
.description = "Whether store paths copied into this store should have a trusted signature.",
},
.readOnly = {
.name = "read-only",
.description = R"(
Allow this store to be opened when its [database](@docroot@/glossary.md#gloss-nix-database) is on a read-only filesystem.
Normally Nix will attempt to open the store database in read-write mode, even for querying (when write access is not needed), causing it to fail if the database is on a read-only filesystem.
Enable read-only mode to disable locking and open the SQLite database with the [`immutable` parameter](https://www.sqlite.org/c3ref/open.html) set.
> **Warning**
> Do not use this unless the filesystem is read-only.
>
> Using it when the filesystem is writable can cause incorrect query results or corruption errors if the database is changed by another process.
> While the filesystem the database resides on might appear to be read-only, consider whether another user or system might have write access to it.
)",
},
}
{}

const LocalStore::Config::Descriptions LocalStore::Config::descriptions{};

decltype(LocalStore::Config::defaults) LocalStore::Config::defaults = {
.requireSigs = {.value = settings.requireSigs },
.readOnly = {.value = false },
};

LocalStore::Config::LocalStoreConfig(
std::string_view scheme,
std::string_view authority,
const Params & params)
: StoreConfig(params)
, LocalFSStoreConfig(authority, params)
const StoreReference::Params & params)
: Store::Config(params)
, LocalFSStore::Config(authority, params)
, LocalStoreConfigT<config::JustValue>{
CONFIG_ROW(requireSigs),
CONFIG_ROW(readOnly),
}
{
}

Expand All @@ -72,6 +113,11 @@ std::string LocalStoreConfig::doc()
;
}

std::shared_ptr<Store> LocalStore::Config::openStore()
{
return std::make_shared<LocalStore>(*this);
}

struct LocalStore::State::Stmts {
/* Some precompiled SQLite statements. */
SQLiteStmt RegisterValidPath;
Expand Down Expand Up @@ -192,15 +238,12 @@ void migrateCASchema(SQLite& db, Path schemaPath, AutoCloseFD& lockFd)
}
}

LocalStore::LocalStore(
std::string_view scheme,
PathView path,
const Params & params)
: StoreConfig(params)
, LocalFSStoreConfig(path, params)
, LocalStoreConfig(scheme, path, params)
, Store(params)
, LocalFSStore(params)
LocalStore::LocalStore(const Config & config)
: Store::Config{config}
, LocalFSStore::Config{config}
, LocalStore::Config{config}
, Store{static_cast<const Store::Config &>(*this)}
, LocalFSStore{static_cast<const LocalFSStore::Config &>(*this)}
, dbDir(stateDir + "/db")
, linksDir(realStoreDir + "/.links")
, reservedPath(dbDir + "/reserved")
Expand Down Expand Up @@ -477,12 +520,6 @@ LocalStore::LocalStore(
}


LocalStore::LocalStore(const Params & params)
: LocalStore("local", "", params)
{
}


AutoCloseFD LocalStore::openGCLock()
{
Path fnGCLock = stateDir + "/gc.lock";
Expand Down Expand Up @@ -1512,7 +1549,7 @@ LocalStore::VerificationResult LocalStore::verifyAllValidPaths(RepairFlag repair
database and the filesystem) in the loop below, in order to catch
invalid states.
*/
for (auto & i : std::filesystem::directory_iterator{realStoreDir.to_string()}) {
for (auto & i : std::filesystem::directory_iterator{realStoreDir.get()}) {
checkInterrupt();
try {
storePathsInStoreDir.insert({i.path().filename().string()});
Expand Down Expand Up @@ -1802,6 +1839,6 @@ std::optional<std::string> LocalStore::getVersion()
return nixVersion;
}

static RegisterStoreImplementation<LocalStore, LocalStoreConfig> regLocalStore;
static RegisterStoreImplementation<LocalStore> regLocalStore;

} // namespace nix
69 changes: 38 additions & 31 deletions src/libstore/local-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,60 @@ struct OptimiseStats
uint64_t bytesFreed = 0;
};

struct LocalStoreConfig : virtual LocalFSStoreConfig
template<template<typename> class F>
struct LocalStoreConfigT
{
using LocalFSStoreConfig::LocalFSStoreConfig;
const F<bool> requireSigs;

LocalStoreConfig(
std::string_view scheme,
std::string_view authority,
const Params & params);
const F<bool> readOnly;
};

Setting<bool> requireSigs{this,
settings.requireSigs,
"require-sigs",
"Whether store paths copied into this store should have a trusted signature."};
struct LocalStoreConfig :
virtual Store::Config,
virtual LocalFSStore::Config,
LocalStoreConfigT<config::JustValue>
{
struct Descriptions :
virtual Store::Config::Descriptions,
virtual LocalFSStore::Config::Descriptions,
LocalStoreConfigT<config::SettingInfo>
{
Descriptions();
};

Setting<bool> readOnly{this,
false,
"read-only",
R"(
Allow this store to be opened when its [database](@docroot@/glossary.md#gloss-nix-database) is on a read-only filesystem.
static const Descriptions descriptions;

Normally Nix will attempt to open the store database in read-write mode, even for querying (when write access is not needed), causing it to fail if the database is on a read-only filesystem.
/**
* The other defaults depend on the choice of `storeDir` and `rootDir`
*/
static LocalStoreConfigT<config::JustValue> defaults;

Enable read-only mode to disable locking and open the SQLite database with the [`immutable` parameter](https://www.sqlite.org/c3ref/open.html) set.
LocalStoreConfig(const StoreReference::Params &);

> **Warning**
> Do not use this unless the filesystem is read-only.
>
> Using it when the filesystem is writable can cause incorrect query results or corruption errors if the database is changed by another process.
> While the filesystem the database resides on might appear to be read-only, consider whether another user or system might have write access to it.
)"};
LocalStoreConfig(
std::string_view scheme,
std::string_view authority,
const StoreReference::Params & params);

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

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

std::string doc() override;

std::shared_ptr<Store> openStore() override;
};

class LocalStore : public virtual LocalStoreConfig
, public virtual IndirectRootStore
, public virtual GcStore
class LocalStore :
public virtual LocalStoreConfig,
public virtual IndirectRootStore,
public virtual GcStore
{
public:

using Config = LocalStoreConfig;

private:

/**
Expand Down Expand Up @@ -144,11 +155,7 @@ public:
* Initialise the local store, upgrading the schema if
* necessary.
*/
LocalStore(const Params & params);
LocalStore(
std::string_view scheme,
PathView path,
const Params & params);
LocalStore(const Config & params);

~LocalStore();

Expand Down
2 changes: 2 additions & 0 deletions src/libstore/machines.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
///@file

#include <nlohmann/json.hpp>

#include "ref.hh"
#include "store-reference.hh"

Expand Down
12 changes: 6 additions & 6 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ std::pair<StorePath, Hash> StoreDirConfig::computeStorePath(
}


StoreConfig::Descriptions::Descriptions()
Store::Config::Descriptions::Descriptions()
: StoreDirConfig::Descriptions{StoreDirConfig::descriptions}
, StoreConfigT<config::SettingInfo>{
.pathInfoCacheSize = {
Expand Down Expand Up @@ -235,19 +235,19 @@ StoreConfig::Descriptions::Descriptions()
}


const StoreConfig::Descriptions StoreConfig::descriptions{};
const Store::Config::Descriptions Store::Config::descriptions{};


decltype(StoreConfig::defaults) StoreConfig::defaults = {
decltype(Store::Config::defaults) Store::Config::defaults = {
.pathInfoCacheSize = { .value = 65536 },
.isTrusted = { .value = false },
.priority = { .value = 0 },
.wantMassQuery = { .value = false },
.systemFeatures = { .value = StoreConfig::getDefaultSystemFeatures() },
.systemFeatures = { .value = Store::Config::getDefaultSystemFeatures() },
};


StoreConfig::StoreConfig(const StoreReference::Params & params)
Store::Config::StoreConfig(const StoreReference::Params & params)
: StoreDirConfig{params}
, StoreConfigT<config::JustValue>{
CONFIG_ROW(pathInfoCacheSize),
Expand Down Expand Up @@ -491,7 +491,7 @@ ValidPathInfo Store::addToStoreSlow(
return info;
}

StringSet StoreConfig::getDefaultSystemFeatures()
StringSet Store::Config::getDefaultSystemFeatures()
{
auto res = settings.systemFeatures.get();

Expand Down
Loading

0 comments on commit 307440b

Please sign in to comment.