From e2b6c77bfc4d00ba275d8029fe333d6064c268e9 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sun, 31 Dec 2023 14:28:51 +0200 Subject: feat(tvix/cli): allow configuring different services At some point, tvix-cli needs to talk to the outside world to persist things into a real store. Introduce the same CLI options to configure {Blob,Directory,PathInfo}Service URLs. We need to be a bit careful with how we set up stores, and make this separate from setting up TvixStoreIO, as it's holding a Rc> which not Send. At some point, we might make this a Arc> later anyways, and then this can be simplified a bit, but for now, this is sufficient. Change-Id: I87d84ca3a10ce947e194ff985073791469773f35 Reviewed-on: https://cl.tvl.fyi/c/depot/+/10474 Autosubmit: flokli Reviewed-by: raitobezarius Tested-by: BuildkiteCI --- tvix/cli/src/main.rs | 67 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/tvix/cli/src/main.rs b/tvix/cli/src/main.rs index 149b08fa8e..a45061412c 100644 --- a/tvix/cli/src/main.rs +++ b/tvix/cli/src/main.rs @@ -7,12 +7,12 @@ use tvix_glue::{builtins::add_derivation_builtins, configure_nix_path}; use clap::Parser; use rustyline::{error::ReadlineError, Editor}; -use tvix_castore::blobservice::MemoryBlobService; -use tvix_castore::directoryservice::MemoryDirectoryService; +use tvix_castore::blobservice::{self, BlobService}; +use tvix_castore::directoryservice::{self, DirectoryService}; use tvix_eval::observer::{DisassemblingObserver, TracingObserver}; use tvix_eval::Value; use tvix_glue::tvix_store_io::TvixStoreIO; -use tvix_store::pathinfoservice::MemoryPathInfoService; +use tvix_store::pathinfoservice::{self, PathInfoService}; #[derive(Parser)] struct Args { @@ -56,6 +56,37 @@ struct Args { /// return value. #[clap(long)] strict: bool, + + #[arg(long, env, default_value = "memory://")] + blob_service_addr: String, + + #[arg(long, env, default_value = "memory://")] + directory_service_addr: String, + + #[arg(long, env, default_value = "memory://")] + path_info_service_addr: String, +} + +/// Construct the three store handles from their addrs. +async fn construct_services( + blob_service_addr: impl AsRef, + directory_service_addr: impl AsRef, + path_info_service_addr: impl AsRef, +) -> std::io::Result<( + Arc, + Arc, + Box, +)> { + let blob_service = blobservice::from_addr(blob_service_addr.as_ref()).await?; + let directory_service = directoryservice::from_addr(directory_service_addr.as_ref()).await?; + let path_info_service = pathinfoservice::from_addr( + path_info_service_addr.as_ref(), + blob_service.clone(), + directory_service.clone(), + ) + .await?; + + Ok((blob_service, directory_service, path_info_service)) } /// Interprets the given code snippet, printing out warnings, errors @@ -63,27 +94,35 @@ struct Args { /// evaluation succeeded. fn interpret(code: &str, path: Option, args: &Args, explain: bool) -> bool { let mut eval = tvix_eval::Evaluation::new_impure(); - eval.strict = args.strict; - let blob_service = Arc::new(MemoryBlobService::default()); - let directory_service = Arc::new(MemoryDirectoryService::default()); - let path_info_service = Arc::new(MemoryPathInfoService::new( - blob_service.clone(), - directory_service.clone(), - )); + let tokio_runtime = tokio::runtime::Runtime::new().expect("failed to setup tokio runtime"); + + let (blob_service, directory_service, path_info_service) = tokio_runtime + .block_on({ + let blob_service_addr = args.blob_service_addr.clone(); + let directory_service_addr = args.directory_service_addr.clone(); + let path_info_service_addr = args.path_info_service_addr.clone(); + async move { + construct_services( + blob_service_addr, + directory_service_addr, + path_info_service_addr, + ) + .await + } + }) + .expect("unable to setup {blob|directory|pathinfo}service before interpreter setup"); let known_paths: Rc> = Default::default(); add_derivation_builtins(&mut eval, known_paths.clone()); configure_nix_path(&mut eval, &args.nix_search_path); - - let tokio_runtime = tokio::runtime::Runtime::new().unwrap(); eval.io_handle = Box::new(tvix_glue::tvix_io::TvixIO::new( - known_paths.clone(), + known_paths, TvixStoreIO::new( blob_service, directory_service, - path_info_service, + path_info_service.into(), // we need an Arc<_> here. tokio_runtime.handle().clone(), ), )); -- cgit 1.4.1