{ config, lib, pkgs, ... }: let cfg = config.samfelag.modules.app.nextcloud; in { options.samfelag.modules.app.nextcloud = { enable = lib.mkEnableOption "nextcloud"; syncClient = { enable = lib.mkEnableOption "headless nextcloud sync via nextcloudcmd on a timer"; user = lib.mkOption { type = lib.types.str; description = "User to run the sync as, and Nextcloud login name"; }; localDir = lib.mkOption { type = lib.types.str; description = "Local directory to sync"; }; remoteUrl = lib.mkOption { type = lib.types.str; description = "Nextcloud server URL"; }; remotePath = lib.mkOption { type = lib.types.str; default = ""; description = "Remote path on the Nextcloud server to sync"; }; passwordFile = lib.mkOption { type = lib.types.str; description = "Path to an EnvironmentFile containing NC_PASSWORD=... (should be secret)"; }; interval = lib.mkOption { type = lib.types.str; default = "1min"; description = "How often to run the sync (systemd time span)"; }; }; }; config = lib.mkMerge [ (lib.mkIf cfg.enable { environment.systemPackages = with pkgs; [ nextcloud-client ]; hm.services.nextcloud-client = { enable = true; startInBackground = true; }; }) (lib.mkIf cfg.syncClient.enable { systemd.services.nextcloud-sync = { description = "Nextcloud headless sync (nextcloudcmd)"; after = [ "network-online.target" ]; wants = [ "network-online.target" ]; serviceConfig = { Type = "oneshot"; User = cfg.syncClient.user; EnvironmentFile = cfg.syncClient.passwordFile; ExecStart = "${pkgs.nextcloud-client}/bin/nextcloudcmd --non-interactive -u ${cfg.syncClient.user} --path ${cfg.syncClient.remotePath} ${cfg.syncClient.localDir} ${cfg.syncClient.remoteUrl}"; }; }; systemd.timers.nextcloud-sync = { wantedBy = [ "timers.target" ]; timerConfig = { OnBootSec = cfg.syncClient.interval; OnUnitActiveSec = cfg.syncClient.interval; }; }; }) ]; }