summary refs log tree commit diff
path: root/nixos/modules/services/network-filesystems/rsyncd.nix
blob: fa29e18a93950362456d3bab25dffc3883127af4 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
{ config, pkgs, lib, ... }:

with lib;

let

  cfg = config.services.rsyncd;

  motdFile = builtins.toFile "rsyncd-motd" cfg.motd;

  foreach = attrs: f:
    concatStringsSep "\n" (mapAttrsToList f attrs);

  cfgFile = ''
    ${optionalString (cfg.motd != "") "motd file = ${motdFile}"}
    ${optionalString (cfg.address != "") "address = ${cfg.address}"}
    ${optionalString (cfg.port != 873) "port = ${toString cfg.port}"}
    ${cfg.extraConfig}
    ${foreach cfg.modules (name: module: ''
      [${name}]
      ${foreach module (k: v:
        "${k} = ${v}"
      )}
    '')}
  '';
in

{
  options = {
    services.rsyncd = {

      enable = mkEnableOption "the rsync daemon";

      motd = mkOption {
        type = types.str;
        default = "";
        description = ''
          Message of the day to display to clients on each connect.
          This usually contains site information and any legal notices.
        '';
      };

      port = mkOption {
        default = 873;
        type = types.int;
        description = "TCP port the daemon will listen on.";
      };

      address = mkOption {
        default = "";
        example = "192.168.1.2";
        description = ''
          IP address the daemon will listen on; rsyncd will listen on
          all addresses if this is not specified.
        '';
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = ''
            Lines of configuration to add to rsyncd globally.
            See <command>man rsyncd.conf</command> for options.
          '';
      };

      modules = mkOption {
        default = {};
        description = ''
            A set describing exported directories.
            See <command>man rsyncd.conf</command> for options.
          '';
        type = types.attrsOf (types.attrsOf types.str);
        example = literalExample ''
          { srv =
             { path = "/srv";
               "read only" = "yes";
               comment = "Public rsync share.";
             };
          }
        '';
      };

      user = mkOption {
        type = types.str;
        default = "root";
        description = ''
          The user to run the daemon as.
          By default the daemon runs as root.
        '';
      };

      group = mkOption {
        type = types.str;
        default = "root";
        description = ''
          The group to run the daemon as.
          By default the daemon runs as root.
        '';
      };

    };
  };

  ###### implementation

  config = mkIf cfg.enable {

    environment.etc."rsyncd.conf".text = cfgFile;

    systemd.services.rsyncd = {
      description = "Rsync daemon";
      wantedBy = [ "multi-user.target" ];
      restartTriggers = [ config.environment.etc."rsyncd.conf".source ];
      serviceConfig = {
        ExecStart = "${pkgs.rsync}/bin/rsync --daemon --no-detach";
        User = cfg.user;
        Group = cfg.group;
      };
    };
  };
}