nix/modules/services/n8n.nix
2026-02-15 23:20:36 +01:00

88 lines
1.8 KiB
Nix

{
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;
};
# Add npm/nodejs to n8n's PATH
systemd.services.n8n.path = [
pkgs.nodejs
pkgs.gnutar
pkgs.gzip
];
})
(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}
'';
};
})
];
}