From 3b1bda741b5e870fefa6974a82fad7b532595cd9 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Thu, 23 Oct 2025 14:55:18 +0200 Subject: [PATCH] Remove codename from NixOS build display - Strip codename part (e.g., '(Warbler)') from nixos-version output - Display clean version format: '25.05.20251004.3bcc93c' - Simplify parsing to use raw nixos-version output as requested --- agent/src/collectors/nixos.rs | 69 ++++++++++++----------------------- 1 file changed, 24 insertions(+), 45 deletions(-) diff --git a/agent/src/collectors/nixos.rs b/agent/src/collectors/nixos.rs index 26e4fa7..ddbd3a5 100644 --- a/agent/src/collectors/nixos.rs +++ b/agent/src/collectors/nixos.rs @@ -20,42 +20,30 @@ impl NixOSCollector { Self { config } } - /// Get NixOS build information (short hash and timestamp) - fn get_nixos_build_info(&self) -> Result<(String, String), Box> { - // Try nixos-version command first - if let Ok(output) = Command::new("nixos-version").output() { - if output.status.success() { - let version_line = String::from_utf8_lossy(&output.stdout); - let version = version_line.trim(); - - // Parse format: "24.05.20241023.abcdef (Vicuna)" - if let Some(parts) = version.split('.').collect::>().get(2) { - if parts.len() >= 14 { // 8 digits + 6 hex chars minimum - let date_part = &parts[..8]; // YYYYMMDD - let hash_part = &parts[8..]; // remaining is hash - - // Extract short hash (first 6 characters) - let short_hash = if hash_part.len() >= 6 { - &hash_part[..6] - } else { - hash_part - }; - - // Format date from YYYYMMDD to dd/mm/yy - if date_part.len() == 8 { - let year = &date_part[2..4]; // YY - let month = &date_part[4..6]; // MM - let day = &date_part[6..8]; // DD - let formatted_date = format!("{}/{}/{}", day, month, year); - - return Ok((short_hash.to_string(), formatted_date)); - } - } - } - } + /// Get NixOS build information + fn get_nixos_build_info(&self) -> Result> { + // Get nixos-version output directly + let output = Command::new("nixos-version").output()?; + + if !output.status.success() { + return Err("nixos-version command failed".into()); } - Err("Could not parse NixOS build information".into()) + let version_line = String::from_utf8_lossy(&output.stdout); + let version = version_line.trim(); + + if version.is_empty() { + return Err("Empty nixos-version output".into()); + } + + // Remove codename part (e.g., "(Warbler)") + let clean_version = if let Some(pos) = version.find(" (") { + version[..pos].to_string() + } else { + version.to_string() + }; + + Ok(clean_version) } /// Get currently active users @@ -94,19 +82,10 @@ impl Collector for NixOSCollector { // Collect NixOS build information match self.get_nixos_build_info() { - Ok((short_hash, formatted_date)) => { - // Create combined build string: "hash dd/mm/yy H:M:S" - // For now, use current time for H:M:S (could be enhanced to get actual build time) - let now = chrono::Local::now(); - let build_string = format!("{} {} {}", - short_hash, - formatted_date, - now.format("%H:%M:%S") - ); - + Ok(build_info) => { metrics.push(Metric { name: "system_nixos_build".to_string(), - value: MetricValue::String(build_string), + value: MetricValue::String(build_info), unit: None, description: Some("NixOS build information".to_string()), status: Status::Ok,