about summary refs log tree commit diff
path: root/tvix/nix-compat/src/nixhash/mod.rs
diff options
context:
space:
mode:
authorPeter Kolloch <info@eigenvalue.net>2024-02-21T11·16+0700
committerclbot <clbot@tvl.fyi>2024-02-21T11·34+0000
commita44a8985cc0ae126f86d26493d97d80a09b211f8 (patch)
tree0d8f645b9d26fab2214e0ba4caf9de365233af58 /tvix/nix-compat/src/nixhash/mod.rs
parentf50800a9dffb18572ca28b8afd592b8a7c203607 (diff)
feat(tvix/nix-compat): generalize aterm writing for derivation r/7582
...so that we can also use `StorePath`s in
derivation.input_derivations.

Towards https://b.tvl.fyi/issues/264

Change-Id: I71d296ca273979c70f277a7f4f88a5f76de3d8be
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10973
Reviewed-by: Peter Kolloch <info@eigenvalue.net>
Autosubmit: Peter Kolloch <info@eigenvalue.net>
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/nix-compat/src/nixhash/mod.rs')
-rw-r--r--tvix/nix-compat/src/nixhash/mod.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/tvix/nix-compat/src/nixhash/mod.rs b/tvix/nix-compat/src/nixhash/mod.rs
index 727bf77dc57d..a6f2b967996a 100644
--- a/tvix/nix-compat/src/nixhash/mod.rs
+++ b/tvix/nix-compat/src/nixhash/mod.rs
@@ -1,5 +1,7 @@
 use crate::nixbase32;
 use data_encoding::{BASE64, BASE64_NOPAD, HEXLOWER};
+use std::cmp::Ordering;
+use std::fmt::Display;
 use thiserror;
 
 mod algos;
@@ -17,6 +19,34 @@ pub enum NixHash {
     Sha512(Box<[u8; 64]>),
 }
 
+/// Same order as sorting the corresponding nixbase32 strings.
+///
+/// This order is used in the ATerm serialization of a derivation
+/// and thus affects the calculated output hash.
+impl Ord for NixHash {
+    fn cmp(&self, other: &NixHash) -> Ordering {
+        self.digest_as_bytes().cmp(other.digest_as_bytes())
+    }
+}
+
+// See Ord for reason to implement this manually.
+impl PartialOrd for NixHash {
+    fn partial_cmp(&self, other: &NixHash) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+impl Display for NixHash {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
+        write!(
+            f,
+            "{}-{}",
+            self.algo(),
+            nixbase32::encode(self.digest_as_bytes())
+        )
+    }
+}
+
 /// convenience Result type for all nixhash parsing Results.
 pub type Result<V> = std::result::Result<V, Error>;