Fix nginx config parsing for NixOS systemd format

Improve parsing of nginx config path from systemd ExecStart to handle
both traditional format and NixOS argv[] format. This should fix nginx
sites not being detected when running as a systemd service.
This commit is contained in:
Christoffer Martinsson 2025-10-13 12:12:36 +02:00
parent b0d7d5ce35
commit f786d054f2

View File

@ -640,16 +640,19 @@ impl ServiceCollector {
// Parse ExecStart to extract -c config path
for line in stdout.lines() {
if line.starts_with("ExecStart=") {
// Look for "-c /path/to/config" in the command line
// Handle both traditional and NixOS systemd formats
// Traditional: ExecStart=/path/nginx -c /config
// NixOS: ExecStart={ path=...; argv[]=...nginx -c /config; ... }
if let Some(c_index) = line.find(" -c ") {
let after_c = &line[c_index + 4..];
if let Some(space_index) = after_c.find(' ') {
return Some(after_c[..space_index].to_string());
} else {
// -c is the last argument, clean up systemd formatting
let cleaned = after_c.trim_end_matches(" ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }");
return Some(cleaned.to_string());
}
// Find the end of the config path
let end_pos = after_c.find(' ')
.or_else(|| after_c.find(" ;")) // NixOS format ends with " ;"
.unwrap_or(after_c.len());
let config_path = after_c[..end_pos].trim();
return Some(config_path.to_string());
}
}
}