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