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 (lib.mdDoc "vikunja service");
14 package-api = mkOption {
15 default = pkgs.vikunja-api;
16 type = types.package;
17 defaultText = literalExpression "pkgs.vikunja-api";
18 description = lib.mdDoc "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 = lib.mdDoc "vikunja-frontend derivation to use.";
25 };
26 environmentFiles = mkOption {
27 type = types.listOf types.path;
28 default = [ ];
29 description = lib.mdDoc ''
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 = lib.mdDoc ''
39 Whether to setup NGINX.
40 Further nginx configuration can be done by changing
41 {option}`services.nginx.virtualHosts.<frontendHostname>`.
42 This does not enable TLS or ACME by default. To enable this, set the
43 {option}`services.nginx.virtualHosts.<frontendHostname>.enableACME` to
44 `true` and if appropriate do the same for
45 {option}`services.nginx.virtualHosts.<frontendHostname>.forceSSL`.
46 '';
47 };
48 frontendScheme = mkOption {
49 type = types.enum [ "http" "https" ];
50 description = lib.mdDoc ''
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 = lib.mdDoc "The Hostname under which the frontend is running.";
58 };
59 port = mkOption {
60 type = types.port;
61 default = 3456;
62 description = lib.mdDoc "The TCP port exposed by the API.";
63 };
64
65 settings = mkOption {
66 type = format.type;
67 default = {};
68 description = lib.mdDoc ''
69 Vikunja configuration. Refer to
70 <https://vikunja.io/docs/config-options/>
71 for details on supported values.
72 '';
73 };
74 database = {
75 type = mkOption {
76 type = types.enum [ "sqlite" "mysql" "postgres" ];
77 example = "postgres";
78 default = "sqlite";
79 description = lib.mdDoc "Database engine to use.";
80 };
81 host = mkOption {
82 type = types.str;
83 default = "localhost";
84 description = lib.mdDoc "Database host address. Can also be a socket.";
85 };
86 user = mkOption {
87 type = types.str;
88 default = "vikunja";
89 description = lib.mdDoc "Database user.";
90 };
91 database = mkOption {
92 type = types.str;
93 default = "vikunja";
94 description = lib.mdDoc "Database name.";
95 };
96 path = mkOption {
97 type = types.str;
98 default = "/var/lib/vikunja/vikunja.db";
99 description = lib.mdDoc "Path to the sqlite3 database file.";
100 };
101 };
102 };
103 config = lib.mkIf cfg.enable {
104 services.vikunja.settings = {
105 database = {
106 inherit (cfg.database) type host user database path;
107 };
108 service = {
109 interface = ":${toString cfg.port}";
110 frontendurl = "${cfg.frontendScheme}://${cfg.frontendHostname}/";
111 };
112 files = {
113 basepath = "/var/lib/vikunja/files";
114 };
115 };
116
117 systemd.services.vikunja-api = {
118 description = "vikunja-api";
119 after = [ "network.target" ] ++ lib.optional usePostgresql "postgresql.service" ++ lib.optional useMysql "mysql.service";
120 wantedBy = [ "multi-user.target" ];
121 path = [ cfg.package-api ];
122 restartTriggers = [ configFile ];
123
124 serviceConfig = {
125 Type = "simple";
126 DynamicUser = true;
127 StateDirectory = "vikunja";
128 ExecStart = "${cfg.package-api}/bin/vikunja";
129 Restart = "always";
130 EnvironmentFile = cfg.environmentFiles;
131 };
132 };
133
134 services.nginx.virtualHosts."${cfg.frontendHostname}" = mkIf cfg.setupNginx {
135 locations = {
136 "/" = {
137 root = cfg.package-frontend;
138 tryFiles = "try_files $uri $uri/ /";
139 };
140 "~* ^/(api|dav|\\.well-known)/" = {
141 proxyPass = "http://localhost:${toString cfg.port}";
142 extraConfig = ''
143 client_max_body_size 20M;
144 '';
145 };
146 };
147 };
148
149 environment.etc."vikunja/config.yaml".source = configFile;
150 };
151}