Print built derivations as json for build

Add --json option to nix build to allow machine readable output on
stdout with all built derivations

Fixes #1930
This commit is contained in:
Matthew Kenigsberg 2020-10-22 23:59:01 -05:00
parent 21830cb044
commit 8abb80a478
3 changed files with 37 additions and 1 deletions

View file

@ -5,9 +5,11 @@
#include "store-api.hh"
#include "local-fs-store.hh"
#include <nlohmann/json.hpp>
using namespace nix;
struct CmdBuild : InstallablesCommand, MixDryRun, MixProfile
struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON, MixProfile
{
Path outLink = "result";
BuildMode buildMode = bmNormal;
@ -86,6 +88,8 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixProfile
}, buildables[i]);
updateProfile(buildables);
if (json) logger->cout("%s", buildablesToJSON(buildables, store).dump());
}
};

View file

@ -16,8 +16,35 @@
#include <regex>
#include <queue>
#include <nlohmann/json.hpp>
namespace nix {
nlohmann::json BuildableOpaque::toJSON(ref<Store> store) const {
nlohmann::json res;
res["path"] = store->printStorePath(path);
return res;
}
nlohmann::json BuildableFromDrv::toJSON(ref<Store> store) const {
nlohmann::json res;
res["drvPath"] = store->printStorePath(drvPath);
for (const auto& [output, path] : outputs) {
res["outputs"][output] = path ? store->printStorePath(*path) : "";
}
return res;
}
nlohmann::json buildablesToJSON(const Buildables & buildables, ref<Store> store) {
auto res = nlohmann::json::array();
for (const Buildable & buildable : buildables) {
std::visit([&res, store](const auto & buildable) {
res.push_back(buildable.toJSON(store));
}, buildable);
}
return res;
}
void completeFlakeInputPath(
ref<EvalState> evalState,
const FlakeRef & flakeRef,

View file

@ -7,6 +7,8 @@
#include <optional>
#include <nlohmann/json_fwd.hpp>
namespace nix {
struct DrvInfo;
@ -16,11 +18,13 @@ namespace eval_cache { class EvalCache; class AttrCursor; }
struct BuildableOpaque {
StorePath path;
nlohmann::json toJSON(ref<Store> store) const;
};
struct BuildableFromDrv {
StorePath drvPath;
std::map<std::string, std::optional<StorePath>> outputs;
nlohmann::json toJSON(ref<Store> store) const;
};
typedef std::variant<
@ -29,6 +33,7 @@ typedef std::variant<
> Buildable;
typedef std::vector<Buildable> Buildables;
nlohmann::json buildablesToJSON(const Buildables & buildables, ref<Store> store);
struct App
{