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 10, 2020
1 parent 21830cb commit 2bf3d71
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/nix/build.cc
Expand Up @@ -7,7 +7,7 @@

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 +86,8 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixProfile
}, buildables[i]);

updateProfile(buildables);

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

Expand Down
25 changes: 25 additions & 0 deletions src/nix/installables.cc
Expand Up @@ -18,6 +18,31 @@

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
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 2bf3d71

Please sign in to comment.