self: { config, lib, pkgs, ... }: let cfg = config.programs.restray; daemon = config.services.restray; shared = import ./shared.nix {inherit lib pkgs;}; configDir = if pkgs.stdenv.hostPlatform.isDarwin then "${config.home.homeDirectory}/Library/Application Support/restray" else "${config.xdg.configHome}/restray"; in { options.programs.restray = { enable = lib.mkEnableOption "Restray backup scheduler"; package = lib.mkPackageOption self.packages.${pkgs.stdenv.hostPlatform.system} "default" {}; profiles = lib.mkOption { type = lib.types.attrsOf (lib.types.submodule shared.profileOpts); default = {}; description = "Restray backup profiles."; }; gui = lib.mkOption { type = lib.types.submodule shared.guiOpts; default = {}; description = "GUI-only settings, ignored by restray daemon."; }; }; options.services.restray.enable = lib.mkEnableOption "Restray backup scheduler daemon"; config = lib.mkMerge [ (lib.mkIf daemon.enable { programs.restray.enable = lib.mkDefault true; }) (lib.mkIf cfg.enable { home.packages = [cfg.package]; home.file."${configDir}/config.toml".source = shared.configFile cfg.profiles cfg.gui; }) (lib.mkIf (daemon.enable && cfg.enable) { assertions = [ { assertion = cfg.profiles != {}; message = "programs.restray.enable requires at least one programs.restray.profiles. to be configured."; } ]; }) (lib.mkIf (daemon.enable && cfg.enable && pkgs.stdenv.hostPlatform.isLinux) { systemd.user.services.restray = { Unit.Description = "Restray backup scheduler daemon"; Install.WantedBy = ["default.target"]; Service = { Type = "simple"; ExecStart = "${lib.getExe cfg.package} daemon"; ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; Restart = "on-failure"; }; }; }) (lib.mkIf (daemon.enable && cfg.enable && pkgs.stdenv.hostPlatform.isDarwin) { launchd.agents.restray = { enable = true; config = { ProgramArguments = ["${lib.getExe cfg.package}" "daemon"]; KeepAlive = true; RunAtLoad = true; StandardErrorPath = "${config.home.homeDirectory}/Library/Logs/restray.daemon.log"; StandardOutPath = "${config.home.homeDirectory}/Library/Logs/restray.daemon.log"; }; }; }) ]; }