diff options
author | Vova Kryachko <v.kryachko@gmail.com> | 2024-10-29T18·00-0400 |
---|---|---|
committer | Vladimir Kryachko <v.kryachko@gmail.com> | 2024-10-30T15·00+0000 |
commit | 9e294db8206ee8f8ed99887367222c2197c5c83c (patch) | |
tree | a36c6a2cc819024c0fa8fd12d9970901c2f9c3fc /tvix/nix-daemon/src/bin | |
parent | eb2ca5f07991cc526b22b1701896f40d38c46d46 (diff) |
feat(tvix/nix-daemon): Initial skeleton for nix-daemon r/8878
The goal is to create a drop-in replacement nix-daemon that nix-cpp can use as a `daemon` store. Change-Id: Ie092047dcc6a24a3b8d8d1b808f3e6fd2c493bf2 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12711 Reviewed-by: flokli <flokli@flokli.de> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-daemon/src/bin')
-rw-r--r-- | tvix/nix-daemon/src/bin/nix-daemon.rs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/tvix/nix-daemon/src/bin/nix-daemon.rs b/tvix/nix-daemon/src/bin/nix-daemon.rs new file mode 100644 index 000000000000..769e968309f8 --- /dev/null +++ b/tvix/nix-daemon/src/bin/nix-daemon.rs @@ -0,0 +1,90 @@ +use clap::Parser; +use mimalloc::MiMalloc; +use std::error::Error; +use tokio::io::AsyncWriteExt; +use tokio_listener::SystemOptions; +use tvix_store::utils::{construct_services, ServiceUrlsGrpc}; + +#[global_allocator] +static GLOBAL: MiMalloc = MiMalloc; + +/// Run Nix-compatible store daemon backed by tvix. +#[derive(Parser)] +struct Cli { + #[clap(flatten)] + service_addrs: ServiceUrlsGrpc, + + /// The address to listen on. Must be a unix domain socket. + #[clap(flatten)] + listen_args: tokio_listener::ListenerAddressLFlag, + + #[cfg(feature = "otlp")] + /// Whether to configure OTLP. Set --otlp=false to disable. + #[arg(long, default_missing_value = "true", default_value = "true", num_args(0..=1), require_equals(true), action(clap::ArgAction::Set))] + otlp: bool, +} + +#[tokio::main] +async fn main() -> Result<(), Box<dyn Error + Send + Sync>> { + let cli = Cli::parse(); + + let tracing_handle = { + let mut builder = tvix_tracing::TracingBuilder::default(); + builder = builder.enable_progressbar(); + #[cfg(feature = "otlp")] + { + if cli.otlp { + builder = builder.enable_otlp("tvix.daemon"); + } + } + builder.build()? + }; + + tokio::select! { + res = tokio::signal::ctrl_c() => { + res?; + if let Err(e) = tracing_handle.force_shutdown().await { + eprintln!("failed to shutdown tracing: {e}"); + } + Ok(()) + }, + res = run(cli) => { + if let Err(e) = tracing_handle.shutdown().await { + eprintln!("failed to shutdown tracing: {e}"); + } + res + } + } +} + +async fn run(cli: Cli) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { + let (_blob_service, _directory_service, _path_info_service, _nar_calculation_service) = + construct_services(cli.service_addrs).await?; + + let listen_address = cli.listen_args.listen_address.unwrap_or_else(|| { + "/tmp/tvix-daemon.sock" + .parse() + .expect("invalid fallback listen address") + }); + + let mut listener = tokio_listener::Listener::bind( + &listen_address, + &SystemOptions::default(), + &cli.listen_args.listener_options, + ) + .await?; + + while let Ok((mut connection, _)) = listener.accept().await { + tokio::spawn(async move { + let ucred = connection + .try_borrow_unix() + .and_then(|u| u.peer_cred().ok()); + + // For now we just write the connected process credentials into the connection. + let _ = connection + .write_all(format!("Hello {:?}", ucred).as_bytes()) + .await; + }); + } + Ok(()) +} |