···
1
+
{ pkgs, lib, config, ... }:
5
+
cfg = config.services.gammu-smsd;
7
+
configFile = pkgs.writeText "gammu-smsd.conf" ''
9
+
Device = ${cfg.device.path}
10
+
Connection = ${cfg.device.connection}
11
+
SynchronizeTime = ${if cfg.device.synchronizeTime then "yes" else "no"}
12
+
LogFormat = ${cfg.log.format}
13
+
${if (cfg.device.pin != null) then "PIN = ${cfg.device.pin}" else ""}
14
+
${cfg.extraConfig.gammu}
18
+
LogFile = ${cfg.log.file}
19
+
Service = ${cfg.backend.service}
21
+
${optionalString (cfg.backend.service == "files") ''
22
+
InboxPath = ${cfg.backend.files.inboxPath}
23
+
OutboxPath = ${cfg.backend.files.outboxPath}
24
+
SentSMSPath = ${cfg.backend.files.sentSMSPath}
25
+
ErrorSMSPath = ${cfg.backend.files.errorSMSPath}
28
+
${optionalString (cfg.backend.service == "sql" && cfg.backend.sql.driver == "sqlite") ''
29
+
Driver = ${cfg.backend.sql.driver}
30
+
DBDir = ${cfg.backend.sql.database}
33
+
${optionalString (cfg.backend.service == "sql" && cfg.backend.sql.driver == "native_pgsql") (
34
+
with cfg.backend; ''
35
+
Driver = ${sql.driver}
36
+
${if (sql.database!= null) then "Database = ${sql.database}" else ""}
37
+
${if (sql.host != null) then "Host = ${sql.host}" else ""}
38
+
${if (sql.user != null) then "User = ${sql.user}" else ""}
39
+
${if (sql.password != null) then "Password = ${sql.password}" else ""}
42
+
${cfg.extraConfig.smsd}
45
+
initDBDir = "share/doc/gammu/examples/sql";
47
+
gammuPackage = with cfg.backend; (pkgs.gammu.override {
48
+
dbiSupport = (service == "sql" && sql.driver == "sqlite");
49
+
postgresSupport = (service == "sql" && sql.driver == "native_pgsql");
54
+
services.gammu-smsd = {
56
+
enable = mkEnableOption "gammu-smsd daemon";
61
+
description = "User that has access to the device";
67
+
description = "Device node or address of the phone";
68
+
example = "/dev/ttyUSB2";
74
+
description = "Owner group of the device";
75
+
example = "dialout";
78
+
connection = mkOption {
81
+
description = "Protocol which will be used to talk to the phone";
84
+
synchronizeTime = mkOption {
87
+
description = "Whether to set time from computer to the phone during starting connection";
91
+
type = types.nullOr types.str;
93
+
description = "PIN code for the simcard";
101
+
default = "syslog";
102
+
description = "Path to file where information about communication will be stored";
105
+
format = mkOption {
106
+
type = types.enum [ "nothing" "text" "textall" "textalldate" "errors" "errorsdate" "binary" ];
107
+
default = "errors";
108
+
description = "Determines what will be logged to the LogFile";
115
+
type = types.lines;
117
+
description = "Extra config lines to be added into [gammu] section";
122
+
type = types.lines;
124
+
description = "Extra config lines to be added into [smsd] section";
130
+
service = mkOption {
131
+
type = types.enum [ "null" "files" "sql" ];
133
+
description = "Service to use to store sms data.";
137
+
inboxPath = mkOption {
139
+
default = "/var/spool/sms/inbox/";
140
+
description = "Where the received SMSes are stored";
143
+
outboxPath = mkOption {
145
+
default = "/var/spool/sms/outbox/";
146
+
description = "Where SMSes to be sent should be placed";
149
+
sentSMSPath = mkOption {
151
+
default = "/var/spool/sms/sent/";
152
+
description = "Where the transmitted SMSes are placed";
155
+
errorSMSPath = mkOption {
157
+
default = "/var/spool/sms/error/";
158
+
description = "Where SMSes with error in transmission is placed";
163
+
driver = mkOption {
164
+
type = types.enum [ "native_mysql" "native_pgsql" "odbc" "dbi" ];
165
+
description = "DB driver to use";
168
+
sqlDialect = mkOption {
169
+
type = types.nullOr types.str;
171
+
description = "SQL dialect to use (odbc driver only)";
174
+
database = mkOption {
177
+
description = "Database name to store sms data";
182
+
default = "localhost";
183
+
description = "Database server address";
187
+
type = types.nullOr types.str;
189
+
description = "User name used for connection to the database";
192
+
password = mkOption {
193
+
type = types.nullOr types.str;
195
+
description = "User password used for connetion to the database";
202
+
config = mkIf cfg.enable {
203
+
users.extraUsers.${cfg.user} = {
204
+
description = "gammu-smsd user";
205
+
uid = config.ids.uids.gammu-smsd;
206
+
extraGroups = [ "${cfg.device.group}" ];
209
+
environment.systemPackages = with cfg.backend; [ gammuPackage ]
210
+
++ optionals (service == "sql" && sql.driver == "sqlite") [ pkgs.sqlite ];
212
+
systemd.services.gammu-smsd = {
213
+
description = "gammu-smsd daemon";
215
+
wantedBy = [ "multi-user.target" ];
217
+
wants = with cfg.backend; [ ]
218
+
++ optionals (service == "sql" && sql.driver == "native_pgsql") [ "postgresql.service" ];
220
+
preStart = with cfg.backend;
222
+
optionalString (service == "files") (with files; ''
223
+
mkdir -m 755 -p ${inboxPath} ${outboxPath} ${sentSMSPath} ${errorSMSPath}
224
+
chown ${cfg.user} -R ${inboxPath}
225
+
chown ${cfg.user} -R ${outboxPath}
226
+
chown ${cfg.user} -R ${sentSMSPath}
227
+
chown ${cfg.user} -R ${errorSMSPath}
229
+
+ optionalString (service == "sql" && sql.driver == "sqlite") ''
230
+
cat "${gammuPackage}/${initDBDir}/sqlite.sql" \
231
+
| ${pkgs.sqlite}/bin/sqlite3 ${sql.database}
233
+
+ (let execPsql = extraArgs: concatStringsSep " " [
234
+
(optionalString (sql.password != null) "PGPASSWORD=${sql.password}")
235
+
"${config.services.postgresql.package}/bin/psql"
236
+
(optionalString (sql.host != null) "-h ${sql.host}")
237
+
(optionalString (sql.user != null) "-U ${sql.user}")
240
+
]; in optionalString (service == "sql" && sql.driver == "native_pgsql") ''
241
+
echo '\i '"${gammuPackage}/${initDBDir}/pgsql.sql" | ${execPsql ""}
245
+
User = "${cfg.user}";
246
+
Group = "${cfg.device.group}";
247
+
PermissionsStartOnly = true;
248
+
ExecStart = "${gammuPackage}/bin/gammu-smsd -c ${configFile}";