lix/src/nix/show-derivation.cc

113 lines
3.9 KiB
C++
Raw Normal View History

// FIXME: integrate this with nix path-info?
2020-07-24 18:44:31 +00:00
// FIXME: rename to 'nix store show-derivation' or 'nix debug show-derivation'?
#include "command.hh"
#include "common-args.hh"
#include "store-api.hh"
#include "archive.hh"
#include "derivations.hh"
#include <nlohmann/json.hpp>
using namespace nix;
using json = nlohmann::json;
struct CmdShowDerivation : InstallablesCommand
{
bool recursive = false;
CmdShowDerivation()
{
2020-05-04 20:40:19 +00:00
addFlag({
.longName = "recursive",
.shortName = 'r',
.description = "Include the dependencies of the specified derivations.",
2020-05-04 20:40:19 +00:00
.handler = {&recursive, true}
});
}
std::string description() override
{
return "show the contents of a store derivation";
}
2020-12-10 18:14:18 +00:00
std::string doc() override
{
2020-12-10 18:14:18 +00:00
return
#include "show-derivation.md"
;
}
2020-05-05 13:18:23 +00:00
Category category() override { return catUtility; }
void run(ref<Store> store) override
{
2022-03-02 12:54:08 +00:00
auto drvPaths = Installable::toDerivations(store, installables, true);
if (recursive) {
StorePathSet closure;
store->computeFSClosure(drvPaths, closure);
drvPaths = std::move(closure);
}
json jsonRoot = json::object();
for (auto & drvPath : drvPaths) {
if (!drvPath.isDerivation()) continue;
json& drvObj = jsonRoot[store->printStorePath(drvPath)];
auto drv = store->readDerivation(drvPath);
{
json& outputsObj = drvObj["outputs"];
outputsObj = json::object();
2020-08-14 18:51:31 +00:00
for (auto & [_outputName, output] : drv.outputs) {
auto & outputName = _outputName; // work around clang bug
auto& outputObj = outputsObj[outputName];
outputObj = json::object();
std::visit(overloaded {
[&](const DerivationOutput::InputAddressed & doi) {
outputObj["path"] = store->printStorePath(doi.path);
},
[&](const DerivationOutput::CAFixed & dof) {
outputObj["path"] = store->printStorePath(dof.path(*store, drv.name, outputName));
outputObj["hashAlgo"] = dof.hash.printMethodAlgo();
outputObj["hash"] = dof.hash.hash.to_string(Base16, false);
},
[&](const DerivationOutput::CAFloating & dof) {
outputObj["hashAlgo"] = makeFileIngestionPrefix(dof.method) + printHashType(dof.hashType);
},
[&](const DerivationOutput::Deferred &) {},
[&](const DerivationOutput::Impure & doi) {
outputObj["hashAlgo"] = makeFileIngestionPrefix(doi.method) + printHashType(doi.hashType);
outputObj["impure"] = true;
},
}, output.raw());
}
}
{
auto& inputsList = drvObj["inputSrcs"];
inputsList = json::array();
for (auto & input : drv.inputSrcs)
inputsList.emplace_back(store->printStorePath(input));
}
{
auto& inputDrvsObj = drvObj["inputDrvs"];
inputDrvsObj = json::object();
for (auto & input : drv.inputDrvs)
inputDrvsObj[store->printStorePath(input.first)] = input.second;
}
drvObj["system"] = drv.platform;
drvObj["builder"] = drv.builder;
drvObj["args"] = drv.args;
drvObj["env"] = drv.env;
}
std::cout << jsonRoot.dump(2) << std::endl;
}
};
static auto rCmdShowDerivation = registerCommand<CmdShowDerivation>("show-derivation");