refactor(nix-compat): use StorePathRef for hash derivation modulo

Rather than passing strings around, use a StorePathRef.

This makes things a bit more typesafe, and more aligned with what we
want to do in b/264.

Change-Id: Ib7080addf27e7f1a9c8da1d8aaa66744468e3b5a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10633
Tested-by: BuildkiteCI
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
This commit is contained in:
Florian Klink 2024-01-15 20:32:55 +02:00 committed by flokli
parent c8114810c9
commit e2c79e39f0
4 changed files with 34 additions and 30 deletions

View file

@ -434,25 +434,24 @@ pub(crate) mod derivation_builtins {
// 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));
let derivation_or_fod_hash_tmp = drv.derivation_or_fod_hash(|drv_path| {
known_paths.get_hash_derivation_modulo(&drv_path.to_owned())
});
// Mutate the Derivation struct and set output paths
drv.calculate_output_paths(&name, &derivation_or_fod_hash_tmp)
.map_err(DerivationError::InvalidDerivation)?;
let derivation_path = drv
let drv_path = drv
.calculate_derivation_path(&name)
.map_err(DerivationError::InvalidDerivation)?;
// 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));
let derivation_or_fod_hash_final = drv.derivation_or_fod_hash(|drv_path| {
known_paths.get_hash_derivation_modulo(&drv_path.to_owned())
});
known_paths.add_hash_derivation_modulo(
derivation_path.to_absolute_path(),
&derivation_or_fod_hash_final,
);
known_paths.add_hash_derivation_modulo(drv_path.clone(), &derivation_or_fod_hash_final);
let mut new_attrs: Vec<(String, NixString)> = drv
.outputs
@ -465,7 +464,7 @@ pub(crate) mod derivation_builtins {
Some(
NixContextElement::Single {
name,
derivation: derivation_path.to_absolute_path(),
derivation: drv_path.to_absolute_path(),
}
.into(),
),
@ -478,8 +477,8 @@ pub(crate) mod derivation_builtins {
new_attrs.push((
"drvPath".to_string(),
(
derivation_path.to_absolute_path(),
Some(NixContextElement::Derivation(derivation_path.to_absolute_path()).into()),
drv_path.to_absolute_path(),
Some(NixContextElement::Derivation(drv_path.to_absolute_path()).into()),
)
.into(),
));

View file

@ -8,7 +8,7 @@
//! This data is required to find the derivation needed to actually trigger the
//! build, if necessary.
use nix_compat::nixhash::NixHash;
use nix_compat::{nixhash::NixHash, store_path::StorePath};
use std::collections::HashMap;
#[derive(Debug, Default)]
@ -16,26 +16,26 @@ pub struct KnownPaths {
/// All known derivation or FOD hashes.
///
/// Keys are derivation paths, values is the NixHash.
derivation_or_fod_hashes: HashMap<String, NixHash>,
derivation_or_fod_hashes: HashMap<StorePath, NixHash>,
}
impl KnownPaths {
/// Fetch the opaque "hash derivation modulo" for a given derivation path.
pub fn get_hash_derivation_modulo(&self, drv_path: &str) -> NixHash {
pub fn get_hash_derivation_modulo(&self, drv_path: &StorePath) -> NixHash {
// TODO: we rely on an invariant that things *should* have
// been calculated if we get this far.
self.derivation_or_fod_hashes[drv_path].clone()
}
pub fn add_hash_derivation_modulo<D: ToString>(
pub fn add_hash_derivation_modulo(
&mut self,
drv: D,
drv_path: StorePath,
hash_derivation_modulo: &NixHash,
) {
#[allow(unused_variables)] // assertions on this only compiled in debug builds
let old = self
.derivation_or_fod_hashes
.insert(drv.to_string(), hash_derivation_modulo.to_owned());
.insert(drv_path, hash_derivation_modulo.to_owned());
#[cfg(debug_assertions)]
{