-
Notifications
You must be signed in to change notification settings - Fork 2
/
nextcloud-extras.nix
187 lines (163 loc) · 5.47 KB
/
nextcloud-extras.nix
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
{ config
, lib
, options
, ...
}: let
inherit
(lib)
optionalString
escapeShellArg
types
concatStringsSep
mapAttrsToList
mkIf
mkOption
mkDefault
mkForce
;
cfg = config.services.nextcloud;
fpm = config.services.phpfpm.pools.nextcloud;
webserver = config.services.${cfg.webserver};
in {
options = {
services.nextcloud = {
ensureUsers = mkOption {
default = {};
description = lib.mdDoc ''
List of user accounts which get automatically created if they don't
exist yet. This option does not delete accounts which are not listed
anymore.
'';
example = {
user1 = {
passwordFile = /secrets/user1-localhost;
email = "user1@localhost";
};
user2 = {
passwordFile = /secrets/user2-localhost;
email = "user2@localhost";
};
};
type = types.attrsOf (types.submodule {
options = {
passwordFile = mkOption {
type = types.path;
example = "/path/to/file";
default = null;
description = lib.mdDoc ''
Specifies the path to a file containing the
clear text password for the user.
'';
};
email = mkOption {
type = types.str;
example = "user1@localhost";
default = null;
};
};
});
};
webserver = mkOption {
type = types.enum [ "nginx" "caddy" ];
default = "nginx";
description = ''
Whether to use nginx or caddy for virtual host management.
Further nginx configuration can be done by adapting <literal>services.nginx.virtualHosts.<name></literal>.
See <xref linkend="opt-services.nginx.virtualHosts"/> for further information.
'';
};
extraOCCCommands = mkOption {
default = "";
type = types.lines;
example = "nextcloud-occ app:enable cleanup";
description = lib.mdDoc ''
Extra OCC commands which get executed after setup.
'';
};
};
};
config = mkIf cfg.enable {
systemd.services.nextcloud-ensure-users = {
enable = true;
script = ''
${optionalString (cfg.ensureUsers != {}) ''
${concatStringsSep "\n" (mapAttrsToList (name: cfg: ''
if ${config.services.nextcloud.occ}/bin/nextcloud-occ user:info "${name}" | grep "user not found"; then
export OC_PASS="$(cat ${escapeShellArg cfg.passwordFile})"
${config.services.nextcloud.occ}/bin/nextcloud-occ user:add --password-from-env "${name}"
fi
${optionalString (cfg.email != null) ''
${config.services.nextcloud.occ}/bin/nextcloud-occ user:setting "${name}" settings email "${cfg.email}"
''}
'') cfg.ensureUsers)}
''}
'';
wantedBy = [ "multi-user.target" ];
after = ["nextcloud-setup.service"];
};
systemd.services.nextcloud-extra-occ-commands = {
enable = true;
script = ''
${cfg.extraOCCCommands}
'';
wantedBy = [ "multi-user.target" ];
after = ["nextcloud-setup.service"];
};
services.phpfpm.pools.nextcloud.settings = {
"listen.owner" = webserver.user;
"listen.group" = webserver.group;
};
users.groups.nextcloud.members = [ "nextcloud" webserver.user ];
services.nginx = lib.mkIf (cfg.webserver == "caddy") {
enable = mkForce false;
};
services.caddy = lib.mkIf (cfg.webserver == "caddy") {
enable = mkDefault true;
virtualHosts."${if cfg.https then "https" else "http"}://${cfg.hostName}" = {
extraConfig = ''
encode zstd gzip
root * ${config.services.nginx.virtualHosts.${cfg.hostName}.root}
redir /.well-known/carddav /remote.php/dav 301
redir /.well-known/caldav /remote.php/dav 301
redir /.well-known/* /index.php{uri} 301
redir /remote/* /remote.php{uri} 301
header {
Strict-Transport-Security max-age=31536000
Permissions-Policy interest-cohort=()
X-Content-Type-Options nosniff
X-Frame-Options SAMEORIGIN
Referrer-Policy no-referrer
X-XSS-Protection "1; mode=block"
X-Permitted-Cross-Domain-Policies none
X-Robots-Tag "noindex, nofollow"
-X-Powered-By
}
php_fastcgi unix/${fpm.socket} {
root ${config.services.nginx.virtualHosts.${cfg.hostName}.root}
env front_controller_active true
env modHeadersAvailable true
}
@forbidden {
path /build/* /tests/* /config/* /lib/* /3rdparty/* /templates/* /data/*
path /.* /autotest* /occ* /issue* /indie* /db_* /console*
not path /.well-known/*
}
error @forbidden 404
@immutable {
path *.css *.js *.mjs *.svg *.gif *.png *.jpg *.ico *.wasm *.tflite
query v=*
}
header @immutable Cache-Control "max-age=15778463, immutable"
@static {
path *.css *.js *.mjs *.svg *.gif *.png *.jpg *.ico *.wasm *.tflite
not query v=*
}
header @static Cache-Control "max-age=15778463"
@woff2 path *.woff2
header @woff2 Cache-Control "max-age=604800"
file_server
'';
};
};
};
}