expand the path to support working in systemd service
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-02-27 09:27:49 +01:00
parent ba34ac0d67
commit 29fe7f76c1
3 changed files with 27 additions and 3 deletions

View file

@ -4,6 +4,7 @@
Changes yet to be released are documented here.
- Fix path expansion when running in systemd
- Add nixos, home-manager, and darwin-nix modules
## v0.1.0

View file

@ -37,8 +37,8 @@ func Load(path string) (*Config, error) {
return nil, fmt.Errorf("parsing config file: %w", err)
}
cfg.NetrcPath = os.ExpandEnv(cfg.NetrcPath)
cfg.ClientSecretFile = os.ExpandEnv(cfg.ClientSecretFile)
cfg.NetrcPath = expandPath(cfg.NetrcPath)
cfg.ClientSecretFile = expandPath(cfg.ClientSecretFile)
if cfg.ClientSecretFile != "" {
secret, err := os.ReadFile(cfg.ClientSecretFile)
@ -71,6 +71,29 @@ func (c *Config) validate() error {
return nil
}
// expandPath expands environment variables in a path. XDG base directory
// variables are resolved using the xdg library, which applies the XDG spec
// fallbacks (e.g. $HOME/.config when $XDG_CONFIG_HOME is unset). This ensures
// correct behaviour in systemd user services, which do not set XDG variables.
func expandPath(s string) string {
return os.Expand(s, func(key string) string {
switch key {
case "XDG_CONFIG_HOME":
return xdg.ConfigHome
case "XDG_DATA_HOME":
return xdg.DataHome
case "XDG_CACHE_HOME":
return xdg.CacheHome
case "XDG_STATE_HOME":
return xdg.StateHome
case "XDG_RUNTIME_DIR":
return xdg.RuntimeDir
default:
return os.Getenv(key)
}
})
}
// RefreshTokenPath returns the path to the stored refresh token.
func RefreshTokenPath() string {
return filepath.Join(xdg.ConfigHome, "nix-cache-login", "refresh-token")

View file

@ -6,7 +6,7 @@
buildGoModule {
pname = "nix-cache-login";
version = "0.1.1";
version = "0.1.2";
src = ./.;
# src = fetchgit {
# url = "https://guardianproject.dev/ops/nix-cache-login.git";