nextcloud-container/nextcloud-container.nix

258 lines
9.4 KiB
Nix
Raw Normal View History

2023-08-31 14:48:31 -07:00
{ config, lib, pkgs, ... }@toplevel:
with lib;
let
cfg = config.services.nextcloudContainer;
hostname = config.instance.hostname;
2023-08-31 15:16:38 -07:00
hostSecrets = config.fudo.secrets.host-secrets."${hostname}";
2023-08-31 14:48:31 -07:00
mkEnvFile = envVars:
let
envLines =
mapAttrsToList (var: val: ''${var}="${toString val}"'') envVars;
in pkgs.writeText "envFile" (concatStringsSep "\n" envLines);
mkUserMap = uid: "${toString uid}:${toString uid}";
postgresPasswdFile =
pkgs.lib.passwd.stablerandom-passwd-file "nextcloud-postgres-passwd"
config.instance.build-seed;
in {
options.services.nextcloudContainer = with types; {
enable = mkEnableOption "Enable Nextcloud running in an Arion container.";
state-directory = mkOption {
type = str;
description = "Directory at which to store server state data.";
};
images = {
nextcloud = mkOption { type = str; };
postgres = mkOption { type = str; };
};
uids = {
nextcloud = mkOption {
type = int;
default = 740;
};
postgres = mkOption {
type = int;
default = 741;
};
};
port = mkOption {
type = port;
description = "Intenal port on which to listen for requests.";
default = 6093;
};
timezone = mkOption {
type = str;
default = "America/Winnipeg";
};
};
config = mkIf cfg.enable {
systemd = {
tmpfiles.rules = [
"d ${cfg.state-directory}/nextcloud 0700 nextcloud root - -"
"d ${cfg.state-directory}/data 0700 nextcloud root - -"
"d ${cfg.state-directory}/postgres 0700 nextcloud-postgres root - -"
];
services.arion-nextcloud = {
after = [ "network-online.target" ];
requires = [ "network-online.target" ];
};
};
users.users = {
nextcloud = {
isSystemUser = true;
group = "nextcloud";
uid = cfg.uids.nextcloud;
};
nextcloud-postgres = {
isSystemUser = true;
group = "nextcloud";
uid = cfg.uids.postgres;
};
};
fudo.secrets.host-secrets."${hostname}" = {
nextcloudEnv = {
source-file = mkEnvFile {
POSTGRES_HOST = "postgres";
POSTGRES_DB = "nextcloud";
POSTGRES_USER = "nextcloud";
POSTGRES_PASSWORD = readFile postgresPasswdFile;
TZ = cfg.timezone;
};
target-file = "/run/nextcloud/nextcloud.env";
};
nextcloudPostgresEnv = {
source-file = mkEnvFile {
POSTGRES_DB = "nextcloud";
POSTGRES_USER = "nextcloud";
POSTGRES_PASSWORD = readFile postgresPasswdFile;
};
target-file = "/run/nextcloud/postgres.env";
};
};
2023-08-31 14:53:44 -07:00
virtualisation.arion.projects.nextcloud.settings = let
2023-08-31 14:48:31 -07:00
image = { ... }: {
project.name = "nextcloud";
services = {
nextcloud.service = {
image = cfg.images.nextcloud;
restart = "always";
env_file = [ hostSecrets.nextcloudEnv.target-file ];
volumes = [
"${cfg.state-directory}/nextcloud:/var/www/html"
"${cfg.state-directory}/data:/data"
];
user = mkUserMap cfg.uids.nextcloud;
depends_on = [ "postgres" ];
};
postgres.service = {
image = cfg.images.postgres;
restart = "always";
command = "-c 'max_connections=300'";
env_file = [ hostSecrets.nextcloudPostgresEnv.target-file ];
volumes =
[ "${cfg.state-directory}/postgres:/var/lib/postgresql/data" ];
healthcheck = {
2023-09-02 12:47:53 -07:00
test = [ "CMD" "pg_isready" "-U" "nextcloud" "-d" "nextcloud" ];
2023-08-31 14:48:31 -07:00
start_period = "20s";
interval = "30s";
timeout = "3s";
retries = 5;
};
user = mkUserMap cfg.uids.postgres;
};
2023-09-01 20:35:12 -07:00
proxy = { lib, ... }: {
2023-08-31 14:48:31 -07:00
nixos = {
useSystemd = true;
configuration = {
boot.tmpOnTmpfs = true;
2023-08-31 15:24:24 -07:00
system.nssModules = lib.mkForce [ ];
2023-08-31 15:26:22 -07:00
systemd.services.nginx.serviceConfig.AmbientCapabilities =
lib.mkForce [ "CAP_NET_BIND_SERVICE" ];
2023-09-02 10:22:08 -07:00
services = {
nscd.enable = false;
nginx = {
enable = true;
recommendedOptimisation = true;
recommendedGzipSettings = true;
recommendedProxySettings = true;
upstreams.php-handler.extraConfig =
"server nextcloud:9000;";
virtualHosts."localhost" = {
extraConfig = ''
add_header Referrer-Policy "no-referrer" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Download-Options "noopen" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
add_header X-Robots-Tag "none" always;
add_header X-XSS-Protection "1; mode=block" always;
fastcgi_hide_header X-Powered-By;
client_max_body_size 10G;
fastcgi_buffers 64 4K;
2023-08-31 14:48:31 -07:00
'';
2023-09-02 10:22:08 -07:00
locations = {
"/robots.txt".extraConfig = ''
allow all;
log_not_found off;
2023-08-31 14:48:31 -07:00
access_log off;
'';
2023-09-02 10:22:08 -07:00
"/.well-known/carddav" = {
return =
"301 $scheme://$host:$server_port/remote.hph/dav";
};
"/.well-known/caldav" = {
return =
"301 $scheme://$host:$server_port/remote.hph/dav";
};
2023-09-02 12:47:53 -07:00
"/" = { extraConfig = "rewrite ^ /index.php;"; };
2023-09-02 10:22:08 -07:00
"~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/".extraConfig =
"deny all;";
"~ ^/(?:.|autotest|occ|issue|indie|db_|console)".extraConfig =
"deny all;";
2023-09-02 18:28:18 -07:00
"~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|oc[ms]-provider/.+)\\.php(?:$|/)" =
2023-09-02 17:39:43 -07:00
{
fastcgiParams = {
SCRIPT_FILENAME =
"$document_root$fastcgi_script_name";
PATH_INFO = "$path_info";
2023-09-02 17:45:12 -07:00
modHeadersAvailable = "true";
front_controller_active = "true";
2023-09-02 17:39:43 -07:00
};
extraConfig = ''
2023-09-02 18:28:18 -07:00
fastcgi_split_path_info ^(.+?\\.php)(\\/.*|)$;
2023-09-02 17:39:43 -07:00
set $path_info $fastcgi_path_info;
try_files $fastcgi_script_name =404;
# Enable pretty urls
fastcgi_pass php-handler;
fastcgi_intercept_errors on;
fastcgi_request_buffering off;
'';
};
2023-09-02 10:22:08 -07:00
"~ ^/(?:updater|oc[ms]-provider)(?:$|/)" = {
index = "index.php";
tryFiles = "$uri/ =404";
};
"~ .(?:css|js|woff2?|svg|gif|map)$" = {
tryFiles = "$uri /index.php$request_uri";
extraConfig = ''
add_header Cache-Control "public, max-age=15778463";
add_header Referrer-Policy "no-referrer" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Download-Options "noopen" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
add_header X-Robots-Tag "none" always;
add_header X-XSS-Protection "1; mode=block" always;
access_log off;
'';
};
"~ .(?:png|html|ttf|ico|jpg|jpeg|bcmap|mp4|webm)$" = {
tryFiles = "$uri /index.php$request_uri";
extraConfig = "access_log off;";
};
2023-08-31 14:48:31 -07:00
};
};
};
};
};
};
service = {
2023-09-01 20:35:12 -07:00
# useHostStore = true;
2023-08-31 14:48:31 -07:00
ports = [ "${toString cfg.port}:80" ];
healthcheck = {
test = [
2023-09-02 10:43:49 -07:00
"CMD"
2023-08-31 14:48:31 -07:00
''
curl -sSf 'http://localhost/status.php' | grep '"installed":true' | grep '"maintenance":false' | grep '"needsDbUpgrade":false' || exit 1''
];
start_period = "20s";
interval = "30s";
timeout = "3s";
retries = 5;
};
depends_on = [ "nextcloud" ];
};
};
};
};
in { imports = [ image ]; };
};
}