summary refs log tree commit diff
path: root/nixos/modules/services/misc/ankisyncd.nix
blob: 69e471f4f577b1901c95e5d0ff780482fdec91bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.ankisyncd;

  name = "ankisyncd";

  stateDir = "/var/lib/${name}";

  authDbPath = "${stateDir}/auth.db";

  sessionDbPath = "${stateDir}/session.db";

  configFile = pkgs.writeText "ankisyncd.conf" (lib.generators.toINI {} {
    sync_app = {
      host = cfg.host;
      port = cfg.port;
      data_root = stateDir;
      auth_db_path = authDbPath;
      session_db_path = sessionDbPath;

      base_url = "/sync/";
      base_media_url = "/msync/";
    };
  });
in
  {
    options.services.ankisyncd = {
      enable = mkEnableOption "ankisyncd";

      package = mkOption {
        type = types.package;
        default = pkgs.ankisyncd;
        defaultText = literalExpression "pkgs.ankisyncd";
        description = "The package to use for the ankisyncd command.";
      };

      host = mkOption {
        type = types.str;
        default = "localhost";
        description = "ankisyncd host";
      };

      port = mkOption {
        type = types.int;
        default = 27701;
        description = "ankisyncd port";
      };

      openFirewall = mkOption {
        default = false;
        type = types.bool;
        description = "Whether to open the firewall for the specified port.";
      };
    };

    config = mkIf cfg.enable {
      networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];

      environment.etc."ankisyncd/ankisyncd.conf".source = configFile;

      systemd.services.ankisyncd = {
        description = "ankisyncd - Anki sync server";
        after = [ "network.target" ];
        wantedBy = [ "multi-user.target" ];
        path = [ cfg.package ];

        serviceConfig = {
          Type = "simple";
          DynamicUser = true;
          StateDirectory = name;
          ExecStart = "${cfg.package}/bin/ankisyncd";
          Restart = "always";
        };
      };
    };
  }