summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Kampka <christian@kampka.net>2019-12-06 14:20:39 +0100
committerChristian Kampka <christian@kampka.net>2019-12-19 10:14:13 +0100
commit9f97485399e8766565374126a4278292e4e82623 (patch)
tree405a53b2d67a3011418d8851238f586c5e10e176
parent82f038d46861062140511c55ece98bb37954dc93 (diff)
downloadnixpkgs-9f97485399e8766565374126a4278292e4e82623.tar
nixpkgs-9f97485399e8766565374126a4278292e4e82623.tar.gz
nixpkgs-9f97485399e8766565374126a4278292e4e82623.tar.bz2
nixpkgs-9f97485399e8766565374126a4278292e4e82623.tar.lz
nixpkgs-9f97485399e8766565374126a4278292e4e82623.tar.xz
nixpkgs-9f97485399e8766565374126a4278292e4e82623.tar.zst
nixpkgs-9f97485399e8766565374126a4278292e4e82623.zip
trilium-server: Add nginx reverse proxy configuration to module
-rw-r--r--nixos/modules/services/web-apps/trilium.nix54
-rw-r--r--nixos/tests/trilium-server.nix16
2 files changed, 68 insertions, 2 deletions
diff --git a/nixos/modules/services/web-apps/trilium.nix b/nixos/modules/services/web-apps/trilium.nix
index 6fdbef9e617..6f47193c62b 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 ca9e8ba2c56..6346575b33d 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/"
+          )
     '';
 })