-
Notifications
You must be signed in to change notification settings - Fork 3
/
module.nix
192 lines (171 loc) · 6.01 KB
/
module.nix
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.rememberportal;
rememberportalPackage = import ./. { inherit pkgs; };
projectName = "rememberportal";
stateDir = "/var/lib/${projectName}";
user = projectName;
group = projectName;
env = {
RMP_STATIC_DIR = "${rememberportalPackage}/static/";
RMP_HOST = "127.0.0.1";
RMP_PORT = toString cfg.port;
RMP_IP_FROM_HEADER = "true";
RMP_SENDMAIL_BIN = cfg.mail.sendmail;
RMP_MAIL_FROM = cfg.mail.from;
RMP_ORG_NAME = cfg.orgName;
RMP_CURRENCY = cfg.currency;
RMP_FIO_TOKEN_PATH = cfg.payments.fio.tokenFile;
RMP_FLEXIBLE_FEES = builtins.toJSON cfg.flexibleFees;
} // optionalAttrs (cfg.feeAccount != null) {
RMP_FEE_ACCOUNT = cfg.feeAccount;
} // optionalAttrs (cfg.mail.replyTo != null) {
RMP_MAIL_REPLY_TO = cfg.mail.replyTo;
} // optionalAttrs (cfg.appRoot != null) {
RMP_APPROOT = cfg.appRoot;
};
srv = name: {
ExecStart = "${rememberportalPackage}/bin/${name}";
WorkingDirectory = stateDir;
User = user;
Group = group;
};
in
{
options = {
services.rememberportal = {
enable = mkEnableOption "rememberportal";
orgName = mkOption {
type = types.str;
default = "memberportal";
description = "Organization name shown in the page header";
};
appRoot = mkOption {
type = types.nullOr types.str;
default = null;
example = "https://m.example.com";
description = "Root application URL, required for reminders";
};
mail = {
sendmail = mkOption {
type = types.str;
default = "/run/current-system/sw/bin/sendmail";
description = "Path to sendmail binary (see also: msmtp)";
};
from = mkOption {
type = types.str;
default = "[email protected]";
description = "Email address to use in From: header";
};
replyTo = mkOption {
type = types.str;
default = "[email protected]";
description = "Email address to use in Reply-To: header";
};
};
currency = mkOption {
type = types.str;
default = "BTC";
description = "Membership fees currency";
};
flexibleFees = mkOption {
type = types.bool;
default = false;
description = ''
When flexible fees are enabled, each user can choose membership fee amount they'll be
charged, provided that the amount is greater or equal to the basic fee.
'';
};
port = mkOption {
type = types.port;
default = 13337;
description = "Port number on which the application listens. Note that it always listens on loopback interface because https proxy in front of it is required.";
};
payments.fio.tokenFile = mkOption {
type = types.nullOr types.str;
default = null;
example = "/run/keys/rememberportal-fio-token";
description = "A file containing the token to FIO bank API.";
};
feeAccount = mkOption {
type = types.nullOr types.str;
default = null;
example = "2900086515/2010";
description = "Account number for membership fees, used in email reminders";
};
reminders = mkOption {
type = types.bool;
default = false;
description = "Whether to send email reminders";
};
};
};
config = mkIf cfg.enable {
environment.systemPackages = [ rememberportalPackage ];
systemd.services = {
rememberportal = {
description = "Member portal";
environment = env;
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = srv "rememberportal";
};
rememberportal-create-fees = {
description = "Member portal fees task";
environment = env;
after = [ "rememberportal.service" ];
requires = [ "rememberportal.service" ];
serviceConfig = srv "rememberportal-create-fees";
};
rememberportal-sync-fio = mkIf (cfg.payments.fio.tokenFile != null) {
description = "FIO payments sync task";
environment = env;
after = [ "rememberportal.service" ];
requires = [ "rememberportal.service" ];
serviceConfig = srv "rememberportal-sync-fio";
};
rememberportal-send-reminders = mkIf cfg.reminders {
description = "Send email reminders to members with missing payments";
environment = env;
after = [ "rememberportal.service" ];
requires = [ "rememberportal.service" ];
serviceConfig = srv "rememberportal-send-reminders";
};
};
systemd.timers = {
rememberportal-create-fees = {
description = "Periodically insert new membership fees";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = mkDefault "*-*-1 23:00:00";
Persistent = true;
};
};
rememberportal-sync-fio = mkIf (cfg.payments.fio.tokenFile != null) {
description = "Periodically download payment information from FIO bank API";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = mkDefault "hourly";
Persistent = true;
};
};
rememberportal-send-reminders = mkIf cfg.reminders {
description = "Periodically remind members about missing payments";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = mkDefault "*-*-17 16:45:00";
Persistent = true;
};
};
};
users.users.${projectName} = {
isSystemUser = true;
group = group;
home = stateDir;
createHome = true;
description = "User for ${projectName} daemon";
};
users.groups.${projectName} = {};
};
}