From 979ad69132d9dcc0c9460abdcd217dad30fa2cc7 Mon Sep 17 00:00:00 2001 From: Cole Mickens Date: Fri, 30 Aug 2024 09:05:51 -0700 Subject: [PATCH] cli: add defaults for flakehub endpoints, nix.conf path --- Cargo.lock | 1 + magic-nix-cache/Cargo.toml | 1 + magic-nix-cache/src/main.rs | 30 +++++++++++++++++++++--------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e37eff..311858e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2685,6 +2685,7 @@ dependencies = [ "tracing-appender", "tracing-subscriber", "uuid", + "xdg", ] [[package]] diff --git a/magic-nix-cache/Cargo.toml b/magic-nix-cache/Cargo.toml index 2a8c357..69b3149 100644 --- a/magic-nix-cache/Cargo.toml +++ b/magic-nix-cache/Cargo.toml @@ -57,6 +57,7 @@ http = "1.0" http-body-util = "0.1" hyper = { version = "1.0.0", features = ["full"] } hyper-util = { version = "0.1", features = ["tokio", "server-auto", "http1"] } +xdg = { version = "2.5.2" } [dependencies.tokio] version = "1.28.0" diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index 48556f4..424e03f 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -113,7 +113,7 @@ struct Args { /// The location of `nix.conf`. #[arg(long)] - nix_conf: PathBuf, + nix_conf: Option, /// Whether to use the GHA cache. #[arg(long)] @@ -199,16 +199,24 @@ async fn main_cli() -> Result<()> { let dnixd_uds_socket_path = dnixd_uds_socket_dir.join(DETERMINATE_NIXD_SOCKET_NAME); let dnixd_available = dnixd_uds_socket_path.exists(); + let nix_conf_path: PathBuf = match args.nix_conf { + Some(nc) => nc, + None => { + let xdg = xdg::BaseDirectories::new().with_context(|| "failed to get xdg base dirs")?; + xdg.get_config_file("nix/nix.conf") + } + }; + // NOTE: we expect this to point to a user nix.conf // we always open/append to it to be able to append the extra-substituter for github-actions cache // but we don't write to it for initializing flakehub_cache unless dnixd is unavailable - if let Some(parent) = Path::new(&args.nix_conf).parent() { + if let Some(parent) = Path::new(&nix_conf_path).parent() { create_dir_all(parent).with_context(|| "Creating parent directories of nix.conf")?; } let mut nix_conf = std::fs::OpenOptions::new() .create(true) .append(true) - .open(&args.nix_conf) + .open(&nix_conf_path) .with_context(|| "Creating nix.conf")?; let store = Arc::new(NixStore::connect()?); @@ -216,9 +224,10 @@ async fn main_cli() -> Result<()> { let narinfo_negative_cache = Arc::new(RwLock::new(HashSet::new())); let flakehub_state = if args.use_flakehub { - let flakehub_cache_server = args - .flakehub_cache_server - .ok_or_else(|| anyhow!("--flakehub-cache-server is required"))?; + let flakehub_cache_server = args.flakehub_cache_server.unwrap_or( + reqwest::Url::parse("https://cache.flakehub.com") + .expect("failed to parse default flakehub cache url"), + ); let flakehub_api_server_netrc = if dnixd_available { let dnixd_netrc_path = PathBuf::from(DETERMINATE_STATE_DIR).join("netrc"); @@ -231,13 +240,16 @@ async fn main_cli() -> Result<()> { })? }; + let flakehub_api_server = &args.flakehub_api_server.unwrap_or( + reqwest::Url::parse("https://api.flakehub.com") + .expect("failed to parse default flakehub api server"), + ); + let flakehub_flake_name = args.flakehub_flake_name; match flakehub::init_cache( environment, - &args - .flakehub_api_server - .ok_or_else(|| anyhow!("--flakehub-api-server is required"))?, + flakehub_api_server, &flakehub_api_server_netrc, &flakehub_cache_server, flakehub_flake_name,