2024-01-08 17:39:45 +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 14:16:11 +01:00
|
|
|
|
2024-01-08 17:39:45 +01:00
|
|
|
mkConfig = { name, value, i }: {
|
2024-01-07 10:26:47 +01:00
|
|
|
services.wordpress = {
|
|
|
|
webserver = "nginx";
|
|
|
|
sites.${name} = value;
|
|
|
|
};
|
|
|
|
|
2024-01-08 14:16:11 +01:00
|
|
|
services.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-08 17:39:45 +01:00
|
|
|
networking = {
|
|
|
|
hostName = mkName name;
|
2024-01-08 17:59:22 +01:00
|
|
|
hosts.${mkIp "10.0.0" i} = [ name ];
|
2024-01-08 17:39:45 +01:00
|
|
|
|
|
|
|
firewall.allowedTCPPorts = [ 443 ];
|
|
|
|
};
|
2024-01-08 14:16:11 +01:00
|
|
|
|
2024-01-08 17:39:45 +01:00
|
|
|
environment.systemPackages = [ pkgs.wp-cli ];
|
2024-01-08 14:16:11 +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-01-07 10:26:47 +01:00
|
|
|
forwardPorts = [{
|
2024-01-08 14:16:11 +01:00
|
|
|
containerPort = 443;
|
2024-01-07 10:26:47 +01:00
|
|
|
hostPort = cfg.basePort + i;
|
|
|
|
}];
|
|
|
|
|
2024-01-08 14:16:11 +01:00
|
|
|
bindMounts.certs = {
|
|
|
|
hostPath = certs.${site.name}.directory;
|
|
|
|
mountPoint = certs.${site.name}.directory;
|
|
|
|
};
|
|
|
|
|
2024-01-08 17:59:22 +01:00
|
|
|
hostAddress = mkIp "10.31.41" i;
|
|
|
|
localAddress = mkIp "10.0.0" 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-01-08 17:59:22 +01:00
|
|
|
locations."/".proxyPass =
|
|
|
|
"https://${mkIp "10.31.41" i}:${builtins.toString (cfg.basePort + i)}";
|
2024-01-07 10:26:47 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
siteList = lib.attrsToList cfg.sites;
|
|
|
|
in {
|
|
|
|
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 {
|
|
|
|
containers = builtins.listToAttrs (lib.imap0 mkContainer siteList);
|
|
|
|
|
2024-01-08 14:16:11 +01:00
|
|
|
services.nginx.virtualHosts =
|
|
|
|
builtins.listToAttrs (lib.imap0 mkVhost siteList);
|
2024-01-07 10:26:47 +01:00
|
|
|
};
|
|
|
|
}
|