Skip to content

Commit 4c02828

Browse files
committed
Remove the source accessor from path values
We can use the rootFS accessor for everything and just mounted any other accessor (like corepkgsFS) on top of that.
1 parent 8ff43c2 commit 4c02828

File tree

20 files changed

+57
-71
lines changed

20 files changed

+57
-71
lines changed

src/libcmd/installable-value.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ ref<InstallableValue> InstallableValue::require(ref<Installable> installable)
4545
std::optional<DerivedPathWithInfo> InstallableValue::trySinglePathToDerivedPaths(Value & v, const PosIdx pos, std::string_view errorCtx)
4646
{
4747
if (v.type() == nPath) {
48-
auto storePath = fetchToStore(*state->store, v.path(), FetchMode::Copy);
48+
auto storePath = fetchToStore(*state->store, state->rootPath(v.path()), FetchMode::Copy);
4949
return {{
5050
.path = DerivedPath::Opaque {
5151
.path = std::move(storePath),

src/libexpr-c/nix_api_value.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ nix_err nix_init_path_string(nix_c_context * context, EvalState * s, nix_value *
427427
context->last_err_code = NIX_OK;
428428
try {
429429
auto & v = check_value_out(value);
430-
v.mkPath(s->state.rootPath(nix::CanonPath(str)));
430+
v.mkPath(nix::CanonPath(str));
431431
}
432432
NIXC_CATCH_ERRS
433433
}

src/libexpr-test-support/include/nix/expr/tests/libexpr.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ namespace nix {
121121
return false;
122122
} else {
123123
auto path = arg.path();
124-
if (path.path != CanonPath(p)) {
125-
*result_listener << "Expected a path that equals \"" << p << "\" but got: " << path.path;
124+
if (path != CanonPath(p)) {
125+
*result_listener << "Expected a path that equals \"" << p << "\" but got: " << path;
126126
return false;
127127
}
128128
}

src/libexpr-tests/json.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace nix {
6262
// not supported by store 'dummy'" thrown in the test body.
6363
TEST_F(JSONValueTest, DISABLED_Path) {
6464
Value v;
65-
v.mkPath(state.rootPath(CanonPath("/test")));
65+
v.mkPath("/test");
6666
ASSERT_EQ(getJSONValue(v), "\"/nix/store/g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-x\"");
6767
}
6868
} /* namespace nix */

src/libexpr-tests/value/print.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ TEST_F(ValuePrintingTests, ansiColorsStringElided)
380380
TEST_F(ValuePrintingTests, ansiColorsPath)
381381
{
382382
Value v;
383-
v.mkPath(state.rootPath(CanonPath("puppy")));
383+
v.mkPath("/puppy");
384384

385385
test(v,
386386
ANSI_GREEN "/puppy" ANSI_NORMAL,

src/libexpr/eval-cache.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,8 @@ Value & AttrCursor::forceValue()
473473
cachedValue = {root->db->setString(getKey(), v.c_str(), v.context()),
474474
string_t{v.c_str(), {}}};
475475
else if (v.type() == nPath) {
476-
auto path = v.path().path;
477-
cachedValue = {root->db->setString(getKey(), path.abs()), string_t{path.abs(), {}}};
476+
auto path = v.path().abs();
477+
cachedValue = {root->db->setString(getKey(), path), string_t{path, {}}};
478478
}
479479
else if (v.type() == nBool)
480480
cachedValue = {root->db->setBool(getKey(), v.boolean()), v.boolean()};
@@ -607,7 +607,7 @@ std::string AttrCursor::getString()
607607
if (v.type() != nString && v.type() != nPath)
608608
root->state.error<TypeError>("'%s' is not a string but %s", getAttrPathStr(), showType(v)).debugThrow();
609609

610-
return v.type() == nString ? v.c_str() : v.path().to_string();
610+
return v.type() == nString ? v.c_str() : v.path().abs();
611611
}
612612

613613
string_t AttrCursor::getStringWithContext()
@@ -654,7 +654,7 @@ string_t AttrCursor::getStringWithContext()
654654
return {v.c_str(), std::move(context)};
655655
}
656656
else if (v.type() == nPath)
657-
return {v.path().to_string(), {}};
657+
return {v.path().abs(), {}};
658658
else
659659
root->state.error<TypeError>("'%s' is not a string but %s", getAttrPathStr(), showType(v)).debugThrow();
660660
}

src/libexpr/eval.cc

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ EvalState::EvalState(
248248
}
249249
, repair(NoRepair)
250250
, emptyBindings(0)
251+
, corepkgsFS(make_ref<MemorySourceAccessor>())
252+
, corepkgsPath(CanonPath(store->printStorePath(StorePath::random("source"))))
251253
, storeFS(
252254
makeMountedSourceAccessor(
253255
{
@@ -301,7 +303,6 @@ EvalState::EvalState(
301303

302304
accessor;
303305
}))
304-
, corepkgsFS(make_ref<MemorySourceAccessor>())
305306
, internalFS(make_ref<MemorySourceAccessor>())
306307
, derivationInternal{corepkgsFS->addFile(
307308
CanonPath("derivation-internal.nix"),
@@ -327,6 +328,9 @@ EvalState::EvalState(
327328
corepkgsFS->setPathDisplay("<nix", ">");
328329
internalFS->setPathDisplay("«nix-internal»", "");
329330

331+
storeFS->mount(corepkgsPath, corepkgsFS);
332+
allowPath(corepkgsPath.abs());
333+
330334
countCalls = getEnv("NIX_COUNT_CALLS").value_or("0") != "0";
331335

332336
assertGCInitialized();
@@ -904,9 +908,9 @@ void Value::mkStringMove(const char * s, const NixStringContext & context)
904908
mkString(s, encodeContext(context));
905909
}
906910

907-
void Value::mkPath(const SourcePath & path)
911+
void Value::mkPath(const CanonPath & path)
908912
{
909-
mkPath(&*path.accessor, makeImmutableString(path.path.abs()));
913+
mkPath(makeImmutableString(path.abs()));
910914
}
911915

912916

@@ -2098,7 +2102,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
20982102
else if (firstType == nPath) {
20992103
if (hasContext(context))
21002104
state.error<EvalError>("a string that refers to a store path cannot be appended to a path").atPos(pos).withFrame(env, *this).debugThrow();
2101-
v.mkPath(state.rootPath(CanonPath(str())));
2105+
v.mkPath(CanonPath(str()));
21022106
} else
21032107
v.mkStringMove(c_str(), context);
21042108
}
@@ -2356,16 +2360,16 @@ BackedStringView EvalState::coerceToString(
23562360
// slash, as in /foo/${x}.
23572361
v.payload.path.path
23582362
: copyToStore
2359-
? store->printStorePath(copyPathToStore(context, v.path()))
2363+
? store->printStorePath(copyPathToStore(context, rootPath(v.path())))
23602364
: ({
23612365
auto path = v.path();
2362-
if (path.accessor == rootFS && store->isInStore(path.path.abs())) {
2366+
if (store->isInStore(path.abs())) {
23632367
context.insert(
23642368
NixStringContextElem::Path{
2365-
.storePath = store->toStorePath(path.path.abs()).first
2369+
.storePath = store->toStorePath(path.abs()).first
23662370
});
23672371
}
2368-
std::string(path.path.abs());
2372+
std::string(path.abs());
23692373
});
23702374
}
23712375

@@ -2476,7 +2480,7 @@ SourcePath EvalState::coerceToPath(const PosIdx pos, Value & v, NixStringContext
24762480

24772481
/* Handle path values directly, without coercing to a string. */
24782482
if (v.type() == nPath)
2479-
return v.path();
2483+
return rootPath(v.path());
24802484

24812485
/* Similarly, handle __toString where the result may be a path
24822486
value. */
@@ -2635,13 +2639,6 @@ void EvalState::assertEqValues(Value & v1, Value & v2, const PosIdx pos, std::st
26352639
return;
26362640

26372641
case nPath:
2638-
if (v1.payload.path.accessor != v2.payload.path.accessor) {
2639-
error<AssertionError>(
2640-
"path '%s' is not equal to path '%s' because their accessors are different",
2641-
ValuePrinter(*this, v1, errorPrintOptions),
2642-
ValuePrinter(*this, v2, errorPrintOptions))
2643-
.debugThrow();
2644-
}
26452642
if (strcmp(v1.payload.path.path, v2.payload.path.path) != 0) {
26462643
error<AssertionError>(
26472644
"path '%s' is not equal to path '%s'",
@@ -2808,9 +2805,7 @@ bool EvalState::eqValues(Value & v1, Value & v2, const PosIdx pos, std::string_v
28082805

28092806
case nPath:
28102807
return
2811-
// FIXME: compare accessors by their fingerprint.
2812-
v1.payload.path.accessor == v2.payload.path.accessor
2813-
&& strcmp(v1.payload.path.path, v2.payload.path.path) == 0;
2808+
strcmp(v1.payload.path.path, v2.payload.path.path) == 0;
28142809

28152810
case nNull:
28162811
return true;
@@ -3125,7 +3120,7 @@ SourcePath EvalState::findFile(const LookupPath & lookupPath, const std::string_
31253120
}
31263121

31273122
if (hasPrefix(path, "nix/"))
3128-
return {corepkgsFS, CanonPath(path.substr(3))};
3123+
return rootPath(corepkgsPath / CanonPath(path.substr(3)));
31293124

31303125
error<ThrownError>(
31313126
settings.pureEval

src/libexpr/include/nix/expr/eval.hh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ public:
270270
/** `"unknown"` */
271271
Value vStringUnknown;
272272

273+
/**
274+
* The in-memory filesystem for <nix/...> paths.
275+
*/
276+
const ref<MemorySourceAccessor> corepkgsFS;
277+
278+
const CanonPath corepkgsPath;
279+
273280
/**
274281
* The accessor corresponding to `store`.
275282
*/
@@ -280,11 +287,6 @@ public:
280287
*/
281288
const ref<SourceAccessor> rootFS;
282289

283-
/**
284-
* The in-memory filesystem for <nix/...> paths.
285-
*/
286-
const ref<MemorySourceAccessor> corepkgsFS;
287-
288290
/**
289291
* In-memory filesystem for internal, non-user-callable Nix
290292
* expressions like `derivation.nix`.

src/libexpr/include/nix/expr/nixexpr.hh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,11 @@ struct ExprString : Expr
135135

136136
struct ExprPath : Expr
137137
{
138-
ref<SourceAccessor> accessor;
139138
std::string s;
140139
Value v;
141-
ExprPath(ref<SourceAccessor> accessor, std::string s) : accessor(accessor), s(std::move(s))
140+
ExprPath(std::string s) : s(std::move(s))
142141
{
143-
v.mkPath(&*accessor, this->s.c_str());
142+
v.mkPath(this->s.c_str());
144143
}
145144
Value * maybeThunk(EvalState & state, Env & env) override;
146145
COMMON_METHODS

src/libexpr/include/nix/expr/value.hh

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ public:
216216
};
217217

218218
struct Path {
219-
SourceAccessor * accessor;
220219
const char * path;
221220
};
222221

@@ -336,12 +335,11 @@ public:
336335
mkString(s.c_str());
337336
}
338337

339-
void mkPath(const SourcePath & path);
340-
void mkPath(std::string_view path);
338+
void mkPath(const CanonPath & path);
341339

342-
inline void mkPath(SourceAccessor * accessor, const char * path)
340+
inline void mkPath(const char * path)
343341
{
344-
finishValue(tPath, { .path = { .accessor = accessor, .path = path } });
342+
finishValue(tPath, {.path = {.path = path}});
345343
}
346344

347345
inline void mkNull()
@@ -440,12 +438,10 @@ public:
440438
*/
441439
bool isTrivial() const;
442440

443-
SourcePath path() const
441+
CanonPath path() const
444442
{
445443
assert(internalType == tPath);
446-
return SourcePath(
447-
ref(payload.path.accessor->shared_from_this()),
448-
CanonPath(CanonPath::unchecked_t(), payload.path.path));
444+
return CanonPath(CanonPath::unchecked_t(), payload.path.path);
449445
}
450446

451447
std::string_view string_view() const

0 commit comments

Comments
 (0)