fix: use nixos-version --json for configuration revision

- Use `nixos-version --json` command instead of reading files directly
- Add nixpkgs_rev and nixos_version labels to nixos_flake_info metric
- Show "unknown" for current_rev when system.configurationRevision not set
- Document configurationRevision setup in README

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 00:55:23 +01:00
parent 86eaeb4b2a
commit 04eba77ac0
4 changed files with 99 additions and 33 deletions

View File

@@ -2,23 +2,46 @@ package collector
import (
"encoding/json"
"os"
"os/exec"
"testing"
)
func TestGetCurrentSystemRevision(t *testing.T) {
// Skip if not on NixOS with system.configurationRevision set
if _, err := os.Stat(configRevisionPath); os.IsNotExist(err) {
t.Skip("not running on NixOS with system.configurationRevision set")
func TestGetNixosVersionInfo(t *testing.T) {
// Skip if nixos-version command is not available
if _, err := exec.LookPath("nixos-version"); err != nil {
t.Skip("nixos-version command not available")
}
rev, err := getCurrentSystemRevision()
info, err := getNixosVersionInfo()
if err != nil {
t.Fatal(err)
}
// Just check it returns something reasonable
t.Logf("current system revision: %s", rev)
t.Logf("NixOS version info: configurationRevision=%s, nixosVersion=%s, nixpkgsRevision=%s",
info.ConfigurationRevision, info.NixosVersion, info.NixpkgsRevision)
}
func TestNixosVersionInfoUnmarshal(t *testing.T) {
jsonData := `{
"configurationRevision": "138aeb9e450890efd7ca3be6159b1eb22256c2e3",
"nixosVersion": "25.11.20260203.e576e3c",
"nixpkgsRevision": "e576e3c9cf9bad747afcddd9e34f51d18c855b4e"
}`
var info nixosVersionInfo
if err := json.Unmarshal([]byte(jsonData), &info); err != nil {
t.Fatal(err)
}
if info.ConfigurationRevision != "138aeb9e450890efd7ca3be6159b1eb22256c2e3" {
t.Errorf("expected configurationRevision 138aeb9e450890efd7ca3be6159b1eb22256c2e3, got %s", info.ConfigurationRevision)
}
if info.NixosVersion != "25.11.20260203.e576e3c" {
t.Errorf("expected nixosVersion 25.11.20260203.e576e3c, got %s", info.NixosVersion)
}
if info.NixpkgsRevision != "e576e3c9cf9bad747afcddd9e34f51d18c855b4e" {
t.Errorf("expected nixpkgsRevision e576e3c9cf9bad747afcddd9e34f51d18c855b4e, got %s", info.NixpkgsRevision)
}
}
func TestFlakeLocksUnmarshal(t *testing.T) {