Skip to content

Commit

Permalink
Read configuration once only (#341)
Browse files Browse the repository at this point in the history
Before
```sh
⌨️  Preparing to develop project: .
❄️  nix --version️
❄️  nix --extra-experimental-features nix-command show-config --json️
❄️  nix eval '.#om.develop' --json️
❄️  nix eval '.#om.health' --json️
✅ Nix environment is healthy.

🍾 Welcome to the omnix project
```
After
```sh
❯ just run develop
❄️  nix --version️
❄️  nix --extra-experimental-features nix-command show-config --json️
❄️  nix eval '.#om' --json️
⌨️  Preparing to develop project: .
✅ Nix environment is healthy.

🍾 Welcome to the omnix project
```

Notice that we only evaluate the `om` config once in: `nix eval '.#om'
--json️`

---------

Co-authored-by: Sridhar Ratnakumar <[email protected]>
Co-authored-by: Sridhar Ratnakumar <[email protected]>
  • Loading branch information
3 people authored Nov 14, 2024
1 parent 0ed2a38 commit 4ef869e
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 203 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/nix_rs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- **`flake::url`**:
- Remove `qualified_attr` module
- **`eval::nix_eval`**
- Display evaluation progress
- Decrease logging verbosity
Expand Down
1 change: 0 additions & 1 deletion crates/nix_rs/src/flake/url/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Work with flake URLs
pub mod attr;
mod core;
pub mod qualified_attr;

pub use core::*;
49 changes: 0 additions & 49 deletions crates/nix_rs/src/flake/url/qualified_attr.rs

This file was deleted.

22 changes: 8 additions & 14 deletions crates/omnix-ci/src/command/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nix_rs::command::NixCmd;
use omnix_common::config::OmConfig;
use tracing::instrument;

use crate::{config::subflakes::SubflakesConfig, flake_ref::FlakeRef};
use crate::flake_ref::FlakeRef;

use super::{gh_matrix::GHMatrixCommand, run::RunCommand};

Expand Down Expand Up @@ -38,26 +38,20 @@ impl Command {
#[instrument(name = "run", skip(self))]
pub async fn run(self, nixcmd: &NixCmd, verbose: bool) -> anyhow::Result<()> {
tracing::info!("{}", "\n👟 Reading om.ci config from flake".bold());
let cfg = self.get_config(nixcmd).await?;
let url = self.get_flake_ref().to_flake_url().await?;
let cfg = OmConfig::from_flake_url(nixcmd, &url).await?;
tracing::debug!("OmConfig: {cfg:?}");
match self {
Command::Run(cmd) => cmd.run(nixcmd, verbose, cfg).await,
Command::DumpGithubActionsMatrix(cmd) => cmd.run(cfg).await,
}
}

/// Get the omnix-ci [config::Config] associated with this subcommand
async fn get_config(&self, cmd: &NixCmd) -> anyhow::Result<OmConfig<SubflakesConfig>> {
let url = self.get_flake_ref().to_flake_url().await?;
let cfg = crate::config::core::ci_config_from_flake_url(cmd, &url).await?;
tracing::debug!("Config: {cfg:?}");
Ok(cfg)
}

/// Get the flake ref associated with this subcommand
fn get_flake_ref(&self) -> FlakeRef {
/// Get the [FlakeRef] associated with this subcommand
fn get_flake_ref(&self) -> &FlakeRef {
match self {
Command::Run(cmd) => cmd.flake_ref.clone(),
Command::DumpGithubActionsMatrix(cmd) => cmd.flake_ref.clone(),
Command::Run(cmd) => &cmd.flake_ref,
Command::DumpGithubActionsMatrix(cmd) => &cmd.flake_ref,
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/omnix-ci/src/command/gh_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub struct GHMatrixCommand {

impl GHMatrixCommand {
/// Run the command
pub async fn run(&self, cfg: OmConfig<SubflakesConfig>) -> anyhow::Result<()> {
let (config, _rest) = cfg.get_referenced()?;
pub async fn run(&self, cfg: OmConfig) -> anyhow::Result<()> {
let (config, _rest) = cfg.get_sub_config_under::<SubflakesConfig>("ci")?;
let matrix = github::matrix::GitHubMatrix::from(self.systems.clone(), &config);
println!("{}", serde_json::to_string(&matrix)?);
Ok(())
Expand Down
29 changes: 11 additions & 18 deletions crates/omnix-ci/src/command/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,15 @@ impl RunCommand {
}

/// Run the build command which decides whether to do ci run on current machine or a remote machine
pub async fn run(
&self,
nixcmd: &NixCmd,
verbose: bool,
cfg: OmConfig<SubflakesConfig>,
) -> anyhow::Result<()> {
pub async fn run(&self, nixcmd: &NixCmd, verbose: bool, cfg: OmConfig) -> anyhow::Result<()> {
match &self.on {
Some(store_uri) => run_remote::run_on_remote_store(nixcmd, self, &cfg, store_uri).await,
None => self.run_local(nixcmd, verbose, cfg).await,
}
}

/// Run [RunCommand] on local Nix store.
async fn run_local(
&self,
nixcmd: &NixCmd,
verbose: bool,
cfg: OmConfig<SubflakesConfig>,
) -> anyhow::Result<()> {
async fn run_local(&self, nixcmd: &NixCmd, verbose: bool, cfg: OmConfig) -> anyhow::Result<()> {
// TODO: We'll refactor this function to use steps
// https://github.com/juspay/omnix/issues/216

Expand All @@ -95,7 +85,7 @@ impl RunCommand {

// First, run the necessary health checks
tracing::info!("{}", "\n🫀 Performing health check".bold());
check_nix_version(&cfg.flake_url, nix_info).await?;
check_nix_version(&cfg, nix_info).await?;

// Then, do the CI steps
tracing::info!(
Expand Down Expand Up @@ -153,9 +143,11 @@ impl RunCommand {
}

/// Check that Nix version is not too old.
pub async fn check_nix_version(flake_url: &FlakeUrl, nix_info: &NixInfo) -> anyhow::Result<()> {
let omnix_health = NixHealth::from_flake(flake_url).await?;
let checks = omnix_health.nix_version.check(nix_info, Some(flake_url));
pub async fn check_nix_version(cfg: &OmConfig, nix_info: &NixInfo) -> anyhow::Result<()> {
let omnix_health = NixHealth::from_om_config(cfg)?;
let checks = omnix_health
.nix_version
.check(nix_info, Some(&cfg.flake_url));
let exit_code = NixHealth::print_report_returning_exit_code(&checks).await?;

if exit_code != 0 {
Expand All @@ -169,13 +161,14 @@ pub async fn ci_run(
cmd: &NixCmd,
verbose: bool,
run_cmd: &RunCommand,
cfg: &OmConfig<SubflakesConfig>,
cfg: &OmConfig,
nix_config: &NixConfig,
) -> anyhow::Result<RunResult> {
let mut res = HashMap::new();
let systems = run_cmd.get_systems(cmd, nix_config).await?;

let (config, attrs) = cfg.get_referenced()?;
let (config, attrs) = cfg.get_sub_config_under::<SubflakesConfig>("ci")?;

// User's filter by subflake name
let only_subflake = attrs.first();

Expand Down
9 changes: 2 additions & 7 deletions crates/omnix-ci/src/command/run_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use omnix_common::config::OmConfig;
use std::path::PathBuf;
use tokio::process::Command;

use crate::config::subflakes::SubflakesConfig;

use super::run::RunCommand;

/// Path to Rust source corresponding to this (running) instance of Omnix
Expand All @@ -21,7 +19,7 @@ const OMNIX_SOURCE: &str = env!("OMNIX_SOURCE");
pub async fn run_on_remote_store(
nixcmd: &NixCmd,
run_cmd: &RunCommand,
cfg: &OmConfig<SubflakesConfig>,
cfg: &OmConfig,
store_uri: &StoreURI,
) -> anyhow::Result<()> {
tracing::info!(
Expand All @@ -48,10 +46,7 @@ pub async fn run_on_remote_store(
}

/// Return the locally cached [FlakeUrl] for the given flake url that points to same selected [ConfigRef].
async fn cache_flake(
nixcmd: &NixCmd,
cfg: &OmConfig<SubflakesConfig>,
) -> anyhow::Result<(PathBuf, FlakeUrl)> {
async fn cache_flake(nixcmd: &NixCmd, cfg: &OmConfig) -> anyhow::Result<(PathBuf, FlakeUrl)> {
let metadata = FlakeMetadata::from_nix(nixcmd, &cfg.flake_url).await?;
let path = metadata.path.to_string_lossy().into_owned();
let attr = cfg.reference.join(".");
Expand Down
35 changes: 6 additions & 29 deletions crates/omnix-ci/src/config/core.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
//! The top-level configuration of omnix-ci, as defined in flake.nix
use anyhow::Result;
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
use omnix_common::config::OmConfig;

use super::subflakes::SubflakesConfig;

/// Create a `Config` pointed to by this [FlakeUrl]
///
/// Example:
/// ```text
/// let url = FlakeUrl("github:srid/haskell-flake#default.dev".to_string());
/// let cfg = Config::from_flake_url(&url).await?;
/// ```
/// along with the config.
pub async fn ci_config_from_flake_url(
cmd: &NixCmd,
url: &FlakeUrl,
) -> Result<OmConfig<SubflakesConfig>> {
let v = omnix_common::config::OmConfig::<SubflakesConfig>::from_flake_url(
cmd,
url,
&["om.ci", "nixci"],
)
.await?;
Ok(v)
}

#[cfg(test)]
mod tests {
use super::*;
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
use omnix_common::config::OmConfig;

use crate::config::subflakes::SubflakesConfig;

#[tokio::test]
async fn test_config_loading() {
Expand All @@ -38,10 +15,10 @@ mod tests {
"github:srid/haskell-flake/76214cf8b0d77ed763d1f093ddce16febaf07365#default.dev"
.to_string(),
);
let cfg = ci_config_from_flake_url(&NixCmd::default(), url)
let cfg = OmConfig::from_flake_url(&NixCmd::default(), url)
.await
.unwrap();
let (config, attrs) = cfg.get_referenced().unwrap();
let (config, attrs) = cfg.get_sub_config_under::<SubflakesConfig>("ci").unwrap();
assert_eq!(attrs, &["dev"]);
// assert_eq!(cfg.selected_subconfig, Some("dev".to_string()));
assert_eq!(config.0.len(), 7);
Expand Down
8 changes: 6 additions & 2 deletions crates/omnix-cli/src/command/develop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Parser;
use nix_rs::flake::url::FlakeUrl;
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
use omnix_common::config::OmConfig;

/// Prepare to develop on a flake project
#[derive(Parser, Debug)]
Expand All @@ -26,8 +27,11 @@ enum Stage {
impl DevelopCommand {
pub async fn run(&self) -> anyhow::Result<()> {
let flake = self.flake_shell.without_attr();

let om_config = OmConfig::from_flake_url(NixCmd::get().await, &self.flake_shell).await?;

tracing::info!("⌨️ Preparing to develop project: {:}", &flake);
let prj = omnix_develop::core::Project::new(flake).await?;
let prj = omnix_develop::core::Project::new(flake, om_config).await?;
match self.stage {
Some(Stage::PreShell) => omnix_develop::core::develop_on_pre_shell(&prj).await?,
Some(Stage::PostShell) => omnix_develop::core::develop_on_post_shell(&prj).await?,
Expand Down
1 change: 1 addition & 0 deletions crates/omnix-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
which = { workspace = true }
serde_json = { workspace = true }
Loading

0 comments on commit 4ef869e

Please sign in to comment.