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")