diff --git a/CHANGELOG.md b/CHANGELOG.md index 7731311..b728640 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,6 @@ 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 Initial release. diff --git a/flake.nix b/flake.nix index 72df013..a5a5919 100644 --- a/flake.nix +++ b/flake.nix @@ -6,7 +6,6 @@ let systems = [ "x86_64-linux" - "aarch64-linux" "aarch64-darwin" ]; forAllSystems = fn: nixpkgs.lib.genAttrs systems (system: fn nixpkgs.legacyPackages.${system}); @@ -24,24 +23,18 @@ }; }); - checks = forAllSystems ( - pkgs: - { - tests = self.packages.${pkgs.stdenv.hostPlatform.system}.default.overrideAttrs (_: { - pname = "nix-cache-login-tests"; - checkPhase = '' - runHook preCheck - go test ./... - runHook postCheck - ''; - doCheck = true; - }); - devShell = self.devShells.${pkgs.stdenv.hostPlatform.system}.default; - } - // pkgs.lib.optionalAttrs pkgs.stdenv.isLinux { - nixos-module = pkgs.testers.runNixOSTest (import ./nixos-test.nix self); - } - ); + checks = forAllSystems (pkgs: { + tests = self.packages.${pkgs.stdenv.hostPlatform.system}.default.overrideAttrs (_: { + pname = "nix-cache-login-tests"; + checkPhase = '' + runHook preCheck + go test ./... + runHook postCheck + ''; + doCheck = true; + }); + devShell = self.devShells.${pkgs.stdenv.hostPlatform.system}.default; + }); devShells = forAllSystems (pkgs: { default = pkgs.mkShell { @@ -52,23 +45,6 @@ }; }); - homeModules = { - # Workstation (Linux + macOS): home-manager module running `nix-cache-login refresh` - default = - { - config, - lib, - pkgs, - ... - }: - { - imports = [ ./home-module.nix ]; - services.nix-cache-login.package = - lib.mkDefault - self.packages.${pkgs.stdenv.hostPlatform.system}.default; - }; - }; - nixosModules = { # Workstation: systemd user timer+service running `nix-cache-login refresh` default = diff --git a/home-module.nix b/home-module.nix deleted file mode 100644 index 90e80ab..0000000 --- a/home-module.nix +++ /dev/null @@ -1,62 +0,0 @@ -{ - config, - lib, - ... -}: -let - cfg = config.services.nix-cache-login; -in -{ - options.services.nix-cache-login = { - enable = lib.mkEnableOption "nix-cache-login automatic token refresh"; - package = lib.mkOption { - type = lib.types.package; - description = "The nix-cache-login package to use."; - }; - refreshInterval = lib.mkOption { - type = lib.types.ints.positive; - default = 900; - description = '' - How often to attempt token refresh, in seconds. - If no valid session exists, the service logs an error and retries on - the next interval. Run {command}`nix-cache-login` to log in. - ''; - example = 1800; - }; - }; - - config = lib.mkIf cfg.enable { - home.packages = [ cfg.package ]; - systemd.user.services.nix-cache-login = { - Unit.Description = "Nix cache login - refresh access token"; - Service = { - Type = "oneshot"; - ExecStart = "${cfg.package}/bin/nix-cache-login refresh"; - }; - }; - - systemd.user.timers.nix-cache-login = { - Unit.Description = "Nix cache login - periodic token refresh"; - Timer = { - OnBootSec = "2min"; - OnUnitActiveSec = "${toString cfg.refreshInterval}s"; - }; - Install.WantedBy = [ "timers.target" ]; - }; - - launchd.agents.nix-cache-login = { - enable = true; - config = { - ProgramArguments = [ - "${cfg.package}/bin/nix-cache-login" - "refresh" - ]; - StartInterval = cfg.refreshInterval; - RunAtLoad = true; - ProcessType = "Background"; - StandardOutPath = "${config.home.homeDirectory}/Library/Logs/nix-cache-login.log"; - StandardErrorPath = "${config.home.homeDirectory}/Library/Logs/nix-cache-login.log"; - }; - }; - }; -} diff --git a/internal/config/config.go b/internal/config/config.go index cc37736..77faaa1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -37,8 +37,8 @@ func Load(path string) (*Config, error) { return nil, fmt.Errorf("parsing config file: %w", err) } - cfg.NetrcPath = expandPath(cfg.NetrcPath) - cfg.ClientSecretFile = expandPath(cfg.ClientSecretFile) + cfg.NetrcPath = os.ExpandEnv(cfg.NetrcPath) + cfg.ClientSecretFile = os.ExpandEnv(cfg.ClientSecretFile) if cfg.ClientSecretFile != "" { secret, err := os.ReadFile(cfg.ClientSecretFile) @@ -71,29 +71,6 @@ 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") diff --git a/nixos-test.nix b/nixos-test.nix deleted file mode 100644 index 9c5840f..0000000 --- a/nixos-test.nix +++ /dev/null @@ -1,29 +0,0 @@ -self: { - name = "nix-cache-login-nixos-module"; - - nodes.machine = - { ... }: - { - imports = [ self.nixosModules.default ]; - services.nix-cache-login.enable = true; - }; - - testScript = '' - machine.wait_for_unit("multi-user.target") - - # The module should install timer and service unit files for all users - machine.succeed("test -f /etc/systemd/user/nix-cache-login.timer") - machine.succeed("test -f /etc/systemd/user/nix-cache-login.service") - - # wantedBy = ["timers.target"] should create this symlink - machine.succeed( - "test -L /etc/systemd/user/timers.target.wants/nix-cache-login.timer" - ) - - # Service unit should reference the correct subcommand - unit = machine.succeed("cat /etc/systemd/user/nix-cache-login.service") - assert "nix-cache-login refresh" in unit, ( - f"ExecStart not found in service unit:\n{unit}" - ) - ''; -} diff --git a/package.nix b/package.nix index e85a434..cb25f28 100644 --- a/package.nix +++ b/package.nix @@ -6,7 +6,7 @@ buildGoModule { pname = "nix-cache-login"; - version = "0.1.2"; + version = "0.1.1"; src = ./.; # src = fetchgit { # url = "https://guardianproject.dev/ops/nix-cache-login.git";