lix/src/nix/show-derivation.cc
Eelco Dolstra bbe97dff8b 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).
2019-12-10 22:06:05 +01:00

117 lines
3.3 KiB
C++

// FIXME: integrate this with nix path-info?
#include "command.hh"
#include "common-args.hh"
#include "store-api.hh"
#include "archive.hh"
#include "json.hh"
#include "derivations.hh"
using namespace nix;
struct CmdShowDerivation : InstallablesCommand
{
bool recursive = false;
CmdShowDerivation()
{
mkFlag()
.longName("recursive")
.shortName('r')
.description("include the dependencies of the specified derivations")
.set(&recursive, true);
}
std::string description() override
{
return "show the contents of a store derivation";
}
Examples examples() override
{
return {
Example{
"To show the store derivation that results from evaluating the Hello package:",
"nix show-derivation nixpkgs.hello"
},
Example{
"To show the full derivation graph (if available) that produced your NixOS system:",
"nix show-derivation -r /run/current-system"
},
};
}
void run(ref<Store> store) override
{
auto drvPaths = toDerivations(store, installables, true);
if (recursive) {
StorePathSet closure;
store->computeFSClosure(drvPaths, closure);
drvPaths = std::move(closure);
}
{
JSONObject jsonRoot(std::cout, true);
for (auto & drvPath : drvPaths) {
if (!drvPath.isDerivation()) continue;
auto drvPathS = store->printStorePath(drvPath);
auto drvObj(jsonRoot.object(drvPathS));
auto drv = readDerivation(*store, drvPathS);
{
auto outputsObj(drvObj.object("outputs"));
for (auto & output : drv.outputs) {
auto outputObj(outputsObj.object(output.first));
outputObj.attr("path", store->printStorePath(output.second.path));
if (output.second.hash != "") {
outputObj.attr("hashAlgo", output.second.hashAlgo);
outputObj.attr("hash", output.second.hash);
}
}
}
{
auto inputsList(drvObj.list("inputSrcs"));
for (auto & input : drv.inputSrcs)
inputsList.elem(store->printStorePath(input));
}
{
auto inputDrvsObj(drvObj.object("inputDrvs"));
for (auto & input : drv.inputDrvs) {
auto inputList(inputDrvsObj.list(store->printStorePath(input.first)));
for (auto & outputId : input.second)
inputList.elem(outputId);
}
}
drvObj.attr("platform", drv.platform);
drvObj.attr("builder", drv.builder);
{
auto argsList(drvObj.list("args"));
for (auto & arg : drv.args)
argsList.elem(arg);
}
{
auto envObj(drvObj.object("env"));
for (auto & var : drv.env)
envObj.attr(var.first, var.second);
}
}
}
std::cout << "\n";
}
};
static auto r1 = registerCommand<CmdShowDerivation>("show-derivation");