Release v0.2.0
All checks were successful
buildbot/nix-eval Build done.
buildbot/nix-build Build done.
buildbot/nix-effects Build done.

This commit is contained in:
Abel Luck 2026-03-02 07:25:06 +01:00
parent f0e29d38a4
commit c24af42fc0
9 changed files with 153 additions and 6 deletions

View file

@ -21,13 +21,23 @@ type Config struct {
ClientSecret string `toml:"-"`
}
const configPathEnvVar = "NIX_CACHE_LOGIN_CONFIG"
var systemConfigPath = "/etc/nix-cache-login/config.toml"
// Load reads the config from the given path, or from the default XDG location.
func Load(path string) (*Config, error) {
if path == "" {
path = filepath.Join(xdg.ConfigHome, "nix-cache-login", "config.toml")
}
path = resolveConfigPath(path)
data, err := os.ReadFile(path)
if err != nil && path == defaultXDGConfigPath() {
// On servers, allow a system-wide fallback when per-user XDG config is absent.
if systemData, systemErr := os.ReadFile(systemConfigPath); systemErr == nil {
data = systemData
err = nil
} else {
return nil, fmt.Errorf("reading config file: %w", err)
}
}
if err != nil {
return nil, fmt.Errorf("reading config file: %w", err)
}
@ -55,6 +65,20 @@ func Load(path string) (*Config, error) {
return &cfg, nil
}
func resolveConfigPath(path string) string {
if path != "" {
return path
}
if envPath := strings.TrimSpace(os.Getenv(configPathEnvVar)); envPath != "" {
return envPath
}
return defaultXDGConfigPath()
}
func defaultXDGConfigPath() string {
return filepath.Join(xdg.ConfigHome, "nix-cache-login", "config.toml")
}
func (c *Config) validate() error {
if c.Issuer == "" {
return fmt.Errorf("config: issuer is required")

View file

@ -4,6 +4,8 @@ import (
"os"
"path/filepath"
"testing"
"github.com/adrg/xdg"
)
func TestLoadValidConfig(t *testing.T) {
@ -192,6 +194,65 @@ func TestMissingRequiredFields(t *testing.T) {
}
}
func TestLoadUsesNixCacheLoginConfigEnvWhenPathEmpty(t *testing.T) {
dir := t.TempDir()
cfgFile := filepath.Join(dir, "custom-config.toml")
content := `
issuer = "https://id.example.com/realms/test"
client_id = "nix-cache-server"
cache_host = "cache.example.com"
netrc_path = "/tmp/netrc"
`
if err := os.WriteFile(cfgFile, []byte(content), 0644); err != nil {
t.Fatal(err)
}
t.Setenv(configPathEnvVar, cfgFile)
cfg, err := Load("")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.ClientID != "nix-cache-server" {
t.Fatalf("client_id = %q, want %q", cfg.ClientID, "nix-cache-server")
}
}
func TestLoadFallsBackToSystemConfigPath(t *testing.T) {
dir := t.TempDir()
systemCfg := filepath.Join(dir, "system-config.toml")
systemConfigPathOrig := systemConfigPath
systemConfigPath = systemCfg
defer func() {
systemConfigPath = systemConfigPathOrig
}()
content := `
issuer = "https://id.example.com/realms/test"
client_id = "nix-cache-server"
cache_host = "cache.example.com"
netrc_path = "/tmp/netrc"
`
if err := os.WriteFile(systemCfg, []byte(content), 0644); err != nil {
t.Fatal(err)
}
t.Setenv(configPathEnvVar, "")
origConfigHome := xdg.ConfigHome
xdg.ConfigHome = filepath.Join(dir, "xdg-missing")
defer func() {
xdg.ConfigHome = origConfigHome
}()
cfg, err := Load("")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.ClientID != "nix-cache-server" {
t.Fatalf("client_id = %q, want %q", cfg.ClientID, "nix-cache-server")
}
}
func contains(s, substr string) bool {
return len(s) >= len(substr) && searchString(s, substr)
}