2020-10-08 18:36:51 +03:00
|
|
|
|
#include "realisation.hh"
|
|
|
|
|
#include "store-api.hh"
|
2021-05-19 11:26:58 +03:00
|
|
|
|
#include "closure.hh"
|
2024-01-03 22:02:20 +02:00
|
|
|
|
#include "signature/local-keys.hh"
|
2020-12-08 22:07:52 +02:00
|
|
|
|
#include <nlohmann/json.hpp>
|
2020-10-08 18:36:51 +03:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
MakeError(InvalidDerivationOutputId, Error);
|
|
|
|
|
|
|
|
|
|
DrvOutput DrvOutput::parse(const std::string &strRep) {
|
2020-12-09 17:56:56 +02:00
|
|
|
|
size_t n = strRep.find("!");
|
|
|
|
|
if (n == strRep.npos)
|
2020-10-08 18:36:51 +03:00
|
|
|
|
throw InvalidDerivationOutputId("Invalid derivation output id %s", strRep);
|
|
|
|
|
|
|
|
|
|
return DrvOutput{
|
2020-12-09 17:56:56 +02:00
|
|
|
|
.drvHash = Hash::parseAnyPrefixed(strRep.substr(0, n)),
|
|
|
|
|
.outputName = strRep.substr(n+1),
|
2020-10-08 18:36:51 +03:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string DrvOutput::to_string() const {
|
2020-12-09 17:56:56 +02:00
|
|
|
|
return strHash() + "!" + outputName;
|
2020-10-08 18:36:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 17:37:45 +03:00
|
|
|
|
std::set<Realisation> Realisation::closure(Store & store, const std::set<Realisation> & startOutputs)
|
2021-05-19 11:26:58 +03:00
|
|
|
|
{
|
|
|
|
|
std::set<Realisation> res;
|
|
|
|
|
Realisation::closure(store, startOutputs, res);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 17:37:45 +03:00
|
|
|
|
void Realisation::closure(Store & store, const std::set<Realisation> & startOutputs, std::set<Realisation> & res)
|
2021-05-19 11:26:58 +03:00
|
|
|
|
{
|
|
|
|
|
auto getDeps = [&](const Realisation& current) -> std::set<Realisation> {
|
|
|
|
|
std::set<Realisation> res;
|
2021-05-26 17:09:02 +03:00
|
|
|
|
for (auto& [currentDep, _] : current.dependentRealisations) {
|
2021-05-19 11:26:58 +03:00
|
|
|
|
if (auto currentRealisation = store.queryRealisation(currentDep))
|
|
|
|
|
res.insert(*currentRealisation);
|
|
|
|
|
else
|
|
|
|
|
throw Error(
|
|
|
|
|
"Unrealised derivation '%s'", currentDep.to_string());
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
computeClosure<Realisation>(
|
|
|
|
|
startOutputs, res,
|
|
|
|
|
[&](const Realisation& current,
|
|
|
|
|
std::function<void(std::promise<std::set<Realisation>>&)>
|
|
|
|
|
processEdges) {
|
|
|
|
|
std::promise<std::set<Realisation>> promise;
|
|
|
|
|
try {
|
|
|
|
|
auto res = getDeps(current);
|
|
|
|
|
promise.set_value(res);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
promise.set_exception(std::current_exception());
|
|
|
|
|
}
|
|
|
|
|
return processEdges(promise);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 22:07:52 +02:00
|
|
|
|
nlohmann::json Realisation::toJSON() const {
|
2021-05-26 17:09:02 +03:00
|
|
|
|
auto jsonDependentRealisations = nlohmann::json::object();
|
|
|
|
|
for (auto & [depId, depOutPath] : dependentRealisations)
|
|
|
|
|
jsonDependentRealisations.emplace(depId.to_string(), depOutPath.to_string());
|
2020-12-08 22:07:52 +02:00
|
|
|
|
return nlohmann::json{
|
|
|
|
|
{"id", id.to_string()},
|
|
|
|
|
{"outPath", outPath.to_string()},
|
2021-03-08 12:56:33 +02:00
|
|
|
|
{"signatures", signatures},
|
2021-05-26 17:09:02 +03:00
|
|
|
|
{"dependentRealisations", jsonDependentRealisations},
|
2020-12-08 22:07:52 +02:00
|
|
|
|
};
|
2020-10-08 18:36:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 22:07:52 +02:00
|
|
|
|
Realisation Realisation::fromJSON(
|
|
|
|
|
const nlohmann::json& json,
|
|
|
|
|
const std::string& whence) {
|
2021-03-08 12:56:33 +02:00
|
|
|
|
auto getOptionalField = [&](std::string fieldName) -> std::optional<std::string> {
|
2020-12-08 22:07:52 +02:00
|
|
|
|
auto fieldIterator = json.find(fieldName);
|
|
|
|
|
if (fieldIterator == json.end())
|
2021-03-08 12:56:33 +02:00
|
|
|
|
return std::nullopt;
|
2022-01-19 15:37:54 +02:00
|
|
|
|
return {*fieldIterator};
|
2021-03-08 12:56:33 +02:00
|
|
|
|
};
|
|
|
|
|
auto getField = [&](std::string fieldName) -> std::string {
|
|
|
|
|
if (auto field = getOptionalField(fieldName))
|
|
|
|
|
return *field;
|
|
|
|
|
else
|
2020-12-08 22:07:52 +02:00
|
|
|
|
throw Error(
|
|
|
|
|
"Drv output info file '%1%' is corrupt, missing field %2%",
|
|
|
|
|
whence, fieldName);
|
2020-10-08 18:36:51 +03:00
|
|
|
|
};
|
|
|
|
|
|
2021-03-08 12:56:33 +02:00
|
|
|
|
StringSet signatures;
|
|
|
|
|
if (auto signaturesIterator = json.find("signatures"); signaturesIterator != json.end())
|
|
|
|
|
signatures.insert(signaturesIterator->begin(), signaturesIterator->end());
|
|
|
|
|
|
2021-05-26 17:09:02 +03:00
|
|
|
|
std::map <DrvOutput, StorePath> dependentRealisations;
|
|
|
|
|
if (auto jsonDependencies = json.find("dependentRealisations"); jsonDependencies != json.end())
|
|
|
|
|
for (auto & [jsonDepId, jsonDepOutPath] : jsonDependencies->get<std::map<std::string, std::string>>())
|
|
|
|
|
dependentRealisations.insert({DrvOutput::parse(jsonDepId), StorePath(jsonDepOutPath)});
|
2020-11-10 15:49:25 +02:00
|
|
|
|
|
2020-12-08 22:07:52 +02:00
|
|
|
|
return Realisation{
|
|
|
|
|
.id = DrvOutput::parse(getField("id")),
|
|
|
|
|
.outPath = StorePath(getField("outPath")),
|
2021-03-08 12:56:33 +02:00
|
|
|
|
.signatures = signatures,
|
2021-05-26 17:09:02 +03:00
|
|
|
|
.dependentRealisations = dependentRealisations,
|
2020-10-08 18:36:51 +03:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 12:56:33 +02:00
|
|
|
|
std::string Realisation::fingerprint() const
|
|
|
|
|
{
|
|
|
|
|
auto serialized = toJSON();
|
|
|
|
|
serialized.erase("signatures");
|
|
|
|
|
return serialized.dump();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 22:02:20 +02:00
|
|
|
|
void Realisation::sign(const Signer &signer)
|
2021-03-08 12:56:33 +02:00
|
|
|
|
{
|
2024-01-03 22:02:20 +02:00
|
|
|
|
signatures.insert(signer.signDetached(fingerprint()));
|
2021-03-08 12:56:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Realisation::checkSignature(const PublicKeys & publicKeys, const std::string & sig) const
|
|
|
|
|
{
|
|
|
|
|
return verifyDetached(fingerprint(), sig, publicKeys);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t Realisation::checkSignatures(const PublicKeys & publicKeys) const
|
|
|
|
|
{
|
|
|
|
|
// FIXME: Maybe we should return `maxSigs` if the realisation corresponds to
|
|
|
|
|
// an input-addressed one − because in that case the drv is enough to check
|
|
|
|
|
// it − but we can't know that here.
|
|
|
|
|
|
|
|
|
|
size_t good = 0;
|
|
|
|
|
for (auto & sig : signatures)
|
|
|
|
|
if (checkSignature(publicKeys, sig))
|
|
|
|
|
good++;
|
|
|
|
|
return good;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 13:51:12 +02:00
|
|
|
|
|
|
|
|
|
SingleDrvOutputs filterDrvOutputs(const OutputsSpec& wanted, SingleDrvOutputs&& outputs)
|
|
|
|
|
{
|
|
|
|
|
SingleDrvOutputs ret = std::move(outputs);
|
|
|
|
|
for (auto it = ret.begin(); it != ret.end(); ) {
|
|
|
|
|
if (!wanted.contains(it->first))
|
|
|
|
|
it = ret.erase(it);
|
|
|
|
|
else
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 18:24:30 +02:00
|
|
|
|
StorePath RealisedPath::path() const {
|
2021-02-04 16:15:22 +02:00
|
|
|
|
return std::visit([](auto && arg) { return arg.getPath(); }, raw);
|
2020-12-14 18:24:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 17:19:46 +03:00
|
|
|
|
bool Realisation::isCompatibleWith(const Realisation & other) const
|
|
|
|
|
{
|
|
|
|
|
assert (id == other.id);
|
2021-06-22 11:50:28 +03:00
|
|
|
|
if (outPath == other.outPath) {
|
2021-07-16 12:40:56 +03:00
|
|
|
|
if (dependentRealisations.empty() != other.dependentRealisations.empty()) {
|
|
|
|
|
warn(
|
|
|
|
|
"Encountered a realisation for '%s' with an empty set of "
|
|
|
|
|
"dependencies. This is likely an artifact from an older Nix. "
|
|
|
|
|
"I’ll try to fix the realisation if I can",
|
|
|
|
|
id.to_string());
|
|
|
|
|
return true;
|
|
|
|
|
} else if (dependentRealisations == other.dependentRealisations) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-06-22 11:50:28 +03:00
|
|
|
|
}
|
|
|
|
|
return false;
|
2021-05-19 17:19:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 18:24:30 +02:00
|
|
|
|
void RealisedPath::closure(
|
|
|
|
|
Store& store,
|
|
|
|
|
const RealisedPath::Set& startPaths,
|
|
|
|
|
RealisedPath::Set& ret)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: This only builds the store-path closure, not the real realisation
|
|
|
|
|
// closure
|
|
|
|
|
StorePathSet initialStorePaths, pathsClosure;
|
|
|
|
|
for (auto& path : startPaths)
|
|
|
|
|
initialStorePaths.insert(path.path());
|
|
|
|
|
store.computeFSClosure(initialStorePaths, pathsClosure);
|
|
|
|
|
ret.insert(startPaths.begin(), startPaths.end());
|
|
|
|
|
ret.insert(pathsClosure.begin(), pathsClosure.end());
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 15:47:56 +02:00
|
|
|
|
void RealisedPath::closure(Store& store, RealisedPath::Set & ret) const
|
2020-12-14 18:24:30 +02:00
|
|
|
|
{
|
|
|
|
|
RealisedPath::closure(store, {*this}, ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RealisedPath::Set RealisedPath::closure(Store& store) const
|
|
|
|
|
{
|
|
|
|
|
RealisedPath::Set ret;
|
|
|
|
|
closure(store, ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 18:36:51 +03:00
|
|
|
|
} // namespace nix
|