From 3fd6eeb2083e52bc16a2ab5f410ba5df439f9d62 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Tue, 5 Nov 2024 21:11:26 -0500 Subject: [PATCH 1/5] Make the FlakeHubArg a generic Trinary so we can use it for GHA Cache too --- magic-nix-cache/src/main.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index 3b06ed5..1991e2a 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -114,7 +114,7 @@ struct Args { /// Whether to use the FlakeHub binary cache. #[arg(long)] - use_flakehub: Option>, + use_flakehub: Option>, /// URL to which to post startup notification. #[arg(long)] @@ -130,17 +130,17 @@ struct Args { } #[derive(Debug, Clone, Copy, PartialEq, clap::ValueEnum)] -pub enum FlakeHubArg { +pub enum CacheTrinary { NoPreference, Enabled, Disabled, } -impl From>> for FlakeHubArg { - fn from(b: Option>) -> Self { +impl From>> for CacheTrinary { + fn from(b: Option>) -> Self { match b { - None => FlakeHubArg::NoPreference, - Some(None) => FlakeHubArg::Enabled, + None => CacheTrinary::NoPreference, + Some(None) => CacheTrinary::Enabled, Some(Some(v)) => v, } } @@ -170,7 +170,7 @@ impl Args { ))); } - if environment.is_gitlab_ci() && self.flakehub_preference() != FlakeHubArg::Enabled { + if environment.is_gitlab_ci() && self.flakehub_preference() != CacheTrinary::Enabled { return Err(error::Error::Config(String::from( "you must set --use-flakehub in GitLab CI", ))); @@ -179,7 +179,7 @@ impl Args { Ok(()) } - fn flakehub_preference(&self) -> FlakeHubArg { + fn flakehub_preference(&self) -> CacheTrinary { self.use_flakehub.into() } } @@ -287,17 +287,17 @@ async fn main_cli() -> Result<()> { dnixd_available, ) { // User has explicitly pyassed --use-flakehub=disabled, so just straight up don't - (FlakeHubArg::Disabled, _, _) => { + (CacheTrinary::Disabled, _, _) => { tracing::info!("Disabling FlakeHub cache."); None } // User has no preference, did not pass a netrc, and determinate-nixd is not available - (FlakeHubArg::NoPreference, None, Dnixd::Missing) => None, + (CacheTrinary::NoPreference, None, Dnixd::Missing) => None, // Use it when determinate-nixd is available, and let the user know what's going on (pref, user_netrc_path, Dnixd::Available) => { - if pref == FlakeHubArg::NoPreference { + if pref == CacheTrinary::NoPreference { tracing::info!("Enabling FlakeHub cache because determinate-nixd is available."); } @@ -312,7 +312,7 @@ async fn main_cli() -> Result<()> { (_, Some(path), Dnixd::Missing) => Some(FlakeHubAuthSource::Netrc(path.to_owned())), // User explicitly turned on flakehub cache, but we have no netrc and determinate-nixd is not present - (FlakeHubArg::Enabled, None, Dnixd::Missing) => { + (CacheTrinary::Enabled, None, Dnixd::Missing) => { return Err(anyhow!( "--flakehub-api-server-netrc is required when determinate-nixd is unavailable" )); From 65060bc705494d81549795f115de5dab279365c0 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Tue, 5 Nov 2024 21:19:18 -0500 Subject: [PATCH 2/5] Switch the GHA Caceh preference to a trinary, but treat it as a straight bool for the moment --- magic-nix-cache/src/flakehub.rs | 2 +- magic-nix-cache/src/main.rs | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/magic-nix-cache/src/flakehub.rs b/magic-nix-cache/src/flakehub.rs index c06b244..9d3240a 100644 --- a/magic-nix-cache/src/flakehub.rs +++ b/magic-nix-cache/src/flakehub.rs @@ -33,7 +33,7 @@ pub async fn init_cache( environment: Environment, flakehub_api_server: &Url, flakehub_cache_server: &Url, - flakehub_flake_name: Option, + flakehub_flake_name: &Option, store: Arc, auth_method: &super::FlakeHubAuthSource, ) -> Result { diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index 1991e2a..0b62a72 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -110,7 +110,7 @@ struct Args { /// Whether to use the GHA cache. #[arg(long)] - use_gha_cache: bool, + use_gha_cache: Option>, /// Whether to use the FlakeHub binary cache. #[arg(long)] @@ -164,7 +164,7 @@ impl From for Dnixd { impl Args { fn validate(&self, environment: env::Environment) -> Result<(), error::Error> { - if environment.is_gitlab_ci() && self.use_gha_cache { + if environment.is_gitlab_ci() && self.github_cache_preference() == CacheTrinary::Enabled { return Err(error::Error::Config(String::from( "the --use-gha-cache flag should not be applied in GitLab CI", ))); @@ -179,6 +179,10 @@ impl Args { Ok(()) } + fn github_cache_preference(&self) -> CacheTrinary { + self.use_gha_cache.into() + } + fn flakehub_preference(&self) -> CacheTrinary { self.use_flakehub.into() } @@ -320,16 +324,16 @@ async fn main_cli() -> Result<()> { }; let flakehub_state = if let Some(auth_method) = flakehub_auth_method { - let flakehub_cache_server = args.flakehub_cache_server; + let flakehub_cache_server = &args.flakehub_cache_server; let flakehub_api_server = &args.flakehub_api_server; - let flakehub_flake_name = args.flakehub_flake_name; + let flakehub_flake_name = &args.flakehub_flake_name; match flakehub::init_cache( environment, flakehub_api_server, - &flakehub_cache_server, + flakehub_cache_server, flakehub_flake_name, store.clone(), &auth_method, @@ -363,7 +367,7 @@ async fn main_cli() -> Result<()> { None }; - let gha_cache = if args.use_gha_cache { + let gha_cache = if args.github_cache_preference() != CacheTrinary::Disabled { tracing::info!("Loading credentials from environment"); let credentials = Credentials::load_from_env() From bf844027bc25fddfeabcb617ce109227875c7e39 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Tue, 5 Nov 2024 21:22:38 -0500 Subject: [PATCH 3/5] Turn off the GitHub actions cache if the user expresses no preference, and flakehub cache is in use --- magic-nix-cache/src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index 0b62a72..97ae9c9 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -367,7 +367,10 @@ async fn main_cli() -> Result<()> { None }; - let gha_cache = if args.github_cache_preference() != CacheTrinary::Disabled { + let gha_cache = if (args.github_cache_preference() == CacheTrinary::Enabled) + || (args.github_cache_preference() == CacheTrinary::NoPreference + && flakehub_state.is_none()) + { tracing::info!("Loading credentials from environment"); let credentials = Credentials::load_from_env() From a68e1c4d54f91ba475a0ec8a5defc3864cdc7f2e Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Tue, 5 Nov 2024 22:27:17 -0500 Subject: [PATCH 4/5] Test the patched action --- .github/workflows/check-and-test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check-and-test.yaml b/.github/workflows/check-and-test.yaml index 116a6d4..0a5ba69 100644 --- a/.github/workflows/check-and-test.yaml +++ b/.github/workflows/check-and-test.yaml @@ -72,7 +72,7 @@ jobs: uses: DeterminateSystems/nix-installer-action@main - name: Test magic-nix-cache-action@main on ${{ matrix.systems.runner }} - uses: DeterminateSystems/magic-nix-cache-action@main + uses: DeterminateSystems/magic-nix-cache-action@graham/fh-433-magic-nix-cache-should-disable-github-actions-cache-if with: source-binary: "${{ env.ARTIFACT_KEY }}/${{ env.ARCHIVE_NAME }}" _internal-strict-mode: true From d1983bbdff49902bde41d7ff020db257438044a6 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Wed, 6 Nov 2024 09:47:44 -0500 Subject: [PATCH 5/5] Don't try to use the netrc if itdoesn't exist --- magic-nix-cache/src/main.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index 97ae9c9..9a2b9b2 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -313,7 +313,14 @@ async fn main_cli() -> Result<()> { } // When determinate-nixd is not available, but the user specified a netrc - (_, Some(path), Dnixd::Missing) => Some(FlakeHubAuthSource::Netrc(path.to_owned())), + (_, Some(path), Dnixd::Missing) => { + if path.exists() { + Some(FlakeHubAuthSource::Netrc(path.to_owned())) + } else { + tracing::debug!(path = %path.display(), "User-provided netrc does not exist"); + None + } + } // User explicitly turned on flakehub cache, but we have no netrc and determinate-nixd is not present (CacheTrinary::Enabled, None, Dnixd::Missing) => {