blob: f6141e22d5c4e36854d03ea2fe7c2ac3ecf0ee29 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
{
config,
options,
lib,
pkgs,
...
}:
let
cfg = config.services.nextcloud.notify_push;
cfgN = config.services.nextcloud;
in
{
options.services.nextcloud.notify_push = {
enable = lib.mkEnableOption "Notify push";
package = lib.mkPackageOption pkgs "nextcloud-notify_push" { };
socketPath = lib.mkOption {
type = lib.types.str;
default = "/run/nextcloud-notify_push/sock";
description = "Socket path to use for notify_push";
};
logLevel = lib.mkOption {
type = lib.types.enum [
"error"
"warn"
"info"
"debug"
"trace"
];
default = "error";
description = "Log level";
};
nextcloudUrl = lib.mkOption {
type = lib.types.str;
default = "http${lib.optionalString cfgN.https "s"}://${cfgN.hostName}";
defaultText = lib.literalExpression ''"http''${lib.optionalString config.services.nextcloud.https "s"}://''${config.services.nextcloud.hostName}"'';
description = "Configure the nextcloud URL notify_push tries to connect to.";
};
bendDomainToLocalhost = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to add an entry to `/etc/hosts` for the configured nextcloud domain to point to `localhost` and add `localhost `to nextcloud's `trusted_proxies` config option.
This is useful when nextcloud's domain is not a static IP address and when the reverse proxy cannot be bypassed because the backend connection is done via unix socket.
'';
};
}
// (lib.genAttrs
[
"dbtype"
"dbname"
"dbuser"
"dbpassFile"
"dbhost"
"dbport"
"dbtableprefix"
]
(
opt:
options.services.nextcloud.config.${opt}
// {
default = config.services.nextcloud.config.${opt};
defaultText = lib.literalExpression "config.services.nextcloud.config.${opt}";
}
)
);
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = config.services.nextcloud.config.dbtype != "sqlite";
message = "notify_push only supports Nextcloud's with either a Postgres or MariaDB database, not sqlite.";
}
];
systemd.services = {
nextcloud-notify_push = {
description = "Push daemon for Nextcloud clients";
documentation = [ "https://github.com/nextcloud/notify_push" ];
after = [
"nextcloud-setup.service"
"phpfpm-nextcloud.service"
"redis-nextcloud.service"
];
requires = [
"nextcloud-setup.service"
"phpfpm-nextcloud.service"
];
wantedBy = [ "multi-user.target" ];
environment = {
NEXTCLOUD_URL = cfg.nextcloudUrl;
SOCKET_PATH = cfg.socketPath;
DATABASE_PREFIX = cfg.dbtableprefix;
LOG = cfg.logLevel;
};
script =
let
dbType = if cfg.dbtype == "pgsql" then "postgresql" else cfg.dbtype;
dbUser = lib.optionalString (cfg.dbuser != null) cfg.dbuser;
dbPass = lib.optionalString (cfg.dbpassFile != null) ":$DATABASE_PASSWORD";
dbHostHasPrefix = prefix: lib.hasPrefix prefix (toString cfg.dbhost);
isPostgresql = dbType == "postgresql";
isMysql = dbType == "mysql";
isSocket = (isPostgresql && dbHostHasPrefix "/") || (isMysql && dbHostHasPrefix "localhost:/");
dbHost = lib.optionalString (cfg.dbhost != null) (
if isSocket then lib.optionalString isMysql "@localhost" else "@${cfg.dbhost}"
);
dbOpts = lib.optionalString (cfg.dbhost != null && isSocket) (
if isPostgresql then
"?host=${cfg.dbhost}"
else if isMysql then
"?socket=${lib.removePrefix "localhost:" cfg.dbhost}"
else
throw "unsupported dbtype"
);
dbName = lib.optionalString (cfg.dbname != null) "/${cfg.dbname}";
dbUrl = "${dbType}://${dbUser}${dbPass}${dbHost}${dbName}${dbOpts}";
in
lib.optionalString (cfg.dbpassFile != null) ''
export DATABASE_PASSWORD="$(<"$CREDENTIALS_DIRECTORY/dbpass")"
''
+ ''
export DATABASE_URL="${dbUrl}"
exec ${cfg.package}/bin/notify_push '${cfgN.datadir}/config/config.php'
'';
serviceConfig = {
User = "nextcloud";
Group = "nextcloud";
RuntimeDirectory = [ "nextcloud-notify_push" ];
Restart = "on-failure";
RestartSec = "5s";
Type = "notify";
LoadCredential = lib.optional (cfg.dbpassFile != null) "dbpass:${cfg.dbpassFile}";
};
};
nextcloud-notify_push_setup = {
wantedBy = [ "multi-user.target" ];
requiredBy = [ "nextcloud-notify_push.service" ];
after = [ "nextcloud-notify_push.service" ];
serviceConfig = {
Type = "oneshot";
User = "nextcloud";
Group = "nextcloud";
ExecStart = "${lib.getExe cfgN.occ} notify_push:setup ${cfg.nextcloudUrl}/push";
LoadCredential = config.systemd.services.nextcloud-cron.serviceConfig.LoadCredential;
RestartMode = "direct";
Restart = "on-failure";
RestartSec = "5s";
};
unitConfig = {
StartLimitIntervalSec = 30;
StartLimitBurst = 5;
};
};
};
networking.hosts = lib.mkIf cfg.bendDomainToLocalhost {
"127.0.0.1" = [ cfgN.hostName ];
"::1" = [ cfgN.hostName ];
};
services = {
nginx.virtualHosts.${cfgN.hostName}.locations."^~ /push/" = {
proxyPass = "http://unix:${cfg.socketPath}";
proxyWebsockets = true;
recommendedProxySettings = lib.mkDefault true;
extraConfig = # nginx
''
# disable in case it was configured on a higher level
keepalive_timeout 0;
proxy_buffering off;
'';
};
nextcloud = {
extraApps = {
inherit (config.services.nextcloud.package.packages.apps)
notify_push
;
};
settings.trusted_proxies = lib.mkIf cfg.bendDomainToLocalhost [
"127.0.0.1"
"::1"
];
};
};
};
}
|