summaryrefslogtreecommitdiff
path: root/modules/hledger-web.nix
blob: 31ced5bdb71caf01f909b4406cfafb278ea9bf49 (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
{ config, lib, pkgs, ... }:

let
  cfg = config.orbekk.hledger-web;
  aliases = import ../data/aliases.nix;
in {
  options = {
    orbekk.hledger-web = {
      enable = lib.mkEnableOption "Enable hledger-web";
      journalFile = lib.mkOption {
        type = lib.types.str;
        description = "Path to journal file";
      };
      port = lib.mkOption {
        type = lib.types.int;
        default = aliases.services.hledger-web.port;
      };
      domain = lib.mkOption {
        type = lib.types.str;
        default = "money.orbekk.com";
      };
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.hledger-web = {
      description = "Hledger Web";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      path = with pkgs; [ hledger hledger-web git ];

      serviceConfig = {
        User = "hledger";
        Group = "hledger";
        StateDirectory = "hledger-web";
        StateDirectoryMode = "770";
      };

      script = ''
        cd /var/lib/hledger-web
        hledger-web -f ${cfg.journalFile} --port ${toString cfg.port} --serve --base-url "https://${cfg.domain}"
      '';
    };

    services.nginx.virtualHosts."${cfg.domain}" = {
      enableACME = true;
      forceSSL = true;
      locations."/" = {
        proxyPass = "http://127.0.0.1:${toString cfg.port}";
        proxyWebsockets = true;
        extraConfig = ''
          auth_basic money;
          auth_basic_user_file "/opt/secret/nginx-money.htpasswd";
        '';
      };
    };
  };
}