forked from ralphbean/mbta2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
198 lines (185 loc) · 7.35 KB
/
Copy pathflake.nix
File metadata and controls
198 lines (185 loc) · 7.35 KB
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
{
description = "Flake for mbta2mqtt package and module";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }:
let
inherit (nixpkgs) lib;
forAllSystems = lib.genAttrs lib.systems.flakeExposed;
in {
packages = forAllSystems (system:
let pkgs = nixpkgs.legacyPackages.${system}; in rec {
default = mbta2mqtt;
mbta2mqtt = pkgs.callPackage ./default.nix { };
});
nixosModule = { config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.mbta2mqtt;
pkg = self.packages.${pkgs.system}.default;
format = pkgs.formats.yaml {};
configFile = format.generate "mbta2mqtt.conf" cfg.settings;
in {
options.services.mbta2mqtt = {
enable = mkEnableOption "Enables the mbta2mqtt service";
user = mkOption {
type = types.str;
default = "mbta2mqtt";
example = "mbta2mqtt";
description = "User running mbta2mqtt service";
};
group = mkOption {
type = types.str;
default = "mbta2mqtt";
example = "mbta2mqtt";
description = "Group running mbta2mqtt service";
};
environmentFile = mkOption {
type = types.nullOr types.path;
default = null;
example = "/run/keys/secrets.env";
description = ''
An environment file as defined in {manpage}`systemd.exec(5)`.
This should be used to store secrets, such as your API key or MQTT broker password, and then pass the environment variables to mbta2mqtt by setting a configuration value to !ENV <MY_SECRET>.
'';
};
settings = mkOption {
type = types.nullOr (types.submodule {
freeformType = format.type;
options = {
mbta = {
api_key = mkOption {
type = types.nullOr types.str;
default = "!ENV MBTA_API_KEY";
example = "!ENV MBTA_API_KEY";
description = "API key for MBTA real-time API. If prefixed with !ENV, will read the API key from an environment variable";
};
stops = mkOption {
type = types.listOf types.str;
default = [];
example = ''
[ "110" "2168" "22549" ]
'';
description = "A list of stop IDs to monitor";
};
include = mkOption {
type = types.listOf types.str;
default = [ "schedule"
"stop"
"stop.connecting_stops"
"stop.child_stops"
"stop.parent_station"
"route"
"route.alerts"
"route.line"
"route.route_patterns.representative_trip.shape"
"trip"
"trip.shape"
"trip.service"
"trip.stops"
"trip.alerts"
"trip.occupancies"
"trip.route_pattern.representative_trip.shape"
"vehicle"
"vehicle.route"
"vehicle.trip"
"vehicle.stop"
"alerts"
"alerts.facilities" ];
example = ''
[ "schedule"
"stop"
"stop.connecting_stops"
"stop.child_stops"
"stop.parent_station"
"route"
"route.alerts"
"route.line"
"route.route_patterns.representative_trip.shape"
"trip"
"trip.shape"
"trip.service"
"trip.stops"
"trip.alerts"
"trip.occupancies"
"trip.route_pattern.representative_trip.shape"
"vehicle"
"vehicle.route"
"vehicle.trip"
"vehicle.stop"
"alerts"
"alerts.facilities" ]
'';
description = ''
Relationships to include in MBTA predictions.
'';
};
};
mqtt = {
host = mkOption {
type = types.nullOr types.str;
default = "127.0.0.1";
example = "mqtt.example.com";
description = "Hostname for mqtt server";
};
port = mkOption {
type = types.nullOr types.port;
default = 1883;
example = 1883;
description = "Port for mqtt server";
};
};
};
});
example = literalExpression ''
{
mbta = {
api_key = "ENV! MBTA_API_KEY";
stops = [ "110" "2168" "22549"];
};
mqtt = {
host = "127.0.0.1";
port = 1883;
}
}
'';
description = ''
Settings to write to /etc/mbta2mqtt/mbta2mqtt.conf.
All available options are listed on the project's [defaults.conf](https://github.com/negatethis/mbta2mqtt/raw/refs/heads/main/defaults.conf) file.
'';
};
};
config = mkIf cfg.enable {
users = {
users = {
mbta2mqtt = mkIf (cfg.user == "mbta2mqtt") {
isSystemUser = true;
group = cfg.group;
};
};
groups = {
${cfg.group} = { };
};
};
environment.etc = lib.mkMerge [
(lib.mkIf (cfg.settings != null) {
"mbta2mqtt/mbta2mqtt.conf".source = configFile;
})
];
systemd.services.mbta2mqtt = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
ExecStart = "${pkg}/bin/mbta2mqtt";
LogsDirectory = "mbta2mqtt";
LogsDirectoryMode = "0755";
Restart = "on-failure";
EnvironmentFile = lib.optional (cfg.environmentFile != null) cfg.environmentFile;
};
};
};
};
};
}