Merge pull request #89 from DeterminateSystems/colemickens/mnc-netrc-uds

netrc: fixup handling when using dnixd/uds
This commit is contained in:
Graham Christensen 2024-08-29 23:32:22 -04:00 committed by GitHub
commit b5a094c7a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 37 deletions

View file

@ -1,5 +1,5 @@
use crate::env::Environment; use crate::env::Environment;
use crate::error::{Error, Result}; use crate::error::{self, Error, Result};
use anyhow::Context; use anyhow::Context;
use attic::cache::CacheName; use attic::cache::CacheName;
use attic::nix_store::{NixStore, StorePath}; use attic::nix_store::{NixStore, StorePath};
@ -9,6 +9,7 @@ use attic_client::{
config::ServerConfig, config::ServerConfig,
push::{PushConfig, Pusher}, push::{PushConfig, Pusher},
}; };
use reqwest::header::HeaderValue; use reqwest::header::HeaderValue;
use reqwest::Url; use reqwest::Url;
use serde::Deserialize; use serde::Deserialize;
@ -35,7 +36,16 @@ pub async fn init_cache(
flakehub_cache_server: &Url, flakehub_cache_server: &Url,
flakehub_flake_name: Option<String>, flakehub_flake_name: Option<String>,
store: Arc<NixStore>, store: Arc<NixStore>,
using_dnixd: bool,
) -> Result<State> { ) -> Result<State> {
if using_dnixd {
let dnixd_state_dir: &Path = Path::new(&crate::DETERMINATE_STATE_DIR);
let expected_netrc_path = dnixd_state_dir.join("netrc");
if flakehub_api_server_netrc != expected_netrc_path {
let err = format!("flakehub-api-server-netrc was ({}), expected ({}) since determinate-nixd is available", flakehub_api_server_netrc.display(), expected_netrc_path.display());
return Err(error::Error::Config(err));
}
}
// Parse netrc to get the credentials for api.flakehub.com. // Parse netrc to get the credentials for api.flakehub.com.
let netrc = { let netrc = {
let mut netrc_file = File::open(flakehub_api_server_netrc).await.map_err(|e| { let mut netrc_file = File::open(flakehub_api_server_netrc).await.map_err(|e| {
@ -89,6 +99,7 @@ pub async fn init_cache(
)) ))
})?; })?;
if !using_dnixd {
// Append an entry for the FlakeHub cache server to netrc. // Append an entry for the FlakeHub cache server to netrc.
if !netrc if !netrc
.machines .machines
@ -125,6 +136,7 @@ pub async fn init_cache(
)) ))
})?; })?;
} }
}
let server_config = ServerConfig { let server_config = ServerConfig {
endpoint: flakehub_cache_server.to_string(), endpoint: flakehub_cache_server.to_string(),

View file

@ -219,9 +219,18 @@ async fn main_cli() -> Result<()> {
let flakehub_cache_server = args let flakehub_cache_server = args
.flakehub_cache_server .flakehub_cache_server
.ok_or_else(|| anyhow!("--flakehub-cache-server is required"))?; .ok_or_else(|| anyhow!("--flakehub-cache-server is required"))?;
let flakehub_api_server_netrc = args
.flakehub_api_server_netrc let flakehub_api_server_netrc = if dnixd_available {
.ok_or_else(|| anyhow!("--flakehub-api-server-netrc is required"))?; let dnixd_netrc_path = PathBuf::from(DETERMINATE_STATE_DIR).join("netrc");
args.flakehub_api_server_netrc.unwrap_or(dnixd_netrc_path)
} else {
args.flakehub_api_server_netrc.ok_or_else(|| {
anyhow!(
"--flakehub-api-server-netrc is required when determinate-nixd is unavailable"
)
})?
};
let flakehub_flake_name = args.flakehub_flake_name; let flakehub_flake_name = args.flakehub_flake_name;
match flakehub::init_cache( match flakehub::init_cache(
@ -233,6 +242,7 @@ async fn main_cli() -> Result<()> {
&flakehub_cache_server, &flakehub_cache_server,
flakehub_flake_name, flakehub_flake_name,
store.clone(), store.clone(),
dnixd_available,
) )
.await .await
{ {