{ config, lib, options, ... }: let inherit (lib) mkEnableOption mkIf mkOption; inherit (lib.types) anything attrsOf port; cfg = config.services.wp-containers; mkConfig = { name, value }: { services.wordpress = { webserver = "nginx"; sites.${name} = value; }; networking.hostName = builtins.replaceStrings [ "." ] [ "-" ] name; }; mkContainer = i: site: { inherit (site) name; value = { privateNetwork = true; forwardPorts = [{ containerPort = 80; hostPort = cfg.basePort + i; }]; config = mkConfig site; }; }; mkVhost = i: site: { inherit (site) name; value = { enableACME = true; forceSSL = true; locations."/".proxyPass = "http://127.0.0.1:${builtins.toString (cfg.basePort + i)}"; }; }; 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); services.nginx.virtualHosts = builtins.listToAttrs (lib.imap0 mkVhost siteList); }; }