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

health: Recognize om.health from flake.nix #159

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 10 additions & 6 deletions crates/nix_health/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ impl NixHealth {
/// Fallback to using the default health check config if the flake doesn't
/// override it.
pub async fn from_flake(url: &FlakeUrl) -> Result<Self, nix_rs::command::NixCmdError> {
nix_eval_attr_json(
&NixCmd::default(),
&url.with_fully_qualified_root_attr("nix-health"),
true,
)
.await
let cmd = NixCmd::default();
let v = nix_eval_attr_json(&cmd, &url.with_fully_qualified_root_attr("om.health")).await?;
match v {
Some(v) => Ok(v),
None => {
let v = nix_eval_attr_json(&cmd, &url.with_fully_qualified_root_attr("nix-health"))
.await?;
Ok(v.unwrap_or_default())
}
}
}

/// Run all checks and collect the results
Expand Down
5 changes: 5 additions & 0 deletions crates/nix_rs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- **eval**
- `nix_eval_attr_json`: No longer takes `default_if_missing`; instead (always) returns `None` if attribute is missing.

## [0.5.0](https://github.com/juspay/nix-rs/compare/0.4.0...0.5.0) (2024-06-05)

### Features
Expand Down
17 changes: 6 additions & 11 deletions crates/nix_rs/src/flake/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@ use super::url::FlakeUrl;

/// Run `nix eval <url> --json` and parse its JSON
///
/// If the flake does not output the given attribute, return the [Default]
/// value of `T`.
pub async fn nix_eval_attr_json<T>(
cmd: &NixCmd,
url: &FlakeUrl,
default_if_missing: bool,
) -> Result<T, NixCmdError>
/// If the attribute is missing, return None.
pub async fn nix_eval_attr_json<T>(cmd: &NixCmd, url: &FlakeUrl) -> Result<Option<T>, NixCmdError>
where
T: Default + serde::de::DeserializeOwned,
{
let result = cmd
.run_with_args_expecting_json(&["eval", url.0.as_str(), "--json"])
.await;
match result {
Err(err) if default_if_missing && error_is_missing_attribute(&err) => {
// The 'nixci' flake output attr is missing. User wants the default config.
Ok(T::default())
Ok(v) => Ok(Some(v)),
Err(err) if error_is_missing_attribute(&err) => {
Ok(None) // Attr is missing
}
r => r,
Err(err) => Err(err),
}
}

Expand Down
5 changes: 2 additions & 3 deletions crates/omnix-cli/src/command/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use nix_rs::flake::url::FlakeUrl;

#[derive(Parser, Debug)]
pub struct HealthConfig {
/// Include health checks defined in the given flake
/// Use `om.health` configuration from the given flake
#[arg(name = "FLAKE")]
pub flake_url: Option<FlakeUrl>,

/// Dump the config schema of the health checks (useful when adding them to
Expand All @@ -19,8 +20,6 @@ impl HealthConfig {
tracing::info!("{}", NixHealth::schema()?);
return Ok(());
}
// TODO: Setup logging (unify `crates/nix_health/src/logging.rs` and `crates/omnix/src/logging.rs`)
// TODO: `om.health` config?
let checks = run_checks_with(self.flake_url.clone()).await?;
let exit_code = NixHealth::print_report_returning_exit_code(&checks);
if exit_code != 0 {
Expand Down
2 changes: 1 addition & 1 deletion crates/omnix-cli/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Command {
/// Build all flake outputs (run CI locally)
CI(ci::CIConfig),

/// Show Nix's health
/// Display the health of your system environment as suited for Nix
Health(health::HealthConfig),
}

Expand Down
5 changes: 3 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
./crates/nix_health/module/flake-module.nix
];

flake = {
nix-health.default = {
# omnix configuration
flake.om = {
health.default = {
nix-version.min-required = "2.16.0";
# We don't use a Nix cache yet
# caches.required = [ "https://cache.juspay.dev" ];
Expand Down