2023-01-31 14:45:42 +01:00
|
|
|
use crate::derivation::{Derivation, DerivationError};
|
2023-07-18 20:34:52 +02:00
|
|
|
use crate::store_path::{self, StorePath};
|
2023-01-04 13:38:28 +01:00
|
|
|
|
|
|
|
impl Derivation {
|
|
|
|
/// validate ensures a Derivation struct is properly populated,
|
2023-01-31 15:07:04 +01:00
|
|
|
/// and returns a [DerivationError] if not.
|
|
|
|
///
|
2023-01-16 15:55:16 +01:00
|
|
|
/// if `validate_output_paths` is set to false, the output paths are
|
|
|
|
/// excluded from validation.
|
2023-01-31 15:07:04 +01:00
|
|
|
///
|
2023-01-16 15:55:16 +01:00
|
|
|
/// This is helpful to validate struct population before invoking
|
|
|
|
/// [Derivation::calculate_output_paths].
|
|
|
|
pub fn validate(&self, validate_output_paths: bool) -> Result<(), DerivationError> {
|
2023-01-04 13:38:28 +01:00
|
|
|
// Ensure the number of outputs is > 1
|
|
|
|
if self.outputs.is_empty() {
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::NoOutputs());
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate all outputs
|
|
|
|
for (output_name, output) in &self.outputs {
|
2023-01-17 11:56:23 +01:00
|
|
|
// empty output names are invalid.
|
|
|
|
//
|
|
|
|
// `drv` is an invalid output name too, as this would cause
|
|
|
|
// a `builtins.derivation` call to return an attrset with a
|
|
|
|
// `drvPath` key (which already exists) and has a different
|
|
|
|
// meaning.
|
|
|
|
//
|
|
|
|
// Other output names that don't match the name restrictions from
|
2023-07-18 20:34:52 +02:00
|
|
|
// [StorePath] will fail the [store_path::validate_name] check.
|
2023-01-17 12:42:36 +01:00
|
|
|
if output_name.is_empty()
|
|
|
|
|| output_name == "drv"
|
2023-07-18 20:34:52 +02:00
|
|
|
|| store_path::validate_name(output_name.as_bytes()).is_err()
|
2023-01-17 12:42:36 +01:00
|
|
|
{
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::InvalidOutputName(output_name.to_string()));
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if output.is_fixed() {
|
|
|
|
if self.outputs.len() != 1 {
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::MoreThanOneOutputButFixed());
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
if output_name != "out" {
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::InvalidOutputNameForFixed(
|
2023-01-16 15:24:02 +01:00
|
|
|
output_name.to_string(),
|
|
|
|
));
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-16 15:55:16 +01:00
|
|
|
if let Err(e) = output.validate(validate_output_paths) {
|
2023-01-17 12:04:15 +01:00
|
|
|
return Err(DerivationError::InvalidOutput(output_name.to_string(), e));
|
|
|
|
}
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate all input_derivations
|
|
|
|
for (input_derivation_path, output_names) in &self.input_derivations {
|
|
|
|
// Validate input_derivation_path
|
2023-07-18 20:34:52 +02:00
|
|
|
if let Err(e) = StorePath::from_absolute_path(input_derivation_path.as_bytes()) {
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::InvalidInputDerivationPath(
|
2023-01-16 15:24:02 +01:00
|
|
|
input_derivation_path.to_string(),
|
|
|
|
e,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2023-01-23 14:46:40 +01:00
|
|
|
if !input_derivation_path.ends_with(".drv") {
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::InvalidInputDerivationPrefix(
|
2023-01-16 15:24:02 +01:00
|
|
|
input_derivation_path.to_string(),
|
|
|
|
));
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if output_names.is_empty() {
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::EmptyInputDerivationOutputNames(
|
2023-01-16 15:24:02 +01:00
|
|
|
input_derivation_path.to_string(),
|
|
|
|
));
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 18:19:48 +01:00
|
|
|
for output_name in output_names.iter() {
|
2023-01-17 11:56:23 +01:00
|
|
|
// empty output names are invalid.
|
|
|
|
//
|
|
|
|
// `drv` is an invalid output name too, as this would cause
|
|
|
|
// a `builtins.derivation` call to return an attrset with a
|
|
|
|
// `drvPath` key (which already exists) and has a different
|
|
|
|
// meaning.
|
|
|
|
//
|
2023-01-17 12:42:36 +01:00
|
|
|
// Other output names that don't match the name restrictions from
|
|
|
|
// [StorePath] will fail the [StorePath::validate_name] check.
|
|
|
|
if output_name.is_empty()
|
|
|
|
|| output_name == "drv"
|
2023-07-18 20:34:52 +02:00
|
|
|
|| store_path::validate_name(output_name.as_bytes()).is_err()
|
2023-01-17 12:42:36 +01:00
|
|
|
{
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::InvalidInputDerivationOutputName(
|
2023-01-16 15:24:02 +01:00
|
|
|
input_derivation_path.to_string(),
|
|
|
|
output_name.to_string(),
|
|
|
|
));
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate all input_sources
|
2023-01-16 15:25:08 +01:00
|
|
|
for input_source in self.input_sources.iter() {
|
2023-07-18 20:34:52 +02:00
|
|
|
if let Err(e) = StorePath::from_absolute_path(input_source.as_bytes()) {
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::InvalidInputSourcesPath(
|
2023-01-16 15:24:02 +01:00
|
|
|
input_source.to_string(),
|
|
|
|
e,
|
|
|
|
));
|
|
|
|
}
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate platform
|
|
|
|
if self.system.is_empty() {
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::InvalidPlatform(self.system.to_string()));
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate builder
|
|
|
|
if self.builder.is_empty() {
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::InvalidBuilder(self.builder.to_string()));
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate env, none of the keys may be empty.
|
|
|
|
// We skip the `name` validation seen in go-nix.
|
|
|
|
for k in self.environment.keys() {
|
|
|
|
if k.is_empty() {
|
2023-01-17 12:00:52 +01:00
|
|
|
return Err(DerivationError::InvalidEnvironmentKey(k.to_string()));
|
2023-01-04 13:38:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|