8 cfg = config.services.wakapi;
10 settingsFormat = pkgs.formats.yaml { };
11 settingsFile = settingsFormat.generate "wakapi-settings" cfg.settings;
26 options.services.wakapi = {
27 enable = mkEnableOption "Wakapi";
28 package = mkPackageOption pkgs "wakapi" { };
31 inherit (settingsFormat) type;
36 See [config.default.yml](https://github.com/muety/wakapi/blob/master/config.default.yml) for a list of all possible options.
40 passwordSalt = mkOption {
41 type = types.nullOr types.str;
44 The password salt to use for Wakapi.
47 passwordSaltFile = mkOption {
48 type = types.nullOr types.path;
51 The path to a file containing the password salt to use for Wakapi.
55 smtpPassword = mkOption {
56 type = types.nullOr types.str;
59 The password used for the smtp mailed to used by Wakapi.
62 smtpPasswordFile = mkOption {
63 type = types.nullOr types.path;
66 The path to a file containing the password for the smtp mailer used by Wakapi.
71 createLocally = mkEnableOption ''
72 automatic database configuration.
75 Only PostgreSQL is supported for the time being.
89 default = cfg.settings.db.dialect or null; # handle case where dialect is not set
91 Database dialect from settings if {option}`services.wakatime.settings.db.dialect`
92 is set, or `null` otherwise.
95 The database type to use for Wakapi.
101 default = cfg.settings.db.name or "wakapi";
103 Database name from settings if {option}`services.wakatime.settings.db.name`
104 is set, or "wakapi" otherwise.
107 The name of the database to use for Wakapi.
113 default = cfg.settings.db.user or "wakapi";
115 User from settings if {option}`services.wakatime.settings.db.user`
116 is set, or "wakapi" otherwise.
119 The name of the user to use for Wakapi.
125 config = mkIf cfg.enable {
126 systemd.services.wakapi = {
127 description = "Wakapi (self-hosted WakaTime-compatible backend)";
129 "network-online.target"
130 ] ++ optional (cfg.database.dialect == "postgres") "postgresql.service";
132 "network-online.target"
133 ] ++ optional (cfg.database.dialect == "postgres") "postgresql.service";
134 wantedBy = [ "multi-user.target" ];
137 exec ${getExe cfg.package} -config ${settingsFile}
141 Environment = mkMerge [
142 (mkIf (cfg.passwordSalt != null) "WAKAPI_PASSWORD_SALT=${cfg.passwordSalt}")
143 (mkIf (cfg.smtpPassword != null) "WAKAPI_MAIL_SMTP_PASS=${cfg.smtpPassword}")
146 (optional (cfg.passwordSaltFile != null) cfg.passwordSaltFile)
147 (optional (cfg.smtpPasswordFile != null) cfg.smtpPasswordFile)
150 User = config.users.users.wakapi.name;
151 Group = config.users.users.wakapi.group;
155 ProtectHostname = true;
156 ProtectKernelLogs = true;
157 ProtectKernelModules = true;
158 ProtectKernelTunables = true;
159 ProtectProc = "invisible";
160 ProtectSystem = "strict";
161 RestrictAddressFamilies = [
166 RestrictNamespaces = true;
167 RestrictRealtime = true;
168 RestrictSUIDSGID = true;
169 StateDirectory = "wakapi";
170 StateDirectoryMode = "0700";
175 services.wakapi.settings = {
176 env = lib.mkDefault "production";
181 assertion = cfg.passwordSalt != null || cfg.passwordSaltFile != null;
182 message = "Either `services.wakapi.passwordSalt` or `services.wakapi.passwordSaltFile` must be set.";
185 assertion = !(cfg.passwordSalt != null && cfg.passwordSaltFile != null);
186 message = "Both `services.wakapi.passwordSalt` `services.wakapi.passwordSaltFile` should not be set at the same time.";
189 assertion = !(cfg.smtpPassword != null && cfg.smtpPasswordFile != null);
190 message = "Both `services.wakapi.smtpPassword` `services.wakapi.smtpPasswordFile` should not be set at the same time.";
193 assertion = cfg.database.createLocally -> cfg.settings.db.dialect != null;
194 message = "`services.wakapi.database.createLocally` is true, but a database dialect is not set!";
199 (lib.optionalString (cfg.database.createLocally -> cfg.settings.db.dialect != "postgres") ''
200 You have enabled automatic database configuration, but the database dialect is not set to "posgres".
202 The Wakapi module only supports for PostgreSQL. Please set `services.wakapi.database.createLocally`
203 to `false`, or switch to "postgres" as your database dialect.
216 services.postgresql = mkIf (cfg.database.createLocally && cfg.database.dialect == "postgres") {
219 ensureDatabases = singleton cfg.database.name;
220 ensureUsers = singleton {
221 name = cfg.settings.db.user;
222 ensureDBOwnership = true;
226 host ${cfg.settings.db.name} ${cfg.settings.db.user} 127.0.0.1/32 trust
231 meta.maintainers = with lib.maintainers; [