fix: use configuration-revision for current_rev in flake info metric

The nixos_flake_info metric's current_rev label was incorrectly showing
the nixpkgs input revision (from /run/current-system/nixos-version)
instead of the flake's own revision.

Now reads from /run/current-system/configuration-revision which contains
the flake's self.rev when system.configurationRevision is set in the
NixOS configuration.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 00:27:38 +01:00
parent 9c29505814
commit 86eaeb4b2a
3 changed files with 11 additions and 89 deletions

View File

@@ -3,40 +3,13 @@ package collector
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
func TestRevisionPattern(t *testing.T) {
tests := []struct {
version string
wantRev string
}{
{"25.11.20260203.e576e3c", "e576e3c"},
{"1994-294a625", "294a625"},
{"25.05.20250101.abcdef1234567890", "abcdef1234567890"},
{"no-revision-here", ""},
{"", ""},
}
for _, tt := range tests {
t.Run(tt.version, func(t *testing.T) {
matches := revisionPattern.FindStringSubmatch(tt.version)
var got string
if matches != nil {
got = matches[1]
}
if got != tt.wantRev {
t.Errorf("revisionPattern.FindStringSubmatch(%q) = %q, want %q", tt.version, got, tt.wantRev)
}
})
}
}
func TestGetCurrentSystemRevision(t *testing.T) {
// Skip if not on NixOS
if _, err := os.Stat(nixosVersionPath); os.IsNotExist(err) {
t.Skip("not running on NixOS")
// 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")
}
rev, err := getCurrentSystemRevision()
@@ -48,50 +21,6 @@ func TestGetCurrentSystemRevision(t *testing.T) {
t.Logf("current system revision: %s", rev)
}
func TestGetCurrentSystemRevisionFromFile(t *testing.T) {
// Create a temp file to simulate /run/current-system/nixos-version
dir := t.TempDir()
versionPath := filepath.Join(dir, "nixos-version")
tests := []struct {
content string
wantRev string
}{
{"25.11.20260203.e576e3c\n", "e576e3c"},
{"1994-294a625\n", "294a625"},
{"25.05.20250101.abcdef1234567890\n", "abcdef1"},
{"no-hash", ""},
}
for _, tt := range tests {
t.Run(tt.content, func(t *testing.T) {
if err := os.WriteFile(versionPath, []byte(tt.content), 0644); err != nil {
t.Fatal(err)
}
// We can't easily test the actual function without modifying the constant,
// so we test the pattern extraction logic directly
version := tt.content
if len(version) > 0 && version[len(version)-1] == '\n' {
version = version[:len(version)-1]
}
matches := revisionPattern.FindStringSubmatch(version)
var rev string
if matches != nil {
rev = matches[1]
if len(rev) > 7 {
rev = rev[:7]
}
}
if rev != tt.wantRev {
t.Errorf("got revision %q, want %q", rev, tt.wantRev)
}
})
}
}
func TestFlakeLocksUnmarshal(t *testing.T) {
jsonData := `{
"revision": "abc1234567890",