summary refs log tree commit diff
path: root/nixos/modules/services/network-filesystems/rsyncd.nix
blob: 19aa7efd2ff45dbc0ce14b07792bc87332f6513b (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{ config, pkgs, lib, ... }:

with lib;

let

  cfg = config.services.rsyncd;

  motdFile = pkgs.writeText "rsyncd-motd" cfg.motd;

  rsyncdCfg = ""
    + optionalString (cfg.motd != "") "motd file = ${motdFile}\n"
    + optionalString (cfg.address != "") "address = ${cfg.address}\n"
    + optionalString (cfg.port != 873) "port = ${toString cfg.port}\n"
    + cfg.extraConfig
    + "\n"
    + flip concatMapStrings cfg.modules (m: "[${m.name}]\n\tpath = ${m.path}\n"
      + optionalString (m.comment != "") "\tcomment = ${m.comment}\n"
      + m.extraConfig
      + "\n"
    );

  rsyncdCfgFile = pkgs.writeText "rsyncd.conf" rsyncdCfg;

in

{
  options = {

    services.rsyncd = {

      enable = mkOption {
        default = false;
	description = "Whether to enable the rsync daemon.";
      };

      motd = mkOption {
        type = types.string;
        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 <literal>man rsyncd.conf</literal> for more options.
	'';
      };

      modules = mkOption {
        default = [ ];
	example = [ 
	  { name = "ftp"; 
	    path = "/home/ftp"; 
	    comment = "ftp export area";
	    extraConfig = ''
	      secrets file = /etc/rsyncd.secrets
	    '';
	  }
	];
	description = "The list of file paths to export.";
	type = types.listOf types.optionSet;

	options = {

	  name = mkOption {
	    example = "ftp";
	    type = types.string;
	    description = "Name of export module.";
	  };

	  comment = mkOption {
	    default = "";
	    description = ''
	      Description string that is displayed next to the module name
	      when clients obtain a list of available modules.
	    '';
	  };

	  path = mkOption {
	    example = "/home/ftp";
	    type = types.string;
	    description = "Directory to make available in this module.";
   	  };

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

  ###### implementation

  config = mkIf cfg.enable {

    environment.etc = singleton
    { source = rsyncdCfgFile;
      target = "rsyncd.conf";
    };

    systemd.services.rsyncd = {
      description = "Rsync daemon";
      wantedBy = [ "multi-user.target" ];

      path = [ pkgs.rsync ];

      serviceConfig.ExecStart = "${pkgs.rsync}/bin/rsync --daemon --no-detach";
    };

    networking.firewall.allowedTCPPorts = [ cfg.port ];
  };
}