Skip to content

Commit 806492e

Browse files
committed
Add a storeFS accessor for paths resulting from IFD
Hopefully fixes #11503.
1 parent dcdafc6 commit 806492e

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

src/libexpr/eval.cc

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ EvalState::EvalState(
255255
throw RestrictedPathError("access to absolute path '%1%' is forbidden %2%", path, modeInformation);
256256
}))
257257
: getFSSourceAccessor())
258+
, storeFS(
259+
makeMountedSourceAccessor(
260+
{
261+
{CanonPath::root, makeEmptySourceAccessor()},
262+
{CanonPath(store->storeDir), makeFSSourceAccessor(dirOf(store->toRealPath(StorePath::dummy)))}
263+
}))
258264
, corepkgsFS(make_ref<MemorySourceAccessor>())
259265
, internalFS(make_ref<MemorySourceAccessor>())
260266
, derivationInternal{corepkgsFS->addFile(
@@ -422,16 +428,6 @@ void EvalState::checkURI(const std::string & uri)
422428
}
423429

424430

425-
Path EvalState::toRealPath(const Path & path, const NixStringContext & context)
426-
{
427-
// FIXME: check whether 'path' is in 'context'.
428-
return
429-
!context.empty() && store->isInStore(path)
430-
? store->toRealPath(path)
431-
: path;
432-
}
433-
434-
435431
Value * EvalState::addConstant(const std::string & name, Value & v, Constant info)
436432
{
437433
Value * v2 = allocValue();
@@ -2432,7 +2428,7 @@ SourcePath EvalState::coerceToPath(const PosIdx pos, Value & v, NixStringContext
24322428
auto path = coerceToString(pos, v, context, errorCtx, false, false, true).toOwned();
24332429
if (path == "" || path[0] != '/')
24342430
error<EvalError>("string '%1%' doesn't represent an absolute path", path).withTrace(pos, errorCtx).debugThrow();
2435-
return rootPath(CanonPath(path));
2431+
return stringWithContextToPath(path, context);
24362432
}
24372433

24382434

src/libexpr/eval.hh

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ public:
250250
*/
251251
const ref<SourceAccessor> rootFS;
252252

253+
/**
254+
* The accessor for the store.
255+
*/
256+
const ref<SourceAccessor> storeFS;
257+
253258
/**
254259
* The in-memory filesystem for <nix/...> paths.
255260
*/
@@ -389,6 +394,18 @@ public:
389394
*/
390395
SourcePath rootPath(PathView path);
391396

397+
/**
398+
* Convert `s` to a path. If `context` is not empty, the resulting
399+
* path will use the `storeFS` accessor; otherwise it will use
400+
* `rootFS`. When using a chroot store, this allows us to
401+
* distinguish between store paths resulting from
402+
* import-from-derivation and sources stored in the actual
403+
* /nix/store.
404+
*/
405+
SourcePath stringWithContextToPath(
406+
std::string_view s,
407+
const NixStringContext & context);
408+
392409
/**
393410
* Allow access to a path.
394411
*/
@@ -412,17 +429,6 @@ public:
412429

413430
void checkURI(const std::string & uri);
414431

415-
/**
416-
* When using a diverted store and 'path' is in the Nix store, map
417-
* 'path' to the diverted location (e.g. /nix/store/foo is mapped
418-
* to /home/alice/my-nix/nix/store/foo). However, this is only
419-
* done if the context is not empty, since otherwise we're
420-
* probably trying to read from the actual /nix/store. This is
421-
* intended to distinguish between import-from-derivation and
422-
* sources stored in the actual /nix/store.
423-
*/
424-
Path toRealPath(const Path & path, const NixStringContext & context);
425-
426432
/**
427433
* Parse a Nix expression from the specified file.
428434
*/

src/libexpr/paths.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "eval.hh"
2+
#include "store-api.hh"
23

34
namespace nix {
45

@@ -12,4 +13,15 @@ SourcePath EvalState::rootPath(PathView path)
1213
return {rootFS, CanonPath(absPath(path))};
1314
}
1415

16+
SourcePath EvalState::stringWithContextToPath(
17+
std::string_view s,
18+
const NixStringContext & context)
19+
{
20+
auto path = CanonPath(s);
21+
return
22+
!context.empty()
23+
? SourcePath{storeFS, std::move(path)}
24+
: rootPath(std::move(path));
25+
}
26+
1527
}

src/libexpr/primops.cc

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,9 @@ static SourcePath realisePath(EvalState & state, const PosIdx pos, Value & v, st
143143
auto path = state.coerceToPath(noPos, v, context, "while realising the context of a path");
144144

145145
try {
146-
if (!context.empty() && path.accessor == state.rootFS) {
146+
if (!context.empty() && path.accessor == state.storeFS) {
147147
auto rewrites = state.realiseContext(context);
148-
auto realPath = state.toRealPath(rewriteStrings(path.path.abs(), rewrites), context);
149-
path = {path.accessor, CanonPath(realPath)};
148+
path = {path.accessor, CanonPath(rewriteStrings(path.path.abs(), rewrites))};
150149
}
151150
return resolveSymlinks ? path.resolveSymlinks(*resolveSymlinks) : path;
152151
} catch (Error & e) {
@@ -2481,19 +2480,11 @@ static void addPath(
24812480
try {
24822481
StorePathSet refs;
24832482

2484-
if (path.accessor == state.rootFS && state.store->isInStore(path.path.abs())) {
2483+
if (path.accessor == state.storeFS && state.store->isInStore(path.path.abs())) {
24852484
// FIXME: handle CA derivation outputs (where path needs to
24862485
// be rewritten to the actual output).
24872486
auto rewrites = state.realiseContext(context);
2488-
path = {state.rootFS, CanonPath(state.toRealPath(rewriteStrings(path.path.abs(), rewrites), context))};
2489-
2490-
try {
2491-
auto [storePath, subPath] = state.store->toStorePath(path.path.abs());
2492-
// FIXME: we should scanForReferences on the path before adding it
2493-
refs = state.store->queryPathInfo(storePath)->references;
2494-
path = {state.rootFS, CanonPath(state.store->toRealPath(storePath) + subPath)};
2495-
} catch (Error &) { // FIXME: should be InvalidPathError
2496-
}
2487+
path = {path.accessor, CanonPath(rewriteStrings(path.path.abs(), rewrites))};
24972488
}
24982489

24992490
std::unique_ptr<PathFilter> filter;

0 commit comments

Comments
 (0)