refactor(tvix/nix-compat): -derivation::Hash, +NixHash

This stops using our own custom Hash structure, which was mostly only
used because we had to parse the JSON representation somehow.

Since cl/8217, there's a `NixHash` struct, which is better suited to
hold this data. Converting the format requires a bit of serde labor
though, but that only really matters when interacting with JSON
representations (which we mostly don't).

Change-Id: Idc5ee511e36e6726c71f66face8300a441b0bf4c
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8304
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
Florian Klink 2023-03-14 17:16:05 +01:00 committed by clbot
parent e82385dbe5
commit b55d1f97ce
8 changed files with 201 additions and 87 deletions

View file

@ -1,5 +1,5 @@
//! Implements `builtins.derivation`, the core of what makes Nix build packages.
use nix_compat::derivation::{Derivation, Hash};
use nix_compat::derivation::Derivation;
use nix_compat::{hash_placeholder, nixhash};
use std::cell::RefCell;
use std::collections::{btree_map, BTreeSet};
@ -126,19 +126,18 @@ fn populate_output_configuration(
let output_hash = nixhash::from_str(&hash, a).map_err(Error::InvalidOutputHash)?;
// construct the algo string. Depending on hashMode, we prepend a `r:`.
let algo = match hash_mode.as_deref() {
None | Some("flat") => format!("{}", &output_hash.algo),
Some("recursive") => format!("r:{}", &output_hash.algo),
// construct the NixHashWithMode.
out.hash_with_mode = match hash_mode.as_deref() {
None | Some("flat") => Some(nixhash::NixHashWithMode::Flat(
nixhash::NixHash::new(output_hash.algo, output_hash.digest),
)),
Some("recursive") => Some(nixhash::NixHashWithMode::Recursive(
nixhash::NixHash::new(output_hash.algo, output_hash.digest),
)),
Some(other) => {
return Err(Error::InvalidOutputHashMode(other.to_string()).into())
}
};
out.hash = Some(Hash {
algo,
digest: data_encoding::HEXLOWER.encode(&output_hash.digest),
});
}
}
}
}