Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n8n: new service #1630

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/individual-docs/services/n8n.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


[comment]: # (Please add your documentation on top of this line)

@AUTOGEN_OPTIONS@
2 changes: 2 additions & 0 deletions examples/n8n/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{ ... }:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The n8n can be removed in favor of the test.

{ }
1 change: 1 addition & 0 deletions examples/n8n/devenv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allowUnfree: true
86 changes: 86 additions & 0 deletions src/modules/services/n8n.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{ config
, lib
, pkgs
, ...
}:

with lib;

Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inherit is strongly preferred to with, as it can be statically type-checked. In this case, it can be removed entirely.

Suggested change
with lib;

let
inherit (lib) types;
cfg = config.services.n8n;
format = pkgs.formats.json { };
configFile = format.generate "n8n.json" cfg.settings;
in
{
options = {
services.n8n = {
enable = mkEnableOption "n8n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enable = mkEnableOption "n8n";
enable = lib.mkEnableOption "n8n";


address = lib.mkOption {
type = types.str;
description = "Listen address";
default = "127.0.0.1";
example = "127.0.0.1";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
example = "127.0.0.1";

};

port = lib.mkOption {
type = types.port;
default = 5432;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default in their docs is 5678. 5432 is the default for postgres, so we should avoid using that.

Suggested change
default = 5432;
default = 5678;

description = ''
The TCP port to accept connections.
'';
};

webhookUrl = lib.mkOption {
type = lib.types.str;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If null, then the WEBOOK_URL env var won't be set at all, as long as you remove the quotes when setting the env var later.

Suggested change
type = lib.types.str;
type = types.nullOr types.str;

default = "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default = "";
default = null;

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 <https://docs.n8n.io/hosting/environment-variables/configuration-methods/>
for supported values.
'';
};
};
};

config = lib.mkIf cfg.enable {
assertions = [{
assertion = cfg.enable;
message = ''
To use n8n, you have to enable it. (services.n8n.enable = true;)
'';
}];
Comment on lines +56 to +61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to assert this.

Suggested change
assertions = [{
assertion = cfg.enable;
message = ''
To use n8n, you have to enable it. (services.n8n.enable = true;)
'';
}];

env = {
N8N_PORT = cfg.port;
N8N_LISTEN_ADDRESS = cfg.address;
WEBHOOK_URL = "${cfg.webhookUrl}";
N8N_CONFIG_FILES = "${configFile}";
Comment on lines +65 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work, no?

Suggested change
WEBHOOK_URL = "${cfg.webhookUrl}";
N8N_CONFIG_FILES = "${configFile}";
WEBHOOK_URL = cfg.webhookUrl;
N8N_CONFIG_FILES = configFile;

};

processes.n8n = {
exec = "${pkgs.n8n}/bin/n8n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice to have a package option in the module to let people override the package used.


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";
};
};
};
}
7 changes: 7 additions & 0 deletions tests/n8n/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test that it launches, you can add a .test.sh script in this folder and curl a health endpoint of some kind. There are lots of examples of this in the repo.

services.n8n = {
enable = true;
address = "0.0.0.0";
port = 5678;
};
}
1 change: 1 addition & 0 deletions tests/n8n/devenv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allowUnfree: true