{ pkgs, lib, config, mkInstanceServices, ... }: let instanceType = lib.types.submodule { options = { web = { enable = lib.mkEnableOption "enable Kopia web interface"; guiAddress = lib.mkOption { type = lib.types.str; default = "127.0.0.1:51515"; }; serverUsername = lib.mkOption { type = lib.types.str; default = "admin"; description = "Username for the Kopia web server(basic auth)."; }; environmentFile = lib.mkOption { type = lib.types.nullOr lib.types.path; default = null; description = "File containing environment variables for kopia web server like password."; }; }; }; }; in { options.services.kopia.instances = lib.mkOption { type = lib.types.attrsOf instanceType; }; config = lib.mkIf config.services.kopia.enable { # systemd service for repositories open systemd.services = let mkWebService = # refactor with mkRepositoryArgs name: instance: lib.attrsets.nameValuePair "kopia-web-${name}" { description = "Kopia S3 web service"; wants = [ "kopia-repository-${name}.service" ]; after = [ "kopia-repository-${name}.service" ]; environment = { KOPIA_SERVER_USERNAME = instance.web.serverUsername; }; script = '' export KOPIA_SERVER_USERNAME=${instance.web.serverUsername} # Start Kopia web server ${pkgs.kopia}/bin/kopia server start --insecure --address ${instance.web.guiAddress} ''; serviceConfig = { Type = "simple"; User = "${instance.user}"; WorkingDirectory = "~"; SetLoginEnvironment = true; EnvironmentFile = lib.mkIf (instance.web.environmentFile != null) instance.web.environmentFile; # retry on failure Restart = "on-failure"; # wait 30 seconds before restarting RestartSec = "30"; }; unitConfig = { # limit the number of restarts to 5 in 1 day StartLimitInterval = "1d"; StartLimitBurst = "5"; }; }; in mkInstanceServices config.services.kopia.instances mkWebService; }; }