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 <flokli@flokli.de>
Reviewed-by: John Ericson <git@johnericson.me>
This commit is contained in:
Florian Klink 2024-03-14 14:32:52 +02:00 committed by clbot
parent 142c72e070
commit 905a79308e

View file

@ -97,7 +97,9 @@ impl Derivation {
self.to_aterm_bytes_with_replacements(&self.input_derivations) 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( fn to_aterm_bytes_with_replacements(
&self, &self,
input_derivations: &BTreeMap<impl AtermWriteable, BTreeSet<String>>, input_derivations: &BTreeMap<impl AtermWriteable, BTreeSet<String>>,
@ -203,19 +205,18 @@ impl Derivation {
// For each input_derivation, look up the // For each input_derivation, look up the
// derivation_or_fod_hash, and replace the derivation path with // derivation_or_fod_hash, and replace the derivation path with
// it's HEXLOWER digest. // it's HEXLOWER digest.
let input_derivations = BTreeMap::from_iter(self.input_derivations.iter().map( let aterm_bytes = self.to_aterm_bytes_with_replacements(&BTreeMap::from_iter(
|(drv_path, output_names)| { self.input_derivations
let hash = fn_get_derivation_or_fod_hash(&drv_path.into()); .iter()
.map(|(drv_path, output_names)| {
let hash = fn_get_derivation_or_fod_hash(&drv_path.into());
(hash, output_names.to_owned()) (hash, output_names.to_owned())
}, }),
)); ));
// write the ATerm of that to the hash function // write the ATerm of that to the hash function and return its digest.
let mut hasher = Sha256::new(); Sha256::new_with_prefix(aterm_bytes).finalize().into()
hasher.update(self.to_aterm_bytes_with_replacements(&input_derivations));
hasher.finalize().into()
}) })
} }