2013-11-22 17:41:48 +02:00
|
|
|
#include "user-env.hh"
|
2010-04-21 18:08:58 +03:00
|
|
|
#include "derivations.hh"
|
|
|
|
#include "store-api.hh"
|
2021-03-02 05:50:41 +02:00
|
|
|
#include "path-with-outputs.hh"
|
2020-10-09 23:18:08 +03:00
|
|
|
#include "local-fs-store.hh"
|
2010-04-21 18:08:58 +03:00
|
|
|
#include "globals.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "eval.hh"
|
2013-11-19 15:09:03 +02:00
|
|
|
#include "eval-inline.hh"
|
2010-04-21 18:08:58 +03:00
|
|
|
#include "profiles.hh"
|
2010-04-19 13:47:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
DrvInfos queryInstalled(EvalState & state, const Path & userEnv)
|
|
|
|
{
|
2010-04-19 15:10:04 +03:00
|
|
|
DrvInfos elems;
|
2020-04-02 12:54:48 +03:00
|
|
|
if (pathExists(userEnv + "/manifest.json"))
|
|
|
|
throw Error("profile '%s' is incompatible with 'nix-env'; please use 'nix profile' instead", userEnv);
|
2023-04-06 14:15:50 +03:00
|
|
|
auto manifestFile = userEnv + "/manifest.nix";
|
2010-04-21 18:08:58 +03:00
|
|
|
if (pathExists(manifestFile)) {
|
|
|
|
Value v;
|
2023-12-06 00:02:59 +02:00
|
|
|
state.evalFile(state.rootPath(CanonPath(manifestFile)).resolveSymlinks(), v);
|
2014-09-19 17:49:41 +03:00
|
|
|
Bindings & bindings(*state.allocBindings(0));
|
2012-10-04 22:22:25 +03:00
|
|
|
getDerivations(state, v, "", bindings, elems, false);
|
2013-11-19 12:18:13 +02:00
|
|
|
}
|
2010-04-19 13:47:56 +03:00
|
|
|
return elems;
|
2010-04-19 15:10:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
bool createUserEnv(EvalState & state, DrvInfos & elems,
|
|
|
|
const Path & profile, bool keepDerivations,
|
2022-02-25 17:00:00 +02:00
|
|
|
const std::string & lockToken)
|
2010-04-21 18:08:58 +03:00
|
|
|
{
|
|
|
|
/* Build the components in the user environment, if they don't
|
|
|
|
exist already. */
|
2019-12-05 20:11:09 +02:00
|
|
|
std::vector<StorePathWithOutputs> drvsToBuild;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : elems)
|
2022-03-02 11:57:19 +02:00
|
|
|
if (auto drvPath = i.queryDrvPath())
|
|
|
|
drvsToBuild.push_back({*drvPath});
|
2010-04-21 18:08:58 +03:00
|
|
|
|
2023-03-02 16:44:19 +02:00
|
|
|
debug("building user environment dependencies");
|
2021-03-02 05:50:41 +02:00
|
|
|
state.store->buildPaths(
|
2021-04-05 16:48:18 +03:00
|
|
|
toDerivedPaths(drvsToBuild),
|
2021-03-02 05:50:41 +02:00
|
|
|
state.repair ? bmRepair : bmNormal);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
|
|
/* Construct the whole top level derivation. */
|
2019-12-05 20:11:09 +02:00
|
|
|
StorePathSet references;
|
2010-04-21 18:08:58 +03:00
|
|
|
Value manifest;
|
|
|
|
state.mkList(manifest, elems.size());
|
2022-01-04 18:39:16 +02:00
|
|
|
size_t n = 0;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : elems) {
|
2010-04-21 18:08:58 +03:00
|
|
|
/* Create a pseudo-derivation containing the name, system,
|
2012-12-04 15:20:36 +02:00
|
|
|
output paths, and optionally the derivation path, as well
|
|
|
|
as the meta attributes. */
|
2022-03-02 11:57:19 +02:00
|
|
|
std::optional<StorePath> drvPath = keepDerivations ? i.queryDrvPath() : std::nullopt;
|
2022-03-12 14:47:01 +02:00
|
|
|
DrvInfo::Outputs outputs = i.queryOutputs(true, true);
|
2020-12-30 22:20:03 +02:00
|
|
|
StringSet metaNames = i.queryMetaNames();
|
2010-04-21 18:08:58 +03:00
|
|
|
|
2022-01-04 18:39:16 +02:00
|
|
|
auto attrs = state.buildBindings(7 + outputs.size());
|
2010-04-21 18:08:58 +03:00
|
|
|
|
2022-01-04 18:39:16 +02:00
|
|
|
attrs.alloc(state.sType).mkString("derivation");
|
|
|
|
attrs.alloc(state.sName).mkString(i.queryName());
|
2017-07-17 20:02:56 +03:00
|
|
|
auto system = i.querySystem();
|
|
|
|
if (!system.empty())
|
2022-01-04 18:39:16 +02:00
|
|
|
attrs.alloc(state.sSystem).mkString(system);
|
2022-03-02 11:57:19 +02:00
|
|
|
attrs.alloc(state.sOutPath).mkString(state.store->printStorePath(i.queryOutPath()));
|
|
|
|
if (drvPath)
|
|
|
|
attrs.alloc(state.sDrvPath).mkString(state.store->printStorePath(*drvPath));
|
2010-10-24 22:52:33 +03:00
|
|
|
|
2016-02-23 15:19:14 +02:00
|
|
|
// Copy each output meant for installation.
|
2022-01-04 18:39:16 +02:00
|
|
|
auto & vOutputs = attrs.alloc(state.sOutputs);
|
2012-12-04 15:20:36 +02:00
|
|
|
state.mkList(vOutputs, outputs.size());
|
2022-01-04 18:39:16 +02:00
|
|
|
for (const auto & [m, j] : enumerate(outputs)) {
|
|
|
|
(vOutputs.listElems()[m] = state.allocValue())->mkString(j.first);
|
|
|
|
auto outputAttrs = state.buildBindings(2);
|
2022-03-12 14:47:01 +02:00
|
|
|
outputAttrs.alloc(state.sOutPath).mkString(state.store->printStorePath(*j.second));
|
2022-01-04 18:39:16 +02:00
|
|
|
attrs.alloc(j.first).mkAttrs(outputAttrs);
|
2012-12-04 15:20:36 +02:00
|
|
|
|
|
|
|
/* This is only necessary when installing store paths, e.g.,
|
|
|
|
`nix-env -i /nix/store/abcd...-foo'. */
|
2022-03-12 14:47:01 +02:00
|
|
|
state.store->addTempRoot(*j.second);
|
|
|
|
state.store->ensurePath(*j.second);
|
2012-12-04 15:20:36 +02:00
|
|
|
|
2022-03-12 14:47:01 +02:00
|
|
|
references.insert(*j.second);
|
2012-12-04 15:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the meta attributes.
|
2022-01-04 18:39:16 +02:00
|
|
|
auto meta = state.buildBindings(metaNames.size());
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & j : metaNames) {
|
|
|
|
Value * v = i.queryMeta(j);
|
2013-11-19 15:29:39 +02:00
|
|
|
if (!v) continue;
|
2022-01-04 18:39:16 +02:00
|
|
|
meta.insert(state.symbols.create(j), v);
|
2010-04-21 18:08:58 +03:00
|
|
|
}
|
2022-01-04 18:39:16 +02:00
|
|
|
|
|
|
|
attrs.alloc(state.sMeta).mkAttrs(meta);
|
|
|
|
|
|
|
|
(manifest.listElems()[n++] = state.allocValue())->mkAttrs(attrs);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2022-03-02 11:57:19 +02:00
|
|
|
if (drvPath) references.insert(*drvPath);
|
2010-04-21 18:08:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Also write a copy of the list of user environment elements to
|
|
|
|
the store; we need it for future modifications of the
|
|
|
|
environment. */
|
2022-03-22 14:18:11 +02:00
|
|
|
std::ostringstream str;
|
2022-03-05 15:40:24 +02:00
|
|
|
manifest.print(state.symbols, str, true);
|
2019-12-05 20:11:09 +02:00
|
|
|
auto manifestFile = state.store->addTextToStore("env-manifest.nix",
|
2022-03-22 14:18:11 +02:00
|
|
|
str.str(), references);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
|
|
/* Get the environment builder expression. */
|
|
|
|
Value envBuilder;
|
2020-03-11 17:41:22 +02:00
|
|
|
state.eval(state.parseExprFromString(
|
|
|
|
#include "buildenv.nix.gen.hh"
|
2023-04-06 14:15:50 +03:00
|
|
|
, state.rootPath(CanonPath::root)), envBuilder);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
|
|
/* Construct a Nix expression that calls the user environment
|
|
|
|
builder with the manifest as argument. */
|
2022-01-04 18:39:16 +02:00
|
|
|
auto attrs = state.buildBindings(3);
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
state.mkStorePathString(manifestFile, attrs.alloc("manifest"));
|
2022-01-04 18:39:16 +02:00
|
|
|
attrs.insert(state.symbols.create("derivations"), &manifest);
|
|
|
|
Value args;
|
|
|
|
args.mkAttrs(attrs);
|
|
|
|
|
|
|
|
Value topLevel;
|
|
|
|
topLevel.mkApp(&envBuilder, &args);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
/* Evaluate it. */
|
|
|
|
debug("evaluating user environment builder");
|
2022-02-04 01:31:33 +02:00
|
|
|
state.forceValue(topLevel, [&]() { return topLevel.determinePos(noPos); });
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
NixStringContext context;
|
2014-04-04 23:19:33 +03:00
|
|
|
Attr & aDrvPath(*topLevel.attrs->find(state.sDrvPath));
|
2023-01-19 14:23:04 +02:00
|
|
|
auto topLevelDrv = state.coerceToStorePath(aDrvPath.pos, *aDrvPath.value, context, "");
|
2014-04-04 23:19:33 +03:00
|
|
|
Attr & aOutPath(*topLevel.attrs->find(state.sOutPath));
|
2023-01-19 14:23:04 +02:00
|
|
|
auto topLevelOut = state.coerceToStorePath(aOutPath.pos, *aOutPath.value, context, "");
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2010-04-21 18:08:58 +03:00
|
|
|
/* Realise the resulting store expression. */
|
|
|
|
debug("building user environment");
|
2019-12-05 20:11:09 +02:00
|
|
|
std::vector<StorePathWithOutputs> topLevelDrvs;
|
2020-06-16 23:20:18 +03:00
|
|
|
topLevelDrvs.push_back({topLevelDrv});
|
2021-03-02 05:50:41 +02:00
|
|
|
state.store->buildPaths(
|
2021-04-05 16:48:18 +03:00
|
|
|
toDerivedPaths(topLevelDrvs),
|
2021-03-02 05:50:41 +02:00
|
|
|
state.repair ? bmRepair : bmNormal);
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
|
|
/* Switch the current user environment to the output path. */
|
2016-06-02 14:33:49 +03:00
|
|
|
auto store2 = state.store.dynamic_pointer_cast<LocalFSStore>();
|
2010-04-21 18:08:58 +03:00
|
|
|
|
2016-06-02 14:33:49 +03:00
|
|
|
if (store2) {
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
|
|
|
Path lockTokenCur = optimisticLockProfile(profile);
|
|
|
|
if (lockToken != lockTokenCur) {
|
2020-05-13 18:52:36 +03:00
|
|
|
printInfo("profile '%1%' changed while we were busy; restarting", profile);
|
2016-06-02 14:33:49 +03:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2023-03-02 16:44:19 +02:00
|
|
|
debug("switching to new user environment");
|
2023-06-19 07:04:59 +03:00
|
|
|
Path generation = createGeneration(*store2, profile, topLevelOut);
|
2016-06-02 14:33:49 +03:00
|
|
|
switchLink(profile, generation);
|
|
|
|
}
|
2010-04-21 18:08:58 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 13:47:56 +03:00
|
|
|
}
|