Skip to content

Commit 75ed489

Browse files
authored
Use YAML for config (#352)
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`.
1 parent af87950 commit 75ed489

File tree

11 files changed

+161
-106
lines changed

11 files changed

+161
-106
lines changed

Cargo.lock

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ serde = { version = "1.0.197", features = ["derive"] }
5050
serde_json = "1.0"
5151
serde_repr = "0.1.18"
5252
serde_with = { version = "3.2", features = ["json"] }
53+
serde_yaml = "0.9"
5354
shell-words = { version = "1.1.0" }
5455
sysinfo = "0.29.10"
5556
syntect = { version = "5.2.0", features = ["default-syntaxes"] }

crates/omnix-ci/src/command/core.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ impl Command {
3939
pub async fn run(self, nixcmd: &NixCmd, verbose: bool) -> anyhow::Result<()> {
4040
tracing::info!("{}", "\n👟 Reading om.ci config from flake".bold());
4141
let url = self.get_flake_ref().to_flake_url().await?;
42-
let cfg = OmConfig::from_flake_url(nixcmd, &url).await?;
42+
let cfg = OmConfig::get(&url).await?;
43+
4344
tracing::debug!("OmConfig: {cfg:?}");
4445
match self {
4546
Command::Run(cmd) => cmd.run(nixcmd, verbose, cfg).await,

crates/omnix-ci/src/config/core.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
#[cfg(test)]
44
mod tests {
5-
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
5+
use nix_rs::flake::url::FlakeUrl;
66
use omnix_common::config::OmConfig;
77

88
use crate::config::subflakes::SubflakesConfig;
@@ -15,9 +15,7 @@ mod tests {
1515
"github:srid/haskell-flake/76214cf8b0d77ed763d1f093ddce16febaf07365#default.dev"
1616
.to_string(),
1717
);
18-
let cfg = OmConfig::from_flake_url(&NixCmd::default(), url)
19-
.await
20-
.unwrap();
18+
let cfg = OmConfig::get(url).await.unwrap();
2119
let (config, attrs) = cfg.get_sub_config_under::<SubflakesConfig>("ci").unwrap();
2220
assert_eq!(attrs, &["dev"]);
2321
// assert_eq!(cfg.selected_subconfig, Some("dev".to_string()));

crates/omnix-cli/src/command/develop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clap::Parser;
2-
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
2+
use nix_rs::flake::url::FlakeUrl;
33
use omnix_common::config::OmConfig;
44

55
/// Prepare to develop on a flake project
@@ -28,7 +28,7 @@ impl DevelopCommand {
2828
pub async fn run(&self) -> anyhow::Result<()> {
2929
let flake = self.flake_shell.without_attr();
3030

31-
let om_config = OmConfig::from_flake_url(NixCmd::get().await, &flake).await?;
31+
let om_config = OmConfig::get(&flake).await?;
3232

3333
tracing::info!("⌨️ Preparing to develop project: {:}", &flake);
3434
let prj = omnix_develop::core::Project::new(flake, om_config).await?;

crates/omnix-common/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ tracing = { workspace = true }
2727
tracing-subscriber = { workspace = true }
2828
which = { workspace = true }
2929
serde_json = { workspace = true }
30+
serde_yaml = { workspace = true }

crates/omnix-common/src/config.rs

+44-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::BTreeMap;
44

55
use nix_rs::{
66
command::NixCmd,
7-
flake::{eval::nix_eval_attr, url::FlakeUrl},
7+
flake::{eval::nix_eval_attr, metadata::FlakeMetadata, url::FlakeUrl},
88
};
99
use serde::{de::DeserializeOwned, Deserialize};
1010

@@ -24,12 +24,44 @@ pub struct OmConfig {
2424
}
2525

2626
impl OmConfig {
27-
/// Read the om configuration from the flake url
28-
pub async fn from_flake_url(cmd: &NixCmd, flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
27+
/// Fetch the `om` configuration from `om.yaml` if present, falling back to `om` config in flake output
28+
pub async fn get(flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
29+
match Self::from_yaml(flake_url).await? {
30+
None => Self::from_flake(flake_url).await,
31+
Some(config) => Ok(config),
32+
}
33+
}
34+
35+
/// Read the configuration from `om.yaml` in flake root
36+
async fn from_yaml(flake_url: &FlakeUrl) -> Result<Option<Self>, OmConfigError> {
37+
let path = if let Some(local_path) = flake_url.without_attr().as_local_path() {
38+
local_path.to_path_buf()
39+
} else {
40+
FlakeMetadata::from_nix(NixCmd::get().await, flake_url)
41+
.await?
42+
.path
43+
}
44+
.join("om.yaml");
45+
46+
if !path.exists() {
47+
return Ok(None);
48+
}
49+
50+
let yaml_str = std::fs::read_to_string(path)?;
51+
let config: OmConfigTree = serde_yaml::from_str(&yaml_str)?;
52+
Ok(Some(OmConfig {
53+
flake_url: flake_url.without_attr(),
54+
reference: flake_url.get_attr().as_list(),
55+
config,
56+
}))
57+
}
58+
59+
/// Read the configuration from `om` flake output
60+
async fn from_flake(flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
2961
Ok(OmConfig {
3062
flake_url: flake_url.without_attr(),
3163
reference: flake_url.get_attr().as_list(),
32-
config: nix_eval_attr(cmd, &flake_url.with_attr("om"))
64+
config: nix_eval_attr(NixCmd::get().await, &flake_url.with_attr("om"))
3365
.await?
3466
.unwrap_or_default(),
3567
})
@@ -108,4 +140,12 @@ pub enum OmConfigError {
108140
/// Failed to parse JSON
109141
#[error("Failed to decode (json error): {0}")]
110142
DecodeErrorJson(#[from] serde_json::Error),
143+
144+
/// Failed to parse yaml
145+
#[error("Failed to parse yaml: {0}")]
146+
ParseYaml(#[from] serde_yaml::Error),
147+
148+
/// Failed to read yaml
149+
#[error("Failed to read yaml: {0}")]
150+
ReadYaml(#[from] std::io::Error),
111151
}

crates/omnix-health/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use colored::Colorize;
1212
use check::direnv::Direnv;
1313
use nix_rs::env::OS;
1414
use nix_rs::flake::url::FlakeUrl;
15-
use nix_rs::{command::NixCmd, info::NixInfo};
15+
use nix_rs::info::NixInfo;
1616
use omnix_common::config::{OmConfig, OmConfigError};
1717
use omnix_common::markdown::render_markdown;
1818
use serde::{Deserialize, Serialize};
@@ -109,7 +109,7 @@ pub async fn run_all_checks_with(flake_url: Option<FlakeUrl>) -> anyhow::Result<
109109

110110
let health: NixHealth = match flake_url.as_ref() {
111111
Some(flake_url) => {
112-
let om_config = OmConfig::from_flake_url(NixCmd::get().await, flake_url).await?;
112+
let om_config = OmConfig::get(flake_url).await?;
113113
NixHealth::from_om_config(&om_config)
114114
}
115115
None => Ok(NixHealth::default()),

crates/omnix-init/src/config.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::{self, Display, Formatter};
22

33
use colored::Colorize;
4-
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
4+
use nix_rs::flake::url::FlakeUrl;
55
use omnix_common::config::OmConfig;
66

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

3434
/// Load templates from the given flake
3535
pub async fn load_templates<'a>(url: &FlakeUrl) -> anyhow::Result<Vec<FlakeTemplate>> {
36-
let om_config = OmConfig::from_flake_url(NixCmd::get().await, url).await?;
36+
let om_config = OmConfig::get(url).await?;
37+
3738
let templates = om_config
3839
.config
3940
.get::<Template>("templates")?

nix/modules/flake-parts/om.nix

-91
This file was deleted.

om.yaml

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
ci:
2+
default:
3+
omnix:
4+
dir: .
5+
steps:
6+
flake-check:
7+
enable: false
8+
custom:
9+
om-show:
10+
type: app
11+
args:
12+
- show
13+
- .
14+
binary-size-is-small:
15+
type: app
16+
name: check-closure-size
17+
systems:
18+
- x86_64-linux
19+
omnix-source-is-buildable:
20+
type: app
21+
name: omnix-source-is-buildable
22+
cargo-tests:
23+
type: devshell
24+
command:
25+
- just
26+
- cargo-test
27+
systems:
28+
- x86_64-linux
29+
- aarch64-darwin
30+
cargo-clippy:
31+
type: devshell
32+
command:
33+
- just
34+
- clippy
35+
systems:
36+
- x86_64-linux
37+
- aarch64-darwin
38+
cargo-doc:
39+
type: devshell
40+
command:
41+
- just
42+
- cargo-doc
43+
systems:
44+
- x86_64-linux
45+
- aarch64-darwin
46+
doc:
47+
dir: doc
48+
registry:
49+
dir: crates/omnix-init/registry
50+
steps:
51+
build:
52+
enable: false
53+
custom: {}
54+
cli-test-dep-cache:
55+
dir: crates/omnix-cli/tests
56+
steps:
57+
lockfile:
58+
enable: false
59+
flake_check:
60+
enable: false
61+
custom: {}
62+
health:
63+
default:
64+
nix-version:
65+
min-required: "2.16.0"
66+
caches:
67+
required:
68+
- https://om.cachix.org
69+
direnv:
70+
required: true
71+
develop:
72+
default:
73+
readme: |
74+
🍾 Welcome to the **omnix** project
75+
76+
To run omnix,
77+
78+
```sh-session
79+
just watch <args>
80+
```
81+
82+
(Now, as you edit the Rust sources, the above will reload!)
83+
84+
🍎🍎 Run 'just' to see more commands. See <https://nixos.asia/en/vscode> for IDE setup.

0 commit comments

Comments
 (0)