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-01-15 20:32:55 +02:00
|
|
|
use nix_compat::{nixhash::NixHash, store_path::StorePath};
|
2024-01-15 20:13:26 +02:00
|
|
|
use std::collections::HashMap;
|
2023-02-03 00:20:02 +03:00
|
|
|
|
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
|
|
|
///
|
2023-03-13 23:52:23 +01:00
|
|
|
/// Keys are derivation paths, values is the NixHash.
|
2024-01-15 20:32:55 +02:00
|
|
|
derivation_or_fod_hashes: HashMap<StorePath, NixHash>,
|
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-01-15 20:32:55 +02:00
|
|
|
pub fn get_hash_derivation_modulo(&self, drv_path: &StorePath) -> NixHash {
|
2023-01-14 01:45:57 +03:00
|
|
|
// TODO: we rely on an invariant that things *should* have
|
|
|
|
// been calculated if we get this far.
|
2023-03-13 23:52:23 +01:00
|
|
|
self.derivation_or_fod_hashes[drv_path].clone()
|
2023-01-14 01:45:57 +03:00
|
|
|
}
|
|
|
|
|
2024-01-15 20:32:55 +02:00
|
|
|
pub fn add_hash_derivation_modulo(
|
2023-03-13 23:52:23 +01:00
|
|
|
&mut self,
|
2024-01-15 20:32:55 +02:00
|
|
|
drv_path: StorePath,
|
2023-03-13 23:52:23 +01:00
|
|
|
hash_derivation_modulo: &NixHash,
|
|
|
|
) {
|
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
|
2023-03-13 23:52:23 +01:00
|
|
|
.derivation_or_fod_hashes
|
2024-01-15 20:32:55 +02:00
|
|
|
.insert(drv_path, hash_derivation_modulo.to_owned());
|
2023-01-14 01:45:57 +03:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
if let Some(old) = old {
|
|
|
|
debug_assert!(
|
2023-03-13 23:52:23 +01:00
|
|
|
old == *hash_derivation_modulo,
|
|
|
|
"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
|
|
|
}
|