diff options
Diffstat (limited to 'tvix/nix-compat/src/nixhash/mod.rs')
-rw-r--r-- | tvix/nix-compat/src/nixhash/mod.rs | 30 |
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>; |