1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.rsyncd;
8
9 motdFile = builtins.toFile "rsyncd-motd" cfg.motd;
10
11 moduleConfig = name:
12 let module = getAttr name cfg.modules; in
13 "[${name}]\n " + (toString (
14 map
15 (key: "${key} = ${toString (getAttr key module)}\n")
16 (attrNames module)
17 ));
18
19 cfgFile = builtins.toFile "rsyncd.conf"
20 ''
21 ${optionalString (cfg.motd != "") "motd file = ${motdFile}"}
22 ${optionalString (cfg.address != "") "address = ${cfg.address}"}
23 ${optionalString (cfg.port != 873) "port = ${toString cfg.port}"}
24 ${cfg.extraConfig}
25 ${toString (map moduleConfig (attrNames cfg.modules))}
26 '';
27in
28
29{
30 options = {
31 services.rsyncd = {
32
33 enable = mkOption {
34 default = false;
35 description = "Whether to enable the rsync daemon.";
36 };
37
38 motd = mkOption {
39 type = types.string;
40 default = "";
41 description = ''
42 Message of the day to display to clients on each connect.
43 This usually contains site information and any legal notices.
44 '';
45 };
46
47 port = mkOption {
48 default = 873;
49 type = types.int;
50 description = "TCP port the daemon will listen on.";
51 };
52
53 address = mkOption {
54 default = "";
55 example = "192.168.1.2";
56 description = ''
57 IP address the daemon will listen on; rsyncd will listen on
58 all addresses if this is not specified.
59 '';
60 };
61
62 extraConfig = mkOption {
63 type = types.lines;
64 default = "";
65 description = ''
66 Lines of configuration to add to rsyncd globally.
67 See <command>man rsyncd.conf</command> for options.
68 '';
69 };
70
71 modules = mkOption {
72 default = {};
73 description = ''
74 A set describing exported directories.
75 See <command>man rsyncd.conf</command> for options.
76 '';
77 type = types.attrsOf (types.attrsOf types.str);
78 example =
79 { srv =
80 { path = "/srv";
81 "read only" = "yes";
82 comment = "Public rsync share.";
83 };
84 };
85 };
86
87 };
88 };
89
90 ###### implementation
91
92 config = mkIf cfg.enable {
93
94 environment.etc = singleton {
95 source = cfgFile;
96 target = "rsyncd.conf";
97 };
98
99 systemd.services.rsyncd = {
100 description = "Rsync daemon";
101 wantedBy = [ "multi-user.target" ];
102 serviceConfig.ExecStart = "${pkgs.rsync}/bin/rsync --daemon --no-detach";
103 };
104
105 };
106}