Skip to main content

nix_compat/derivation/
validate.rs

1use crate::derivation::{Derivation, DerivationError};
2
3impl Derivation {
4    /// validate ensures a Derivation struct is properly populated,
5    /// and returns a [DerivationError] if not.
6    ///
7    /// This is helpful to validate struct population before invoking
8    /// [Derivation::calculate_output_paths].
9    pub fn validate(&self) -> Result<(), DerivationError> {
10        // Validate all input_derivation paths to end with .drv.
11        // The output names are already validated as we're using the OutputName type.
12        for input_derivation_path in self.input_derivations.keys() {
13            if !input_derivation_path.name().ends_with(".drv") {
14                return Err(DerivationError::InvalidInputDerivationPrefix(
15                    input_derivation_path.to_string(),
16                ));
17            }
18        }
19
20        // validate platform
21        if self.system.is_empty() {
22            return Err(DerivationError::InvalidPlatform(self.system.to_string()));
23        }
24
25        // validate builder
26        if self.builder.is_empty() {
27            return Err(DerivationError::InvalidBuilder(self.builder.to_string()));
28        }
29
30        // validate env, none of the keys may be empty.
31        // We skip the `name` validation seen in go-nix.
32        for k in self.environment.keys() {
33            if k.is_empty() {
34                return Err(DerivationError::InvalidEnvironmentKey(k.to_string()));
35            }
36        }
37
38        Ok(())
39    }
40}