about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-02-22T09·07+0700
committerclbot <clbot@tvl.fyi>2024-02-22T17·57+0000
commit91d5745c3dbb3eb118be969a80e0608794b0e59d (patch)
tree4c81d4933029336748809a4deb70842b44c89da2
parentc461595b7f52697e2e7add79814880a285caeb34 (diff)
refactor(nix-compat/store_path): simplify build_ca_path r/7594
Move the the `fixed:out:[r:]{}:` generation to a helper function,
use matches! for more clarity.

Change-Id: I4e930c42aacbf5c7451d1f8c8c80ccb4c45389f0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11006
Tested-by: BuildkiteCI
Reviewed-by: aspen <root@gws.fyi>
Autosubmit: flokli <flokli@flokli.de>
-rw-r--r--tvix/nix-compat/src/store_path/utils.rs26
1 files changed, 12 insertions, 14 deletions
diff --git a/tvix/nix-compat/src/store_path/utils.rs b/tvix/nix-compat/src/store_path/utils.rs
index 0b75ef50647c..2289594e3a10 100644
--- a/tvix/nix-compat/src/store_path/utils.rs
+++ b/tvix/nix-compat/src/store_path/utils.rs
@@ -62,10 +62,8 @@ pub fn build_ca_path<'a, S: AsRef<str>, I: IntoIterator<Item = S>>(
     self_reference: bool,
 ) -> Result<StorePathRef<'a>, BuildStorePathError> {
     // self references are only allowed for CAHash::Nar(NixHash::Sha256(_)).
-    if self_reference {
-        let CAHash::Nar(NixHash::Sha256(_)) = ca_hash else {
-            return Err(BuildStorePathError::InvalidReference());
-        };
+    if self_reference && matches!(ca_hash, CAHash::Nar(NixHash::Sha256(_))) {
+        return Err(BuildStorePathError::InvalidReference());
     }
 
     let (ty, hash) = match &ca_hash {
@@ -86,11 +84,7 @@ pub fn build_ca_path<'a, S: AsRef<str>, I: IntoIterator<Item = S>>(
 
             (
                 "output:out".to_string(),
-                NixHash::Sha256(
-                    Sha256::new_with_prefix(format!("fixed:out:r:{}:", hash.to_nix_hex_string()))
-                        .finalize()
-                        .into(),
-                ),
+                NixHash::Sha256(fixed_out_digest("fixed:out:r", hash)),
             )
         }
         // CaHash::Flat is using something very similar, except the `r:` prefix.
@@ -101,11 +95,7 @@ pub fn build_ca_path<'a, S: AsRef<str>, I: IntoIterator<Item = S>>(
 
             (
                 "output:out".to_string(),
-                NixHash::Sha256(
-                    Sha256::new_with_prefix(format!("fixed:out:{}:", hash.to_nix_hex_string()))
-                        .finalize()
-                        .into(),
-                ),
+                NixHash::Sha256(fixed_out_digest("fixed:out", hash)),
             )
         }
     };
@@ -114,6 +104,14 @@ pub fn build_ca_path<'a, S: AsRef<str>, I: IntoIterator<Item = S>>(
         .map_err(BuildStorePathError::InvalidStorePath)
 }
 
+/// Helper function, used in [build_ca_path] for the non-sha256 [CAHash::Nar]
+/// and [CAHash::Flat].
+fn fixed_out_digest(prefix: &str, hash: &NixHash) -> [u8; 32] {
+    Sha256::new_with_prefix(format!("{}:{}:", prefix, hash.to_nix_hex_string()))
+        .finalize()
+        .into()
+}
+
 /// For given NAR sha256 digest and name, return the new [StorePathRef] this
 /// would have, or an error, in case the name is invalid.
 pub fn build_nar_based_store_path<'a>(