From 905a79308e8b9b66736e6cc62edc62d60f026cb9 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 14 Mar 2024 14:32:52 +0200 Subject: refactor(nix-compat/derivation): emphasize aterm_bytes derivation_or_fod_hash constructs ATerm bytes and feeds them to sha256. input_derivations being slightly modified is an implementation detail, so move the BTreeMap construction inline, and have aterm_bytes in a let binding (and feed it to the hash function directly while constructing it). This makes it a bit more understandable what's going on. Change-Id: I2f5cfbd1c964fd39ac731ca39e76cfc168f4c7d7 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11147 Tested-by: BuildkiteCI Autosubmit: flokli Reviewed-by: John Ericson --- tvix/nix-compat/src/derivation/mod.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'tvix/nix-compat') diff --git a/tvix/nix-compat/src/derivation/mod.rs b/tvix/nix-compat/src/derivation/mod.rs index 9a7dd04c11..07da127ed0 100644 --- a/tvix/nix-compat/src/derivation/mod.rs +++ b/tvix/nix-compat/src/derivation/mod.rs @@ -97,7 +97,9 @@ impl Derivation { self.to_aterm_bytes_with_replacements(&self.input_derivations) } - /// Like `to_aterm_bytes` but allow input_derivation replacements for hashing. + /// Like `to_aterm_bytes`, but accept a different BTreeMap for input_derivations. + /// This is used to render the ATerm representation of a Derivation "modulo + /// fixed-output derivations". fn to_aterm_bytes_with_replacements( &self, input_derivations: &BTreeMap>, @@ -203,19 +205,18 @@ impl Derivation { // For each input_derivation, look up the // derivation_or_fod_hash, and replace the derivation path with // it's HEXLOWER digest. - let input_derivations = BTreeMap::from_iter(self.input_derivations.iter().map( - |(drv_path, output_names)| { - let hash = fn_get_derivation_or_fod_hash(&drv_path.into()); - - (hash, output_names.to_owned()) - }, + let aterm_bytes = self.to_aterm_bytes_with_replacements(&BTreeMap::from_iter( + self.input_derivations + .iter() + .map(|(drv_path, output_names)| { + let hash = fn_get_derivation_or_fod_hash(&drv_path.into()); + + (hash, output_names.to_owned()) + }), )); - // write the ATerm of that to the hash function - let mut hasher = Sha256::new(); - hasher.update(self.to_aterm_bytes_with_replacements(&input_derivations)); - - hasher.finalize().into() + // write the ATerm of that to the hash function and return its digest. + Sha256::new_with_prefix(aterm_bytes).finalize().into() }) } -- cgit 1.4.1