diff options
author | Peter Kolloch <info@eigenvalue.net> | 2024-02-21T11·16+0700 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2024-02-21T11·34+0000 |
commit | a44a8985cc0ae126f86d26493d97d80a09b211f8 (patch) | |
tree | 0d8f645b9d26fab2214e0ba4caf9de365233af58 /tvix/nix-compat/src/derivation/mod.rs | |
parent | f50800a9dffb18572ca28b8afd592b8a7c203607 (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/derivation/mod.rs')
-rw-r--r-- | tvix/nix-compat/src/derivation/mod.rs | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/tvix/nix-compat/src/derivation/mod.rs b/tvix/nix-compat/src/derivation/mod.rs index 4d27bbb26d66..3a09c9d56632 100644 --- a/tvix/nix-compat/src/derivation/mod.rs +++ b/tvix/nix-compat/src/derivation/mod.rs @@ -22,6 +22,8 @@ pub use crate::nixhash::{CAHash, NixHash}; pub use errors::{DerivationError, OutputError}; pub use output::Output; +use self::write::AtermWriteable; + #[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] pub struct Derivation { #[serde(rename = "args")] @@ -58,7 +60,7 @@ impl Derivation { fn serialize_with_replacements( &self, writer: &mut impl std::io::Write, - input_derivations: &BTreeMap<String, BTreeSet<String>>, + input_derivations: &BTreeMap<impl AtermWriteable, BTreeSet<String>>, ) -> Result<(), io::Error> { use write::*; @@ -68,7 +70,19 @@ impl Derivation { write_outputs(writer, &self.outputs)?; write_char(writer, COMMA)?; - write_input_derivations(writer, input_derivations)?; + // To reproduce the exact hashes of original nix, we need to sort + // these by their aterm representation. + // StorePath has a different sort order, so we convert it here. + let input_derivations: BTreeMap<BString, &BTreeSet<String>> = input_derivations + .iter() + .map(|(k, v)| { + let mut aterm_k = Vec::new(); + k.aterm_write(&mut aterm_k).expect("no write error to Vec"); + (BString::new(aterm_k), v) + }) + .collect(); + + write_input_derivations(writer, &input_derivations)?; write_char(writer, COMMA)?; write_input_sources(writer, &self.input_sources)?; @@ -98,7 +112,7 @@ impl Derivation { /// Like `to_aterm_bytes` but allow input_derivation replacements for hashing. fn to_aterm_bytes_with_replacements( &self, - input_derivations: &BTreeMap<String, BTreeSet<String>>, + input_derivations: &BTreeMap<impl AtermWriteable, BTreeSet<String>>, ) -> Vec<u8> { let mut buffer: Vec<u8> = Vec::new(); @@ -202,10 +216,9 @@ impl Derivation { let drv_path = StorePathRef::from_absolute_path(drv_path_str.as_bytes()) .expect("invalid input derivation path"); - let encoded_hash = data_encoding::HEXLOWER - .encode(fn_get_derivation_or_fod_hash(&drv_path).digest_as_bytes()); + let hash = fn_get_derivation_or_fod_hash(&drv_path); - (encoded_hash, output_names.to_owned()) + (hash, output_names.to_owned()) }, )); |