dotfiles/modules/nixos/services/nextcloud.nix

186 lines
5.7 KiB
Nix
Raw Normal View History

2023-07-30 03:51:01 +00:00
{ config, pkgs, lib, ... }: {
2022-10-02 20:54:26 +00:00
config = lib.mkIf config.services.nextcloud.enable {
2022-10-02 20:54:26 +00:00
services.nextcloud = {
2023-07-13 03:22:45 +00:00
package = pkgs.nextcloud27; # Required to specify
2023-08-02 11:32:54 +00:00
configureRedis = true;
2023-03-03 20:54:27 +00:00
datadir = "/data/nextcloud";
2023-08-02 11:32:54 +00:00
database.createLocally = true;
2022-10-02 20:54:26 +00:00
https = true;
hostName = "localhost";
2022-10-04 12:29:29 +00:00
maxUploadSize = "50G";
2022-10-02 20:54:26 +00:00
config = {
adminpassFile = config.secrets.nextcloud.dest;
2023-08-02 11:32:54 +00:00
dbtype = "mysql";
2023-07-07 16:16:07 +00:00
extraTrustedDomains = [ config.hostnames.content ];
trustedProxies = [ "127.0.0.1" ];
2022-10-02 20:54:26 +00:00
};
2023-08-02 11:32:54 +00:00
extraOptions = { default_phone_region = "US"; };
2023-08-02 12:26:45 +00:00
extraAppsEnable = true;
extraApps = with config.services.nextcloud.package.packages.apps; {
inherit calendar contacts;
news = pkgs.nextcloudApps.news;
external = pkgs.nextcloudApps.external;
cookbook = pkgs.nextcloudApps.cookbook;
2023-08-02 12:26:45 +00:00
};
2022-10-02 20:54:26 +00:00
};
# Don't let Nginx use main ports (using Caddy instead)
2023-07-30 03:51:01 +00:00
services.nginx.enable = false;
services.phpfpm.pools.nextcloud.settings = {
"listen.owner" = config.services.caddy.user;
"listen.group" = config.services.caddy.group;
};
users.users.caddy.extraGroups = [ "nextcloud" ];
2022-10-02 20:54:26 +00:00
# Point Caddy to Nginx
2022-12-21 21:18:03 +00:00
caddy.routes = [{
2023-07-07 16:16:07 +00:00
match = [{ host = [ config.hostnames.content ]; }];
2022-10-02 20:54:26 +00:00
handle = [{
2023-07-30 03:51:01 +00:00
handler = "subroute";
routes = [
# Sets variables and headers
{
handle = [
{
handler = "vars";
root = config.services.nextcloud.package;
}
{
handler = "headers";
response.set.Strict-Transport-Security =
[ "max-age=31536000;" ];
}
];
}
{
2023-07-30 03:56:17 +00:00
match = [{ path = [ "/nix-apps*" "/store-apps*" ]; }];
2023-07-30 03:51:01 +00:00
handle = [{
handler = "vars";
root = config.services.nextcloud.home;
}];
}
# Reroute carddav and caldav traffic
{
match =
[{ path = [ "/.well-known/carddav" "/.well-known/caldav" ]; }];
handle = [{
handler = "static_response";
headers = { Location = [ "/remote.php/dav" ]; };
status_code = 301;
}];
}
# Block traffic to sensitive files
{
match = [{
path = [
"/.htaccess"
"/data/*"
"/config/*"
"/db_structure"
"/.xml"
"/README"
"/3rdparty/*"
"/lib/*"
"/templates/*"
"/occ"
"/console.php"
];
}];
handle = [{
handler = "static_response";
status_code = 404;
}];
}
# Redirect index.php to the homepage
{
match = [{
file = { try_files = [ "{http.request.uri.path}/index.php" ]; };
not = [{ path = [ "*/" ]; }];
}];
handle = [{
handler = "static_response";
headers = { Location = [ "{http.request.orig_uri.path}/" ]; };
status_code = 308;
}];
}
# Rewrite paths to be relative
{
match = [{
file = {
split_path = [ ".php" ];
try_files = [
"{http.request.uri.path}"
"{http.request.uri.path}/index.php"
"index.php"
];
};
}];
handle = [{
handler = "rewrite";
uri = "{http.matchers.file.relative}";
}];
}
# Send all PHP traffic to Nextcloud PHP service
{
match = [{ path = [ "*.php" ]; }];
handle = [{
handler = "reverse_proxy";
transport = {
protocol = "fastcgi";
split_path = [ ".php" ];
};
upstreams = [{ dial = "unix//run/phpfpm/nextcloud.sock"; }];
}];
}
# Finally, send the rest to the file server
{ handle = [{ handler = "file_server"; }]; }
];
2022-10-02 20:54:26 +00:00
}];
2023-07-30 03:51:01 +00:00
terminal = true;
2022-10-02 20:54:26 +00:00
}];
2022-10-08 15:52:05 +00:00
# Create credentials file for nextcloud
secrets.nextcloud = {
2023-03-01 04:54:48 +00:00
source = ../../../private/nextcloud.age;
dest = "${config.secretsDirectory}/nextcloud";
owner = "nextcloud";
group = "nextcloud";
permissions = "0440";
};
systemd.services.nextcloud-secret = {
2022-10-08 15:52:05 +00:00
requiredBy = [ "nextcloud-setup.service" ];
before = [ "nextcloud-setup.service" ];
};
2023-03-03 20:54:27 +00:00
# Grant user access to Nextcloud directories
users.users.${config.user}.extraGroups = [ "nextcloud" ];
2022-10-08 15:52:05 +00:00
# Open to groups, allowing for backups
systemd.services.phpfpm-nextcloud.serviceConfig.StateDirectoryMode =
lib.mkForce "0770";
# Log metrics to prometheus
2023-07-30 03:51:01 +00:00
networking.hosts."127.0.0.1" = [ config.hostnames.content ];
services.prometheus.exporters.nextcloud = {
2023-07-16 13:50:58 +00:00
enable = config.prometheus.exporters.enable;
username = config.services.nextcloud.config.adminuser;
2023-07-30 03:51:01 +00:00
url = "https://${config.hostnames.content}";
passwordFile = config.services.nextcloud.config.adminpassFile;
};
2023-07-16 13:50:58 +00:00
prometheus.scrapeTargets = [
"127.0.0.1:${
builtins.toString config.services.prometheus.exporters.nextcloud.port
}"
];
# Allows nextcloud-exporter to read passwordFile
2023-07-16 13:50:58 +00:00
users.users.nextcloud-exporter.extraGroups =
lib.mkIf config.services.prometheus.exporters.nextcloud.enable
[ "nextcloud" ];
2022-10-02 20:54:26 +00:00
};
}