Skip to content

Commit 0fd3b6f

Browse files
authored
Merge pull request NixOS#14483 from NixOS/purge-toRealPath
Relegate `toRealPath` to `LocalFSStore`
2 parents 948c89b + 099af75 commit 0fd3b6f

File tree

12 files changed

+63
-55
lines changed

12 files changed

+63
-55
lines changed

src/libfetchers/include/nix/fetchers/registry.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
///@file
33

44
#include "nix/util/types.hh"
5+
#include "nix/util/source-path.hh"
56
#include "nix/fetchers/fetchers.hh"
67

78
namespace nix {
@@ -39,7 +40,7 @@ struct Registry
3940
{
4041
}
4142

42-
static std::shared_ptr<Registry> read(const Settings & settings, const Path & path, RegistryType type);
43+
static std::shared_ptr<Registry> read(const Settings & settings, const SourcePath & path, RegistryType type);
4344

4445
void write(const Path & path);
4546

src/libfetchers/registry.cc

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010

1111
namespace nix::fetchers {
1212

13-
std::shared_ptr<Registry> Registry::read(const Settings & settings, const Path & path, RegistryType type)
13+
std::shared_ptr<Registry> Registry::read(const Settings & settings, const SourcePath & path, RegistryType type)
1414
{
1515
debug("reading registry '%s'", path);
1616

1717
auto registry = std::make_shared<Registry>(settings, type);
1818

19-
if (!pathExists(path))
19+
if (!path.pathExists())
2020
return std::make_shared<Registry>(settings, type);
2121

2222
try {
2323

24-
auto json = nlohmann::json::parse(readFile(path));
24+
auto json = nlohmann::json::parse(path.readFile());
2525

2626
auto version = json.value("version", 0);
2727

@@ -97,7 +97,10 @@ static Path getSystemRegistryPath()
9797

9898
static std::shared_ptr<Registry> getSystemRegistry(const Settings & settings)
9999
{
100-
static auto systemRegistry = Registry::read(settings, getSystemRegistryPath(), Registry::System);
100+
static auto systemRegistry = Registry::read(
101+
settings,
102+
SourcePath{getFSSourceAccessor(), CanonPath{getSystemRegistryPath()}}.resolveSymlinks(),
103+
Registry::System);
101104
return systemRegistry;
102105
}
103106

@@ -108,13 +111,17 @@ Path getUserRegistryPath()
108111

109112
std::shared_ptr<Registry> getUserRegistry(const Settings & settings)
110113
{
111-
static auto userRegistry = Registry::read(settings, getUserRegistryPath(), Registry::User);
114+
static auto userRegistry = Registry::read(
115+
settings,
116+
SourcePath{getFSSourceAccessor(), CanonPath{getUserRegistryPath()}}.resolveSymlinks(),
117+
Registry::User);
112118
return userRegistry;
113119
}
114120

115121
std::shared_ptr<Registry> getCustomRegistry(const Settings & settings, const Path & p)
116122
{
117-
static auto customRegistry = Registry::read(settings, p, Registry::Custom);
123+
static auto customRegistry =
124+
Registry::read(settings, SourcePath{getFSSourceAccessor(), CanonPath{p}}.resolveSymlinks(), Registry::Custom);
118125
return customRegistry;
119126
}
120127

@@ -137,14 +144,19 @@ static std::shared_ptr<Registry> getGlobalRegistry(const Settings & settings, re
137144
return std::make_shared<Registry>(settings, Registry::Global); // empty registry
138145
}
139146

140-
if (!isAbsolute(path)) {
141-
auto storePath = downloadFile(store, settings, path, "flake-registry.json").storePath;
142-
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
143-
store2->addPermRoot(storePath, getCacheDir() + "/flake-registry.json");
144-
path = store->toRealPath(storePath);
145-
}
146-
147-
return Registry::read(settings, path, Registry::Global);
147+
return Registry::read(
148+
settings,
149+
[&] -> SourcePath {
150+
if (!isAbsolute(path)) {
151+
auto storePath = downloadFile(store, settings, path, "flake-registry.json").storePath;
152+
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
153+
store2->addPermRoot(storePath, getCacheDir() + "/flake-registry.json");
154+
return {store->requireStoreObjectAccessor(storePath)};
155+
} else {
156+
return SourcePath{getFSSourceAccessor(), CanonPath{path}}.resolveSymlinks();
157+
}
158+
}(),
159+
Registry::Global);
148160
}();
149161

150162
return reg;

src/libstore-c/nix_api_store.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "nix/store/store-api.hh"
88
#include "nix/store/store-open.hh"
99
#include "nix/store/build-result.hh"
10+
#include "nix/store/local-fs-store.hh"
1011

1112
#include "nix/store/globals.hh"
1213

@@ -109,7 +110,8 @@ nix_err nix_store_real_path(
109110
if (context)
110111
context->last_err_code = NIX_OK;
111112
try {
112-
auto res = store->ptr->toRealPath(path->path);
113+
auto store2 = store->ptr.dynamic_pointer_cast<nix::LocalFSStore>();
114+
auto res = store2 ? store2->toRealPath(path->path) : store->ptr->printStorePath(path->path);
113115
return call_nix_get_string_callback(res, callback, user_data);
114116
}
115117
NIXC_CATCH_ERRS

src/libstore/build/derivation-building-goal.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
286286
PathSet lockFiles;
287287
/* FIXME: Should lock something like the drv itself so we don't build same
288288
CA drv concurrently */
289-
if (dynamic_cast<LocalStore *>(&worker.store)) {
289+
if (auto * localStore = dynamic_cast<LocalStore *>(&worker.store)) {
290290
/* If we aren't a local store, we might need to use the local store as
291291
a build remote, but that would cause a deadlock. */
292292
/* FIXME: Make it so we can use ourselves as a build remote even if we
@@ -296,9 +296,9 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
296296
*/
297297
for (auto & i : drv->outputsAndOptPaths(worker.store)) {
298298
if (i.second.second)
299-
lockFiles.insert(worker.store.Store::toRealPath(*i.second.second));
299+
lockFiles.insert(localStore->toRealPath(*i.second.second));
300300
else
301-
lockFiles.insert(worker.store.Store::toRealPath(drvPath) + "." + i.first);
301+
lockFiles.insert(localStore->toRealPath(drvPath) + "." + i.first);
302302
}
303303
}
304304

@@ -331,12 +331,14 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
331331

332332
/* If any of the outputs already exist but are not valid, delete
333333
them. */
334-
for (auto & [_, status] : initialOutputs) {
335-
if (!status.known || status.known->isValid())
336-
continue;
337-
auto storePath = status.known->path;
338-
debug("removing invalid path '%s'", worker.store.printStorePath(status.known->path));
339-
deletePath(worker.store.Store::toRealPath(storePath));
334+
if (auto * localStore = dynamic_cast<LocalFSStore *>(&worker.store)) {
335+
for (auto & [_, status] : initialOutputs) {
336+
if (!status.known || status.known->isValid())
337+
continue;
338+
auto storePath = status.known->path;
339+
debug("removing invalid path '%s'", worker.store.printStorePath(status.known->path));
340+
deletePath(localStore->toRealPath(storePath));
341+
}
340342
}
341343

342344
/* Don't do a remote build if the derivation has the attribute

src/libstore/daemon.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ static void performOp(
896896
auto path = WorkerProto::Serialise<StorePath>::read(*store, rconn);
897897
logger->startWork();
898898
logger->stopWork();
899-
dumpPath(store->toRealPath(path), conn.to);
899+
store->narFromPath(path, conn.to);
900900
break;
901901
}
902902

src/libstore/include/nix/store/local-fs-store.hh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ struct LocalFSStore : virtual Store, virtual GcStore, virtual LogStore
102102
return config.realStoreDir;
103103
}
104104

105-
Path toRealPath(const Path & storePath) override
105+
Path toRealPath(const StorePath & storePath)
106+
{
107+
return toRealPath(printStorePath(storePath));
108+
}
109+
110+
Path toRealPath(const Path & storePath)
106111
{
107112
assert(isInStore(storePath));
108113
return getRealStoreDir() + "/" + std::string(storePath, storeDir.size() + 1);

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -895,16 +895,6 @@ public:
895895
*/
896896
virtual std::optional<TrustedFlag> isTrustedClient() = 0;
897897

898-
virtual Path toRealPath(const Path & storePath)
899-
{
900-
return storePath;
901-
}
902-
903-
Path toRealPath(const StorePath & storePath)
904-
{
905-
return toRealPath(printStorePath(storePath));
906-
}
907-
908898
/**
909899
* Synchronises the options of the client with those of the daemon
910900
* (a no-op when there’s no daemon)

src/libstore/local-overlay-store.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ void LocalOverlayStore::optimiseStore()
246246
if (lowerStore->isValidPath(path)) {
247247
uint64_t bytesFreed = 0;
248248
// Deduplicate store path
249-
deleteStorePath(Store::toRealPath(path), bytesFreed);
249+
deleteStorePath(toRealPath(path), bytesFreed);
250250
}
251251
done++;
252252
act.progress(done, paths.size());

src/libstore/local-store.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ void LocalStore::addToStore(const ValidPathInfo & info, Source & source, RepairF
10631063

10641064
PathLocks outputLock;
10651065

1066-
auto realPath = Store::toRealPath(info.path);
1066+
auto realPath = toRealPath(info.path);
10671067

10681068
/* Lock the output path. But don't lock if we're being called
10691069
from a build hook (whose parent process already acquired a
@@ -1262,7 +1262,7 @@ StorePath LocalStore::addToStoreFromDump(
12621262
/* The first check above is an optimisation to prevent
12631263
unnecessary lock acquisition. */
12641264

1265-
auto realPath = Store::toRealPath(dstPath);
1265+
auto realPath = toRealPath(dstPath);
12661266

12671267
PathLocks outputLock({realPath});
12681268

@@ -1413,7 +1413,7 @@ bool LocalStore::verifyStore(bool checkContents, RepairFlag repair)
14131413

14141414
auto hashSink = HashSink(info->narHash.algo);
14151415

1416-
dumpPath(Store::toRealPath(i), hashSink);
1416+
dumpPath(toRealPath(i), hashSink);
14171417
auto current = hashSink.finish();
14181418

14191419
if (info->narHash != nullHash && info->narHash != current.hash) {

src/libstore/unix/build/chroot-derivation-builder.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
5858
environment using bind-mounts. We put it in the Nix store
5959
so that the build outputs can be moved efficiently from the
6060
chroot to their final location. */
61-
auto chrootParentDir = store.Store::toRealPath(drvPath) + ".chroot";
61+
auto chrootParentDir = store.toRealPath(drvPath) + ".chroot";
6262
deletePath(chrootParentDir);
6363

6464
/* Clean up the chroot directory automatically. */
@@ -171,7 +171,7 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
171171
continue;
172172
if (buildMode != bmCheck && status.known->isValid())
173173
continue;
174-
auto p = store.Store::toRealPath(status.known->path);
174+
auto p = store.toRealPath(status.known->path);
175175
if (pathExists(chrootRootDir + p))
176176
std::filesystem::rename((chrootRootDir + p), p);
177177
}
@@ -185,7 +185,7 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
185185

186186
debug("materialising '%s' in the sandbox", store.printStorePath(path));
187187

188-
Path source = store.Store::toRealPath(path);
188+
Path source = store.toRealPath(path);
189189
Path target = chrootRootDir + store.printStorePath(path);
190190

191191
if (pathExists(target)) {

0 commit comments

Comments
 (0)