1{ lib, pkgs, config, ... }:
2
3with lib;
4
5let
6 cfg = config.services.wiki-js;
7
8 format = pkgs.formats.json { };
9
10 configFile = format.generate "wiki-js.yml" cfg.settings;
11in {
12 options.services.wiki-js = {
13 enable = mkEnableOption "wiki-js";
14
15 environmentFile = mkOption {
16 type = types.nullOr types.path;
17 default = null;
18 example = "/root/wiki-js.env";
19 description = ''
20 Environment file to inject e.g. secrets into the configuration.
21 '';
22 };
23
24 stateDirectoryName = mkOption {
25 default = "wiki-js";
26 type = types.str;
27 description = ''
28 Name of the directory in {file}`/var/lib`.
29 '';
30 };
31
32 settings = mkOption {
33 default = {};
34 type = types.submodule {
35 freeformType = format.type;
36 options = {
37 port = mkOption {
38 type = types.port;
39 default = 3000;
40 description = ''
41 TCP port the process should listen to.
42 '';
43 };
44
45 bindIP = mkOption {
46 default = "0.0.0.0";
47 type = types.str;
48 description = ''
49 IPs the service should listen to.
50 '';
51 };
52
53 db = {
54 type = mkOption {
55 default = "postgres";
56 type = types.enum [ "postgres" "mysql" "mariadb" "mssql" ];
57 description = ''
58 Database driver to use for persistence. Please note that `sqlite`
59 is currently not supported as the build process for it is currently not implemented
60 in `pkgs.wiki-js` and it's not recommended by upstream for
61 production use.
62 '';
63 };
64 host = mkOption {
65 type = types.str;
66 example = "/run/postgresql";
67 description = ''
68 Hostname or socket-path to connect to.
69 '';
70 };
71 db = mkOption {
72 default = "wiki";
73 type = types.str;
74 description = ''
75 Name of the database to use.
76 '';
77 };
78 };
79
80 logLevel = mkOption {
81 default = "info";
82 type = types.enum [ "error" "warn" "info" "verbose" "debug" "silly" ];
83 description = ''
84 Define how much detail is supposed to be logged at runtime.
85 '';
86 };
87
88 offline = mkEnableOption "offline mode" // {
89 description = ''
90 Disable latest file updates and enable
91 [sideloading](https://docs.requarks.io/install/sideload).
92 '';
93 };
94 };
95 };
96 description = ''
97 Settings to configure `wiki-js`. This directly
98 corresponds to [the upstream configuration options](https://docs.requarks.io/install/config).
99
100 Secrets can be injected via the environment by
101 - specifying [](#opt-services.wiki-js.environmentFile)
102 to contain secrets
103 - and setting sensitive values to `$(ENVIRONMENT_VAR)`
104 with this value defined in the environment-file.
105 '';
106 };
107 };
108
109 config = mkIf cfg.enable {
110 services.wiki-js.settings.dataPath = "/var/lib/${cfg.stateDirectoryName}";
111 systemd.services.wiki-js = {
112 description = "A modern and powerful wiki app built on Node.js";
113 documentation = [ "https://docs.requarks.io/" ];
114 wantedBy = [ "multi-user.target" ];
115
116 path = with pkgs; [
117 # Needed for git storage.
118 git
119 # Needed for git+ssh storage.
120 openssh
121 ];
122
123 preStart = ''
124 ln -sf ${configFile} /var/lib/${cfg.stateDirectoryName}/config.yml
125 ln -sf ${pkgs.wiki-js}/server /var/lib/${cfg.stateDirectoryName}
126 ln -sf ${pkgs.wiki-js}/assets /var/lib/${cfg.stateDirectoryName}
127 ln -sf ${pkgs.wiki-js}/package.json /var/lib/${cfg.stateDirectoryName}/package.json
128 '';
129
130 serviceConfig = {
131 EnvironmentFile = mkIf (cfg.environmentFile != null) cfg.environmentFile;
132 StateDirectory = cfg.stateDirectoryName;
133 WorkingDirectory = "/var/lib/${cfg.stateDirectoryName}";
134 DynamicUser = true;
135 PrivateTmp = true;
136 ExecStart = "${pkgs.nodejs_18}/bin/node ${pkgs.wiki-js}/server";
137 };
138 };
139 };
140
141 meta.maintainers = with maintainers; [ ma27 ];
142}