mirror of
https://github.com/NixOS/nix
synced 2025-06-25 10:41:16 +02:00
Make the Store API more type-safe
Most functions now take a StorePath argument rather than a Path (which is just an alias for std::string). The StorePath constructor ensures that the path is syntactically correct (i.e. it looks like <store-dir>/<base32-hash>-<name>). Similarly, functions like buildPaths() now take a StorePathWithOutputs, rather than abusing Path by adding a '!<outputs>' suffix. Note that the StorePath type is implemented in Rust. This involves some hackery to allow Rust values to be used directly in C++, via a helper type whose destructor calls the Rust type's drop() function. The main issue is the dynamic nature of C++ move semantics: after we have moved a Rust value, we should not call the drop function on the original value. So when we move a value, we set the original value to bitwise zero, and the destructor only calls drop() if the value is not bitwise zero. This should be sufficient for most types. Also lots of minor cleanups to the C++ API to make it more modern (e.g. using std::optional and std::string_view in some places).
This commit is contained in:
parent
ebd89999c2
commit
bbe97dff8b
98 changed files with 2638 additions and 2880 deletions
|
@ -32,16 +32,16 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
|
|||
{
|
||||
/* Build the components in the user environment, if they don't
|
||||
exist already. */
|
||||
PathSet drvsToBuild;
|
||||
std::vector<StorePathWithOutputs> drvsToBuild;
|
||||
for (auto & i : elems)
|
||||
if (i.queryDrvPath() != "")
|
||||
drvsToBuild.insert(i.queryDrvPath());
|
||||
drvsToBuild.push_back({state.store->parseStorePath(i.queryDrvPath())});
|
||||
|
||||
debug(format("building user environment dependencies"));
|
||||
state.store->buildPaths(drvsToBuild, state.repair ? bmRepair : bmNormal);
|
||||
|
||||
/* Construct the whole top level derivation. */
|
||||
PathSet references;
|
||||
StorePathSet references;
|
||||
Value manifest;
|
||||
state.mkList(manifest, elems.size());
|
||||
unsigned int n = 0;
|
||||
|
@ -77,10 +77,10 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
|
|||
|
||||
/* This is only necessary when installing store paths, e.g.,
|
||||
`nix-env -i /nix/store/abcd...-foo'. */
|
||||
state.store->addTempRoot(j.second);
|
||||
state.store->ensurePath(j.second);
|
||||
state.store->addTempRoot(state.store->parseStorePath(j.second));
|
||||
state.store->ensurePath(state.store->parseStorePath(j.second));
|
||||
|
||||
references.insert(j.second);
|
||||
references.insert(state.store->parseStorePath(j.second));
|
||||
}
|
||||
|
||||
// Copy the meta attributes.
|
||||
|
@ -95,14 +95,14 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
|
|||
vMeta.attrs->sort();
|
||||
v.attrs->sort();
|
||||
|
||||
if (drvPath != "") references.insert(drvPath);
|
||||
if (drvPath != "") references.insert(state.store->parseStorePath(drvPath));
|
||||
}
|
||||
|
||||
/* Also write a copy of the list of user environment elements to
|
||||
the store; we need it for future modifications of the
|
||||
environment. */
|
||||
Path manifestFile = state.store->addTextToStore("env-manifest.nix",
|
||||
(format("%1%") % manifest).str(), references);
|
||||
auto manifestFile = state.store->addTextToStore("env-manifest.nix",
|
||||
fmt("%s", manifest), references);
|
||||
|
||||
/* Get the environment builder expression. */
|
||||
Value envBuilder;
|
||||
|
@ -113,7 +113,7 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
|
|||
Value args, topLevel;
|
||||
state.mkAttrs(args, 3);
|
||||
mkString(*state.allocAttr(args, state.symbols.create("manifest")),
|
||||
manifestFile, {manifestFile});
|
||||
state.store->printStorePath(manifestFile), {state.store->printStorePath(manifestFile)});
|
||||
args.attrs->push_back(Attr(state.symbols.create("derivations"), &manifest));
|
||||
args.attrs->sort();
|
||||
mkApp(topLevel, envBuilder, args);
|
||||
|
@ -123,13 +123,15 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
|
|||
state.forceValue(topLevel);
|
||||
PathSet context;
|
||||
Attr & aDrvPath(*topLevel.attrs->find(state.sDrvPath));
|
||||
Path topLevelDrv = state.coerceToPath(aDrvPath.pos ? *(aDrvPath.pos) : noPos, *(aDrvPath.value), context);
|
||||
auto topLevelDrv = state.store->parseStorePath(state.coerceToPath(aDrvPath.pos ? *(aDrvPath.pos) : noPos, *(aDrvPath.value), context));
|
||||
Attr & aOutPath(*topLevel.attrs->find(state.sOutPath));
|
||||
Path topLevelOut = state.coerceToPath(aOutPath.pos ? *(aOutPath.pos) : noPos, *(aOutPath.value), context);
|
||||
|
||||
/* Realise the resulting store expression. */
|
||||
debug("building user environment");
|
||||
state.store->buildPaths({topLevelDrv}, state.repair ? bmRepair : bmNormal);
|
||||
std::vector<StorePathWithOutputs> topLevelDrvs;
|
||||
topLevelDrvs.push_back(StorePathWithOutputs{topLevelDrv.clone()});
|
||||
state.store->buildPaths(topLevelDrvs, state.repair ? bmRepair : bmNormal);
|
||||
|
||||
/* Switch the current user environment to the output path. */
|
||||
auto store2 = state.store.dynamic_pointer_cast<LocalFSStore>();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue