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:
@@ -7,7 +7,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -15,11 +14,7 @@ import (
|
|||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const nixosVersionPath = "/run/current-system/nixos-version"
|
const configRevisionPath = "/run/current-system/configuration-revision"
|
||||||
|
|
||||||
// revisionPattern extracts the git hash from nixos-version.
|
|
||||||
// Formats: "25.11.20260203.e576e3c" or "1994-294a625"
|
|
||||||
var revisionPattern = regexp.MustCompile(`[.-]([a-f0-9]{7,40})$`)
|
|
||||||
|
|
||||||
type FlakeCollector struct {
|
type FlakeCollector struct {
|
||||||
flakeURL string
|
flakeURL string
|
||||||
@@ -215,18 +210,16 @@ func fetchFlakeMetadata(flakeURL string) (*flakeMetadata, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getCurrentSystemRevision() (string, error) {
|
func getCurrentSystemRevision() (string, error) {
|
||||||
data, err := os.ReadFile(nixosVersionPath)
|
data, err := os.ReadFile(configRevisionPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
// configuration-revision doesn't exist; user hasn't set system.configurationRevision
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
version := strings.TrimSpace(string(data))
|
rev := strings.TrimSpace(string(data))
|
||||||
matches := revisionPattern.FindStringSubmatch(version)
|
|
||||||
if matches == nil {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
rev := matches[1]
|
|
||||||
if len(rev) > 7 {
|
if len(rev) > 7 {
|
||||||
rev = rev[:7]
|
rev = rev[:7]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,40 +3,13 @@ package collector
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"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) {
|
func TestGetCurrentSystemRevision(t *testing.T) {
|
||||||
// Skip if not on NixOS
|
// Skip if not on NixOS with system.configurationRevision set
|
||||||
if _, err := os.Stat(nixosVersionPath); os.IsNotExist(err) {
|
if _, err := os.Stat(configRevisionPath); os.IsNotExist(err) {
|
||||||
t.Skip("not running on NixOS")
|
t.Skip("not running on NixOS with system.configurationRevision set")
|
||||||
}
|
}
|
||||||
|
|
||||||
rev, err := getCurrentSystemRevision()
|
rev, err := getCurrentSystemRevision()
|
||||||
@@ -48,50 +21,6 @@ func TestGetCurrentSystemRevision(t *testing.T) {
|
|||||||
t.Logf("current system revision: %s", rev)
|
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) {
|
func TestFlakeLocksUnmarshal(t *testing.T) {
|
||||||
jsonData := `{
|
jsonData := `{
|
||||||
"revision": "abc1234567890",
|
"revision": "abc1234567890",
|
||||||
|
|||||||
Reference in New Issue
Block a user