diff --git a/agent/src/collectors/nixos.rs b/agent/src/collectors/nixos.rs index 982d298..bf35def 100644 --- a/agent/src/collectors/nixos.rs +++ b/agent/src/collectors/nixos.rs @@ -63,27 +63,32 @@ impl NixOSCollector { Ok("unknown".to_string()) } - /// Get configuration hash from cloned nixos-config git repository + /// Get configuration hash from deployed nix store system fn get_config_hash(&self) -> Result> { - // Get git hash from the cloned nixos-config directory - let config_path = "/var/lib/cm-dashboard/nixos-config"; - - let output = Command::new("git") - .args(&["log", "-1", "--format=%h"]) - .current_dir(config_path) + // Read the symlink target of /run/current-system to get nix store path + let output = Command::new("readlink") + .arg("/run/current-system") .output()?; if !output.status.success() { - return Err("git log command failed".into()); + return Err("readlink command failed".into()); } - let hash = String::from_utf8_lossy(&output.stdout).trim().to_string(); + let binding = String::from_utf8_lossy(&output.stdout); + let store_path = binding.trim(); - if hash.is_empty() { - return Err("Empty git hash output".into()); + // Extract hash from nix store path + // Format: /nix/store/HASH-nixos-system-HOSTNAME-VERSION + if let Some(hash_part) = store_path.strip_prefix("/nix/store/") { + if let Some(hash) = hash_part.split('-').next() { + if hash.len() >= 8 { + // Return first 8 characters of nix store hash + return Ok(hash[..8].to_string()); + } + } } - Ok(hash) + Err("Could not extract hash from nix store path".into()) } /// Get currently active users @@ -178,7 +183,7 @@ impl Collector for NixOSCollector { name: "system_config_hash".to_string(), value: MetricValue::String(hash), unit: None, - description: Some("NixOS configuration git hash".to_string()), + description: Some("NixOS deployed configuration hash".to_string()), status: Status::Ok, timestamp, }); @@ -189,7 +194,7 @@ impl Collector for NixOSCollector { name: "system_config_hash".to_string(), value: MetricValue::String("unknown".to_string()), unit: None, - description: Some("Config hash (failed to detect)".to_string()), + description: Some("Deployed config hash (failed to detect)".to_string()), status: Status::Unknown, timestamp, });