snix_glue/builtins/
errors.rs

1//! Contains errors that can occur during evaluation of builtins in this crate
2use nix_compat::{
3    nixhash::{self, NixHash},
4    store_path::BuildStorePathError,
5};
6use reqwest::Url;
7use snix_castore::import;
8use std::{path::PathBuf, rc::Rc};
9use thiserror::Error;
10
11/// Errors related to derivation construction
12#[derive(Debug, Error)]
13pub enum DerivationError {
14    #[error("an output with the name '{0}' is already defined")]
15    DuplicateOutput(String),
16    #[error("fixed-output derivations can only have the default `out`-output")]
17    ConflictingOutputTypes,
18    #[error("the environment variable '{0}' has already been set in this derivation")]
19    DuplicateEnvVar(String),
20    #[error("invalid derivation parameters: {0}")]
21    InvalidDerivation(#[from] nix_compat::derivation::DerivationError),
22    #[error("invalid output hash: {0}")]
23    InvalidOutputHash(#[from] nixhash::Error),
24    #[error("invalid output hash mode: '{0}', only 'recursive' and 'flat` are supported")]
25    InvalidOutputHashMode(String),
26}
27
28impl From<DerivationError> for snix_eval::ErrorKind {
29    fn from(err: DerivationError) -> Self {
30        snix_eval::ErrorKind::SnixError(Rc::new(err))
31    }
32}
33
34#[derive(Debug, Error)]
35pub enum FetcherError {
36    #[error("hash mismatch in file downloaded from {url}:\n  wanted: {wanted}\n     got: {got}")]
37    HashMismatch {
38        url: Url,
39        wanted: NixHash,
40        got: NixHash,
41    },
42
43    #[error("Invalid hash type '{0}' for fetcher")]
44    InvalidHashType(&'static str),
45
46    #[error("Unable to parse URL: {0}")]
47    InvalidUrl(#[from] url::ParseError),
48
49    #[error(transparent)]
50    Http(#[from] reqwest::Error),
51
52    #[error(transparent)]
53    Io(#[from] std::io::Error),
54
55    #[error(transparent)]
56    Import(#[from] snix_castore::import::IngestionError<import::archive::Error>),
57
58    #[error("Error calculating store path for fetcher output: {0}")]
59    StorePath(#[from] BuildStorePathError),
60}
61
62/// Errors related to `builtins.path` and `builtins.filterSource`,
63/// a.k.a. "importing" builtins.
64#[derive(Debug, Error)]
65pub enum ImportError {
66    #[error("non-file '{0}' cannot be imported in 'flat' mode")]
67    FlatImportOfNonFile(PathBuf),
68
69    #[error("hash mismatch at ingestion of '{0}', expected: '{1}', got: '{2}'")]
70    HashMismatch(PathBuf, NixHash, NixHash),
71
72    #[error("path '{}' is not absolute or invalid", .0.display())]
73    PathNotAbsoluteOrInvalid(PathBuf),
74}
75
76impl From<ImportError> for snix_eval::ErrorKind {
77    fn from(err: ImportError) -> Self {
78        snix_eval::ErrorKind::SnixError(Rc::new(err))
79    }
80}