about summary refs log tree commit diff
path: root/nixos/modules/services/misc/pghero.nix
blob: 39515f10c8e1d1c8d2afdb699778a2a77c3dbafc (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
{ config, pkgs, lib, utils, ... }:
let
  cfg = config.services.pghero;
  settingsFormat = pkgs.formats.yaml { };
  settingsFile = settingsFormat.generate "pghero.yaml" cfg.settings;
in
{
  options.services.pghero = {
    enable = lib.mkEnableOption "PgHero service";
    package = lib.mkPackageOption pkgs "pghero" { };

    listenAddress = lib.mkOption {
      type = lib.types.str;
      example = "[::1]:3000";
      description = ''
        `hostname:port` to listen for HTTP traffic.

        This is bound using the systemd socket activation.
      '';
    };

    extraArgs = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      default = [ ];
      description = ''
        Additional command-line arguments for the systemd service.

        Refer to the [Puma web server documentation] for available arguments.

        [Puma web server documentation]: https://puma.io/puma#configuration
      '';
    };

    settings = lib.mkOption {
      type = settingsFormat.type;
      default = { };
      example = {
        databases = {
          primary = {
            url = "<%= ENV['PRIMARY_DATABASE_URL'] %>";
          };
        };
      };
      description = ''
        PgHero configuration. Refer to the [PgHero documentation] for more
        details.

        [PgHero documentation]: https://github.com/ankane/pghero/blob/master/guides/Linux.md#multiple-databases
      '';
    };

    environment = lib.mkOption {
      type = lib.types.attrsOf lib.types.str;
      default = { };
      description = ''
        Environment variables to set for the service. Secrets should be
        specified using {option}`environmentFile`.
      '';
    };

    environmentFiles = lib.mkOption {
      type = lib.types.listOf lib.types.path;
      default = [ ];
      description = ''
        File to load environment variables from. Loaded variables override
        values set in {option}`environment`.
      '';
    };

    extraGroups = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      default = [ ];
      example = [ "tlskeys" ];
      description = ''
        Additional groups for the systemd service.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.sockets.pghero = {
      unitConfig.Description = "PgHero HTTP socket";
      wantedBy = [ "sockets.target" ];
      listenStreams = [ cfg.listenAddress ];
    };

    systemd.services.pghero = {
      description = "PgHero performance dashboard for PostgreSQL";
      wantedBy = [ "multi-user.target" ];
      requires = [ "pghero.socket" ];
      after = [ "pghero.socket" "network.target" ];

      environment = {
        RAILS_ENV = "production";
        PGHERO_CONFIG_PATH = settingsFile;
      } // cfg.environment;

      serviceConfig = {
        Type = "notify";
        WatchdogSec = "10";

        ExecStart = utils.escapeSystemdExecArgs ([
          (lib.getExe cfg.package)
          "--bind-to-activated-sockets"
          "only"
        ] ++ cfg.extraArgs);
        Restart = "always";

        WorkingDirectory = "${cfg.package}/share/pghero";

        EnvironmentFile = cfg.environmentFiles;
        SupplementaryGroups = cfg.extraGroups;

        DynamicUser = true;
        UMask = "0077";

        ProtectHome = true;
        ProtectProc = "invisible";
        ProcSubset = "pid";
        ProtectClock = true;
        ProtectHostname = true;
        ProtectControlGroups = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        PrivateUsers = true;
        PrivateDevices = true;
        RestrictRealtime = true;
        RestrictNamespaces = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
        DeviceAllow = [ "" ];
        DevicePolicy = "closed";
        CapabilityBoundingSet = [ "" ];
        MemoryDenyWriteExecute = true;
        LockPersonality = true;
        SystemCallArchitectures = "native";
        SystemCallErrorNumber = "EPERM";
        SystemCallFilter = [ "@system-service" ];
      };
    };
  };
}