From c8f01dbb317a42b2d8cfb79f63fadc7b7753e395 Mon Sep 17 00:00:00 2001 From: Logger Date: Thu, 5 Dec 2024 22:02:33 -0700 Subject: [PATCH] n8n: new service --- examples/n8n/devenv.nix | 14 +++++++ src/modules/services/n8n.nix | 79 ++++++++++++++++++++++++++++++++++++ tests/n8n/devenv.nix | 7 ++++ 3 files changed, 100 insertions(+) create mode 100644 examples/n8n/devenv.nix create mode 100644 src/modules/services/n8n.nix create mode 100644 tests/n8n/devenv.nix diff --git a/examples/n8n/devenv.nix b/examples/n8n/devenv.nix new file mode 100644 index 000000000..9d1b3b42a --- /dev/null +++ b/examples/n8n/devenv.nix @@ -0,0 +1,14 @@ +{ pkgs, ... }: + +{ + packages = [ pkgs.coreutils ]; + services.n8n = { + enable = true; + address = "0.0.0.0"; + port = "5432"; + # settings = { + + # } + # webhookUrl = ""; + }; +} diff --git a/src/modules/services/n8n.nix b/src/modules/services/n8n.nix new file mode 100644 index 000000000..565d86be4 --- /dev/null +++ b/src/modules/services/n8n.nix @@ -0,0 +1,79 @@ +{ config +, lib +, pkgs +, ... +}: + +with lib; + +let + cfg = config.services.n8n; + format = pkgs.formats.json { }; + configFile = format.generate "n8n.json" cfg.settings; +in +{ + options = { + services.n8n = { + enable = mkEnableOption "n8n"; + + address = lib.mkOption { + type = types.str; + description = "Listen address"; + default = "127.0.0.1"; + example = "127.0.0.1"; + }; + + port = lib.mkOption { + type = types.port; + default = 5432; + description = '' + The TCP port to accept connections. + ''; + }; + + webhookUrl = lib.mkOption { + type = lib.types.str; + default = ""; + description = '' + WEBHOOK_URL for n8n, in case we're running behind a reverse proxy. + This cannot be set through configuration and must reside in an environment variable. + ''; + }; + + settings = lib.mkOption { + type = format.type; + default = { }; + description = '' + Configuration for n8n, see + for supported values. + ''; + }; + }; + }; + + config = lib.mkIf cfg.enable { + env = { + N8N_PORT = cfg.port; + N8N_LISTEN_ADDRESS = cfg.address; + WEBHOOK_URL = "${cfg.webhookUrl}"; + N8N_CONFIG_FILES = "${configFile}"; + }; + + processes.n8n = { + exec = "${pkgs.n8n}/bin/n8n"; + + process-compose = { + readiness_probe = { + exec.command = "${pkgs.curl}/bin/curl -f -k ${cfg.address}:${toString cfg.port}"; + initial_delay_seconds = 1; + period_seconds = 10; + timeout_seconds = 2; + success_threshold = 1; + failure_threshold = 5; + }; + + availability.restart = "on_failure"; + }; + }; + }; +} diff --git a/tests/n8n/devenv.nix b/tests/n8n/devenv.nix new file mode 100644 index 000000000..87b00c649 --- /dev/null +++ b/tests/n8n/devenv.nix @@ -0,0 +1,7 @@ +{ + services.n8n = { + enable = true; + address = "0.0.0.0"; + port = 9090; + }; +}