Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Nix configuration path optional #73

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.rs]
indent_size = 4
2 changes: 1 addition & 1 deletion .github/workflows/check-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: nix develop --command cargo fmt --check

- name: Clippy
run: nix develop --command cargo clippy
run: nix develop --command cargo clippy --all --all-targets --all-features -- -Dwarnings

build:
name: Build artifacts
Expand Down
3 changes: 2 additions & 1 deletion magic-nix-cache/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use attic::nix_store::StorePath;
use axum::{extract::Extension, routing::post, Json, Router};
use axum_macros::debug_handler;
use serde::{Deserialize, Serialize};
use tokio::fs::read_to_string;

use super::State;
use crate::error::{Error, Result};
Expand Down Expand Up @@ -114,7 +115,7 @@ async fn workflow_finish(

// NOTE(cole-h): see `init_logging`
if let Some(logfile) = &state.logfile {
let logfile_contents = std::fs::read_to_string(logfile)?;
let logfile_contents = read_to_string(logfile).await?;
println!("Every log line throughout the lifetime of the program:");
println!("\n{logfile_contents}\n");
}
Expand Down
99 changes: 60 additions & 39 deletions magic-nix-cache/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ mod gha;
mod telemetry;
mod util;

use std::borrow::BorrowMut;
use std::collections::HashSet;
use std::fs::{self, create_dir_all};
use std::fs::Permissions;
use std::io::Write;
use std::net::SocketAddr;
use std::os::unix::fs::PermissionsExt;
Expand All @@ -34,7 +35,7 @@ use anyhow::{anyhow, Context, Result};
use axum::{extract::Extension, routing::get, Router};
use clap::Parser;
use tempfile::NamedTempFile;
use tokio::fs::File;
use tokio::fs::{self, create_dir_all, File, OpenOptions};
use tokio::io::AsyncWriteExt;
use tokio::process::Command;
use tokio::sync::{oneshot, Mutex, RwLock};
Expand Down Expand Up @@ -103,7 +104,7 @@ struct Args {

/// The location of `nix.conf`.
#[arg(long)]
nix_conf: PathBuf,
nix_conf: Option<PathBuf>,

/// Whether to use the GHA cache.
#[arg(long)]
Expand Down Expand Up @@ -175,7 +176,7 @@ struct StateInner {
}

async fn main_cli() -> Result<()> {
let guard = init_logging()?;
let guard = init_logging().await?;
let _tracing_guard = guard.appender_guard;

let args = Args::parse();
Expand All @@ -185,15 +186,24 @@ async fn main_cli() -> Result<()> {

let metrics = Arc::new(telemetry::TelemetryReport::new());

if let Some(parent) = Path::new(&args.nix_conf).parent() {
create_dir_all(parent).with_context(|| "Creating parent directories of nix.conf")?;
}
let mut nix_conf_file: Option<File> = if let Some(nix_conf_path) = &args.nix_conf {
if let Some(parent) = Path::new(&nix_conf_path).parent() {
create_dir_all(parent)
.await
.with_context(|| "Creating parent directories of nix.conf")?;
}

let mut nix_conf = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&args.nix_conf)
.with_context(|| "Creating nix.conf")?;
Some(
OpenOptions::new()
.create(true)
.append(true)
.open(nix_conf_path)
.await
.with_context(|| "Creating nix.conf")?,
)
} else {
None
};

let store = Arc::new(NixStore::connect()?);

Expand Down Expand Up @@ -221,16 +231,19 @@ async fn main_cli() -> Result<()> {
.await
{
Ok(state) => {
nix_conf
.write_all(
format!(
"extra-substituters = {}?trusted=1\nnetrc-file = {}\n",
&flakehub_cache_server,
flakehub_api_server_netrc.display()
if let Some(ref mut nix_conf_file) = nix_conf_file {
nix_conf_file
.write_all(
format!(
"extra-substituters = {}?trusted=1\nnetrc-file = {}\n",
&flakehub_cache_server,
flakehub_api_server_netrc.display()
)
.as_bytes(),
)
.as_bytes(),
)
.with_context(|| "Writing to nix.conf")?;
.await
.with_context(|| "Writing to nix.conf")?;
}

tracing::info!("FlakeHub cache is enabled.");
Some(state)
Expand All @@ -250,7 +263,7 @@ async fn main_cli() -> Result<()> {
let gha_cache = if args.use_gha_cache {
let credentials = if let Some(credentials_file) = &args.credentials_file {
tracing::info!("Loading credentials from {:?}", credentials_file);
let bytes = fs::read(credentials_file).with_context(|| {
let bytes = fs::read(credentials_file).await.with_context(|| {
format!(
"Failed to read credentials file '{}'",
credentials_file.display()
Expand Down Expand Up @@ -278,9 +291,12 @@ async fn main_cli() -> Result<()> {
)
.with_context(|| "Failed to initialize GitHub Actions Cache API")?;

nix_conf
.write_all(format!("extra-substituters = http://{}?trusted=1&compression=zstd&parallel-compression=true&priority=1\n", args.listen).as_bytes())
.with_context(|| "Writing to nix.conf")?;
if let Some(ref mut nix_conf_file) = nix_conf_file {
nix_conf_file
.write_all(format!("extra-substituters = http://{}?trusted=1&compression=zstd&parallel-compression=true&priority=1\n", args.listen).as_bytes())
.await
.with_context(|| "Writing to nix.conf")?;
}

tracing::info!("Native GitHub Action cache is enabled.");
Some(gha_cache)
Expand Down Expand Up @@ -315,7 +331,8 @@ async fn main_cli() -> Result<()> {
.with_context(|| "Keeping the post-build hook")?
.1;

fs::set_permissions(&path, fs::Permissions::from_mode(0o755))
fs::set_permissions(&path, Permissions::from_mode(0o755))
.await
.with_context(|| "Setting permissions on the post-build hook")?;

/* Copy the script to the Nix store so we know for sure that
Expand All @@ -340,17 +357,20 @@ async fn main_cli() -> Result<()> {
};

/* Update nix.conf. */
nix_conf
.write_all(
format!(
"fallback = true\npost-build-hook = {}\n",
post_build_hook_script.display()
if let Some(ref mut nix_conf_file) = nix_conf_file.borrow_mut() {
nix_conf_file
.write_all(
format!(
"fallback = true\npost-build-hook = {}\n",
post_build_hook_script.display()
)
.as_bytes(),
)
.as_bytes(),
)
.with_context(|| "Writing to nix.conf")?;
.await
.with_context(|| "Writing to nix.conf")?;
}

drop(nix_conf);
drop(nix_conf_file);

let diagnostic_endpoint = match args.diagnostic_endpoint.as_str() {
"" => {
Expand Down Expand Up @@ -513,7 +533,7 @@ pub struct LogGuard {
logfile: Option<PathBuf>,
}

fn init_logging() -> Result<LogGuard> {
async fn init_logging() -> Result<LogGuard> {
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
#[cfg(debug_assertions)]
return EnvFilter::new("info")
Expand All @@ -531,12 +551,13 @@ fn init_logging() -> Result<LogGuard> {
let (guard, file_layer) = match std::env::var("RUNNER_DEBUG") {
Ok(val) if val == "1" => {
let logfile = debug_logfile();
let file = std::fs::OpenOptions::new()
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&logfile)?;
let (nonblocking, guard) = tracing_appender::non_blocking(file);
.open(&logfile)
.await?;
let (nonblocking, guard) = tracing_appender::non_blocking(file.into_std().await);
let file_layer = tracing_subscriber::fmt::layer()
.with_writer(nonblocking)
.pretty();
Expand Down
Loading