summaryrefslogtreecommitdiff
path: root/os/fscusat/modules/mirror/debian/ftpsync.nix
blob: 29fb55b76f5762cd87036c0fa700ffc7cc104fc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{ config, lib, pkgs, ... }:

let
  cfg = config.services.ftpsync;
  archvsync = pkgs.callPackage ../../../pkgs/archvsync {};

  formatKeyValue = k: v: '' ${k}="${v}" '';
  configFormat = pkgs.formats.keyValue { mkKeyValue = formatKeyValue; };
  configFile = configFormat.generate "ftpsync.conf" cfg.settings;
in
{
  meta.maintainers = with lib.maintainers; [ sinanmohd ];

  options.services.ftpsync = {
    enable = lib.mkEnableOption (lib.mdDoc "ftpsync");

    settings = lib.mkOption {
      inherit (configFormat) type;
      default = {};
      description = lib.mdDoc ''
        Configuration options for ftpsync.
        See ftpsync.conf(5) man page for available options.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    environment.etc."ftpsync/ftpsync.conf".source = configFile;
    environment.systemPackages = [ archvsync ];

    services.ftpsync.settings = {
      TO = lib.mkDefault "$STATE_DIRECTORY";
      LOGDIR = lib.mkDefault "$LOGS_DIRECTORY";
    };

    systemd = let
      name = "ftpsync";
      meta = {
        description = "Mirror Debian repositories of packages";
        documentation = [ "man:ftpsync(1)" ];
      };
    in {
      timers.${name} = meta // {
        wantedBy = [ "timers.target" ];

        timerConfig = {
          OnCalendar = "*-*-* 00,06,12,18:00:00";
          Unit="%i.service";
          Persistent = true;
          FixedRandomDelay = true;
          RandomizedDelaySec = "6h";
        };
      };

      services.${name} = meta // {
        serviceConfig = {
          LogsDirectory = name;
          StateDirectory = name;

          ExecStart = "${archvsync}/bin/ftpsync sync:all";
        };
      };
    };
  };
}