diff --git a/nixos/modules/services/web-apps/trilium.nix b/nixos/modules/services/web-apps/trilium.nix index 6fdbef9e6175..6f47193c62b9 100644 --- a/nixos/modules/services/web-apps/trilium.nix +++ b/nixos/modules/services/web-apps/trilium.nix @@ -55,9 +55,36 @@ in The port number to bind to. ''; }; + + nginx = mkOption { + default = {}; + description = '' + Configuration for nginx reverse proxy. + ''; + + type = types.submodule { + options = { + enable = mkOption { + type = types.bool; + default = false; + description = '' + Configure the nginx reverse proxy settings. + ''; + }; + + hostName = mkOption { + type = types.str; + description = '' + The hostname use to setup the virtualhost configuration + ''; + }; + }; + }; + }; }; - config = lib.mkIf cfg.enable { + config = lib.mkIf cfg.enable (lib.mkMerge [ + { meta.maintainers = with lib.maintainers; [ kampka ]; users.groups.trilium = {}; @@ -83,5 +110,28 @@ in "d ${cfg.dataDir} 0750 trilium trilium - -" "L+ ${cfg.dataDir}/config.ini - - - - ${configIni}" ]; - }; + + } + + (lib.mkIf cfg.nginx.enable { + services.nginx = { + enable = true; + virtualHosts."${cfg.nginx.hostName}" = { + locations."/" = { + proxyPass = "http://${cfg.host}:${toString cfg.port}/"; + extraConfig = '' + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + ''; + }; + extraConfig = '' + client_max_body_size 0; + ''; + }; + }; + }) + ]); } diff --git a/nixos/tests/trilium-server.nix b/nixos/tests/trilium-server.nix index ca9e8ba2c56f..6346575b33da 100644 --- a/nixos/tests/trilium-server.nix +++ b/nixos/tests/trilium-server.nix @@ -10,6 +10,14 @@ import ./make-test-python.nix ({ ... }: { dataDir = "/data/trilium"; }; }; + + nginx = { + services.trilium-server = { + enable = true; + nginx.enable = true; + nginx.hostName = "trilium.example.com"; + }; + }; }; testScript = @@ -33,5 +41,13 @@ import ./make-test-python.nix ({ ... }: { with subtest("configured with custom data store"): configured.wait_for_unit("trilium-server.service") configured.succeed("test -f /data/trilium/document.db") + + with subtest("nginx with custom host name"): + nginx.wait_for_unit("trilium-server.service") + nginx.wait_for_unit("nginx.service") + + nginx.succeed( + "curl --resolve 'trilium.example.com:80:127.0.0.1' http://trilium.example.com/" + ) ''; })