1{ config, lib, pkgs, ... }:
2
3let
4 cfg = config.services.trilium-server;
5 configIni = pkgs.writeText "trilium-config.ini" ''
6 [General]
7 # Instance name can be used to distinguish between different instances
8 instanceName=${cfg.instanceName}
9
10 # Disable automatically generating desktop icon
11 noDesktopIcon=true
12
13 [Network]
14 # host setting is relevant only for web deployments - set the host on which the server will listen
15 host=${cfg.host}
16 # port setting is relevant only for web deployments, desktop builds run on random free port
17 port=${toString cfg.port}
18 # true for TLS/SSL/HTTPS (secure), false for HTTP (unsecure).
19 https=false
20 '';
21in
22{
23
24 options.services.trilium-server = with lib; {
25 enable = mkEnableOption "trilium-server";
26
27 dataDir = mkOption {
28 type = types.str;
29 default = "/var/lib/trilium";
30 description = ''
31 The directory storing the nodes database and the configuration.
32 '';
33 };
34
35 instanceName = mkOption {
36 type = types.str;
37 default = "Trilium";
38 description = ''
39 Instance name used to distinguish between different instances
40 '';
41 };
42
43 host = mkOption {
44 type = types.str;
45 default = "127.0.0.1";
46 description = ''
47 The host address to bind to (defaults to localhost).
48 '';
49 };
50
51 port = mkOption {
52 type = types.int;
53 default = 8080;
54 description = ''
55 The port number to bind to.
56 '';
57 };
58
59 nginx = mkOption {
60 default = {};
61 description = ''
62 Configuration for nginx reverse proxy.
63 '';
64
65 type = types.submodule {
66 options = {
67 enable = mkOption {
68 type = types.bool;
69 default = false;
70 description = ''
71 Configure the nginx reverse proxy settings.
72 '';
73 };
74
75 hostName = mkOption {
76 type = types.str;
77 description = ''
78 The hostname use to setup the virtualhost configuration
79 '';
80 };
81 };
82 };
83 };
84 };
85
86 config = lib.mkIf cfg.enable (lib.mkMerge [
87 {
88 meta.maintainers = with lib.maintainers; [ ];
89
90 users.groups.trilium = {};
91 users.users.trilium = {
92 description = "Trilium User";
93 group = "trilium";
94 home = cfg.dataDir;
95 isSystemUser = true;
96 };
97
98 systemd.services.trilium-server = {
99 wantedBy = [ "multi-user.target" ];
100 environment.TRILIUM_DATA_DIR = cfg.dataDir;
101 serviceConfig = {
102 ExecStart = "${pkgs.trilium-server}/bin/trilium-server";
103 User = "trilium";
104 Group = "trilium";
105 PrivateTmp = "true";
106 };
107 };
108
109 systemd.tmpfiles.rules = [
110 "d ${cfg.dataDir} 0750 trilium trilium - -"
111 "L+ ${cfg.dataDir}/config.ini - - - - ${configIni}"
112 ];
113
114 }
115
116 (lib.mkIf cfg.nginx.enable {
117 services.nginx = {
118 enable = true;
119 virtualHosts."${cfg.nginx.hostName}" = {
120 locations."/" = {
121 proxyPass = "http://${cfg.host}:${toString cfg.port}/";
122 extraConfig = ''
123 proxy_http_version 1.1;
124 proxy_set_header Upgrade $http_upgrade;
125 proxy_set_header Connection 'upgrade';
126 proxy_set_header Host $host;
127 proxy_cache_bypass $http_upgrade;
128 '';
129 };
130 extraConfig = ''
131 client_max_body_size 0;
132 '';
133 };
134 };
135 })
136 ]);
137}