diff --git a/crates/nix_health/src/check/caches.rs b/crates/nix_health/src/check/caches.rs index e22de59f..d88df84e 100644 --- a/crates/nix_health/src/check/caches.rs +++ b/crates/nix_health/src/check/caches.rs @@ -5,23 +5,29 @@ use url::Url; use crate::traits::*; /// Check that [nix_rs::config::NixConfig::substituters] is set to a good value. -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[serde(rename_all = "kebab-case")] pub struct Caches { + #[serde(default = "default_caches")] pub required_caches: Vec, } impl Default for Caches { fn default() -> Self { Caches { - required_caches: vec![ - Url::parse("https://cache.nixos.org").unwrap(), - // TODO: Hardcoding this for now, so as to test failed reports - Url::parse("https://nix-community.cachix.org").unwrap(), - ], + required_caches: default_caches(), } } } +fn default_caches() -> Vec { + vec![ + Url::parse("https://cache.nixos.org").unwrap(), + // TODO: Hardcoding this for now, so as to test failed reports + Url::parse("https://nix-community.cachix.org").unwrap(), + ] +} + impl Checkable for Caches { fn check(&self, nix_info: &info::NixInfo, _nix_env: &env::NixEnv) -> Option { let val = &nix_info.nix_config.substituters.value; diff --git a/crates/nix_health/src/check/flake_enabled.rs b/crates/nix_health/src/check/flake_enabled.rs index 2aabe329..c61a7173 100644 --- a/crates/nix_health/src/check/flake_enabled.rs +++ b/crates/nix_health/src/check/flake_enabled.rs @@ -5,7 +5,7 @@ use crate::traits::*; /// Check that [nix_rs::config::NixConfig::experimental_features] is set to a good value. #[derive(Debug, Default, Serialize, Deserialize, Clone)] -pub struct FlakeEnabled(); +pub struct FlakeEnabled {} impl Checkable for FlakeEnabled { fn check(&self, nix_info: &info::NixInfo, _nix_env: &env::NixEnv) -> Option { diff --git a/crates/nix_health/src/check/max_jobs.rs b/crates/nix_health/src/check/max_jobs.rs index 76b0c996..732706f2 100644 --- a/crates/nix_health/src/check/max_jobs.rs +++ b/crates/nix_health/src/check/max_jobs.rs @@ -5,7 +5,7 @@ use crate::traits::{Check, CheckResult, Checkable}; /// Check that [nix_rs::config::NixConfig::max_jobs] is set to a good value. #[derive(Debug, Default, Serialize, Deserialize, Clone)] -pub struct MaxJobs(); +pub struct MaxJobs {} impl Checkable for MaxJobs { fn check(&self, nix_info: &info::NixInfo, _nix_env: &env::NixEnv) -> Option { diff --git a/crates/nix_health/src/check/min_nix_version.rs b/crates/nix_health/src/check/min_nix_version.rs index 36ee444e..98dc24ad 100644 --- a/crates/nix_health/src/check/min_nix_version.rs +++ b/crates/nix_health/src/check/min_nix_version.rs @@ -4,23 +4,29 @@ use serde::{Deserialize, Serialize}; use crate::traits::*; /// Check that [nix_rs::version::NixVersion] is set to a good value. -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[serde(rename_all = "kebab-case")] pub struct MinNixVersion { + #[serde(default = "default_min_required")] pub min_required: NixVersion, } impl Default for MinNixVersion { fn default() -> Self { MinNixVersion { - min_required: NixVersion { - major: 2, - minor: 13, - patch: 0, - }, + min_required: default_min_required(), } } } +fn default_min_required() -> NixVersion { + NixVersion { + major: 2, + minor: 13, + patch: 0, + } +} + impl Checkable for MinNixVersion { fn check(&self, nix_info: &info::NixInfo, _nix_env: &env::NixEnv) -> Option { let val = &nix_info.nix_version; diff --git a/crates/nix_health/src/check/trusted_users.rs b/crates/nix_health/src/check/trusted_users.rs index a26f4763..a865af41 100644 --- a/crates/nix_health/src/check/trusted_users.rs +++ b/crates/nix_health/src/check/trusted_users.rs @@ -5,7 +5,7 @@ use crate::traits::*; /// Check that [crate::nix::config::NixConfig::trusted_users] is set to a good value. #[derive(Debug, Default, Serialize, Deserialize, Clone)] -pub struct TrustedUsers(); +pub struct TrustedUsers {} impl Checkable for TrustedUsers { fn check(&self, nix_info: &info::NixInfo, nix_env: &env::NixEnv) -> Option { diff --git a/crates/nix_health/src/lib.rs b/crates/nix_health/src/lib.rs index f0e5540a..2e04f5ec 100644 --- a/crates/nix_health/src/lib.rs +++ b/crates/nix_health/src/lib.rs @@ -5,6 +5,7 @@ pub mod check; pub mod report; pub mod traits; +use nix_rs::flake::url::FlakeUrl; use nix_rs::{env, info}; use serde::{Deserialize, Serialize}; @@ -16,17 +17,19 @@ use self::traits::*; /// Nix Health check information for user's install /// -/// Each field represents an individual check which satisfies the [Check] trait. -/// -/// NOTE: This struct is isomorphic to [Vec>]. We cannot use the -/// latter due to (wasm) serialization limitation with dyn trait objects. An -// [IntoIterator] impl is provide towards this end. +/// Each field represents an individual check which satisfies the [Checkable] trait. #[derive(Debug, Default, Serialize, Deserialize, Clone)] +#[serde(rename_all = "kebab-case")] pub struct NixHealth { + #[serde(default)] pub max_jobs: MaxJobs, + #[serde(default)] pub caches: Caches, + #[serde(default)] pub flake_enabled: FlakeEnabled, + #[serde(default)] pub min_nix_version: MinNixVersion, + #[serde(default)] pub trusted_users: TrustedUsers, } @@ -48,6 +51,14 @@ impl<'a> IntoIterator for &'a NixHealth { } impl NixHealth { + pub fn new(m_flake: Option) -> Self { + match m_flake { + None => Self::default(), + // cf. https://github.com/juspay/nix-browser/issues/60 + Some(_) => unimplemented!("Per-flake health checks are not yet supported"), + } + } + /// Run all checks and collect the results pub fn run_checks(&self, nix_info: &info::NixInfo, nix_env: &env::NixEnv) -> Vec { self.into_iter() @@ -55,3 +66,25 @@ impl NixHealth { .collect() } } + +#[cfg(test)] +mod tests { + use crate::check::{min_nix_version::MinNixVersion, caches::Caches}; + + #[test] + fn test_json_deserialize_empty() { + let json = r#"{}"#; + let v: super::NixHealth = serde_json::from_str(json).unwrap(); + assert_eq!(v.min_nix_version, MinNixVersion::default()); + assert_eq!(v.caches, Caches::default()); + println!("{:?}", v); + } + + #[test] + fn test_json_deserialize_some() { + let json = r#"{ "min-nix-version": { "min-required": "2.17.0" } }"#; + let v: super::NixHealth = serde_json::from_str(json).unwrap(); + assert_eq!(v.min_nix_version.min_required.to_string(), "2.17.0"); + assert_eq!(v.caches, Caches::default()); + } +} diff --git a/crates/nix_health/src/main.rs b/crates/nix_health/src/main.rs index 07d5116e..bbafd2ce 100644 --- a/crates/nix_health/src/main.rs +++ b/crates/nix_health/src/main.rs @@ -12,7 +12,7 @@ async fn main() -> anyhow::Result<()> { let nix_env = NixEnv::detect() .await .with_context(|| "Unable to gather system info")?; - let health = NixHealth::default(); + let health = NixHealth::new(None); let checks = &health.run_checks(&nix_info, &nix_env); println!("Checking the health of your Nix setup:\n"); for check in checks { diff --git a/src/app/health.rs b/src/app/health.rs index 90d1fbc5..24bb7242 100644 --- a/src/app/health.rs +++ b/src/app/health.rs @@ -93,7 +93,7 @@ pub async fn get_nix_health(_unit: ()) -> Result, use nix_rs::{env, info}; let nix_info = info::NixInfo::from_nix(&nix_rs::command::NixCmd::default()).await?; let nix_env = env::NixEnv::detect().await?; - let health = NixHealth::default(); + let health = NixHealth::new(None); let checks = health.run_checks(&nix_info, &nix_env); Ok(checks) }