about summary refs log tree commit diff
path: root/tvix/nix-compat/src/derivation
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-14T16·48+0100
committerclbot <clbot@tvl.fyi>2023-10-14T17·55+0000
commit4ae0f428bd207fdf3730d0f6ff73c7410ae9cd7b (patch)
tree341376899389f822e4bd40b3843aba8954fc77a3 /tvix/nix-compat/src/derivation
parent786b0324a9df1a63606bae72011978fc415f6f07 (diff)
refactor(tvix/nix-compat): make NixHash an enum with fixed-len bytes r/6807
Less Vec<u8> passed around.

Change-Id: Ie153a6bfaa084d7490ffa38634efdf5f3c31a768
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9722
Reviewed-by: Connor Brewster <cbrewster@hey.com>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/derivation')
-rw-r--r--tvix/nix-compat/src/derivation/mod.rs10
-rw-r--r--tvix/nix-compat/src/derivation/output.rs4
-rw-r--r--tvix/nix-compat/src/derivation/tests/mod.rs7
-rw-r--r--tvix/nix-compat/src/derivation/write.rs24
4 files changed, 19 insertions, 26 deletions
diff --git a/tvix/nix-compat/src/derivation/mod.rs b/tvix/nix-compat/src/derivation/mod.rs
index 0e98e24f43dd..3c08d61831ba 100644
--- a/tvix/nix-compat/src/derivation/mod.rs
+++ b/tvix/nix-compat/src/derivation/mod.rs
@@ -168,7 +168,8 @@ impl Derivation {
             // This is not the [NixHash::to_nix_hash_string], but without the sha256: prefix).
             for (drv_path, output_names) in &self.input_derivations {
                 replaced_input_derivations.insert(
-                    data_encoding::HEXLOWER.encode(&fn_get_derivation_or_fod_hash(drv_path).digest),
+                    data_encoding::HEXLOWER
+                        .encode(fn_get_derivation_or_fod_hash(drv_path).digest_as_bytes()),
                     output_names.clone(),
                 );
             }
@@ -186,12 +187,7 @@ impl Derivation {
             hasher.finalize().to_vec()
         });
 
-        // We populate the struct directly, as we know the sha256 digest has the
-        // right size.
-        NixHash {
-            algo: crate::nixhash::HashAlgo::Sha256,
-            digest: digest.to_vec(),
-        }
+        NixHash::Sha256(digest.try_into().unwrap())
     }
 
     /// This calculates all output paths of a Derivation and updates the struct.
diff --git a/tvix/nix-compat/src/derivation/output.rs b/tvix/nix-compat/src/derivation/output.rs
index 982d8222e90e..78a83b03be45 100644
--- a/tvix/nix-compat/src/derivation/output.rs
+++ b/tvix/nix-compat/src/derivation/output.rs
@@ -44,8 +44,8 @@ impl Output {
         if let Some(hash) = &self.hash_with_mode {
             match hash {
                 NixHashWithMode::Flat(h) | NixHashWithMode::Recursive(h) => {
-                    if h.algo != HashAlgo::Sha1 || h.algo != HashAlgo::Sha256 {
-                        return Err(OutputError::InvalidHashAlgo(h.algo.to_string()));
+                    if h.algo() != HashAlgo::Sha1 || h.algo() != HashAlgo::Sha256 {
+                        return Err(OutputError::InvalidHashAlgo(h.algo().to_string()));
                     }
                 }
             }
diff --git a/tvix/nix-compat/src/derivation/tests/mod.rs b/tvix/nix-compat/src/derivation/tests/mod.rs
index de4ebb6cb20e..833a442c8f47 100644
--- a/tvix/nix-compat/src/derivation/tests/mod.rs
+++ b/tvix/nix-compat/src/derivation/tests/mod.rs
@@ -238,17 +238,16 @@ fn output_path_construction() {
         Output {
             path: "".to_string(), // will be calculated
             hash_with_mode: Some(crate::nixhash::NixHashWithMode::Recursive(
-                (
+                crate::nixhash::from_algo_and_digest(
                     crate::nixhash::HashAlgo::Sha256,
-                    data_encoding::HEXLOWER
+                    &data_encoding::HEXLOWER
                         .decode(
                             "08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba"
                                 .as_bytes(),
                         )
                         .unwrap(),
                 )
-                    .try_into()
-                    .unwrap(),
+                .unwrap(),
             )),
         },
     );
diff --git a/tvix/nix-compat/src/derivation/write.rs b/tvix/nix-compat/src/derivation/write.rs
index 9a6234424816..5e1aefa16f06 100644
--- a/tvix/nix-compat/src/derivation/write.rs
+++ b/tvix/nix-compat/src/derivation/write.rs
@@ -79,22 +79,20 @@ pub fn write_outputs(
 
         let mut elements: Vec<&str> = vec![output_name, &output.path];
 
-        let (e2, e3) = match &output.hash_with_mode {
-            Some(hash) => match hash {
-                crate::nixhash::NixHashWithMode::Flat(h) => (
-                    h.algo.to_string(),
-                    data_encoding::HEXLOWER.encode(&h.digest),
-                ),
-                crate::nixhash::NixHashWithMode::Recursive(h) => (
-                    format!("r:{}", h.algo),
-                    data_encoding::HEXLOWER.encode(&h.digest),
-                ),
-            },
+        let (mode_and_algo, digest) = match &output.hash_with_mode {
+            Some(crate::nixhash::NixHashWithMode::Flat(h)) => (
+                h.algo().to_string(),
+                data_encoding::HEXLOWER.encode(h.digest_as_bytes()),
+            ),
+            Some(crate::nixhash::NixHashWithMode::Recursive(h)) => (
+                format!("r:{}", h.algo()),
+                data_encoding::HEXLOWER.encode(h.digest_as_bytes()),
+            ),
             None => ("".to_string(), "".to_string()),
         };
 
-        elements.push(&e2);
-        elements.push(&e3);
+        elements.push(&mode_and_algo);
+        elements.push(&digest);
 
         write_array_elements(writer, &elements)?;