1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.alerta;
7
8 alertaConf = pkgs.writeTextFile {
9 name = "alertad.conf";
10 text = ''
11 DATABASE_URL = '${cfg.databaseUrl}'
12 DATABASE_NAME = '${cfg.databaseName}'
13 LOG_FILE = '${cfg.logDir}/alertad.log'
14 LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
15 CORS_ORIGINS = [ ${concatMapStringsSep ", " (s: "\"" + s + "\"") cfg.corsOrigins} ];
16 AUTH_REQUIRED = ${if cfg.authenticationRequired then "True" else "False"}
17 SIGNUP_ENABLED = ${if cfg.signupEnabled then "True" else "False"}
18 ${cfg.extraConfig}
19 '';
20 };
21in
22{
23 options.services.alerta = {
24 enable = mkEnableOption "alerta";
25
26 port = mkOption {
27 type = types.int;
28 default = 5000;
29 description = "Port of Alerta";
30 };
31
32 bind = mkOption {
33 type = types.str;
34 default = "0.0.0.0";
35 example = literalExample "0.0.0.0";
36 description = "Address to bind to. The default is to bind to all addresses";
37 };
38
39 logDir = mkOption {
40 type = types.path;
41 description = "Location where the logfiles are stored";
42 default = "/var/log/alerta";
43 };
44
45 databaseUrl = mkOption {
46 type = types.str;
47 description = "URL of the MongoDB or PostgreSQL database to connect to";
48 default = "mongodb://localhost";
49 example = "mongodb://localhost";
50 };
51
52 databaseName = mkOption {
53 type = types.str;
54 description = "Name of the database instance to connect to";
55 default = "monitoring";
56 example = "monitoring";
57 };
58
59 corsOrigins = mkOption {
60 type = types.listOf types.str;
61 description = "List of URLs that can access the API for Cross-Origin Resource Sharing (CORS)";
62 example = [ "http://localhost" "http://localhost:5000" ];
63 default = [ "http://localhost" "http://localhost:5000" ];
64 };
65
66 authenticationRequired = mkOption {
67 type = types.bool;
68 description = "Whether users must authenticate when using the web UI or command-line tool";
69 default = false;
70 };
71
72 signupEnabled = mkOption {
73 type = types.bool;
74 description = "Whether to prevent sign-up of new users via the web UI";
75 default = true;
76 };
77
78 extraConfig = mkOption {
79 description = "These lines go into alertad.conf verbatim.";
80 default = "";
81 type = types.lines;
82 };
83 };
84
85 config = mkIf cfg.enable {
86 systemd.tmpfiles.rules = [
87 "d '${cfg.logDir}' - alerta alerta - -"
88 ];
89
90 systemd.services.alerta = {
91 description = "Alerta Monitoring System";
92 wantedBy = [ "multi-user.target" ];
93 after = [ "networking.target" ];
94 environment = {
95 ALERTA_SVR_CONF_FILE = alertaConf;
96 };
97 serviceConfig = {
98 ExecStart = "${pkgs.alerta-server}/bin/alertad run --port ${toString cfg.port} --host ${cfg.bind}";
99 User = "alerta";
100 Group = "alerta";
101 };
102 };
103
104 environment.systemPackages = [ pkgs.alerta ];
105
106 users.users.alerta = {
107 uid = config.ids.uids.alerta;
108 description = "Alerta user";
109 };
110
111 users.groups.alerta = {
112 gid = config.ids.gids.alerta;
113 };
114 };
115}