2024-12-12 14:41:43 +01:00
|
|
|
# SPDX-FileCopyrightText: 2024 Tom Hubrecht <tom.hubrecht@dgnum.eu>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: EUPL-1.2
|
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
2024-01-07 10:26:47 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
inherit (lib) mkEnableOption mkIf mkOption;
|
|
|
|
inherit (lib.types) anything attrsOf port;
|
|
|
|
|
2024-01-08 14:16:11 +01:00
|
|
|
inherit (config.security.acme) certs;
|
|
|
|
|
2024-01-07 10:26:47 +01:00
|
|
|
cfg = config.services.wp-containers;
|
|
|
|
|
2024-01-08 14:16:11 +01:00
|
|
|
mkName = builtins.replaceStrings [ "." ] [ "-" ];
|
2024-01-08 17:59:22 +01:00
|
|
|
mkIp = ip: i: "${ip}.${builtins.toString i}";
|
2024-01-08 18:01:32 +01:00
|
|
|
mkHost = mkIp "10.31.41";
|
|
|
|
mkLocal = mkIp "10.0.0";
|
2024-01-08 14:16:11 +01:00
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
mkConfig =
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
i,
|
|
|
|
}:
|
|
|
|
{
|
2024-02-06 17:04:16 +01:00
|
|
|
services = {
|
|
|
|
wordpress = {
|
|
|
|
webserver = "nginx";
|
|
|
|
sites.${name} = value;
|
|
|
|
};
|
|
|
|
|
|
|
|
nginx.virtualHosts.${name} = {
|
|
|
|
onlySSL = true;
|
|
|
|
sslCertificate = "${certs.${name}.directory}/fullchain.pem";
|
|
|
|
sslCertificateKey = "${certs.${name}.directory}/key.pem";
|
|
|
|
sslTrustedCertificate = "${certs.${name}.directory}/chain.pem";
|
|
|
|
};
|
2024-01-07 10:26:47 +01:00
|
|
|
|
2024-02-06 17:04:16 +01:00
|
|
|
# Increase the max post size
|
|
|
|
phpfpm.pools."wordpress-${name}".phpOptions = ''
|
|
|
|
post_max_size = 64M;
|
|
|
|
upload_max_filesize = 64M;
|
|
|
|
memory_limit = 128M;
|
|
|
|
'';
|
2024-02-02 10:51:31 +01:00
|
|
|
};
|
2024-01-08 14:16:11 +01:00
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
networking = {
|
|
|
|
hostName = mkName name;
|
|
|
|
hosts.${mkLocal i} = [ name ];
|
2024-01-08 17:39:45 +01:00
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
firewall.allowedTCPPorts = [ 443 ];
|
|
|
|
};
|
2024-01-08 14:16:11 +01:00
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
environment.systemPackages = [
|
|
|
|
pkgs.wp-cli
|
|
|
|
pkgs.neovim
|
|
|
|
];
|
2024-01-08 14:16:11 +01:00
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
system.stateVersion = "23.11";
|
|
|
|
};
|
2024-01-07 10:26:47 +01:00
|
|
|
|
|
|
|
mkContainer = i: site: {
|
2024-01-08 14:16:11 +01:00
|
|
|
name = mkName site.name;
|
2024-01-07 10:26:47 +01:00
|
|
|
|
|
|
|
value = {
|
|
|
|
privateNetwork = true;
|
2024-01-08 14:16:11 +01:00
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
forwardPorts = [
|
|
|
|
{
|
|
|
|
containerPort = 443;
|
|
|
|
hostPort = cfg.basePort + i;
|
|
|
|
}
|
|
|
|
];
|
2024-01-07 10:26:47 +01:00
|
|
|
|
2024-01-08 14:16:11 +01:00
|
|
|
bindMounts.certs = {
|
|
|
|
hostPath = certs.${site.name}.directory;
|
|
|
|
mountPoint = certs.${site.name}.directory;
|
|
|
|
};
|
|
|
|
|
2024-01-08 18:01:32 +01:00
|
|
|
hostAddress = mkHost i;
|
|
|
|
localAddress = mkLocal i;
|
2024-01-08 14:16:11 +01:00
|
|
|
|
|
|
|
autoStart = true;
|
|
|
|
|
2024-01-08 17:39:45 +01:00
|
|
|
config = mkConfig (site // { inherit i; });
|
2024-01-07 10:26:47 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkVhost = i: site: {
|
|
|
|
inherit (site) name;
|
|
|
|
value = {
|
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
locations."/".proxyPass = "https://${mkHost i}:${builtins.toString (cfg.basePort + i)}";
|
2024-01-07 10:26:47 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
siteList = lib.attrsToList cfg.sites;
|
2024-02-02 10:51:31 +01:00
|
|
|
in
|
|
|
|
{
|
2024-01-07 10:26:47 +01:00
|
|
|
options.services.wp-containers = {
|
|
|
|
enable = mkEnableOption "wordpress sites in containers";
|
|
|
|
|
|
|
|
basePort = mkOption {
|
|
|
|
type = port;
|
|
|
|
default = 9090;
|
|
|
|
};
|
|
|
|
|
|
|
|
sites = mkOption {
|
|
|
|
type = attrsOf anything;
|
|
|
|
default = { };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2024-01-08 20:10:04 +01:00
|
|
|
containers = builtins.listToAttrs (lib.imap1 mkContainer siteList);
|
2024-01-07 10:26:47 +01:00
|
|
|
|
2024-02-02 10:51:31 +01:00
|
|
|
services.nginx.virtualHosts = builtins.listToAttrs (lib.imap1 mkVhost siteList);
|
2024-01-07 10:26:47 +01:00
|
|
|
};
|
|
|
|
}
|