1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.kibana;
7
8 cfgFile = pkgs.writeText "kibana.json" (builtins.toJSON (
9 (filterAttrsRecursive (n: v: v != null) ({
10 server.host = cfg.listenAddress;
11 server.port = cfg.port;
12 server.ssl.certificate = cfg.cert;
13 server.ssl.key = cfg.key;
14
15 kibana.index = cfg.index;
16 kibana.defaultAppId = cfg.defaultAppId;
17
18 elasticsearch.url = cfg.elasticsearch.url;
19 elasticsearch.username = cfg.elasticsearch.username;
20 elasticsearch.password = cfg.elasticsearch.password;
21
22 elasticsearch.ssl.certificate = cfg.elasticsearch.cert;
23 elasticsearch.ssl.key = cfg.elasticsearch.key;
24 elasticsearch.ssl.certificateAuthorities = cfg.elasticsearch.certificateAuthorities;
25 } // cfg.extraConf)
26 )));
27
28in {
29 options.services.kibana = {
30 enable = mkEnableOption "enable kibana service";
31
32 listenAddress = mkOption {
33 description = "Kibana listening host";
34 default = "127.0.0.1";
35 type = types.str;
36 };
37
38 port = mkOption {
39 description = "Kibana listening port";
40 default = 5601;
41 type = types.int;
42 };
43
44 cert = mkOption {
45 description = "Kibana ssl certificate.";
46 default = null;
47 type = types.nullOr types.path;
48 };
49
50 key = mkOption {
51 description = "Kibana ssl key.";
52 default = null;
53 type = types.nullOr types.path;
54 };
55
56 index = mkOption {
57 description = "Elasticsearch index to use for saving kibana config.";
58 default = ".kibana";
59 type = types.str;
60 };
61
62 defaultAppId = mkOption {
63 description = "Elasticsearch default application id.";
64 default = "discover";
65 type = types.str;
66 };
67
68 elasticsearch = {
69 url = mkOption {
70 description = "Elasticsearch url";
71 default = "http://localhost:9200";
72 type = types.str;
73 };
74
75 username = mkOption {
76 description = "Username for elasticsearch basic auth.";
77 default = null;
78 type = types.nullOr types.str;
79 };
80
81 password = mkOption {
82 description = "Password for elasticsearch basic auth.";
83 default = null;
84 type = types.nullOr types.str;
85 };
86
87 ca = mkOption {
88 description = ''
89 CA file to auth against elasticsearch.
90
91 It's recommended to use the <option>certificateAuthorities</option> option
92 when using kibana-5.4 or newer.
93 '';
94 default = null;
95 type = types.nullOr types.path;
96 };
97
98 certificateAuthorities = mkOption {
99 description = ''
100 CA files to auth against elasticsearch.
101
102 Please use the <option>ca</option> option when using kibana < 5.4
103 because those old versions don't support setting multiple CA's.
104
105 This defaults to the singleton list [ca] when the <option>ca</option> option is defined.
106 '';
107 default = if isNull cfg.elasticsearch.ca then [] else [ca];
108 type = types.listOf types.path;
109 };
110
111 cert = mkOption {
112 description = "Certificate file to auth against elasticsearch.";
113 default = null;
114 type = types.nullOr types.path;
115 };
116
117 key = mkOption {
118 description = "Key file to auth against elasticsearch.";
119 default = null;
120 type = types.nullOr types.path;
121 };
122 };
123
124 package = mkOption {
125 description = "Kibana package to use";
126 default = pkgs.kibana;
127 defaultText = "pkgs.kibana";
128 example = "pkgs.kibana5";
129 type = types.package;
130 };
131
132 dataDir = mkOption {
133 description = "Kibana data directory";
134 default = "/var/lib/kibana";
135 type = types.path;
136 };
137
138 extraConf = mkOption {
139 description = "Kibana extra configuration";
140 default = {};
141 type = types.attrs;
142 };
143 };
144
145 config = mkIf (cfg.enable) {
146 systemd.services.kibana = {
147 description = "Kibana Service";
148 wantedBy = [ "multi-user.target" ];
149 after = [ "network.target" "elasticsearch.service" ];
150 environment = { BABEL_CACHE_PATH = "${cfg.dataDir}/.babelcache.json"; };
151 serviceConfig = {
152 ExecStart = "${cfg.package}/bin/kibana --config ${cfgFile}";
153 User = "kibana";
154 WorkingDirectory = cfg.dataDir;
155 };
156 };
157
158 environment.systemPackages = [ cfg.package ];
159
160 users.users = singleton {
161 name = "kibana";
162 uid = config.ids.uids.kibana;
163 description = "Kibana service user";
164 home = cfg.dataDir;
165 createHome = true;
166 };
167 };
168}