tvl-depot/tvix/glue/src/builtins/errors.rs
Aspen Smith de727bccf9 feat(tvix/glue): Implement builtins.fetchurl
Implement the fetchurl builtin, and lay the groundwork for implementing
the fetchTarball builtin (which works very similarly, and is implemented
using almost the same code in C++ nix).

An overview of how this works:

1. First, we check if the store path that *would* result from the
   download already exists in the store - if it does, we just return
   that
2. If we need to download the URL, TvixStoreIO has an `http_client:
   reqwest::Client` field now which we use to make the request
3. As we're downloading the blob, we hash the data incrementally into a
   SHA256 hasher
4. We compare the hash against the expected hash (if any) and bail out
   if it doesn't match
5. Finally, we put the blob in the store and return the store path

Since the logic is very similar, this commit also implements a *chunk*
of `fetchTarball` (though the actual implementation will likely include
a refactor to some of the code reuse here).

The main thing that's missing here is caching of downloaded blobs when
fetchurl is called without a hash - I've opened b/381 to track the TODO
there.

Adding the `SSL_CERT_FILE` here is necessary to teach reqwest how to
load it during tests - see 1c16dee20 (feat(tvix/store): use reqwests'
rustls-native-roots feature, 2024-03-03) for  more info.

Change-Id: I83c4abbc7c0c3bfe92461917e23d6d3430fbf137
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11017
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Autosubmit: aspen <root@gws.fyi>
2024-03-11 02:21:54 +00:00

55 lines
1.7 KiB
Rust

//! Contains errors that can occur during evaluation of builtins in this crate
use nix_compat::{
nixhash::{self, NixHash},
store_path::BuildStorePathError,
};
use std::rc::Rc;
use thiserror::Error;
/// Errors related to derivation construction
#[derive(Debug, Error)]
pub enum DerivationError {
#[error("an output with the name '{0}' is already defined")]
DuplicateOutput(String),
#[error("fixed-output derivations can only have the default `out`-output")]
ConflictingOutputTypes,
#[error("the environment variable '{0}' has already been set in this derivation")]
DuplicateEnvVar(String),
#[error("invalid derivation parameters: {0}")]
InvalidDerivation(#[from] nix_compat::derivation::DerivationError),
#[error("invalid output hash: {0}")]
InvalidOutputHash(#[from] nixhash::Error),
#[error("invalid output hash mode: '{0}', only 'recursive' and 'flat` are supported")]
InvalidOutputHashMode(String),
}
impl From<DerivationError> for tvix_eval::ErrorKind {
fn from(err: DerivationError) -> Self {
tvix_eval::ErrorKind::TvixError(Rc::new(err))
}
}
#[derive(Debug, Error)]
pub enum FetcherError {
#[error("hash mismatch in file downloaded from {url}:\n wanted: {wanted}\n got: {got}")]
HashMismatch {
url: String,
wanted: NixHash,
got: NixHash,
},
#[error("Invalid hash type '{0}' for fetcher")]
InvalidHashType(&'static str),
#[error("Error in store path for fetcher output: {0}")]
StorePath(#[from] BuildStorePathError),
#[error(transparent)]
Http(#[from] reqwest::Error),
}
impl From<FetcherError> for tvix_eval::ErrorKind {
fn from(err: FetcherError) -> Self {
tvix_eval::ErrorKind::TvixError(Rc::new(err))
}
}