fix: use absolute path for nixos-version executable

Use /run/current-system/sw/bin/nixos-version instead of relying on
PATH, since the systemd service may not have the system binaries in
its PATH.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 01:02:02 +01:00
parent 04eba77ac0
commit cf0ce85899
3 changed files with 7 additions and 5 deletions

View File

@@ -226,11 +226,13 @@ func fetchFlakeMetadata(flakeURL string) (*flakeMetadata, error) {
return &data, nil return &data, nil
} }
const nixosVersionPath = "/run/current-system/sw/bin/nixos-version"
func getNixosVersionInfo() (*nixosVersionInfo, error) { func getNixosVersionInfo() (*nixosVersionInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
cmd := exec.CommandContext(ctx, "nixos-version", "--json") cmd := exec.CommandContext(ctx, nixosVersionPath, "--json")
output, err := cmd.Output() output, err := cmd.Output()
if err != nil { if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok { if exitErr, ok := err.(*exec.ExitError); ok {

View File

@@ -2,14 +2,14 @@ package collector
import ( import (
"encoding/json" "encoding/json"
"os/exec" "os"
"testing" "testing"
) )
func TestGetNixosVersionInfo(t *testing.T) { func TestGetNixosVersionInfo(t *testing.T) {
// Skip if nixos-version command is not available // Skip if nixos-version command is not available
if _, err := exec.LookPath("nixos-version"); err != nil { if _, err := os.Stat(nixosVersionPath); os.IsNotExist(err) {
t.Skip("nixos-version command not available") t.Skip("nixos-version command not available (not running on NixOS)")
} }
info, err := getNixosVersionInfo() info, err := getNixosVersionInfo()

View File

@@ -15,7 +15,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
) )
const version = "0.2.2" const version = "0.2.3"
func main() { func main() {
cfg, err := config.Parse() cfg, err := config.Parse()