From 3d51b20cca61c957d96a1133ce4b8931d52ce717 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sat, 17 Feb 2024 11:18:03 +0700 Subject: 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 Reviewed-by: Peter Kolloch Autosubmit: flokli Tested-by: BuildkiteCI --- tvix/nix-compat/src/derivation/mod.rs | 51 +++++++++++++++-------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/tvix/nix-compat/src/derivation/mod.rs b/tvix/nix-compat/src/derivation/mod.rs index 2f90c54d8f76..9b1db0f68e8b 100644 --- a/tvix/nix-compat/src/derivation/mod.rs +++ b/tvix/nix-compat/src/derivation/mod.rs @@ -160,40 +160,35 @@ impl Derivation { /// /// 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 - /// 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). pub fn derivation_or_fod_hash(&self, fn_get_derivation_or_fod_hash: F) -> NixHash where F: Fn(&StorePathRef) -> NixHash, { // Fixed-output derivations return a fixed hash. - // Non-Fixed-output derivations return a hash of the ATerm notation, but with all - // input_derivation paths replaced by a recursive call to this function. + // Non-Fixed-output derivations return the sha256 digest of the ATerm + // 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. - let digest = self.fod_digest().unwrap_or({ - // This is a new map from derivation_or_fod_hash.digest (as lowerhex) - // to list of output names - let mut replaced_input_derivations: BTreeMap> = - 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 + NixHash::Sha256(self.fod_digest().unwrap_or({ + // construct a new Derivation struct with input derivation strings replaced. 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() }; @@ -202,9 +197,7 @@ impl Derivation { hasher.update(replaced_derivation.to_aterm_bytes()); hasher.finalize().into() - }); - - NixHash::Sha256(digest) + })) } /// This calculates all output paths of a Derivation and updates the struct. -- cgit 1.4.1