syncthing.nix (3280B)
1 { 2 config, 3 lib, 4 pkgs, 5 haravara, 6 ... 7 }: let 8 cfg = config.services.haravara.syncthing; 9 10 s3SyncScript = pkgs.writeShellScript "syncthing-s3-sync" '' 11 export RCLONE_CONFIG_SEAWEEDFS_TYPE=s3 12 export RCLONE_CONFIG_SEAWEEDFS_PROVIDER=Other 13 export RCLONE_CONFIG_SEAWEEDFS_ENDPOINT="${cfg.s3Endpoint}" 14 export RCLONE_CONFIG_SEAWEEDFS_FORCE_PATH_STYLE=true 15 export RCLONE_CONFIG_SEAWEEDFS_ACCESS_KEY_ID="$S3_ACCESS_KEY_ID" 16 export RCLONE_CONFIG_SEAWEEDFS_SECRET_ACCESS_KEY="$S3_SECRET_ACCESS_KEY" 17 exec ${lib.getExe pkgs.rclone} copy seaweedfs:mobius "${cfg.syncDir}" --create-empty-src-dirs 18 ''; 19 in { 20 options.services.haravara.syncthing = { 21 enable = lib.mkEnableOption "Syncthing with S3 archive sync"; 22 23 domain = lib.mkOption { 24 type = lib.types.str; 25 description = "Domain for the Syncthing web GUI"; 26 }; 27 28 authLabel = haravara.authLabelOption; 29 30 guiPort = lib.mkOption { 31 type = lib.types.nullOr lib.types.str; 32 default = "8384"; 33 description = "Port on which the web interface runs"; 34 }; 35 36 s3Endpoint = lib.mkOption { 37 type = lib.types.str; 38 default = "http://127.0.0.1:8333"; 39 description = "SeaweedFS S3 endpoint URL"; 40 }; 41 42 s3CredentialsFile = lib.mkOption { 43 type = lib.types.path; 44 description = "Environment file with S3_ACCESS_KEY_ID and S3_SECRET_ACCESS_KEY"; 45 }; 46 47 syncDir = lib.mkOption { 48 type = lib.types.str; 49 description = "Local path where the mobius bucket is synced to and shared via Syncthing"; 50 }; 51 52 syncInterval = lib.mkOption { 53 type = lib.types.str; 54 default = "1h"; 55 description = "How often to pull from S3 bucket"; 56 }; 57 }; 58 59 config = lib.mkIf cfg.enable { 60 systemd = { 61 tmpfiles.rules = [ 62 "d /data/syncthing 0750 syncthing syncthing -" 63 "d /data/syncthing/mobius 0750 syncthing syncthing -" 64 ]; 65 66 services.syncthing-s3-sync = { 67 description = "Pull s3://mobius into syncthing folder"; 68 after = ["network-online.target" "syncthing.service"] ++ lib.optionals config.services.haravara.seaweedfs.enable ["seaweedfs-s3.service"]; 69 wants = ["network-online.target"] ++ lib.optionals config.services.haravara.seaweedfs.enable ["seaweedfs-s3.service"]; 70 serviceConfig = { 71 Type = "oneshot"; 72 User = "syncthing"; 73 EnvironmentFile = cfg.s3CredentialsFile; 74 ExecStart = s3SyncScript; 75 }; 76 }; 77 78 timers.syncthing-s3-sync = { 79 wantedBy = ["timers.target"]; 80 timerConfig = { 81 OnActiveSec = "5m"; 82 OnUnitActiveSec = cfg.syncInterval; 83 }; 84 }; 85 }; 86 87 services.syncthing = { 88 enable = true; 89 openDefaultPorts = true; 90 guiAddress = "127.0.0.1:${toString cfg.guiPort}"; 91 settings = { 92 folders.mobius = { 93 path = cfg.syncDir; 94 label = "mobius"; 95 type = "sendonly"; 96 overrideDevices = false; 97 }; 98 options.urAccepted = -1; 99 }; 100 }; 101 102 services.caddy.virtualHosts."${cfg.domain}".extraConfig = '' 103 ${haravara.mkAuthImport cfg.authLabel} 104 reverse_proxy 127.0.0.1:${toString cfg.guiPort} { 105 header_up Host 127.0.0.1:${toString cfg.guiPort} 106 } 107 ''; 108 }; 109 }