2021-04-05 17:33:28 +03:00
|
|
|
|
#include "derived-path.hh"
|
2022-03-18 00:29:15 +02:00
|
|
|
|
#include "derivations.hh"
|
2021-03-01 07:48:01 +02:00
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
2022-05-04 08:44:32 +03:00
|
|
|
|
#include <optional>
|
|
|
|
|
|
2021-03-01 07:48:01 +02:00
|
|
|
|
namespace nix {
|
|
|
|
|
|
2021-04-05 16:48:18 +03:00
|
|
|
|
nlohmann::json DerivedPath::Opaque::toJSON(ref<Store> store) const {
|
2021-03-01 07:48:01 +02:00
|
|
|
|
nlohmann::json res;
|
|
|
|
|
res["path"] = store->printStorePath(path);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 12:34:31 +02:00
|
|
|
|
nlohmann::json DerivedPath::Built::toJSON(ref<Store> store) const {
|
|
|
|
|
nlohmann::json res;
|
|
|
|
|
res["drvPath"] = store->printStorePath(drvPath);
|
|
|
|
|
// Fallback for the input-addressed derivation case: We expect to always be
|
|
|
|
|
// able to print the output paths, so let’s do it
|
2022-05-04 08:44:32 +03:00
|
|
|
|
const auto knownOutputs = store->queryPartialDerivationOutputMap(drvPath);
|
2022-03-17 12:34:31 +02:00
|
|
|
|
for (const auto& output : outputs) {
|
2022-05-04 08:44:32 +03:00
|
|
|
|
auto knownOutput = get(knownOutputs, output);
|
|
|
|
|
res["outputs"][output] = (knownOutput && *knownOutput)
|
|
|
|
|
? store->printStorePath(**knownOutput)
|
|
|
|
|
: nullptr;
|
2022-03-17 12:34:31 +02:00
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 17:19:51 +03:00
|
|
|
|
nlohmann::json BuiltPath::Built::toJSON(ref<Store> store) const {
|
2021-03-01 07:48:01 +02:00
|
|
|
|
nlohmann::json res;
|
|
|
|
|
res["drvPath"] = store->printStorePath(drvPath);
|
|
|
|
|
for (const auto& [output, path] : outputs) {
|
2021-05-17 09:45:08 +03:00
|
|
|
|
res["outputs"][output] = store->printStorePath(path);
|
2021-03-01 07:48:01 +02:00
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 09:45:08 +03:00
|
|
|
|
StorePathSet BuiltPath::outPaths() const
|
|
|
|
|
{
|
|
|
|
|
return std::visit(
|
|
|
|
|
overloaded{
|
2021-10-01 00:31:21 +03:00
|
|
|
|
[](const BuiltPath::Opaque & p) { return StorePathSet{p.path}; },
|
|
|
|
|
[](const BuiltPath::Built & b) {
|
2021-05-17 09:45:08 +03:00
|
|
|
|
StorePathSet res;
|
|
|
|
|
for (auto & [_, path] : b.outputs)
|
|
|
|
|
res.insert(path);
|
|
|
|
|
return res;
|
|
|
|
|
},
|
|
|
|
|
}, raw()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 12:34:31 +02:00
|
|
|
|
template<typename T>
|
|
|
|
|
nlohmann::json stuffToJSON(const std::vector<T> & ts, ref<Store> store) {
|
2021-03-01 07:48:01 +02:00
|
|
|
|
auto res = nlohmann::json::array();
|
2022-03-17 12:34:31 +02:00
|
|
|
|
for (const T & t : ts) {
|
|
|
|
|
std::visit([&res, store](const auto & t) {
|
|
|
|
|
res.push_back(t.toJSON(store));
|
|
|
|
|
}, t.raw());
|
2021-03-01 07:48:01 +02:00
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 12:34:31 +02:00
|
|
|
|
nlohmann::json derivedPathsWithHintsToJSON(const BuiltPaths & buildables, ref<Store> store)
|
|
|
|
|
{ return stuffToJSON<BuiltPath>(buildables, store); }
|
|
|
|
|
nlohmann::json derivedPathsToJSON(const DerivedPaths & paths, ref<Store> store)
|
|
|
|
|
{ return stuffToJSON<DerivedPath>(paths, store); }
|
|
|
|
|
|
2021-03-02 05:50:41 +02:00
|
|
|
|
|
2021-04-05 16:48:18 +03:00
|
|
|
|
std::string DerivedPath::Opaque::to_string(const Store & store) const {
|
2021-03-02 05:50:41 +02:00
|
|
|
|
return store.printStorePath(path);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 16:48:18 +03:00
|
|
|
|
std::string DerivedPath::Built::to_string(const Store & store) const {
|
2021-03-02 05:50:41 +02:00
|
|
|
|
return store.printStorePath(drvPath)
|
|
|
|
|
+ "!"
|
|
|
|
|
+ (outputs.empty() ? std::string { "*" } : concatStringsSep(",", outputs));
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 16:48:18 +03:00
|
|
|
|
std::string DerivedPath::to_string(const Store & store) const
|
2021-03-02 05:50:41 +02:00
|
|
|
|
{
|
|
|
|
|
return std::visit(
|
|
|
|
|
[&](const auto & req) { return req.to_string(store); },
|
2021-04-05 16:24:42 +03:00
|
|
|
|
this->raw());
|
2021-03-02 05:50:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-05 16:48:18 +03:00
|
|
|
|
DerivedPath::Opaque DerivedPath::Opaque::parse(const Store & store, std::string_view s)
|
2021-03-02 05:50:41 +02:00
|
|
|
|
{
|
|
|
|
|
return {store.parseStorePath(s)};
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 16:48:18 +03:00
|
|
|
|
DerivedPath::Built DerivedPath::Built::parse(const Store & store, std::string_view s)
|
2021-03-02 05:50:41 +02:00
|
|
|
|
{
|
|
|
|
|
size_t n = s.find("!");
|
|
|
|
|
assert(n != s.npos);
|
|
|
|
|
auto drvPath = store.parseStorePath(s.substr(0, n));
|
|
|
|
|
auto outputsS = s.substr(n + 1);
|
2022-02-25 17:00:00 +02:00
|
|
|
|
std::set<std::string> outputs;
|
2021-03-02 05:50:41 +02:00
|
|
|
|
if (outputsS != "*")
|
2022-02-25 17:00:00 +02:00
|
|
|
|
outputs = tokenizeString<std::set<std::string>>(outputsS, ",");
|
2021-03-02 05:50:41 +02:00
|
|
|
|
return {drvPath, outputs};
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 16:48:18 +03:00
|
|
|
|
DerivedPath DerivedPath::parse(const Store & store, std::string_view s)
|
2021-03-02 05:50:41 +02:00
|
|
|
|
{
|
|
|
|
|
size_t n = s.find("!");
|
|
|
|
|
return n == s.npos
|
2021-04-05 16:48:18 +03:00
|
|
|
|
? (DerivedPath) DerivedPath::Opaque::parse(store, s)
|
|
|
|
|
: (DerivedPath) DerivedPath::Built::parse(store, s);
|
2021-03-02 05:50:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 09:45:08 +03:00
|
|
|
|
RealisedPath::Set BuiltPath::toRealisedPaths(Store & store) const
|
|
|
|
|
{
|
|
|
|
|
RealisedPath::Set res;
|
|
|
|
|
std::visit(
|
|
|
|
|
overloaded{
|
2021-10-01 00:31:21 +03:00
|
|
|
|
[&](const BuiltPath::Opaque & p) { res.insert(p.path); },
|
|
|
|
|
[&](const BuiltPath::Built & p) {
|
2021-05-17 09:45:08 +03:00
|
|
|
|
auto drvHashes =
|
|
|
|
|
staticOutputHashes(store, store.readDerivation(p.drvPath));
|
|
|
|
|
for (auto& [outputName, outputPath] : p.outputs) {
|
|
|
|
|
if (settings.isExperimentalFeatureEnabled(
|
2021-10-25 16:53:01 +03:00
|
|
|
|
Xp::CaDerivations)) {
|
2022-05-04 08:44:32 +03:00
|
|
|
|
auto drvOutput = get(drvHashes, outputName);
|
|
|
|
|
if (!drvOutput)
|
|
|
|
|
throw Error(
|
|
|
|
|
"the derivation '%s' has unrealised output '%s' (derived-path.cc/toRealisedPaths)",
|
|
|
|
|
store.printStorePath(p.drvPath), outputName);
|
2021-05-17 09:45:08 +03:00
|
|
|
|
auto thisRealisation = store.queryRealisation(
|
2022-05-04 08:44:32 +03:00
|
|
|
|
DrvOutput{*drvOutput, outputName});
|
|
|
|
|
assert(thisRealisation); // We’ve built it, so we must
|
|
|
|
|
// have the realisation
|
2021-05-17 09:45:08 +03:00
|
|
|
|
res.insert(*thisRealisation);
|
|
|
|
|
} else {
|
|
|
|
|
res.insert(outputPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
raw());
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2021-03-01 07:48:01 +02:00
|
|
|
|
}
|