From a44a8985cc0ae126f86d26493d97d80a09b211f8 Mon Sep 17 00:00:00 2001 From: Peter Kolloch Date: Wed, 21 Feb 2024 18:16:36 +0700 Subject: feat(tvix/nix-compat): generalize aterm writing for derivation ...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 Autosubmit: Peter Kolloch Tested-by: BuildkiteCI Reviewed-by: flokli --- tvix/nix-compat/src/nixhash/mod.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'tvix/nix-compat/src/nixhash') 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 { + 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 = std::result::Result; -- cgit 1.4.1