Use correct port and compression for nix copy

This commit is contained in:
Zhaofeng Li 2023-05-19 13:00:36 -06:00
parent ec04905db4
commit fc80531e5e
3 changed files with 25 additions and 8 deletions

View file

@ -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::<Vec<_>>();
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&parallel-compression=true")
.build()
.unwrap()
.to_string()
}

View file

@ -92,6 +92,11 @@ struct StateInner {
/// Set of store path hashes that are not present in GHAC.
narinfo_nagative_cache: RwLock<HashSet<String>>,
/// 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()

View file

@ -38,16 +38,15 @@ pub async fn get_store_paths() -> Result<HashSet<PathBuf>> {
Ok(paths)
}
/// Uploads a list of store paths to the cache.
pub async fn upload_paths(mut paths: Vec<PathBuf>) -> Result<()> {
/// Uploads a list of store paths to a store URI.
pub async fn upload_paths(mut paths: Vec<PathBuf>, 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<PathBuf>) -> 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()