Skip to content

Commit 5c10bbf

Browse files
committed
nixos/weblate: improve smtp setup; make local postgresql optional; fix ssh
1 parent 2ff53fe commit 5c10bbf

File tree

1 file changed

+61
-28
lines changed

1 file changed

+61
-28
lines changed

nixos/modules/services/web-apps/weblate.nix

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ let
4848
COMPRESS_OFFLINE = True
4949
DEBUG = False
5050
51-
DATABASES = {
52-
"default": {
53-
"ENGINE": "django.db.backends.postgresql",
54-
"HOST": "/run/postgresql",
55-
"NAME": "weblate",
56-
"USER": "weblate",
57-
}
58-
}
59-
6051
with open("${cfg.djangoSecretKeyFile}") as f:
6152
SECRET_KEY = f.read().rstrip("\n")
6253
@@ -65,9 +56,9 @@ let
6556
"BACKEND": "django_redis.cache.RedisCache",
6657
"LOCATION": "unix://${config.services.redis.servers.weblate.unixSocket}",
6758
"OPTIONS": {
68-
"CLIENT_CLASS": "django_redis.client.DefaultClient",
69-
"PASSWORD": None,
70-
"CONNECTION_POOL_KWARGS": {},
59+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
60+
"PASSWORD": None,
61+
"CONNECTION_POOL_KWARGS": {},
7162
},
7263
"KEY_PREFIX": "weblate",
7364
"TIMEOUT": 3600,
@@ -80,7 +71,6 @@ let
8071
}
8172
}
8273
83-
8474
CELERY_TASK_ALWAYS_EAGER = False
8575
CELERY_BROKER_URL = "redis+socket://${config.services.redis.servers.weblate.unixSocket}"
8676
CELERY_RESULT_BACKEND = CELERY_BROKER_URL
@@ -93,20 +83,31 @@ let
9383
OTP_WEBAUTHN_RP_NAME = SITE_TITLE
9484
OTP_WEBAUTHN_RP_ID = SITE_DOMAIN.split(":")[0]
9585
OTP_WEBAUTHN_ALLOWED_ORIGINS = [SITE_URL]
96-
86+
''
87+
+ lib.optionalString cfg.configurePostgresql ''
88+
DATABASES = {
89+
"default": {
90+
"ENGINE": "django.db.backends.postgresql",
91+
"HOST": "/run/postgresql",
92+
"NAME": "weblate",
93+
"USER": "weblate",
94+
}
95+
}
9796
''
9897
+ lib.optionalString cfg.smtp.enable ''
99-
ADMINS = (("Weblate Admin", "${cfg.smtp.user}"),)
100-
10198
EMAIL_HOST = "${cfg.smtp.host}"
10299
EMAIL_USE_TLS = True
100+
EMAIL_PORT = ${builtins.toString cfg.smtp.port}
101+
SERVER_EMAIL = "${cfg.smtp.from}"
102+
DEFAULT_FROM_EMAIL = "${cfg.smtp.from}"
103+
''
104+
+ lib.optionalString (cfg.smtp.enable && cfg.smtp.user != null) ''
105+
ADMINS = (("Weblate Admin", "${cfg.smtp.user}"),)
103106
EMAIL_HOST_USER = "${cfg.smtp.user}"
104-
SERVER_EMAIL = "${cfg.smtp.user}"
105-
DEFAULT_FROM_EMAIL = "${cfg.smtp.user}"
106-
EMAIL_PORT = 587
107+
''
108+
+ lib.optionalString (cfg.smtp.enable && cfg.smtp.passwordFile != null) ''
107109
with open("${cfg.smtp.passwordFile}") as f:
108110
EMAIL_HOST_PASSWORD = f.read().rstrip("\n")
109-
110111
''
111112
+ cfg.extraConfig;
112113
settings_py =
@@ -139,6 +140,7 @@ let
139140
tesseract
140141
licensee
141142
mercurial
143+
openssh
142144
];
143145
in
144146
{
@@ -166,6 +168,15 @@ in
166168
type = lib.types.path;
167169
};
168170

171+
configurePostgresql = lib.mkOption {
172+
type = lib.types.bool;
173+
default = true;
174+
description = ''
175+
Whether to enable and configure a local PostgreSQL server by creating a user and database for weblate.
176+
The default `settings` reference this database, if you disable this option you must provide a database URL in `extraConfig`.
177+
'';
178+
};
179+
169180
extraConfig = lib.mkOption {
170181
type = lib.types.lines;
171182
default = "";
@@ -176,10 +187,20 @@ in
176187

177188
smtp = {
178189
enable = lib.mkEnableOption "Weblate SMTP support";
190+
191+
from = lib.mkOption {
192+
description = "The from address being used in sent emails.";
193+
example = "[email protected]";
194+
default = config.services.weblate.smtp.user;
195+
defaultText = "config.services.weblate.smtp.user";
196+
type = lib.types.str;
197+
};
198+
179199
user = lib.mkOption {
180200
description = "SMTP login name.";
181201
example = "[email protected]";
182-
type = lib.types.str;
202+
type = lib.types.nullOr lib.types.str;
203+
default = null;
183204
};
184205

185206
host = lib.mkOption {
@@ -188,16 +209,23 @@ in
188209
example = "127.0.0.1";
189210
};
190211

212+
port = lib.mkOption {
213+
description = "SMTP port used when sending emails to users.";
214+
type = lib.types.port;
215+
default = 587;
216+
example = 25;
217+
};
218+
191219
passwordFile = lib.mkOption {
192220
description = ''
193221
Location of a file containing the SMTP password.
194222
195223
This should be a path pointing to a file with secure permissions (not /nix/store).
196224
'';
197-
type = lib.types.path;
225+
type = lib.types.nullOr lib.types.path;
226+
default = null;
198227
};
199228
};
200-
201229
};
202230
};
203231

@@ -218,7 +246,6 @@ in
218246
"/media/".alias = "/var/lib/weblate/media/";
219247
"/".proxyPass = "http://unix:///run/weblate.socket";
220248
};
221-
222249
};
223250
};
224251

@@ -237,8 +264,14 @@ in
237264

238265
systemd.services.weblate-migrate = {
239266
description = "Weblate migration";
240-
after = [ "weblate-postgresql-setup.service" ];
241-
requires = [ "weblate-postgresql-setup.service" ];
267+
after = [
268+
"weblate-postgresql-setup.service"
269+
"redis-weblate.service"
270+
];
271+
requires = [
272+
"weblate-postgresql-setup.service"
273+
"redis-weblate.service"
274+
];
242275
# We want this to be active on boot, not just on socket activation
243276
wantedBy = [ "multi-user.target" ];
244277
inherit environment;
@@ -256,7 +289,7 @@ in
256289
description = "Weblate Celery";
257290
after = [
258291
"network.target"
259-
"redis.service"
292+
"redis-weblate.service"
260293
"postgresql.service"
261294
];
262295
# We want this to be active on boot, not just on socket activation
@@ -371,7 +404,7 @@ in
371404
unixSocketPerm = 770;
372405
};
373406

374-
services.postgresql = {
407+
services.postgresql = lib.mkIf cfg.configurePostgresql {
375408
enable = true;
376409
ensureUsers = [
377410
{

0 commit comments

Comments
 (0)