1{ config, options, lib, pkgs, ... }:
2
3let
4 cfg = config.services.nextcloud.notify_push;
5 cfgN = config.services.nextcloud;
6in
7{
8 options.services.nextcloud.notify_push = {
9 enable = lib.mkEnableOption (lib.mdDoc "Notify push");
10
11 package = lib.mkOption {
12 type = lib.types.package;
13 default = pkgs.nextcloud-notify_push;
14 defaultText = lib.literalMD "pkgs.nextcloud-notify_push";
15 description = lib.mdDoc "Which package to use for notify_push";
16 };
17
18 socketPath = lib.mkOption {
19 type = lib.types.str;
20 default = "/run/nextcloud-notify_push/sock";
21 description = lib.mdDoc "Socket path to use for notify_push";
22 };
23
24 logLevel = lib.mkOption {
25 type = lib.types.enum [ "error" "warn" "info" "debug" "trace" ];
26 default = "error";
27 description = lib.mdDoc "Log level";
28 };
29
30 bendDomainToLocalhost = lib.mkOption {
31 type = lib.types.bool;
32 default = false;
33 description = lib.mdDoc ''
34 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.
35
36 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.
37 '';
38 };
39 } // (
40 lib.genAttrs [
41 "dbtype"
42 "dbname"
43 "dbuser"
44 "dbpassFile"
45 "dbhost"
46 "dbport"
47 "dbtableprefix"
48 ] (
49 opt: options.services.nextcloud.config.${opt} // {
50 default = config.services.nextcloud.config.${opt};
51 defaultText = "config.services.nextcloud.config.${opt}";
52 }
53 )
54 );
55
56 config = lib.mkIf cfg.enable {
57 systemd.services.nextcloud-notify_push = let
58 nextcloudUrl = "http${lib.optionalString cfgN.https "s"}://${cfgN.hostName}";
59 in {
60 description = "Push daemon for Nextcloud clients";
61 documentation = [ "https://github.com/nextcloud/notify_push" ];
62 after = [
63 "phpfpm-nextcloud.service"
64 "redis-nextcloud.service"
65 ];
66 wantedBy = [ "multi-user.target" ];
67 environment = {
68 NEXTCLOUD_URL = nextcloudUrl;
69 SOCKET_PATH = cfg.socketPath;
70 DATABASE_PREFIX = cfg.dbtableprefix;
71 LOG = cfg.logLevel;
72 };
73 postStart = ''
74 ${cfgN.occ}/bin/nextcloud-occ notify_push:setup ${nextcloudUrl}/push
75 '';
76 script = let
77 dbType = if cfg.dbtype == "pgsql" then "postgresql" else cfg.dbtype;
78 dbUser = lib.optionalString (cfg.dbuser != null) cfg.dbuser;
79 dbPass = lib.optionalString (cfg.dbpassFile != null) ":$DATABASE_PASSWORD";
80 isSocket = lib.hasPrefix "/" (toString cfg.dbhost);
81 dbHost = lib.optionalString (cfg.dbhost != null) (if
82 isSocket then
83 if dbType == "postgresql" then "?host=${cfg.dbhost}" else
84 if dbType == "mysql" then "?socket=${cfg.dbhost}" else throw "unsupported dbtype"
85 else
86 "@${cfg.dbhost}");
87 dbName = lib.optionalString (cfg.dbname != null) "/${cfg.dbname}";
88 dbUrl = "${dbType}://${dbUser}${dbPass}${lib.optionalString (!isSocket) dbHost}${dbName}${lib.optionalString isSocket dbHost}";
89 in lib.optionalString (dbPass != "") ''
90 export DATABASE_PASSWORD="$(<"${cfg.dbpassFile}")"
91 '' + ''
92 export DATABASE_URL="${dbUrl}"
93 ${cfg.package}/bin/notify_push '${cfgN.datadir}/config/config.php'
94 '';
95 serviceConfig = {
96 User = "nextcloud";
97 Group = "nextcloud";
98 RuntimeDirectory = [ "nextcloud-notify_push" ];
99 Restart = "on-failure";
100 RestartSec = "5s";
101 };
102 };
103
104 networking.hosts = lib.mkIf cfg.bendDomainToLocalhost {
105 "127.0.0.1" = [ cfgN.hostName ];
106 "::1" = [ cfgN.hostName ];
107 };
108
109 services = lib.mkMerge [
110 {
111 nginx.virtualHosts.${cfgN.hostName}.locations."^~ /push/" = {
112 proxyPass = "http://unix:${cfg.socketPath}";
113 proxyWebsockets = true;
114 recommendedProxySettings = true;
115 };
116 }
117
118 (lib.mkIf cfg.bendDomainToLocalhost {
119 nextcloud.extraOptions.trusted_proxies = [ "127.0.0.1" "::1" ];
120 })
121 ];
122 };
123}