From 5f260edf7fba9a6ad1e0ee16779df11d92686828 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Mon, 13 Mar 2023 23:52:23 +0100 Subject: refactor(tvix/nix-compat): replace calculate_drv_replacement_str Call this function derivation_or_fod_hash, and return a NixHash. This is more in line with how cppnix calls this, and allows using to_nix_hash_string() in some places. Change-Id: Iebf5355f08ed5c9a044844739350f829f874f0ce Reviewed-on: https://cl.tvl.fyi/c/depot/+/8293 Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/cli/src/derivation.rs | 22 ++++++++++++++-------- tvix/cli/src/known_paths.rs | 28 ++++++++++++++++------------ 2 files changed, 30 insertions(+), 20 deletions(-) (limited to 'tvix/cli/src') diff --git a/tvix/cli/src/derivation.rs b/tvix/cli/src/derivation.rs index 15c4c6f858f7..61c4489954f1 100644 --- a/tvix/cli/src/derivation.rs +++ b/tvix/cli/src/derivation.rs @@ -339,21 +339,27 @@ mod derivation_builtins { // eval data structures. drv.validate(false).map_err(Error::InvalidDerivation)?; - let tmp_replacement_str = - drv.calculate_drv_replacement_str(|drv| known_paths.get_replacement_string(drv)); + // Calculate the derivation_or_fod_hash for the current derivation. + // This one is still intermediate (so not added to known_paths) + let derivation_or_fod_hash_tmp = + drv.derivation_or_fod_hash(|drv| known_paths.get_hash_derivation_modulo(drv)); - drv.calculate_output_paths(&name, &tmp_replacement_str) + // Mutate the Derivation struct and set output paths + drv.calculate_output_paths(&name, &derivation_or_fod_hash_tmp) .map_err(Error::InvalidDerivation)?; - let actual_replacement_str = - drv.calculate_drv_replacement_str(|drv| known_paths.get_replacement_string(drv)); - let derivation_path = drv .calculate_derivation_path(&name) .map_err(Error::InvalidDerivation)?; - known_paths - .add_replacement_string(derivation_path.to_absolute_path(), &actual_replacement_str); + // recompute the hash derivation modulo and add to known_paths + let derivation_or_fod_hash_final = + drv.derivation_or_fod_hash(|drv| known_paths.get_hash_derivation_modulo(drv)); + + known_paths.add_hash_derivation_modulo( + derivation_path.to_absolute_path(), + &derivation_or_fod_hash_final, + ); // mark all the new paths as known let output_names: Vec = drv.outputs.keys().map(Clone::clone).collect(); diff --git a/tvix/cli/src/known_paths.rs b/tvix/cli/src/known_paths.rs index f16fad902654..07373ef0da7a 100644 --- a/tvix/cli/src/known_paths.rs +++ b/tvix/cli/src/known_paths.rs @@ -12,6 +12,7 @@ //! information. use crate::refscan::{ReferenceScanner, STORE_PATH_LEN}; +use nix_compat::nixhash::NixHash; use std::{ collections::{hash_map, BTreeSet, HashMap}, ops::Index, @@ -67,11 +68,10 @@ pub struct KnownPaths { /// path used for reference scanning. paths: HashMap, - /// All known replacement strings for derivations. + /// All known derivation or FOD hashes. /// - /// Keys are derivation paths, values are the opaque replacement - /// strings. - replacements: HashMap, + /// Keys are derivation paths, values is the NixHash. + derivation_or_fod_hashes: HashMap, } impl Index<&PathName> for KnownPaths { @@ -156,25 +156,29 @@ impl KnownPaths { ReferenceScanner::new(candidates) } - /// Fetch the opaque "replacement string" for a given derivation path. - pub fn get_replacement_string(&self, drv: &str) -> String { + /// Fetch the opaque "hash derivation modulo" for a given derivation path. + pub fn get_hash_derivation_modulo(&self, drv_path: &str) -> NixHash { // TODO: we rely on an invariant that things *should* have // been calculated if we get this far. - self.replacements[drv].clone() + self.derivation_or_fod_hashes[drv_path].clone() } - pub fn add_replacement_string(&mut self, drv: D, replacement_str: &str) { + pub fn add_hash_derivation_modulo( + &mut self, + drv: D, + hash_derivation_modulo: &NixHash, + ) { #[allow(unused_variables)] // assertions on this only compiled in debug builds let old = self - .replacements - .insert(drv.to_string(), replacement_str.to_owned()); + .derivation_or_fod_hashes + .insert(drv.to_string(), hash_derivation_modulo.to_owned()); #[cfg(debug_assertions)] { if let Some(old) = old { debug_assert!( - old == replacement_str, - "replacement string for a given derivation should always match" + old == *hash_derivation_modulo, + "hash derivation modulo for a given derivation should always be calculated the same" ); } } -- cgit 1.4.1