2023-01-12 13:59:58 +03:00
|
|
|
//! This module implements logic required for persisting known paths
|
|
|
|
//! during an evaluation.
|
|
|
|
//!
|
|
|
|
//! Tvix needs to be able to keep track of each Nix store path that it
|
|
|
|
//! knows about during the scope of a single evaluation and its
|
|
|
|
//! related builds.
|
|
|
|
//!
|
2024-01-15 20:13:26 +02:00
|
|
|
//! This data is required to find the derivation needed to actually trigger the
|
|
|
|
//! build, if necessary.
|
2023-01-12 13:59:58 +03:00
|
|
|
|
2024-02-10 16:04:31 +01:00
|
|
|
use nix_compat::{
|
|
|
|
derivation::Derivation,
|
|
|
|
nixhash::NixHash,
|
|
|
|
store_path::{StorePath, StorePathRef},
|
|
|
|
};
|
2024-01-15 20:13:26 +02:00
|
|
|
use std::collections::HashMap;
|
2023-02-03 00:20:02 +03:00
|
|
|
|
2024-02-10 16:04:31 +01:00
|
|
|
/// Struct keeping track of all known Derivations in the current evaluation.
|
|
|
|
/// This keeps both the Derivation struct, as well as the "Hash derivation
|
|
|
|
/// modulo".
|
2023-01-14 15:45:22 +03:00
|
|
|
#[derive(Debug, Default)]
|
2023-01-12 13:59:58 +03:00
|
|
|
pub struct KnownPaths {
|
2023-03-13 23:52:23 +01:00
|
|
|
/// All known derivation or FOD hashes.
|
2023-01-14 01:45:57 +03:00
|
|
|
///
|
2024-02-10 16:04:31 +01:00
|
|
|
/// Keys are derivation paths, values are a tuple of the "hash derivation
|
|
|
|
/// modulo" and the Derivation struct itself.
|
|
|
|
derivations: HashMap<StorePath, (NixHash, Derivation)>,
|
2024-02-10 16:36:19 +01:00
|
|
|
|
|
|
|
/// A map from output path to (one) drv path.
|
|
|
|
/// Note that in the case of FODs, multiple drvs can produce the same output
|
|
|
|
/// path. We use one of them.
|
|
|
|
outputs_to_drvpath: HashMap<StorePath, StorePath>,
|
2023-01-12 13:59:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KnownPaths {
|
2023-03-13 23:52:23 +01:00
|
|
|
/// Fetch the opaque "hash derivation modulo" for a given derivation path.
|
2024-02-10 16:04:31 +01:00
|
|
|
pub fn get_hash_derivation_modulo(&self, drv_path: &StorePathRef) -> Option<&NixHash> {
|
|
|
|
self.derivations
|
|
|
|
.get(&drv_path.to_owned())
|
|
|
|
.map(|(hash_derivation_modulo, _derivation)| hash_derivation_modulo)
|
2023-01-14 01:45:57 +03:00
|
|
|
}
|
|
|
|
|
2024-02-10 16:04:31 +01:00
|
|
|
/// Return a reference to the Derivation for a given drv path.
|
|
|
|
pub fn get_drv_by_drvpath(&self, drv_path: &StorePath) -> Option<&Derivation> {
|
|
|
|
self.derivations
|
|
|
|
.get(drv_path)
|
|
|
|
.map(|(_hash_derivation_modulo, derivation)| derivation)
|
|
|
|
}
|
|
|
|
|
2024-02-10 16:36:19 +01:00
|
|
|
/// Return the drv path of the derivation producing the passed output path.
|
|
|
|
/// Note there can be multiple Derivations producing the same output path in
|
|
|
|
/// flight; this function will only return one of them.
|
|
|
|
pub fn get_drv_path_for_output_path(&self, output_path: &StorePath) -> Option<&StorePath> {
|
|
|
|
self.outputs_to_drvpath.get(output_path)
|
|
|
|
}
|
|
|
|
|
2024-02-10 16:04:31 +01:00
|
|
|
/// Insert a new Derivation into this struct.
|
|
|
|
/// The Derivation struct must pass validation, and its output paths need to
|
|
|
|
/// be fully calculated.
|
|
|
|
/// All input derivations this refers to must also be inserted to this
|
|
|
|
/// struct.
|
|
|
|
pub fn add(&mut self, drv_path: StorePath, drv: Derivation) {
|
|
|
|
// check input derivations to have been inserted.
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
// TODO: b/264
|
|
|
|
// We assume derivations to be passed validated, so ignoring rest
|
|
|
|
// and expecting parsing is ok.
|
|
|
|
for input_drv_path_str in drv.input_derivations.keys() {
|
|
|
|
let (input_drv_path, _rest) =
|
|
|
|
StorePath::from_absolute_path_full(input_drv_path_str)
|
|
|
|
.expect("parse input drv path");
|
|
|
|
debug_assert!(self.derivations.contains_key(&input_drv_path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// compute the hash derivation modulo
|
|
|
|
let hash_derivation_modulo = drv.derivation_or_fod_hash(|drv_path| {
|
|
|
|
self.get_hash_derivation_modulo(drv_path)
|
|
|
|
.unwrap_or_else(|| panic!("{} not found", drv_path))
|
|
|
|
.to_owned()
|
|
|
|
});
|
|
|
|
|
2024-02-10 16:36:19 +01:00
|
|
|
// For all output paths, update our lookup table.
|
|
|
|
// We only write into the lookup table once.
|
|
|
|
for output in drv.outputs.values() {
|
|
|
|
// We assume derivations to be passed validated, so ignoring rest
|
|
|
|
// and expecting parsing is ok.
|
|
|
|
// TODO: b/264
|
|
|
|
let (output_path, _rest) =
|
|
|
|
StorePath::from_absolute_path_full(&output.path).expect("parse output path");
|
|
|
|
|
|
|
|
self.outputs_to_drvpath
|
|
|
|
.entry(output_path)
|
|
|
|
.or_insert(drv_path.to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert the derivation itself
|
2023-03-12 00:49:09 +03:00
|
|
|
#[allow(unused_variables)] // assertions on this only compiled in debug builds
|
2023-01-14 01:45:57 +03:00
|
|
|
let old = self
|
2024-02-10 16:04:31 +01:00
|
|
|
.derivations
|
|
|
|
.insert(drv_path.to_owned(), (hash_derivation_modulo.clone(), drv));
|
2023-01-14 01:45:57 +03:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
if let Some(old) = old {
|
|
|
|
debug_assert!(
|
2024-02-10 16:04:31 +01:00
|
|
|
old.0 == hash_derivation_modulo,
|
2023-03-13 23:52:23 +01:00
|
|
|
"hash derivation modulo for a given derivation should always be calculated the same"
|
2023-01-14 01:45:57 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 13:59:58 +03:00
|
|
|
}
|