forked from teslamate-org/teslamate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.nix
295 lines (266 loc) Β· 9.27 KB
/
module.nix
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
{ self }:
{ config, lib, pkgs, ...}:
let
teslamate = self.packages.${pkgs.system}.default;
cfg = config.services.teslamate;
inherit (lib) mkEnableOption mkOption types mkIf mkMerge getExe literalExpression;
in {
options.services.teslamate = {
enable = mkEnableOption "Teslamate";
secretsFile = mkOption {
type = types.str;
example = "/run/secrets/teslamate.env";
description = lib.mdDoc ''
Path to an env file containing the secrets used by TeslaMate.
Must contain at least:
- `ENCRYPTION_KEY` - encryption key used to encrypt database
- `DATABASE_PASS` - password used to authenticate to database
- `RELEASE_COOKIE` - unique value used by elixir for clustering
'';
};
autoStart = mkOption {
type = types.bool;
default = true;
description = "Whether to start teslamate on boot.";
};
listenAddress = mkOption {
type = with types; nullOr str;
default = null;
example = "127.0.0.1";
description = "IP address where the web interface is exposed or `null` for all addresses";
};
port = mkOption {
type = types.port;
default = 4000;
description = "Port the TeslaMate service will listen on";
};
virtualHost = mkOption {
type = types.str;
default = if config.networking.domain == null then "localhost" else config.networking.fqdn;
defaultText = literalExpression ''
if config.networking.domain == null then "localhost" else config.networking.fqdn
'';
description = "Host part used for generating URLs throughout the app. Will be combined with urlPath";
};
urlPath = mkOption {
type = types.str;
default = "/";
description = "Path prefix used for generating URLs throughout the app. Will be combined with virtualHost";
};
postgres = {
enable_server = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc ''
Whether to create a postgres server with the recommended configuration.
Other settings will still be used even if `enable` is false to configure
database connection.
'';
};
user = mkOption {
type = types.str;
default = "teslamate";
description = "PostgresQL database user";
};
database = mkOption {
type = types.str;
default = "teslamate";
description = "PostgresQL database to connect to";
};
host = mkOption {
type = types.str;
default = "127.0.0.1";
description = "Hostname of the database server";
};
port = mkOption {
type = types.port;
default = 5432;
description = "Postgresql database port. Must be correct even if `services.teslamate.postgres.enable` is false";
};
};
grafana = {
enable = mkOption {
type = types.bool;
default = false;
description = "Whether to create and provision grafana with the TeslaMate dashboards";
};
listenAddress = mkOption {
type = types.str;
default = "0.0.0.0";
description = "IP address for grafana to listen to.";
};
port = mkOption {
type = types.port;
default = 3000;
description = "Port for grafana web service";
};
urlPath = mkOption {
type = types.str;
default = "/";
description = "Path that grafana is mounted on. Useful if using a reverse proxy to vend teslamate and grafana on the same port";
};
};
mqtt = {
enable = mkEnableOption "TeslaMate MQTT integration";
host = mkOption {
type = types.str;
default = "127.0.0.1";
description = "MQTT host";
};
port = mkOption {
type = with types; nullOr port;
default = null;
example = 1883;
description = "MQTT port.";
};
};
};
config = mkIf cfg.enable
(mkMerge [
{
users.users.teslamate = {
isSystemUser = true;
group = "teslamate";
home = "/var/lib/teslamate";
createHome = true;
};
users.groups.teslamate = {};
systemd.services.teslamate = {
description = "TeslaMate";
after = [ "network.target" "postgresql.service" ];
wantedBy = mkIf cfg.autoStart [ "multi-user.target" ];
serviceConfig = {
User = "teslamate";
Restart = "on-failure";
RestartSec = 5;
WorkingDirectory = "/var/lib/teslamate";
ExecStartPre = ''${getExe teslamate} eval "TeslaMate.Release.migrate"'';
ExecStart = "${getExe teslamate} start";
ExecStop = "${getExe teslamate} stop";
EnvironmentFile = cfg.secretsFile;
};
environment = mkMerge [
{
PORT = toString cfg.port;
DATABASE_USER = cfg.postgres.user;
DATABASE_NAME = cfg.postgres.database;
DATABASE_HOST = cfg.postgres.host;
DATABASE_PORT = toString cfg.postgres.port;
VIRTUAL_HOST = cfg.virtualHost;
URL_PATH = cfg.urlPath;
HTTP_BINDING_ADDRESS = mkIf (cfg.listenAddress != null) cfg.listenAddress;
DISABLE_MQTT = mkIf (!cfg.mqtt.enable) "true";
}
(mkIf cfg.mqtt.enable {
MQTT_HOST = cfg.mqtt.host;
MQTT_PORT = mkIf (cfg.mqtt.port != null) (toString cfg.mqtt.port);
})
];
};
}
(mkIf cfg.postgres.enable_server {
services.postgresql = {
enable = true;
package = pkgs.postgresql_16;
inherit (cfg.postgres) port;
initialScript = pkgs.writeText "teslamate-psql-init" ''
\set password `echo $DATABASE_PASS`
CREATE DATABASE ${cfg.postgres.database};
CREATE USER ${cfg.postgres.user} with encrypted password :'password';
GRANT ALL PRIVILEGES ON DATABASE ${cfg.postgres.database} TO ${cfg.postgres.user};
ALTER USER ${cfg.postgres.user} WITH SUPERUSER;
'';
};
# Include secrets in postgres as well
systemd.services.postgresql = {
serviceConfig = {
EnvironmentFile = cfg.secretsFile;
};
};
})
(mkIf cfg.grafana.enable {
services.grafana = {
enable = true;
settings = {
server = {
domain = cfg.virtualHost;
http_port = cfg.grafana.port;
http_addr = cfg.grafana.listenAddress;
root_url = "http://%(domain)s${cfg.grafana.urlPath}";
serve_from_sub_path = cfg.grafana.urlPath != "/";
};
security = {
allow_embedding = true;
disable_gravatr = true;
};
users = {
allow_sign_up = false;
};
"auth.anonymous".enabled = false;
"auth.basic".enabled = false;
analytics.reporting_enabled = false;
};
provision = {
enable = true;
datasources.path = ./grafana/datasource.yml;
# Need to duplicate dashboards.yml since it contains absolute paths
# which are incompatible with NixOS
dashboards.settings = {
apiVersion = 1;
providers = [
{
name = "teslamate";
orgId = 1;
folder = "TeslaMate";
folderUid = "Nr4ofiDZk";
type = "file";
disableDeletion = false;
editable = true;
updateIntervalSeconds = 86400;
options.path = lib.sources.sourceFilesBySuffices
./grafana/dashboards
[ ".json" ];
}
{
name = "teslamate_internal";
orgId = 1;
folder = "Internal";
folderUid = "Nr5ofiDZk";
type = "file";
disableDeletion = false;
editable = true;
updateIntervalSeconds = 86400;
options.path = lib.sources.sourceFilesBySuffices
./grafana/dashboards/internal
[ ".json" ];
}
{
name = "teslamate_reports";
orgId = 1;
folder = "Reports";
folderUid = "Nr6ofiDZk";
type = "file";
disableDeletion = false;
editable = true;
updateIntervalSeconds = 86400;
options.path = lib.sources.sourceFilesBySuffices
./grafana/dashboards/reports
[ ".json" ];
}
];
};
};
};
systemd.services.grafana = {
serviceConfig.EnvironmentFile = cfg.secretsFile;
environment = {
DATABASE_USER = cfg.postgres.user;
DATABASE_NAME = cfg.postgres.database;
DATABASE_HOST = cfg.postgres.host;
DATABASE_PORT = toString cfg.postgres.port;
DATABASE_SSL_MODE = "disable";
};
};
})
]);
}