Skip to content

Commit 316fef3

Browse files
authored
Merge pull request #13764 from obsidiansystems/simplify-store-dir
Simplify "Store dir" superclass
2 parents 1ed3ae8 + 64c2ee3 commit 316fef3

File tree

4 files changed

+59
-67
lines changed

4 files changed

+59
-67
lines changed

src/libstore/include/nix/store/store-api.hh

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ struct MissingPaths
8181
uint64_t narSize{0};
8282
};
8383

84+
/**
85+
* Need to make this a separate class so I can get the right
86+
* initialization order in the constructor for `StoreConfig`.
87+
*/
88+
struct StoreConfigBase : Config
89+
{
90+
using Config::Config;
91+
92+
const PathSetting storeDir_{
93+
this,
94+
settings.nixStore,
95+
"store",
96+
R"(
97+
Logical location of the Nix store, usually
98+
`/nix/store`. Note that you can only copy store paths
99+
between stores if they have the same `store` setting.
100+
)"};
101+
};
102+
84103
/**
85104
* About the class hierarchy of the store types:
86105
*
@@ -107,10 +126,17 @@ struct MissingPaths
107126
* ```
108127
* cpp static RegisterStoreImplementation<FooConfig> regStore;
109128
* ```
129+
*
130+
* @note The order of `StoreConfigBase` and then `StorerConfig` is
131+
* very important. This ensures that `StoreConfigBase::storeDir_`
132+
* is initialized before we have our one chance (because references are
133+
* immutable) to initialize `StoreConfig::storeDir`.
110134
*/
111-
struct StoreConfig : public StoreDirConfig
135+
struct StoreConfig : public StoreConfigBase, public StoreDirConfig
112136
{
113-
using StoreDirConfig::StoreDirConfig;
137+
using Params = StoreReference::Params;
138+
139+
StoreConfig(const Params & params);
114140

115141
StoreConfig() = delete;
116142

@@ -233,7 +259,7 @@ struct StoreConfig : public StoreDirConfig
233259
* underlying resource, which could be an external process (daemon
234260
* server), file system state, etc.
235261
*/
236-
class Store : public std::enable_shared_from_this<Store>, public MixStoreDirMethods
262+
class Store : public std::enable_shared_from_this<Store>, public StoreDirConfig
237263
{
238264
public:
239265

src/libstore/include/nix/store/store-dir-config.hh

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ MakeError(BadStorePath, Error);
1818
MakeError(BadStorePathName, BadStorePath);
1919

2020
/**
21-
* @todo This should just be part of `StoreDirConfig`. However, it would
22-
* be a huge amount of churn if `Store` didn't have these methods
21+
* @todo This should just be inherited by `StoreConfig`. However, it
22+
* would be a huge amount of churn if `Store` didn't have these methods
2323
* anymore, forcing a bunch of code to go from `store.method(...)` to
2424
* `store.config.method(...)`.
2525
*
26-
* So we instead pull out the methods into their own mix-in, so can put
27-
* them directly on the Store too.
26+
* @todo this should not have "config" in its name, because it no longer
27+
* uses the configuration system for `storeDir` --- in fact, `storeDir`
28+
* isn't even owned, but a mere reference. But doing that rename would
29+
* cause a bunch of churn.
2830
*/
29-
struct MixStoreDirMethods
31+
struct StoreDirConfig
3032
{
3133
const Path & storeDir;
3234

@@ -96,40 +98,4 @@ struct MixStoreDirMethods
9698
PathFilter & filter = defaultPathFilter) const;
9799
};
98100

99-
/**
100-
* Need to make this a separate class so I can get the right
101-
* initialization order in the constructor for `StoreDirConfig`.
102-
*/
103-
struct StoreDirConfigBase : Config
104-
{
105-
using Config::Config;
106-
107-
const PathSetting storeDir_{
108-
this,
109-
settings.nixStore,
110-
"store",
111-
R"(
112-
Logical location of the Nix store, usually
113-
`/nix/store`. Note that you can only copy store paths
114-
between stores if they have the same `store` setting.
115-
)"};
116-
};
117-
118-
/**
119-
* The order of `StoreDirConfigBase` and then `MixStoreDirMethods` is
120-
* very important. This ensures that `StoreDirConfigBase::storeDir_`
121-
* is initialized before we have our one chance (because references are
122-
* immutable) to initialize `MixStoreDirMethods::storeDir`.
123-
*/
124-
struct StoreDirConfig : StoreDirConfigBase, MixStoreDirMethods
125-
{
126-
using Params = StringMap;
127-
128-
StoreDirConfig(const Params & params);
129-
130-
StoreDirConfig() = delete;
131-
132-
virtual ~StoreDirConfig() = default;
133-
};
134-
135101
} // namespace nix

