feat(tvix/store): enable name customization in the store

Sometimes, Nix lets someone customize the `name` in the store for a
path, this is the case for `builtins.path` which takes a `name`
argument, we leave it to the caller to choose the name, which can be the
basename by default of the path.

Change-Id: Icdbf71d1d8f2dca5716b99d20aac885aab905b80
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10653
Tested-by: BuildkiteCI
Autosubmit: raitobezarius <tvl@lahfa.xyz>
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Ryan Lahfa 2024-01-17 08:06:30 +01:00 committed by clbot
parent 4c3ba46ba3
commit 3d8aafb1c7
3 changed files with 19 additions and 15 deletions

View file

@ -276,6 +276,7 @@ impl EvalIO for TvixStoreIO {
let output_path = self.tokio_handle.block_on(async { let output_path = self.tokio_handle.block_on(async {
tvix_store::import::import_path_as_nar_ca( tvix_store::import::import_path_as_nar_ca(
path, path,
tvix_store::import::path_to_name(path)?,
&self.blob_service, &self.blob_service,
&self.directory_service, &self.directory_service,
&self.path_info_service, &self.path_info_service,

View file

@ -305,16 +305,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let path_info_service = path_info_service.clone(); let path_info_service = path_info_service.clone();
async move { async move {
let resp = tvix_store::import::import_path_as_nar_ca( if let Ok(name) = tvix_store::import::path_to_name(&path) {
path, let resp = tvix_store::import::import_path_as_nar_ca(
blob_service, &path,
directory_service, name,
path_info_service, blob_service,
) directory_service,
.await; path_info_service,
if let Ok(output_path) = resp { )
// If the import was successful, print the path to stdout. .await;
println!("{}", output_path.to_absolute_path()); if let Ok(output_path) = resp {
// If the import was successful, print the path to stdout.
println!("{}", output_path.to_absolute_path());
}
} }
} }
}) })

View file

@ -13,7 +13,7 @@ use crate::{
proto::{nar_info, NarInfo, PathInfo}, proto::{nar_info, NarInfo, PathInfo},
}; };
fn log_node(node: &Node, path: &Path) { pub fn log_node(node: &Node, path: &Path) {
match node { match node {
Node::Directory(directory_node) => { Node::Directory(directory_node) => {
debug!( debug!(
@ -86,9 +86,10 @@ pub fn derive_nar_ca_path_info(nar_size: u64, nar_sha256: [u8; 32], root_node: N
/// Ingest the given path [`path`] and register the resulting output path in the /// Ingest the given path [`path`] and register the resulting output path in the
/// [`PathInfoService`] as a recursive fixed output NAR. /// [`PathInfoService`] as a recursive fixed output NAR.
#[instrument(skip_all, fields(path=?path), err)] #[instrument(skip_all, fields(store_name=name, path=?path), err)]
pub async fn import_path_as_nar_ca<BS, DS, PS, P>( pub async fn import_path_as_nar_ca<BS, DS, PS, P>(
path: P, path: P,
name: &str,
blob_service: BS, blob_service: BS,
directory_service: DS, directory_service: DS,
path_info_service: PS, path_info_service: PS,
@ -106,9 +107,8 @@ where
let (nar_size, nar_sha256) = path_info_service.as_ref().calculate_nar(&root_node).await?; let (nar_size, nar_sha256) = path_info_service.as_ref().calculate_nar(&root_node).await?;
// Calculate the output path. This might still fail, as some names are illegal. // Calculate the output path. This might still fail, as some names are illegal.
// FUTUREWORK: take `name` as a parameter here and enforce the validity of the name // FUTUREWORK: express the `name` at the type level to be valid and move the conversion
// at the type level. // at the caller level.
let name = path_to_name(path.as_ref())?;
let output_path = store_path::build_nar_based_store_path(&nar_sha256, name).map_err(|_| { let output_path = store_path::build_nar_based_store_path(&nar_sha256, name).map_err(|_| {
std::io::Error::new( std::io::Error::new(
std::io::ErrorKind::InvalidData, std::io::ErrorKind::InvalidData,