feat(nix-compat/derivation): simplify derivation_or_fod_hash
Populate the input_derivations struct field in-place, using self.input_derivations.from_iter, which seems a bit more readable. Change-Id: I4aa12cfd66e08486ef27aedab1bb731d94b63657 Reviewed-on: https://cl.tvl.fyi/c/depot/+/10934 Reviewed-by: flokli <flokli@flokli.de> Reviewed-by: Peter Kolloch <info@eigenvalue.net> Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI
This commit is contained in:
parent
c7875cfc12
commit
3d51b20cca
1 changed files with 22 additions and 29 deletions
|
@ -160,40 +160,35 @@ impl Derivation {
|
||||||
///
|
///
|
||||||
/// If the derivation is not a fixed derivation, it's up to the caller of
|
/// If the derivation is not a fixed derivation, it's up to the caller of
|
||||||
/// this function to provide a lookup function to lookup these calculation
|
/// this function to provide a lookup function to lookup these calculation
|
||||||
/// results of parent derivations at `fn_get_hash_derivation_modulo` (by
|
/// results of parent derivations at `fn_get_derivation_or_fod_hash` (by
|
||||||
/// drv path).
|
/// drv path).
|
||||||
pub fn derivation_or_fod_hash<F>(&self, fn_get_derivation_or_fod_hash: F) -> NixHash
|
pub fn derivation_or_fod_hash<F>(&self, fn_get_derivation_or_fod_hash: F) -> NixHash
|
||||||
where
|
where
|
||||||
F: Fn(&StorePathRef) -> NixHash,
|
F: Fn(&StorePathRef) -> NixHash,
|
||||||
{
|
{
|
||||||
// Fixed-output derivations return a fixed hash.
|
// Fixed-output derivations return a fixed hash.
|
||||||
// Non-Fixed-output derivations return a hash of the ATerm notation, but with all
|
// Non-Fixed-output derivations return the sha256 digest of the ATerm
|
||||||
// input_derivation paths replaced by a recursive call to this function.
|
// notation, but with all input_derivation paths replaced by a recursive
|
||||||
|
// call to this function.
|
||||||
// We use fn_get_derivation_or_fod_hash here, so callers can precompute this.
|
// We use fn_get_derivation_or_fod_hash here, so callers can precompute this.
|
||||||
let digest = self.fod_digest().unwrap_or({
|
NixHash::Sha256(self.fod_digest().unwrap_or({
|
||||||
// This is a new map from derivation_or_fod_hash.digest (as lowerhex)
|
// construct a new Derivation struct with input derivation strings replaced.
|
||||||
// to list of output names
|
|
||||||
let mut replaced_input_derivations: BTreeMap<String, BTreeSet<String>> =
|
|
||||||
BTreeMap::new();
|
|
||||||
|
|
||||||
// For each input_derivation, look up the
|
|
||||||
// derivation_or_fod_hash, and replace the derivation path with it's HEXLOWER
|
|
||||||
// digest.
|
|
||||||
// This is not the [NixHash::to_nix_hash_string], but without the sha256: prefix).
|
|
||||||
for (drv_path_str, output_names) in &self.input_derivations {
|
|
||||||
// parse drv_path to StorePathRef
|
|
||||||
let drv_path = StorePathRef::from_absolute_path(drv_path_str.as_bytes())
|
|
||||||
.expect("invalid input derivation path");
|
|
||||||
replaced_input_derivations.insert(
|
|
||||||
data_encoding::HEXLOWER
|
|
||||||
.encode(fn_get_derivation_or_fod_hash(&drv_path).digest_as_bytes()),
|
|
||||||
output_names.clone(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// construct a new derivation struct with these replaced input derivation strings
|
|
||||||
let replaced_derivation = Derivation {
|
let replaced_derivation = Derivation {
|
||||||
input_derivations: replaced_input_derivations,
|
// For each input_derivation, look up the
|
||||||
|
// derivation_or_fod_hash, and replace the derivation path with
|
||||||
|
// it's HEXLOWER digest.
|
||||||
|
input_derivations: BTreeMap::from_iter(self.input_derivations.iter().map(
|
||||||
|
|(drv_path_str, output_names)| {
|
||||||
|
// parse drv_path to StorePathRef
|
||||||
|
let drv_path = StorePathRef::from_absolute_path(drv_path_str.as_bytes())
|
||||||
|
.expect("invalid input derivation path");
|
||||||
|
|
||||||
|
let encoded_hash = data_encoding::HEXLOWER
|
||||||
|
.encode(fn_get_derivation_or_fod_hash(&drv_path).digest_as_bytes());
|
||||||
|
|
||||||
|
(encoded_hash, output_names.to_owned())
|
||||||
|
},
|
||||||
|
)),
|
||||||
..self.clone()
|
..self.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -202,9 +197,7 @@ impl Derivation {
|
||||||
hasher.update(replaced_derivation.to_aterm_bytes());
|
hasher.update(replaced_derivation.to_aterm_bytes());
|
||||||
|
|
||||||
hasher.finalize().into()
|
hasher.finalize().into()
|
||||||
});
|
}))
|
||||||
|
|
||||||
NixHash::Sha256(digest)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This calculates all output paths of a Derivation and updates the struct.
|
/// This calculates all output paths of a Derivation and updates the struct.
|
||||||
|
|
Loading…
Reference in a new issue