{ lib, config, pkgs, ... }: let cfg = config.my.services.n8n; in { options.my.services.n8n = { enable = lib.mkEnableOption "Enable n8n module"; environment = lib.mkOption { type = lib.types.attrsOf lib.types.str; default = { }; description = "Extra environment variables for n8n"; example = { N8N_PROTOCOL = "https"; WEBHOOK_URL = "https://n8n.example.com/"; }; }; proxy = { enable = lib.mkEnableOption "Set the proxy entry for this service"; domain = lib.mkOption { default = "example.com"; type = lib.types.str; description = '' The domain where Caddy is reachable ''; }; subdomain = lib.mkOption { default = "n8n"; type = lib.types.str; description = '' n8n subdomain ''; }; host = lib.mkOption { default = "localhost"; type = lib.types.str; description = '' host name where the service is running ''; }; }; }; config = lib.mkMerge [ (lib.mkIf cfg.enable { services.n8n = { enable = true; openFirewall = true; environment = { GENERIC_TIMEZONE = "Europe/Rome"; N8N_PROTOCOL = "https"; WEBHOOK_URL = "https://${cfg.proxy.subdomain}.${cfg.proxy.domain}/"; } // cfg.environment; }; }) (lib.mkIf cfg.proxy.enable { services.caddy = with cfg.proxy; { virtualHosts."${subdomain}.${domain}".extraConfig = '' reverse_proxy http://${host}:${config.services.n8n.environment.N8N_PORT} import cloudflare_${domain} ''; }; }) ]; }