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") (
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 (lib.mdDoc "gammu-smsd daemon");
61 description = lib.mdDoc "User that has access to the device";
67 description = lib.mdDoc "Device node or address of the phone";
68 example = "/dev/ttyUSB2";
74 description = lib.mdDoc "Owner group of the device";
78 connection = mkOption {
81 description = lib.mdDoc "Protocol which will be used to talk to the phone";
84 synchronizeTime = mkOption {
87 description = lib.mdDoc "Whether to set time from computer to the phone during starting connection";
91 type = types.nullOr types.str;
93 description = lib.mdDoc "PIN code for the simcard";
102 description = lib.mdDoc "Path to file where information about communication will be stored";
106 type = types.enum [ "nothing" "text" "textall" "textalldate" "errors" "errorsdate" "binary" ];
108 description = lib.mdDoc "Determines what will be logged to the LogFile";
117 description = lib.mdDoc "Extra config lines to be added into [gammu] section";
124 description = lib.mdDoc "Extra config lines to be added into [smsd] section";
131 type = types.enum [ "null" "files" "sql" ];
133 description = lib.mdDoc "Service to use to store sms data.";
137 inboxPath = mkOption {
139 default = "/var/spool/sms/inbox/";
140 description = lib.mdDoc "Where the received SMSes are stored";
143 outboxPath = mkOption {
145 default = "/var/spool/sms/outbox/";
146 description = lib.mdDoc "Where SMSes to be sent should be placed";
149 sentSMSPath = mkOption {
151 default = "/var/spool/sms/sent/";
152 description = lib.mdDoc "Where the transmitted SMSes are placed";
155 errorSMSPath = mkOption {
157 default = "/var/spool/sms/error/";
158 description = lib.mdDoc "Where SMSes with error in transmission is placed";
164 type = types.enum [ "native_mysql" "native_pgsql" "odbc" "dbi" ];
165 description = lib.mdDoc "DB driver to use";
168 sqlDialect = mkOption {
169 type = types.nullOr types.str;
171 description = lib.mdDoc "SQL dialect to use (odbc driver only)";
174 database = mkOption {
175 type = types.nullOr types.str;
177 description = lib.mdDoc "Database name to store sms data";
182 default = "localhost";
183 description = lib.mdDoc "Database server address";
187 type = types.nullOr types.str;
189 description = lib.mdDoc "User name used for connection to the database";
192 password = mkOption {
193 type = types.nullOr types.str;
195 description = lib.mdDoc "User password used for connetion to the database";
202 config = mkIf cfg.enable {
203 users.users.${cfg.user} = {
204 description = "gammu-smsd user";
206 group = 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}/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}";