fix(tvix/cli): handle SRI hashes in outputHash

Instead of being called with `md5`, `sha1`, `sha256` or `sha512`,
`fetchurl.nix` (from corepkgs / `<nix`) can also be called with a `hash`
attribute, being an SRI hash.

In that case, `builtin.derivation` is called with `outputHashAlgo` being
an empty string, and `outputHash` being an SRI hash string.

In other cases, an SRI hash is passed as outputHash, but outputHashAlgo
is set too.

Nix does modify these values in (single, fixed) output specification it
serializes to ATerm, but keeps it unharmed in `env`.

Move this into a construct_output_hash helper function, that can be
tested better in isolation.

Change-Id: Id9d716a119664c44ea7747540399966752e20187
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7933
Reviewed-by: tazjin <tazjin@tvl.su>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
Florian Klink 2023-01-26 14:18:12 +01:00 committed by clbot
parent bda5fc58d0
commit a94a1434cc
5 changed files with 573 additions and 38 deletions

View file

@ -1,7 +1,7 @@
use std::{error, fmt::Display, rc::Rc};
use tvix_derivation::DerivationError;
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum Error {
// Errors related to derivation construction
DuplicateOutput(String),
@ -10,6 +10,11 @@ pub enum Error {
ShadowedOutput(String),
InvalidDerivation(DerivationError),
InvalidOutputHashMode(String),
UnsupportedSRIAlgo(String),
UnsupportedSRIMultiple(usize),
InvalidSRIDigest(data_encoding::DecodeError),
InvalidSRIString(String),
ConflictingSRIHashAlgo(String, String),
}
impl Display for Error {
@ -38,6 +43,31 @@ impl Display for Error {
f,
"invalid output hash mode: '{mode}', only 'recursive' and 'flat` are supported"
),
Error::UnsupportedSRIAlgo(algo) => {
write!(
f,
"unsupported sri algorithm: {algo}, only sha1, sha256 or sha512 is supported"
)
}
Error::UnsupportedSRIMultiple(n) => {
write!(
f,
"invalid number of sri hashes in string ({n}), only one hash is supported"
)
}
Error::InvalidSRIDigest(err) => {
write!(f, "invalid sri digest: {}", err)
}
Error::InvalidSRIString(err) => {
write!(f, "failed to parse SRI string: {}", err)
}
Error::ConflictingSRIHashAlgo(algo, sri_algo) => {
write!(
f,
"outputHashAlgo is set to {}, but outputHash contains SRI with algo {}",
algo, sri_algo
)
}
}
}
}