summaryrefslogtreecommitdiff
path: root/modules/fathom.nix
blob: dee34b9fb62c358e9b95921903e8804b13f28b78 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{ options
, lib
, config
, pkgs
, modulesPath
}:

with lib;

let
  cfg = config.services.fathom;
in
{
  options.services.fathom = {
    enable = lib.mkEnableOption "Enable the Fathom Analytics service";

    port = mkOption {
      type = types.string;
      default = "3000";
      description = ''
        The port on which Fathom will listen for
        incoming HTTP traffic.
      '';
    };

    gzip = mkOption {
      type = types.bool;
      default = true;
      description = "Whether or not to enable gzip compression.";
    };

    debug = mkOption {
      type = types.bool;
      default = false;
      description = "Whether or not to enable debug mode.";
    };

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/fathom";
      description = "Fathom data directory";
    };
  };

  config = mkIf cfg.enable {
    systemd.services.fathom = {
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      environment = {
        FATHOM_SERVER_ADDR = cfg.port;
        FATHOM_GZIP = builtins.toString cfg.gzip;
        FATHOM_DEBUG = builtins.toString cfg.debug;
        FATHOM_DATABASE_DRIVER = "sqlite3";
        FATHOM_DATABASE_NAME = "${cfg.dataDir}/fathom.db";
        FATHOM_SECRET = "random-secret-string";
      };
      preStart = ''
        echo "[fathom] creating ${cfg.dataDir}"
        mkdir -p ${cfg.dataDir}
        chown -R fathom:fathom ${cfg.dataDir}
        echo "[fathom]" creating ${cfg.dataDir}/.env
        env | grep "^FATHOM" > ${cfg.dataDir}/.env
      '';
      description = ''
        Fathom Analytics
      '';

      serviceConfig = {
        Type = "simple";
        User = "fathom";
        Group = "fathom";
        ExecStart = "${pkgs.fathom}/bin/fathom server";
        KillSignal = "INT";
        WorkingDirectory = cfg.dataDir;
        Restart = "on-failure";
        RestartSec = "10";
        PermissionsStartOnly = "true";
      };
    };

    environment.systemPackages = [ pkgs.fathom ];

    users = {
      groups = { fathom = {}; };
      users.fathom = {
        description = "Fathom daemon user";
        home = cfg.dataDir;
        group = "fathom";
      };
    };
  };
}