Skip to content

Commit

Permalink
Use YAML for config (#352)
Browse files Browse the repository at this point in the history
resolves #343 

If the flake url is a local path and `om.yaml` exists in the project
root, `omnix` will not invoke `nix eval .#om`.
  • Loading branch information
shivaraj-bh authored Nov 28, 2024
1 parent af87950 commit 75ed489
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 106 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0"
serde_repr = "0.1.18"
serde_with = { version = "3.2", features = ["json"] }
serde_yaml = "0.9"
shell-words = { version = "1.1.0" }
sysinfo = "0.29.10"
syntect = { version = "5.2.0", features = ["default-syntaxes"] }
Expand Down
3 changes: 2 additions & 1 deletion crates/omnix-ci/src/command/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ impl Command {
pub async fn run(self, nixcmd: &NixCmd, verbose: bool) -> anyhow::Result<()> {
tracing::info!("{}", "\n👟 Reading om.ci config from flake".bold());
let url = self.get_flake_ref().to_flake_url().await?;
let cfg = OmConfig::from_flake_url(nixcmd, &url).await?;
let cfg = OmConfig::get(&url).await?;

tracing::debug!("OmConfig: {cfg:?}");
match self {
Command::Run(cmd) => cmd.run(nixcmd, verbose, cfg).await,
Expand Down
6 changes: 2 additions & 4 deletions crates/omnix-ci/src/config/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

use crate::config::subflakes::SubflakesConfig;
Expand All @@ -15,9 +15,7 @@ mod tests {
"github:srid/haskell-flake/76214cf8b0d77ed763d1f093ddce16febaf07365#default.dev"
.to_string(),
);
let cfg = OmConfig::from_flake_url(&NixCmd::default(), url)
.await
.unwrap();
let cfg = OmConfig::get(url).await.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()));
Expand Down
4 changes: 2 additions & 2 deletions crates/omnix-cli/src/command/develop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
use nix_rs::flake::url::FlakeUrl;
use omnix_common::config::OmConfig;

/// Prepare to develop on a flake project
Expand Down Expand Up @@ -28,7 +28,7 @@ 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, &flake).await?;
let om_config = OmConfig::get(&flake).await?;

tracing::info!("⌨️ Preparing to develop project: {:}", &flake);
let prj = omnix_develop::core::Project::new(flake, om_config).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 @@ -27,3 +27,4 @@ tracing = { workspace = true }
tracing-subscriber = { workspace = true }
which = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
48 changes: 44 additions & 4 deletions crates/omnix-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::BTreeMap;

use nix_rs::{
command::NixCmd,
flake::{eval::nix_eval_attr, url::FlakeUrl},
flake::{eval::nix_eval_attr, metadata::FlakeMetadata, url::FlakeUrl},
};
use serde::{de::DeserializeOwned, Deserialize};

Expand All @@ -24,12 +24,44 @@ pub struct OmConfig {
}

impl OmConfig {
/// Read the om configuration from the flake url
pub async fn from_flake_url(cmd: &NixCmd, flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
/// Fetch the `om` configuration from `om.yaml` if present, falling back to `om` config in flake output
pub async fn get(flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
match Self::from_yaml(flake_url).await? {
None => Self::from_flake(flake_url).await,
Some(config) => Ok(config),
}
}

/// Read the configuration from `om.yaml` in flake root
async fn from_yaml(flake_url: &FlakeUrl) -> Result<Option<Self>, OmConfigError> {
let path = if let Some(local_path) = flake_url.without_attr().as_local_path() {
local_path.to_path_buf()
} else {
FlakeMetadata::from_nix(NixCmd::get().await, flake_url)
.await?
.path
}
.join("om.yaml");

if !path.exists() {
return Ok(None);
}

let yaml_str = std::fs::read_to_string(path)?;
let config: OmConfigTree = serde_yaml::from_str(&yaml_str)?;
Ok(Some(OmConfig {
flake_url: flake_url.without_attr(),
reference: flake_url.get_attr().as_list(),
config,
}))
}

/// Read the configuration from `om` flake output
async fn from_flake(flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
Ok(OmConfig {
flake_url: flake_url.without_attr(),
reference: flake_url.get_attr().as_list(),
config: nix_eval_attr(cmd, &flake_url.with_attr("om"))
config: nix_eval_attr(NixCmd::get().await, &flake_url.with_attr("om"))
.await?
.unwrap_or_default(),
})
Expand Down Expand Up @@ -108,4 +140,12 @@ pub enum OmConfigError {
/// Failed to parse JSON
#[error("Failed to decode (json error): {0}")]
DecodeErrorJson(#[from] serde_json::Error),

/// Failed to parse yaml
#[error("Failed to parse yaml: {0}")]
ParseYaml(#[from] serde_yaml::Error),

/// Failed to read yaml
#[error("Failed to read yaml: {0}")]
ReadYaml(#[from] std::io::Error),
}
4 changes: 2 additions & 2 deletions crates/omnix-health/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use colored::Colorize;
use check::direnv::Direnv;
use nix_rs::env::OS;
use nix_rs::flake::url::FlakeUrl;
use nix_rs::{command::NixCmd, info::NixInfo};
use nix_rs::info::NixInfo;
use omnix_common::config::{OmConfig, OmConfigError};
use omnix_common::markdown::render_markdown;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -109,7 +109,7 @@ pub async fn run_all_checks_with(flake_url: Option<FlakeUrl>) -> anyhow::Result<

let health: NixHealth = match flake_url.as_ref() {
Some(flake_url) => {
let om_config = OmConfig::from_flake_url(NixCmd::get().await, flake_url).await?;
let om_config = OmConfig::get(flake_url).await?;
NixHealth::from_om_config(&om_config)
}
None => Ok(NixHealth::default()),
Expand Down
5 changes: 3 additions & 2 deletions crates/omnix-init/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::{self, Display, Formatter};

use colored::Colorize;
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
use nix_rs::flake::url::FlakeUrl;
use omnix_common::config::OmConfig;

use crate::template::Template;
Expand Down Expand Up @@ -33,7 +33,8 @@ impl<'a> Display for FlakeTemplate<'a> {

/// Load templates from the given flake
pub async fn load_templates<'a>(url: &FlakeUrl) -> anyhow::Result<Vec<FlakeTemplate>> {
let om_config = OmConfig::from_flake_url(NixCmd::get().await, url).await?;
let om_config = OmConfig::get(url).await?;

let templates = om_config
.config
.get::<Template>("templates")?
Expand Down
91 changes: 0 additions & 91 deletions nix/modules/flake-parts/om.nix

This file was deleted.

84 changes: 84 additions & 0 deletions om.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
ci:
default:
omnix:
dir: .
steps:
flake-check:
enable: false
custom:
om-show:
type: app
args:
- show
- .
binary-size-is-small:
type: app
name: check-closure-size
systems:
- x86_64-linux
omnix-source-is-buildable:
type: app
name: omnix-source-is-buildable
cargo-tests:
type: devshell
command:
- just
- cargo-test
systems:
- x86_64-linux
- aarch64-darwin
cargo-clippy:
type: devshell
command:
- just
- clippy
systems:
- x86_64-linux
- aarch64-darwin
cargo-doc:
type: devshell
command:
- just
- cargo-doc
systems:
- x86_64-linux
- aarch64-darwin
doc:
dir: doc
registry:
dir: crates/omnix-init/registry
steps:
build:
enable: false
custom: {}
cli-test-dep-cache:
dir: crates/omnix-cli/tests
steps:
lockfile:
enable: false
flake_check:
enable: false
custom: {}
health:
default:
nix-version:
min-required: "2.16.0"
caches:
required:
- https://om.cachix.org
direnv:
required: true
develop:
default:
readme: |
🍾 Welcome to the **omnix** project
To run omnix,
```sh-session
just watch <args>
```
(Now, as you edit the Rust sources, the above will reload!)
🍎🍎 Run 'just' to see more commands. See <https://nixos.asia/en/vscode> for IDE setup.

0 comments on commit 75ed489

Please sign in to comment.