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

Read configuration once only #341

Merged
merged 25 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a7ddce8
PoC: Demonstrate reusing om config in `om develop`
shivaraj-bh Nov 8, 2024
e11ba56
needless borrow
shivaraj-bh Nov 8, 2024
f0e792f
Project takes the ownership of OmnixConfig
shivaraj-bh Nov 11, 2024
8fb2156
Switch all sub-commands to use the new OmConfig
shivaraj-bh Nov 11, 2024
b9723ab
use get_sub_config while decoding DevelopConfig
shivaraj-bh Nov 11, 2024
68bb8f6
nix_rs: Remove `qualified_attr` module
shivaraj-bh Nov 12, 2024
1ec3c4f
history: document that we no longer support nix-health and nixci flak…
shivaraj-bh Nov 12, 2024
3ace4a0
get_sub_config -> get_sub_configs
shivaraj-bh Nov 12, 2024
1cb180a
refac
shivaraj-bh Nov 12, 2024
2a9c919
`om ci`: inline `get_config`
shivaraj-bh Nov 12, 2024
4d629fc
Merge branch 'main' into reuse-om-config
srid Nov 12, 2024
30aee76
Avoid clone when possible
srid Nov 12, 2024
73c4d0a
Decouple `Config` from `OmConfig`
shivaraj-bh Nov 14, 2024
a68d5d1
Merge branch 'reuse-om-config' of github.com:juspay/omnix into reuse-…
shivaraj-bh Nov 14, 2024
6d9a2d5
Merge branch 'main' into reuse-om-config
srid Nov 14, 2024
14c3e85
Remove wrapper without purpose
srid Nov 14, 2024
5973e07
Re-order (outer should be before inner)
srid Nov 14, 2024
962858d
rename to be specialized
srid Nov 14, 2024
66516f6
rename and document this function so it can be understood without
srid Nov 14, 2024
4902794
redundant orchestration; remove, leveraging error conversion
srid Nov 14, 2024
1a01745
revert changed semantics of OmConfig::flake_url
srid Nov 14, 2024
a3b6187
explain this logic
srid Nov 14, 2024
2039975
Result should generally be the outer type, to allow working with ? op…
srid Nov 14, 2024
56f163b
use if..else.. it is clarifying
srid Nov 14, 2024
a49b5e6
vastly simplify and fix a logic issue
srid Nov 14, 2024
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
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.

23 changes: 8 additions & 15 deletions crates/omnix-ci/src/command/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
use clap::Subcommand;
use colored::Colorize;
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 +37,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 = crate::config::core::om_config_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_referenced_for::<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_referenced_for::<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
20 changes: 6 additions & 14 deletions crates/omnix-ci/src/config/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ 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:
Expand All @@ -13,21 +11,15 @@ use super::subflakes::SubflakesConfig;
/// 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?;
pub async fn om_config_from_flake_url(cmd: &NixCmd, url: &FlakeUrl) -> Result<OmConfig> {
let v = omnix_common::config::OmConfig::from_flake_url(cmd, url).await?;
Ok(v)
}

#[cfg(test)]
mod tests {
use crate::config::subflakes::SubflakesConfig;

use super::*;

#[tokio::test]
Expand All @@ -38,10 +30,10 @@ mod tests {
"github:srid/haskell-flake/76214cf8b0d77ed763d1f093ddce16febaf07365#default.dev"
.to_string(),
);
let cfg = ci_config_from_flake_url(&NixCmd::default(), url)
let cfg = om_config_from_flake_url(&NixCmd::default(), url)
.await
.unwrap();
let (config, attrs) = cfg.get_referenced().unwrap();
let (config, attrs) = cfg.get_referenced_for::<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