nixos-config/configs/workhorse/nextcloud.nix

217 lines
6.7 KiB
Nix
Raw Normal View History

2020-03-06 19:02:43 +01:00
{ pkgs, ... }:
let
hostAddress = "192.168.100.10";
containerAddress = "192.168.100.11";
in {
2020-03-03 20:09:31 +01:00
containers.nextcloud = {
2020-03-06 19:02:43 +01:00
# mount host folders
2020-03-03 20:09:31 +01:00
bindMounts = {
2020-03-06 14:58:47 +01:00
rootpassword = {
hostPath = toString <secrets/nextcloud/root_password>;
mountPoint = toString <secrets/nextcloud/root_password>;
isReadOnly = true;
};
databasepassword = {
hostPath = toString <secrets/nextcloud/database_password>;
mountPoint = toString <secrets/nextcloud/database_password>;
2020-03-03 20:09:31 +01:00
isReadOnly = true;
};
home = {
2020-03-06 14:58:47 +01:00
# make sure this folder exist on the host
2020-03-03 20:09:31 +01:00
hostPath = toString "/home/nextcloud";
mountPoint = "/var/lib/nextcloud";
isReadOnly = false;
};
2020-03-06 14:58:47 +01:00
db = {
# make sure this folder exist on the host
hostPath = toString "/home/nextcloud_db";
mountPoint = "/var/lib/mysql";
isReadOnly = false;
};
krops-lib = {
mountPoint = toString <krops-lib>;
hostPath = toString <krops-lib>;
isReadOnly = true;
};
modules = {
mountPoint = toString <modules>;
hostPath = toString <modules>;
isReadOnly = true;
};
2020-03-03 20:09:31 +01:00
};
2020-03-06 19:02:43 +01:00
# container network setup
# see also nating on host system.
2020-03-03 20:09:31 +01:00
privateNetwork = true;
2020-03-06 19:02:43 +01:00
hostAddress = hostAddress;
localAddress = containerAddress;
2020-03-03 20:09:31 +01:00
autoStart = true;
2020-03-06 19:02:43 +01:00
config = { config, pkgs, lib, ... }: {
2020-03-03 20:09:31 +01:00
2020-03-06 14:58:47 +01:00
imports = [ <modules> <krops-lib> ];
2020-03-06 19:02:43 +01:00
services.nginx = {
# Use recommended settings
recommendedGzipSettings = lib.mkDefault true;
recommendedOptimisation = lib.mkDefault true;
recommendedProxySettings = lib.mkDefault true;
recommendedTlsSettings = lib.mkDefault true;
# for graylog logging
commonHttpConfig = let
access_log_sink = "${hostAddress}:12304";
error_log_sink = "${hostAddress}:12305";
in ''
log_format graylog2_json escape=json '{ "timestamp": "$time_iso8601", '
'"facility": "nginx", '
'"remote_addr": "$remote_addr", '
'"body_bytes_sent": $body_bytes_sent, '
'"request_time": $request_time, '
'"response_status": $status, '
'"request": "$request", '
'"request_method": "$request_method", '
'"host": "$host",'
'"upstream_cache_status": "$upstream_cache_status",'
'"upstream_addr": "$upstream_addr",'
'"http_x_forwarded_for": "$http_x_forwarded_for",'
'"http_referrer": "$http_referer", '
'"http_user_agent": "$http_user_agent" }';
access_log syslog:server=${access_log_sink} graylog2_json;
error_log syslog:server=${error_log_sink};
'';
};
2020-03-05 17:11:47 +01:00
# don't forget the database backup before doing this
# https://docs.nextcloud.com/server/stable/admin_manual/maintenance/backup.html
# https://docs.nextcloud.com/server/stable/admin_manual/maintenance/upgrade.html
# use snapshots in case of a rollback
nixpkgs.config.packageOverrides = super: {
nextcloud = super.nextcloud.overrideAttrs (old: rec {
name = "nextcloud-${version}";
version = "18.0.1";
src = super.fetchurl {
url =
"https://download.nextcloud.com/server/releases/nextcloud-18.0.1.tar.bz2";
sha256 = "1h0rxpdssn1hc65k41zbvww9r4f79vbd9bixc9ri5n7hp0say3vp";
};
});
};
2020-03-03 20:09:31 +01:00
networking.firewall.allowedTCPPorts = [ 80 ];
networking.firewall.allowedUDPPorts = [ 80 ];
2020-03-06 14:58:47 +01:00
# nextcloud database
# ==================
#
# set user password:
# -----------------
# #> mysql
# mysql> ALTER USER 'nextcloud'@'localhost' IDENTIFIED BY 'nextcloud-password';
#
# recreate database:
# ------------------
# mysql> DROP DATABASE nextcloud;
# mysql> CREATE DATABASE nextcloud;
#
# migration:
# ----------
# nextcloud-occ db:convert-type --all-apps mysql nextcloud 127.0.0.1 nextcloud
#
# 4-byte stuff:
# -------------
# https://docs.nextcloud.com/server/18/admin_manual/configuration_database/mysql_4byte_support.html
# if you do this don't forget --default-character-set=utf8mb4 for mysqldump
services.mysql = {
enable = true;
package = pkgs.mysql;
ensureDatabases = [ "nextcloud" ];
ensureUsers = [{
name = "nextcloud";
ensurePermissions = { "nextcloud.*" = "ALL PRIVILEGES"; };
}];
extraOptions = ''
innodb_large_prefix=true
innodb_file_format=barracuda
innodb_file_per_table=1
'';
};
# nextcloud setup
later.services.nextcloud = {
2020-03-03 20:09:31 +01:00
enable = true;
autoUpdateApps.enable = true;
nginx.enable = true;
hostName = "nextcloud.ingolf-wagner.de";
2020-03-05 17:11:47 +01:00
#logLevel = 0;
2020-03-06 14:58:47 +01:00
https = true;
config = {
adminpassFile = toString <secrets/nextcloud/root_password>;
overwriteProtocol = "https";
2020-03-06 19:02:43 +01:00
trustedProxies = [ "195.201.134.247" hostAddress ];
2020-03-06 14:58:47 +01:00
dbtype = "mysql";
dbpassFile = toString config.krops.userKeys."nextcloud".target;
dbport = 3306;
};
};
# provide password file for database with proper rights
krops.userKeys."nextcloud" = {
user = "nextcloud";
source = toString <secrets/nextcloud/database_password>;
requiredBy = [ "nginx.service" ];
2020-03-03 20:09:31 +01:00
};
2020-03-04 13:50:41 +01:00
environment.systemPackages = [ pkgs.smbclient ];
2020-03-06 19:02:43 +01:00
# send log to host systems graylog (use tinc or wireguard if host is not graylog)
services.SystemdJournal2Gelf.enable = true;
services.SystemdJournal2Gelf.graylogServer = "${hostAddress}:11201";
2020-03-03 20:09:31 +01:00
};
};
2020-03-03 18:45:35 +01:00
2020-03-03 23:20:34 +01:00
# give containers internet access
networking.nat.enable = true;
networking.nat.internalInterfaces = [ "ve-nextcloud" ];
networking.nat.externalInterface = "eth0";
# don't let networkmanager manger container network
networking.networkmanager.unmanaged = [ "interface-name:ve-*" ];
2020-03-06 19:02:43 +01:00
# open ports for logging
networking.firewall.interfaces."ve-nextcloud".allowedTCPPorts =
[ 11201 12304 12305 ];
networking.firewall.interfaces."ve-nextcloud".allowedUDPPorts =
[ 11201 12304 12305 ];
2020-03-03 20:09:31 +01:00
# host nginx setup
services.nginx = {
enable = true;
2020-03-04 07:37:03 +01:00
recommendedProxySettings = true;
2020-03-03 20:09:31 +01:00
virtualHosts = {
"nextcloud.workhorse.private" = {
serverAliases = [
"nextcloud.ingolf-wagner.de"
# "nextcloud.gaykraft.com"
];
2020-03-03 20:09:31 +01:00
locations."/" = {
2020-03-06 19:02:43 +01:00
proxyPass = "http://${containerAddress}";
2020-03-03 20:09:31 +01:00
extraConfig = ''
# allow big uploads
# -----------------
client_max_body_size 0;
2020-03-03 20:09:31 +01:00
'';
};
};
};
};
2020-03-03 18:45:35 +01:00
}