Skip to content

Commit

Permalink
Use YAML for config
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaraj-bh committed Nov 25, 2024
1 parent af87950 commit 7d3e496
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 10 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
7 changes: 5 additions & 2 deletions crates/omnix-ci/src/command/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use clap::Subcommand;
use colored::Colorize;
use nix_rs::command::NixCmd;
use omnix_common::config::OmConfig;
use omnix_common::config::{OmConfig, OmConfigError};
use tracing::instrument;

use crate::flake_ref::FlakeRef;
Expand Down Expand Up @@ -39,7 +39,10 @@ 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 = match OmConfig::from_yaml(nixcmd, &url).await {
Err(OmConfigError::ReadYaml(_)) => OmConfig::from_flake(nixcmd, &url).await,
other => other,
}?;
tracing::debug!("OmConfig: {cfg:?}");
match self {
Command::Run(cmd) => cmd.run(nixcmd, verbose, cfg).await,
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,6 +1,6 @@
use clap::Parser;
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
use omnix_common::config::OmConfig;
use omnix_common::config::{OmConfig, OmConfigError};

/// Prepare to develop on a flake project
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -28,7 +28,11 @@ 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 nix_cmd = NixCmd::get().await;
let om_config = match OmConfig::from_yaml(nix_cmd, &flake).await {
Err(OmConfigError::ReadYaml(_)) => OmConfig::from_flake(nix_cmd, &flake).await,
other => other,
}?;

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 }
32 changes: 29 additions & 3 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,8 +24,26 @@ 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> {
/// Read the configuration from `om.yaml` in flake root
pub async fn from_yaml(cmd: &NixCmd, flake_url: &FlakeUrl) -> Result<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(cmd, flake_url).await?.path
}
.join("om.yaml");

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

/// Read the configuration from `om` flake output
pub async fn from_flake(cmd: &NixCmd, flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
Ok(OmConfig {
flake_url: flake_url.without_attr(),
reference: flake_url.get_attr().as_list(),
Expand Down Expand Up @@ -108,4 +126,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),
}
6 changes: 5 additions & 1 deletion crates/omnix-health/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ 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 nix_cmd = NixCmd::get().await;
let om_config = match OmConfig::from_yaml(nix_cmd, flake_url).await {
Err(OmConfigError::ReadYaml(_)) => OmConfig::from_flake(nix_cmd, flake_url).await,
other => other,
}?;
NixHealth::from_om_config(&om_config)
}
None => Ok(NixHealth::default()),
Expand Down
8 changes: 6 additions & 2 deletions crates/omnix-init/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::{self, Display, Formatter};

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

use crate::template::Template;

Expand Down Expand Up @@ -33,7 +33,11 @@ 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 nix_cmd = NixCmd::get().await;
let om_config = match OmConfig::from_yaml(nix_cmd, url).await {
Err(OmConfigError::ReadYaml(_)) => OmConfig::from_flake(nix_cmd, url).await,
other => other,
}?;
let templates = om_config
.config
.get::<Template>("templates")?
Expand Down
85 changes: 85 additions & 0 deletions om.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
om:
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 7d3e496

Please sign in to comment.