1 { config, pkgs, lib, ... }:
6 cfg = config.services.n8n;
7 format = pkgs.formats.json {};
8 configFile = format.generate "n8n.json" cfg.settings;
11 options.services.n8n = {
13 enable = mkEnableOption (lib.mdDoc "n8n server");
15 openFirewall = mkOption {
18 description = lib.mdDoc "Open ports in the firewall for the n8n web interface.";
24 description = lib.mdDoc ''
25 Configuration for n8n, see <https://docs.n8n.io/reference/configuration.html>
32 config = mkIf cfg.enable {
33 services.n8n.settings = {
34 # We use this to open the firewall, so we need to know about the default at eval time
35 port = lib.mkDefault 5678;
38 systemd.services.n8n = {
39 description = "N8N service";
40 after = [ "network.target" ];
41 wantedBy = [ "multi-user.target" ];
43 # This folder must be writeable as the application is storing
44 # its data in it, so the StateDirectory is a good choice
45 N8N_USER_FOLDER = "/var/lib/n8n";
46 HOME = "/var/lib/n8n";
47 N8N_CONFIG_FILES = "${configFile}";
51 ExecStart = "${pkgs.n8n}/bin/n8n";
52 Restart = "on-failure";
53 StateDirectory = "n8n";
56 NoNewPrivileges = "yes";
58 PrivateDevices = "yes";
59 DevicePolicy = "closed";
61 ProtectSystem = "strict";
62 ProtectHome = "read-only";
63 ProtectControlGroups = "yes";
64 ProtectKernelModules = "yes";
65 ProtectKernelTunables = "yes";
66 RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_NETLINK";
67 RestrictNamespaces = "yes";
68 RestrictRealtime = "yes";
69 RestrictSUIDSGID = "yes";
70 MemoryDenyWriteExecute = "no"; # v8 JIT requires memory segments to be Writable-Executable.
71 LockPersonality = "yes";
75 networking.firewall = mkIf cfg.openFirewall {
76 allowedTCPPorts = [ cfg.settings.port ];