1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.services.rss2email;
9in
10{
11
12 ###### interface
13
14 options = {
15
16 services.rss2email = {
17
18 enable = lib.mkOption {
19 type = lib.types.bool;
20 default = false;
21 description = "Whether to enable rss2email.";
22 };
23
24 to = lib.mkOption {
25 type = lib.types.str;
26 description = "Mail address to which to send emails";
27 };
28
29 interval = lib.mkOption {
30 type = lib.types.str;
31 default = "12h";
32 description = "How often to check the feeds, in systemd interval format";
33 };
34
35 config = lib.mkOption {
36 type =
37 with lib.types;
38 attrsOf (oneOf [
39 str
40 int
41 bool
42 ]);
43 default = { };
44 description = ''
45 The configuration to give rss2email.
46
47 Default will use system-wide `sendmail` to send the
48 email. This is rss2email's default when running
49 `r2e new`.
50
51 This set contains key-value associations that will be set in the
52 `[DEFAULT]` block along with the
53 `to` parameter.
54
55 See `man r2e` for more information on which
56 parameters are accepted.
57 '';
58 };
59
60 feeds = lib.mkOption {
61 description = "The feeds to watch.";
62 type = lib.types.attrsOf (
63 lib.types.submodule {
64 options = {
65 url = lib.mkOption {
66 type = lib.types.str;
67 description = "The URL at which to fetch the feed.";
68 };
69
70 to = lib.mkOption {
71 type = with lib.types; nullOr str;
72 default = null;
73 description = ''
74 Email address to which to send feed items.
75
76 If `null`, this will not be set in the
77 configuration file, and rss2email will make it default to
78 `rss2email.to`.
79 '';
80 };
81 };
82 }
83 );
84 };
85 };
86
87 };
88
89 ###### implementation
90
91 config = lib.mkIf cfg.enable {
92 users.groups = {
93 rss2email.gid = config.ids.gids.rss2email;
94 };
95
96 users.users = {
97 rss2email = {
98 description = "rss2email user";
99 uid = config.ids.uids.rss2email;
100 group = "rss2email";
101 };
102 };
103
104 environment.systemPackages = with pkgs; [ rss2email ];
105
106 services.rss2email.config.to = cfg.to;
107
108 systemd.tmpfiles.settings."10-rss2email"."/var/rss2email".d = {
109 user = "rss2email";
110 group = "rss2email";
111 mode = "0700";
112 };
113
114 systemd.services.rss2email =
115 let
116 conf = pkgs.writeText "rss2email.cfg" (
117 lib.generators.toINI { } (
118 {
119 DEFAULT = cfg.config;
120 }
121 // lib.mapAttrs' (
122 name: feed:
123 lib.nameValuePair "feed.${name}" (
124 { inherit (feed) url; } // lib.optionalAttrs (feed.to != null) { inherit (feed) to; }
125 )
126 ) cfg.feeds
127 )
128 );
129 in
130 {
131 preStart = ''
132 if [ ! -f /var/rss2email/db.json ]; then
133 echo '{"version":2,"feeds":[]}' > /var/rss2email/db.json
134 fi
135 '';
136 path = [ pkgs.system-sendmail ];
137 serviceConfig = {
138 ExecStart = "${pkgs.rss2email}/bin/r2e -c ${conf} -d /var/rss2email/db.json run";
139 User = "rss2email";
140 };
141 };
142
143 systemd.timers.rss2email = {
144 partOf = [ "rss2email.service" ];
145 wantedBy = [ "timers.target" ];
146 timerConfig.OnBootSec = "0";
147 timerConfig.OnUnitActiveSec = cfg.interval;
148 };
149 };
150
151 meta.maintainers = with lib.maintainers; [ ekleog ];
152}