2013-11-22 15:41:48 +00:00
|
|
|
#include "user-env.hh"
|
2010-04-19 10:47:56 +00:00
|
|
|
#include "util.hh"
|
2010-04-21 15:08:58 +00:00
|
|
|
#include "derivations.hh"
|
|
|
|
#include "store-api.hh"
|
2021-03-02 03:50:41 +00:00
|
|
|
#include "path-with-outputs.hh"
|
2020-10-09 20:18:08 +00:00
|
|
|
#include "local-fs-store.hh"
|
2010-04-21 15:08:58 +00:00
|
|
|
#include "globals.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "eval.hh"
|
2013-11-19 13:09:03 +00:00
|
|
|
#include "eval-inline.hh"
|
2010-04-21 15:08:58 +00:00
|
|
|
#include "profiles.hh"
|
2010-04-19 10:47:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
DrvInfos queryInstalled(EvalState & state, const Path & userEnv)
|
|
|
|
{
|
2010-04-19 12:10:04 +00:00
|
|
|
DrvInfos elems;
|
2020-04-02 09:54:48 +00:00
|
|
|
if (pathExists(userEnv + "/manifest.json"))
|
|
|
|
throw Error("profile '%s' is incompatible with 'nix-env'; please use 'nix profile' instead", userEnv);
|
2023-04-06 11:15:50 +00:00
|
|
|
auto manifestFile = userEnv + "/manifest.nix";
|
2010-04-21 15:08:58 +00:00
|
|
|
if (pathExists(manifestFile)) {
|
|
|
|
Value v;
|
2023-04-06 11:15:50 +00:00
|
|
|
state.evalFile(state.rootPath(CanonPath(manifestFile)), v);
|
2014-09-19 14:49:41 +00:00
|
|
|
Bindings & bindings(*state.allocBindings(0));
|
2012-10-04 19:22:25 +00:00
|
|
|
getDerivations(state, v, "", bindings, elems, false);
|
2013-11-19 10:18:13 +00:00
|
|
|
}
|
2010-04-19 10:47:56 +00:00
|
|
|
return elems;
|
2010-04-19 12:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 15:08:58 +00:00
|
|
|
bool createUserEnv(EvalState & state, DrvInfos & elems,
|
|
|
|
const Path & profile, bool keepDerivations,
|
2022-02-25 15:00:00 +00:00
|
|
|
const std::string & lockToken)
|
2010-04-21 15:08:58 +00:00
|
|
|
{
|
|
|
|
/* Build the components in the user environment, if they don't
|
|
|
|
exist already. */
|
2019-12-05 18:11:09 +00:00
|
|
|
std::vector<StorePathWithOutputs> drvsToBuild;
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : elems)
|
2022-03-02 09:57:19 +00:00
|
|
|
if (auto drvPath = i.queryDrvPath())
|
|
|
|
drvsToBuild.push_back({*drvPath});
|
2010-04-21 15:08:58 +00:00
|
|
|
|
2023-03-02 14:44:19 +00:00
|
|
|
debug("building user environment dependencies");
|
2021-03-02 03:50:41 +00:00
|
|
|
state.store->buildPaths(
|
2021-04-05 13:48:18 +00:00
|
|
|
toDerivedPaths(drvsToBuild),
|
2021-03-02 03:50:41 +00:00
|
|
|
state.repair ? bmRepair : bmNormal);
|
2010-04-21 15:08:58 +00:00
|
|
|
|
|
|
|
/* Construct the whole top level derivation. */
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet references;
|
2010-04-21 15:08:58 +00:00
|
|
|
Value manifest;
|
|
|
|
state.mkList(manifest, elems.size());
|
2022-01-04 16:39:16 +00:00
|
|
|
size_t n = 0;
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & i : elems) {
|
2010-04-21 15:08:58 +00:00
|
|
|
/* Create a pseudo-derivation containing the name, system,
|
2012-12-04 13:20:36 +00:00
|
|
|
output paths, and optionally the derivation path, as well
|
|
|
|
as the meta attributes. */
|
2022-03-02 09:57:19 +00:00
|
|
|
std::optional<StorePath> drvPath = keepDerivations ? i.queryDrvPath() : std::nullopt;
|
2022-03-12 12:47:01 +00:00
|
|
|
DrvInfo::Outputs outputs = i.queryOutputs(true, true);
|
2020-12-30 20:20:03 +00:00
|
|
|
StringSet metaNames = i.queryMetaNames();
|
2010-04-21 15:08:58 +00:00
|
|
|
|
2022-01-04 16:39:16 +00:00
|
|
|
auto attrs = state.buildBindings(7 + outputs.size());
|
2010-04-21 15:08:58 +00:00
|
|
|
|
2022-01-04 16:39:16 +00:00
|
|
|
attrs.alloc(state.sType).mkString("derivation");
|
|
|
|
attrs.alloc(state.sName).mkString(i.queryName());
|
2017-07-17 17:02:56 +00:00
|
|
|
auto system = i.querySystem();
|
|
|
|
if (!system.empty())
|
2022-01-04 16:39:16 +00:00
|
|
|
attrs.alloc(state.sSystem).mkString(system);
|
2022-03-02 09:57:19 +00: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 19:52:33 +00:00
|
|
|
|
2016-02-23 13:19:14 +00:00
|
|
|
// Copy each output meant for installation.
|
2022-01-04 16:39:16 +00:00
|
|
|
auto & vOutputs = attrs.alloc(state.sOutputs);
|
2012-12-04 13:20:36 +00:00
|
|
|
state.mkList(vOutputs, outputs.size());
|
2022-01-04 16:39:16 +00:00
|
|
|
for (const auto & [m, j] : enumerate(outputs)) {
|
|
|
|
(vOutputs.listElems()[m] = state.allocValue())->mkString(j.first);
|
|
|
|
auto outputAttrs = state.buildBindings(2);
|
2022-03-12 12:47:01 +00:00
|
|
|
outputAttrs.alloc(state.sOutPath).mkString(state.store->printStorePath(*j.second));
|
2022-01-04 16:39:16 +00:00
|
|
|
attrs.alloc(j.first).mkAttrs(outputAttrs);
|
2012-12-04 13:20:36 +00:00
|
|
|
|
|
|
|
/* This is only necessary when installing store paths, e.g.,
|
|
|
|
`nix-env -i /nix/store/abcd...-foo'. */
|
2022-03-12 12:47:01 +00:00
|
|
|
state.store->addTempRoot(*j.second);
|
|
|
|
state.store->ensurePath(*j.second);
|
2012-12-04 13:20:36 +00:00
|
|
|
|
2022-03-12 12:47:01 +00:00
|
|
|
references.insert(*j.second);
|
2012-12-04 13:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the meta attributes.
|
2022-01-04 16:39:16 +00:00
|
|
|
auto meta = state.buildBindings(metaNames.size());
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & j : metaNames) {
|
|
|
|
Value * v = i.queryMeta(j);
|
2013-11-19 13:29:39 +00:00
|
|
|
if (!v) continue;
|
2022-01-04 16:39:16 +00:00
|
|
|
meta.insert(state.symbols.create(j), v);
|
2010-04-21 15:08:58 +00:00
|
|
|
}
|
2022-01-04 16:39:16 +00:00
|
|
|
|
|
|
|
attrs.alloc(state.sMeta).mkAttrs(meta);
|
|
|
|
|
|
|
|
(manifest.listElems()[n++] = state.allocValue())->mkAttrs(attrs);
|
2012-12-03 17:19:49 +00:00
|
|
|
|
2022-03-02 09:57:19 +00:00
|
|
|
if (drvPath) references.insert(*drvPath);
|
2010-04-21 15:08:58 +00: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 12:18:11 +00:00
|
|
|
std::ostringstream str;
|
2022-03-05 13:40:24 +00:00
|
|
|
manifest.print(state.symbols, str, true);
|
2019-12-05 18:11:09 +00:00
|
|
|
auto manifestFile = state.store->addTextToStore("env-manifest.nix",
|
2022-03-22 12:18:11 +00:00
|
|
|
str.str(), references);
|
2010-04-21 15:08:58 +00:00
|
|
|
|
|
|
|
/* Get the environment builder expression. */
|
|
|
|
Value envBuilder;
|
2020-03-11 15:41:22 +00:00
|
|
|
state.eval(state.parseExprFromString(
|
|
|
|
#include "buildenv.nix.gen.hh"
|
2023-04-06 11:15:50 +00:00
|
|
|
, state.rootPath(CanonPath::root)), envBuilder);
|
2010-04-21 15:08:58 +00:00
|
|
|
|
|
|
|
/* Construct a Nix expression that calls the user environment
|
|
|
|
builder with the manifest as argument. */
|
2022-01-04 16:39:16 +00: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 01:31:10 +00:00
|
|
|
state.mkStorePathString(manifestFile, attrs.alloc("manifest"));
|
2022-01-04 16:39:16 +00:00
|
|
|
attrs.insert(state.symbols.create("derivations"), &manifest);
|
|
|
|
Value args;
|
|
|
|
args.mkAttrs(attrs);
|
|
|
|
|
|
|
|
Value topLevel;
|
|
|
|
topLevel.mkApp(&envBuilder, &args);
|
2012-12-03 17:19:49 +00:00
|
|
|
|
2010-04-21 15:08:58 +00:00
|
|
|
/* Evaluate it. */
|
|
|
|
debug("evaluating user environment builder");
|
2022-02-03 23:31:33 +00: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 01:31:10 +00:00
|
|
|
NixStringContext context;
|
2014-04-04 20:19:33 +00:00
|
|
|
Attr & aDrvPath(*topLevel.attrs->find(state.sDrvPath));
|
2023-01-19 12:23:04 +00:00
|
|
|
auto topLevelDrv = state.coerceToStorePath(aDrvPath.pos, *aDrvPath.value, context, "");
|
2014-04-04 20:19:33 +00:00
|
|
|
Attr & aOutPath(*topLevel.attrs->find(state.sOutPath));
|
2023-01-19 12:23:04 +00:00
|
|
|
auto topLevelOut = state.coerceToStorePath(aOutPath.pos, *aOutPath.value, context, "");
|
2012-12-03 17:19:49 +00:00
|
|
|
|
2010-04-21 15:08:58 +00:00
|
|
|
/* Realise the resulting store expression. */
|
|
|
|
debug("building user environment");
|
2019-12-05 18:11:09 +00:00
|
|
|
std::vector<StorePathWithOutputs> topLevelDrvs;
|
2020-06-16 20:20:18 +00:00
|
|
|
topLevelDrvs.push_back({topLevelDrv});
|
2021-03-02 03:50:41 +00:00
|
|
|
state.store->buildPaths(
|
2021-04-05 13:48:18 +00:00
|
|
|
toDerivedPaths(topLevelDrvs),
|
2021-03-02 03:50:41 +00:00
|
|
|
state.repair ? bmRepair : bmNormal);
|
2010-04-21 15:08:58 +00:00
|
|
|
|
|
|
|
/* Switch the current user environment to the output path. */
|
2016-06-02 11:33:49 +00:00
|
|
|
auto store2 = state.store.dynamic_pointer_cast<LocalFSStore>();
|
2010-04-21 15:08:58 +00:00
|
|
|
|
2016-06-02 11:33:49 +00:00
|
|
|
if (store2) {
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
|
|
|
Path lockTokenCur = optimisticLockProfile(profile);
|
|
|
|
if (lockToken != lockTokenCur) {
|
2020-05-13 15:52:36 +00:00
|
|
|
printInfo("profile '%1%' changed while we were busy; restarting", profile);
|
2016-06-02 11:33:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-03 17:19:49 +00:00
|
|
|
|
2023-03-02 14:44:19 +00:00
|
|
|
debug("switching to new user environment");
|
2022-03-02 09:57:19 +00:00
|
|
|
Path generation = createGeneration(ref<LocalFSStore>(store2), profile, topLevelOut);
|
2016-06-02 11:33:49 +00:00
|
|
|
switchLink(profile, generation);
|
|
|
|
}
|
2010-04-21 15:08:58 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 10:47:56 +00:00
|
|
|
}
|