Skip to content

Commit

Permalink
Print built derivations as json for build
Browse files Browse the repository at this point in the history
Add --json option to nix build to allow machine readable output on
stdout with all built derivations

Fixes #1930
  • Loading branch information
mkenigs committed Nov 11, 2020
1 parent 21830cb commit 8abb80a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/nix/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,6 +88,8 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixProfile
}, buildables[i]);

updateProfile(buildables);

if (json) logger->cout("%s", buildablesToJSON(buildables, store).dump());
}
};

Expand Down
27 changes: 27 additions & 0 deletions src/nix/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/nix/installables.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <optional>

#include <nlohmann/json_fwd.hpp>

namespace nix {

struct DrvInfo;
Expand All @@ -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<
Expand All @@ -29,6 +33,7 @@ typedef std::variant<
> Buildable;

typedef std::vector<Buildable> Buildables;
nlohmann::json buildablesToJSON(const Buildables & buildables, ref<Store> store);

struct App
{
Expand Down

0 comments on commit 8abb80a

Please sign in to comment.