1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.ankisyncd;
7
8 name = "ankisyncd";
9
10 stateDir = "/var/lib/${name}";
11
12 authDbPath = "${stateDir}/auth.db";
13
14 sessionDbPath = "${stateDir}/session.db";
15
16 configFile = pkgs.writeText "ankisyncd.conf" (lib.generators.toINI {} {
17 sync_app = {
18 host = cfg.host;
19 port = cfg.port;
20 data_root = stateDir;
21 auth_db_path = authDbPath;
22 session_db_path = sessionDbPath;
23
24 base_url = "/sync/";
25 base_media_url = "/msync/";
26 };
27 });
28in
29 {
30 options.services.ankisyncd = {
31 enable = mkEnableOption (lib.mdDoc "ankisyncd");
32
33 package = mkOption {
34 type = types.package;
35 default = pkgs.ankisyncd;
36 defaultText = literalExpression "pkgs.ankisyncd";
37 description = lib.mdDoc "The package to use for the ankisyncd command.";
38 };
39
40 host = mkOption {
41 type = types.str;
42 default = "localhost";
43 description = lib.mdDoc "ankisyncd host";
44 };
45
46 port = mkOption {
47 type = types.port;
48 default = 27701;
49 description = lib.mdDoc "ankisyncd port";
50 };
51
52 openFirewall = mkOption {
53 default = false;
54 type = types.bool;
55 description = lib.mdDoc "Whether to open the firewall for the specified port.";
56 };
57 };
58
59 config = mkIf cfg.enable {
60 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
61
62 environment.etc."ankisyncd/ankisyncd.conf".source = configFile;
63
64 systemd.services.ankisyncd = {
65 description = "ankisyncd - Anki sync server";
66 after = [ "network.target" ];
67 wantedBy = [ "multi-user.target" ];
68 path = [ cfg.package ];
69
70 serviceConfig = {
71 Type = "simple";
72 DynamicUser = true;
73 StateDirectory = name;
74 ExecStart = "${cfg.package}/bin/ankisyncd";
75 Restart = "always";
76 };
77 };
78 };
79 }