1 { pkgs, lib, config, ... }:
3 cfg = config.services.gammu-smsd;
5 configFile = pkgs.writeText "gammu-smsd.conf" ''
7 Device = ${cfg.device.path}
8 Connection = ${cfg.device.connection}
9 SynchronizeTime = ${if cfg.device.synchronizeTime then "yes" else "no"}
10 LogFormat = ${cfg.log.format}
11 ${lib.optionalString (cfg.device.pin != null) "PIN = ${cfg.device.pin}"}
12 ${cfg.extraConfig.gammu}
16 LogFile = ${cfg.log.file}
17 Service = ${cfg.backend.service}
19 ${lib.optionalString (cfg.backend.service == "files") ''
20 InboxPath = ${cfg.backend.files.inboxPath}
21 OutboxPath = ${cfg.backend.files.outboxPath}
22 SentSMSPath = ${cfg.backend.files.sentSMSPath}
23 ErrorSMSPath = ${cfg.backend.files.errorSMSPath}
26 ${lib.optionalString (cfg.backend.service == "sql" && cfg.backend.sql.driver == "sqlite") ''
27 Driver = ${cfg.backend.sql.driver}
28 DBDir = ${cfg.backend.sql.database}
31 ${lib.optionalString (cfg.backend.service == "sql" && cfg.backend.sql.driver == "native_pgsql") (
33 Driver = ${sql.driver}
34 ${lib.optionalString (sql.database!= null) "Database = ${sql.database}"}
35 ${lib.optionalString (sql.host != null) "Host = ${sql.host}"}
36 ${lib.optionalString (sql.user != null) "User = ${sql.user}"}
37 ${lib.optionalString (sql.password != null) "Password = ${sql.password}"}
40 ${cfg.extraConfig.smsd}
43 initDBDir = "share/doc/gammu/examples/sql";
45 gammuPackage = with cfg.backend; (pkgs.gammu.override {
46 dbiSupport = service == "sql" && sql.driver == "sqlite";
47 postgresSupport = service == "sql" && sql.driver == "native_pgsql";
52 services.gammu-smsd = {
54 enable = lib.mkEnableOption "gammu-smsd daemon";
59 description = "User that has access to the device";
64 type = lib.types.path;
65 description = "Device node or address of the phone";
66 example = "/dev/ttyUSB2";
69 group = lib.mkOption {
72 description = "Owner group of the device";
76 connection = lib.mkOption {
79 description = "Protocol which will be used to talk to the phone";
82 synchronizeTime = lib.mkOption {
83 type = lib.types.bool;
85 description = "Whether to set time from computer to the phone during starting connection";
89 type = lib.types.nullOr lib.types.str;
91 description = "PIN code for the simcard";
100 description = "Path to file where information about communication will be stored";
103 format = lib.mkOption {
104 type = lib.types.enum [ "nothing" "text" "textall" "textalldate" "errors" "errorsdate" "binary" ];
106 description = "Determines what will be logged to the LogFile";
112 gammu = lib.mkOption {
113 type = lib.types.lines;
115 description = "Extra config lines to be added into [gammu] section";
119 smsd = lib.mkOption {
120 type = lib.types.lines;
122 description = "Extra config lines to be added into [smsd] section";
128 service = lib.mkOption {
129 type = lib.types.enum [ "null" "files" "sql" ];
131 description = "Service to use to store sms data.";
135 inboxPath = lib.mkOption {
136 type = lib.types.path;
137 default = "/var/spool/sms/inbox/";
138 description = "Where the received SMSes are stored";
141 outboxPath = lib.mkOption {
142 type = lib.types.path;
143 default = "/var/spool/sms/outbox/";
144 description = "Where SMSes to be sent should be placed";
147 sentSMSPath = lib.mkOption {
148 type = lib.types.path;
149 default = "/var/spool/sms/sent/";
150 description = "Where the transmitted SMSes are placed";
153 errorSMSPath = lib.mkOption {
154 type = lib.types.path;
155 default = "/var/spool/sms/error/";
156 description = "Where SMSes with error in transmission is placed";
161 driver = lib.mkOption {
162 type = lib.types.enum [ "native_mysql" "native_pgsql" "odbc" "dbi" ];
163 description = "DB driver to use";
166 sqlDialect = lib.mkOption {
167 type = lib.types.nullOr lib.types.str;
169 description = "SQL dialect to use (odbc driver only)";
172 database = lib.mkOption {
173 type = lib.types.nullOr lib.types.str;
175 description = "Database name to store sms data";
178 host = lib.mkOption {
179 type = lib.types.str;
180 default = "localhost";
181 description = "Database server address";
184 user = lib.mkOption {
185 type = lib.types.nullOr lib.types.str;
187 description = "User name used for connection to the database";
190 password = lib.mkOption {
191 type = lib.types.nullOr lib.types.str;
193 description = "User password used for connection to the database";
200 config = lib.mkIf cfg.enable {
201 users.users.${cfg.user} = {
202 description = "gammu-smsd user";
204 group = cfg.device.group;
207 environment.systemPackages = with cfg.backend; [ gammuPackage ]
208 ++ lib.optionals (service == "sql" && sql.driver == "sqlite") [ pkgs.sqlite ];
210 systemd.services.gammu-smsd = {
211 description = "gammu-smsd daemon";
213 wantedBy = [ "multi-user.target" ];
215 wants = with cfg.backend; [ ]
216 ++ lib.optionals (service == "sql" && sql.driver == "native_pgsql") [ "postgresql.service" ];
218 preStart = with cfg.backend;
220 lib.optionalString (service == "files") (with files; ''
221 mkdir -m 755 -p ${inboxPath} ${outboxPath} ${sentSMSPath} ${errorSMSPath}
222 chown ${cfg.user} -R ${inboxPath}
223 chown ${cfg.user} -R ${outboxPath}
224 chown ${cfg.user} -R ${sentSMSPath}
225 chown ${cfg.user} -R ${errorSMSPath}
227 + lib.optionalString (service == "sql" && sql.driver == "sqlite") ''
228 cat "${gammuPackage}/${initDBDir}/sqlite.sql" \
229 | ${pkgs.sqlite.bin}/bin/sqlite3 ${sql.database}
231 + (let execPsql = extraArgs: lib.concatStringsSep " " [
232 (lib.optionalString (sql.password != null) "PGPASSWORD=${sql.password}")
233 "${config.services.postgresql.package}/bin/psql"
234 (lib.optionalString (sql.host != null) "-h ${sql.host}")
235 (lib.optionalString (sql.user != null) "-U ${sql.user}")
238 ]; in lib.optionalString (service == "sql" && sql.driver == "native_pgsql") ''
239 echo '\i '"${gammuPackage}/${initDBDir}/pgsql.sql" | ${execPsql ""}
243 User = "${cfg.user}";
244 Group = "${cfg.device.group}";
245 PermissionsStartOnly = true;
246 ExecStart = "${gammuPackage}/bin/gammu-smsd -c ${configFile}";