diff --git a/nix-actions-cache/src/api.rs b/nix-actions-cache/src/api.rs index 66a888f..8e2297f 100644 --- a/nix-actions-cache/src/api.rs +++ b/nix-actions-cache/src/api.rs @@ -2,7 +2,9 @@ //! //! This API is intended to be used by nix-installer-action. -use axum::{extract::Extension, routing::post, Json, Router}; +use std::net::SocketAddr; + +use axum::{extract::Extension, routing::post, http::uri::Uri, Json, Router}; use axum_macros::debug_handler; use serde::Serialize; @@ -55,7 +57,8 @@ async fn workflow_finish( .collect::>(); tracing::info!("Pushing {} new paths", new_paths.len()); - upload_paths(new_paths.clone()).await?; + let store_uri = make_store_uri(&state.self_endpoint); + upload_paths(new_paths.clone(), &store_uri).await?; let sender = state.shutdown_sender.lock().await.take().unwrap(); sender.send(()).unwrap(); @@ -66,3 +69,13 @@ async fn workflow_finish( num_new_paths: new_paths.len(), })) } + +fn make_store_uri(self_endpoint: &SocketAddr) -> String { + Uri::builder() + .scheme("http") + .authority(self_endpoint.to_string()) + .path_and_query("/?compression=zstd¶llel-compression=true") + .build() + .unwrap() + .to_string() +} diff --git a/nix-actions-cache/src/main.rs b/nix-actions-cache/src/main.rs index 1e1346c..7105dd5 100644 --- a/nix-actions-cache/src/main.rs +++ b/nix-actions-cache/src/main.rs @@ -92,6 +92,11 @@ struct StateInner { /// Set of store path hashes that are not present in GHAC. narinfo_nagative_cache: RwLock>, + + /// Endpoint of ourselves. + /// + /// This is used by our Action API to invoke `nix copy` to upload new paths. + self_endpoint: SocketAddr, } fn main() { @@ -124,6 +129,7 @@ fn main() { shutdown_sender: Mutex::new(Some(shutdown_sender)), original_paths: Mutex::new(HashSet::new()), narinfo_nagative_cache: RwLock::new(HashSet::new()), + self_endpoint: args.listen.to_owned(), }); let app = Router::new() diff --git a/nix-actions-cache/src/util.rs b/nix-actions-cache/src/util.rs index c5faeab..40c3b5b 100644 --- a/nix-actions-cache/src/util.rs +++ b/nix-actions-cache/src/util.rs @@ -38,16 +38,15 @@ pub async fn get_store_paths() -> Result> { Ok(paths) } -/// Uploads a list of store paths to the cache. -pub async fn upload_paths(mut paths: Vec) -> Result<()> { +/// Uploads a list of store paths to a store URI. +pub async fn upload_paths(mut paths: Vec, store_uri: &str) -> Result<()> { // When the daemon started Nix may not have been installed let env_path = Command::new("sh") .args(&["-lc", "echo $PATH"]) .output() .await? .stdout; - let env_path = String::from_utf8(env_path) - .expect("PATH contains invalid UTF-8"); + let env_path = String::from_utf8(env_path).expect("PATH contains invalid UTF-8"); while !paths.is_empty() { let mut batch = Vec::new(); @@ -63,8 +62,7 @@ pub async fn upload_paths(mut paths: Vec) -> Result<()> { let status = Command::new("nix") .args(&["--extra-experimental-features", "nix-command"]) - // FIXME: Port and compression settings - .args(&["copy", "--to", "http://127.0.0.1:3000"]) + .args(&["copy", "--to", store_uri]) .args(&batch) .env("PATH", &env_path) .status()