Fix nginx detection when running as root - skip sudo

This commit is contained in:
Christoffer Martinsson 2025-10-13 12:47:28 +02:00
parent 9b6a504e48
commit 92d6b42837

View File

@ -572,6 +572,11 @@ impl ServiceCollector {
}
}
fn is_running_as_root(&self) -> bool {
std::env::var("USER").unwrap_or_default() == "root" ||
std::env::var("UID").unwrap_or_default() == "0"
}
async fn get_nginx_sites(&self) -> Option<Vec<String>> {
// Get the actual nginx config file path from systemd (NixOS uses custom config)
@ -579,8 +584,16 @@ impl ServiceCollector {
Some(path) => path,
None => {
// Fallback to default nginx -T
match Command::new("sudo")
.args(["nginx", "-T"])
let mut cmd = if self.is_running_as_root() {
Command::new("nginx")
} else {
let mut cmd = Command::new("sudo");
cmd.arg("nginx");
cmd
};
match cmd
.args(["-T"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
@ -601,8 +614,16 @@ impl ServiceCollector {
};
// Use the specific config file
let output = match Command::new("sudo")
.args(["nginx", "-T", "-c", &config_path])
let mut cmd = if self.is_running_as_root() {
Command::new("nginx")
} else {
let mut cmd = Command::new("sudo");
cmd.arg("nginx");
cmd
};
let output = match cmd
.args(["-T", "-c", &config_path])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()