refactor(nix-compat/store_path): take [u8;32] for outer fingerprint

The outer fingerprint used for store path calculation is always a sha256
digest. This includes both input and output-addressed store paths.

We used a NixHash here, which can also represent other hash types, and
that had a bunch of annoyances:

 - Whenever we had the bytes, we had to wrap them in a NixHash::Sha256().
 - Things like AtermWriteable had to be implemented on NixHash,
   even though we then had an assertion it was only called in the
   NixHash::Sha256 case.

Change-Id: Ic895503d9b071800d2e52ae057666f44bd0ab9d6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11142
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: John Ericson <git@johnericson.me>
Reviewed-by: picnoir picnoir <picnoir@alternativebit.fr>
This commit is contained in:
Florian Klink 2024-03-14 13:50:56 +02:00 committed by clbot
parent 35f636b684
commit 43c851bc84
5 changed files with 46 additions and 51 deletions

View file

@ -8,7 +8,8 @@ use crate::derivation::{ca_kind_prefix, output::Output};
use crate::nixbase32;
use crate::store_path::{StorePath, StorePathRef, STORE_DIR_WITH_SLASH};
use bstr::BString;
use std::fmt::Display;
use data_encoding::HEXLOWER;
use std::{
collections::{BTreeMap, BTreeSet},
io,
@ -16,8 +17,6 @@ use std::{
io::Write,
};
use super::NixHash;
pub const DERIVATION_PREFIX: &str = "Derive";
pub const PAREN_OPEN: char = '(';
pub const PAREN_CLOSE: char = ')';
@ -31,7 +30,7 @@ pub const QUOTE: char = '"';
/// Note that we mostly use explicit `write_*` calls
/// instead since the serialization of the items depends on
/// the context a lot.
pub(crate) trait AtermWriteable: Display {
pub(crate) trait AtermWriteable {
fn aterm_write(&self, writer: &mut impl Write) -> std::io::Result<()>;
fn aterm_bytes(&self) -> Vec<u8> {
@ -67,12 +66,9 @@ impl AtermWriteable for String {
}
}
impl AtermWriteable for NixHash {
impl AtermWriteable for [u8; 32] {
fn aterm_write(&self, writer: &mut impl Write) -> std::io::Result<()> {
// When we serialize the placeholder hashes,
// they need to be SHA256.
debug_assert!(matches!(self, NixHash::Sha256(_)));
write_field(writer, self.to_plain_hex_string(), false)
write_field(writer, HEXLOWER.encode(self), false)
}
}