1{ pkgs, lib, config, ... }:
2
3with lib;
4
5let
6 cfg = config.services.vikunja;
7 format = pkgs.formats.yaml {};
8 configFile = format.generate "config.yaml" cfg.settings;
9 useMysql = cfg.database.type == "mysql";
10 usePostgresql = cfg.database.type == "postgres";
11in {
12 options.services.vikunja = with lib; {
13 enable = mkEnableOption "vikunja service";
14 package-api = mkOption {
15 default = pkgs.vikunja-api;
16 type = types.package;
17 defaultText = literalExpression "pkgs.vikunja-api";
18 description = "vikunja-api derivation to use.";
19 };
20 package-frontend = mkOption {
21 default = pkgs.vikunja-frontend;
22 type = types.package;
23 defaultText = literalExpression "pkgs.vikunja-frontend";
24 description = "vikunja-frontend derivation to use.";
25 };
26 environmentFiles = mkOption {
27 type = types.listOf types.path;
28 default = [ ];
29 description = ''
30 List of environment files set in the vikunja systemd service.
31 For example passwords should be set in one of these files.
32 '';
33 };
34 setupNginx = mkOption {
35 type = types.bool;
36 default = config.services.nginx.enable;
37 defaultText = literalExpression "config.services.nginx.enable";
38 description = ''
39 Whether to setup NGINX.
40 Further nginx configuration can be done by changing
41 <option>services.nginx.virtualHosts.<frontendHostname></option>.
42 This does not enable TLS or ACME by default. To enable this, set the
43 <option>services.nginx.virtualHosts.<frontendHostname>.enableACME</option> to
44 <literal>true</literal> and if appropriate do the same for
45 <option>services.nginx.virtualHosts.<frontendHostname>.forceSSL</option>.
46 '';
47 };
48 frontendScheme = mkOption {
49 type = types.enum [ "http" "https" ];
50 description = ''
51 Whether the site is available via http or https.
52 This does not configure https or ACME in nginx!
53 '';
54 };
55 frontendHostname = mkOption {
56 type = types.str;
57 description = "The Hostname under which the frontend is running.";
58 };
59
60 settings = mkOption {
61 type = format.type;
62 default = {};
63 description = ''
64 Vikunja configuration. Refer to
65 <link xlink:href="https://vikunja.io/docs/config-options/"/>
66 for details on supported values.
67 '';
68 };
69 database = {
70 type = mkOption {
71 type = types.enum [ "sqlite" "mysql" "postgres" ];
72 example = "postgres";
73 default = "sqlite";
74 description = "Database engine to use.";
75 };
76 host = mkOption {
77 type = types.str;
78 default = "localhost";
79 description = "Database host address. Can also be a socket.";
80 };
81 user = mkOption {
82 type = types.str;
83 default = "vikunja";
84 description = "Database user.";
85 };
86 database = mkOption {
87 type = types.str;
88 default = "vikunja";
89 description = "Database name.";
90 };
91 path = mkOption {
92 type = types.str;
93 default = "/var/lib/vikunja/vikunja.db";
94 description = "Path to the sqlite3 database file.";
95 };
96 };
97 };
98 config = lib.mkIf cfg.enable {
99 services.vikunja.settings = {
100 database = {
101 inherit (cfg.database) type host user database path;
102 };
103 service = {
104 frontendurl = "${cfg.frontendScheme}://${cfg.frontendHostname}/";
105 };
106 files = {
107 basepath = "/var/lib/vikunja/files";
108 };
109 };
110
111 systemd.services.vikunja-api = {
112 description = "vikunja-api";
113 after = [ "network.target" ] ++ lib.optional usePostgresql "postgresql.service" ++ lib.optional useMysql "mysql.service";
114 wantedBy = [ "multi-user.target" ];
115 path = [ cfg.package-api ];
116 restartTriggers = [ configFile ];
117
118 serviceConfig = {
119 Type = "simple";
120 DynamicUser = true;
121 StateDirectory = "vikunja";
122 ExecStart = "${cfg.package-api}/bin/vikunja";
123 Restart = "always";
124 EnvironmentFile = cfg.environmentFiles;
125 };
126 };
127
128 services.nginx.virtualHosts."${cfg.frontendHostname}" = mkIf cfg.setupNginx {
129 locations = {
130 "/" = {
131 root = cfg.package-frontend;
132 tryFiles = "try_files $uri $uri/ /";
133 };
134 "~* ^/(api|dav|\\.well-known)/" = {
135 proxyPass = "http://localhost:3456";
136 extraConfig = ''
137 client_max_body_size 20M;
138 '';
139 };
140 };
141 };
142
143 environment.etc."vikunja/config.yaml".source = configFile;
144 };
145}