about summary refs log tree commit diff
path: root/tvix/nix-compat
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-02-17T04·18+0700
committerflokli <flokli@flokli.de>2024-02-17T04·46+0000
commit3d51b20cca61c957d96a1133ce4b8931d52ce717 (patch)
treee2056c47cfceabdfcef047c16786dd6333b22aea /tvix/nix-compat
parentc7875cfc1226cce8c2676f93dae33f37716d56b5 (diff)
feat(nix-compat/derivation): simplify derivation_or_fod_hash r/7531
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
Diffstat (limited to 'tvix/nix-compat')
-rw-r--r--tvix/nix-compat/src/derivation/mod.rs51
1 files 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<F>(&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<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
+        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.