summaryrefslogtreecommitdiff
path: root/config/pjournal.nix
blob: 721453754ce818d3e228cfc777c61e7ae2305e4e (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
{ config, lib, pkgs, ... }:
with lib;
let
  cfg = config.services.pjournal;
in
{
  options = {
    services.pjournal = {
      enable = mkOption {
        type = types.bool;
        default = false;
      };

      package = mkOption {
        type = types.package;
        default = pkgs.callPackage ../pkgs/pjournal/default.nix {};
      };

      base_url = mkOption {
        type = types.str;
        default = "http://localhost:8080";
      };

      port = mkOption {
        type = types.int;
        default = 8080;
      };
    };
  };

  config = mkIf cfg.enable {
    environment.systemPackages = [
      cfg.package
    ];

    services.postgresql = {
      enable = true;
      ensureDatabases = ["pjournal"];
      ensureUsers = [
        {
          name = "pjournal";
          ensurePermissions = {
            "DATABASE pjournal" = "ALL PRIVILEGES";
          };
        }
      ];
    };

    systemd.services.pjournal = {
      description = "pjournal instance";
      after = [ "multi-user.target" "postgresql.service" ];
      wantedBy = ["multi-user.target"];
      environment = {
        RUST_BACKTRACE = "1";
      };
      script = ''
        # This will fail the first time. Run pjournal init to initialize
        # the database.
        ${cfg.package}/bin/pjournal \
        --database-url postgres://pjournal@/pjournal \
        --port ${toString cfg.port} \
        --base-url ${cfg.base_url} 
      '';
      serviceConfig = {
        User = "pjournal";
      };
    };

  };
}