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: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;
|
|
|
|
hosts."10.0.0.${builtins.toString i}" = [ name ];
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
hostAddress = "10.31.41.${builtins.toString i}";
|
2024-01-08 17:39:45 +01:00
|
|
|
localAddress = "10.0.0.${builtins.toString 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 14:16:11 +01:00
|
|
|
locations."/".proxyPass = "https://10.31.41.${builtins.toString 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
|
|
|
};
|
|
|
|
}
|