src/libstore/store-api.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ using json = nlohmann::json;
2727

2828
namespace nix {
2929

30-
bool MixStoreDirMethods::isInStore(PathView path) const
30+
StoreConfig::StoreConfig(const Params & params)
31+
: StoreConfigBase(params)
32+
, StoreDirConfig{storeDir_}
33+
{
34+
}
35+
36+
bool StoreDirConfig::isInStore(PathView path) const
3137
{
3238
return isInDir(path, storeDir);
3339
}
3440

35-
std::pair<StorePath, Path> MixStoreDirMethods::toStorePath(PathView path) const
41+
std::pair<StorePath, Path> StoreDirConfig::toStorePath(PathView path) const
3642
{
3743
if (!isInStore(path))
3844
throw Error("path '%1%' is not in the Nix store", path);
@@ -293,7 +299,7 @@ StringSet Store::Config::getDefaultSystemFeatures()
293299
}
294300

295301
Store::Store(const Store::Config & config)
296-
: MixStoreDirMethods{config}
302+
: StoreDirConfig{config}
297303
, config{config}
298304
, state({(size_t) config.pathInfoCacheSize})
299305
{
@@ -1082,7 +1088,7 @@ decodeValidPathInfo(const Store & store, std::istream & str, std::optional<HashR
10821088
return std::optional<ValidPathInfo>(std::move(info));
10831089
}
10841090

1085-
std::string MixStoreDirMethods::showPaths(const StorePathSet & paths) const
1091+
std::string StoreDirConfig::showPaths(const StorePathSet & paths) const
10861092
{
10871093
std::string s;
10881094
for (auto & i : paths) {

src/libstore/store-dir-config.cc

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace nix {
88

9-
StorePath MixStoreDirMethods::parseStorePath(std::string_view path) const
9+
StorePath StoreDirConfig::parseStorePath(std::string_view path) const
1010
{
1111
// On Windows, `/nix/store` is not a canonical path. More broadly it
1212
// is unclear whether this function should be using the native
@@ -25,7 +25,7 @@ StorePath MixStoreDirMethods::parseStorePath(std::string_view path) const
2525
return StorePath(baseNameOf(p));
2626
}
2727

28-
std::optional<StorePath> MixStoreDirMethods::maybeParseStorePath(std::string_view path) const
28+
std::optional<StorePath> StoreDirConfig::maybeParseStorePath(std::string_view path) const
2929
{
3030
try {
3131
return parseStorePath(path);
@@ -34,25 +34,25 @@ std::optional<StorePath> MixStoreDirMethods::maybeParseStorePath(std::string_vie
3434
}
3535
}
3636

37-
bool MixStoreDirMethods::isStorePath(std::string_view path) const
37+
bool StoreDirConfig::isStorePath(std::string_view path) const
3838
{
3939
return (bool) maybeParseStorePath(path);
4040
}
4141

42-
StorePathSet MixStoreDirMethods::parseStorePathSet(const PathSet & paths) const
42+
StorePathSet StoreDirConfig::parseStorePathSet(const PathSet & paths) const
4343
{
4444
StorePathSet res;
4545
for (auto & i : paths)
4646
res.insert(parseStorePath(i));
4747
return res;
4848
}
4949

50-
std::string MixStoreDirMethods::printStorePath(const StorePath & path) const
50+
std::string StoreDirConfig::printStorePath(const StorePath & path) const
5151
{
5252
return (storeDir + "/").append(path.to_string());
5353
}
5454

55-
PathSet MixStoreDirMethods::printStorePathSet(const StorePathSet & paths) const
55+
PathSet StoreDirConfig::printStorePathSet(const StorePathSet & paths) const
5656
{
5757
PathSet res;
5858
for (auto & i : paths)
@@ -69,28 +69,28 @@ also update the user-visible behavior, please update the specification
6969
to match.
7070
*/
7171

72-
StorePath MixStoreDirMethods::makeStorePath(std::string_view type, std::string_view hash, std::string_view name) const
72+
StorePath StoreDirConfig::makeStorePath(std::string_view type, std::string_view hash, std::string_view name) const
7373
{
7474
/* e.g., "source:sha256:1abc...:/nix/store:foo.tar.gz" */
7575
auto s = std::string(type) + ":" + std::string(hash) + ":" + storeDir + ":" + std::string(name);
7676
auto h = compressHash(hashString(HashAlgorithm::SHA256, s), 20);
7777
return StorePath(h, name);
7878
}
7979

80-
StorePath MixStoreDirMethods::makeStorePath(std::string_view type, const Hash & hash, std::string_view name) const
80+
StorePath StoreDirConfig::makeStorePath(std::string_view type, const Hash & hash, std::string_view name) const
8181
{
8282
return makeStorePath(type, hash.to_string(HashFormat::Base16, true), name);
8383
}
8484

85-
StorePath MixStoreDirMethods::makeOutputPath(std::string_view id, const Hash & hash, std::string_view name) const
85+
StorePath StoreDirConfig::makeOutputPath(std::string_view id, const Hash & hash, std::string_view name) const
8686
{
8787
return makeStorePath("output:" + std::string{id}, hash, outputPathName(name, id));
8888
}
8989

9090
/* Stuff the references (if any) into the type. This is a bit
9191
hacky, but we can't put them in, say, <s2> (per the grammar above)
9292
since that would be ambiguous. */
93-
static std::string makeType(const MixStoreDirMethods & store, std::string && type, const StoreReferences & references)
93+
static std::string makeType(const StoreDirConfig & store, std::string && type, const StoreReferences & references)
9494
{
9595
for (auto & i : references.others) {
9696
type += ":";
@@ -101,7 +101,7 @@ static std::string makeType(const MixStoreDirMethods & store, std::string && typ
101101
return std::move(type);
102102
}
103103

104-
StorePath MixStoreDirMethods::makeFixedOutputPath(std::string_view name, const FixedOutputInfo & info) const
104+
StorePath StoreDirConfig::makeFixedOutputPath(std::string_view name, const FixedOutputInfo & info) const
105105
{
106106
if (info.method == FileIngestionMethod::Git
107107
&& !(info.hash.algo == HashAlgorithm::SHA1 || info.hash.algo == HashAlgorithm::SHA256)) {
@@ -126,7 +126,7 @@ StorePath MixStoreDirMethods::makeFixedOutputPath(std::string_view name, const F
126126
}
127127

128128
StorePath
129-
MixStoreDirMethods::makeFixedOutputPathFromCA(std::string_view name, const ContentAddressWithReferences & ca) const
129+
StoreDirConfig::makeFixedOutputPathFromCA(std::string_view name, const ContentAddressWithReferences & ca) const
130130
{
131131
// New template
132132
return std::visit(
@@ -148,7 +148,7 @@ MixStoreDirMethods::makeFixedOutputPathFromCA(std::string_view name, const Conte
148148
ca.raw);
149149
}
150150

151-
std::pair<StorePath, Hash> MixStoreDirMethods::computeStorePath(
151+
std::pair<StorePath, Hash> StoreDirConfig::computeStorePath(
152152
std::string_view name,
153153
const SourcePath & path,
154154
ContentAddressMethod method,
@@ -173,10 +173,4 @@ std::pair<StorePath, Hash> MixStoreDirMethods::computeStorePath(
173173
};
174174
}
175175

176-
StoreDirConfig::StoreDirConfig(const Params & params)
177-
: StoreDirConfigBase(params)
178-
, MixStoreDirMethods{storeDir_}
179-
{
180-
}
181-
182176
} // namespace nix

0 commit comments

Comments
 (0)