-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
344 lines (317 loc) · 12 KB
/
flake.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
{
description = "Environmental Controls and Life Support Systems";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
};
outputs = { self, nixpkgs, rust-overlay, ... }:
let
systems = [ "x86_64-linux" "aarch64-linux" "armv7l-linux" "x86_64-darwin" "aarch64-darwin" ];
overlays = [ (import rust-overlay) ];
forAllSystems = function:
nixpkgs.lib.genAttrs systems
(system:
let
pkgs = import nixpkgs {
inherit system overlays;
};
in
function pkgs);
pname = "eclssd";
build-eclssd =
(pkgs: with pkgs; let
# use the Rust toolchain specified in the project's rust-toolchain.toml
rustToolchain =
let
file = pkgsBuildHost.rust-bin.fromRustupToolchainFile
./rust-toolchain.toml;
in
file.override {
extensions = [
"rust-src" # for rust-analyzer
];
};
configuredRustPlatform = makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
src = nix-gitignore.gitignoreSource [ ] ./.;
cargoTOML = lib.importTOML "${src}/eclssd/Cargo.toml";
in
configuredRustPlatform.buildRustPackage {
inherit src pname;
inherit (cargoTOML.package) version;
buildInputs = with pkgs; [
SDL2
SDL2.dev
];
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"sensor-sen5x-0.1.0" = "sha256-G54U1/tb2QWUQbUtcnYvnzGs8XPW1tZhbh5QOR8hiBg=";
"sgp30-0.3.2" = "sha256-e++4WnEACeuYla4kW405VThEOl36simMU9rrTMQA/I8=";
"sht4x-0.2.0" = "sha256-5ST2EJyFym5lbVvAJj3VIWn5z3SC783nLvJYolfgtoo=";
"tinymetrics-0.1.0" = "sha256-zt7lqq2sYY9k+bMQOepxRamg1CGynFafbxBJDISl1U8=";
"bosch-bme680-1.0.2" = "sha256-g06bpJP3PgFF9peraYxr3pU5jzZrA8xL/D6+kwr/Nfc=";
};
};
});
in
{
########################################################################
#### Packages
########################################################################
packages = forAllSystems (pkgs: with pkgs; {
eclssd = build-eclssd pkgs;
default = self.packages.${system}.eclssd;
eclssd-cross-armv7l-linux =
build-eclssd pkgsCross.armv7l-hf-multiplatform;
eclssd-cross-aarch64-linux =
build-eclssd pkgsCross.aarch64-multiplatform;
eclssd-cross-pi = build-eclssd pkgsCross.raspberryPi;
});
########################################################################
#### Dev shell (for `nix develop`)
########################################################################
devShells = forAllSystems
(pkgs: with pkgs; let flakePkgs = self.packages.${system}; in {
default = with flakePkgs; mkShell {
buildInputs = [
eclssd.buildInputs
patchelf
# eclssd-cross-armv7l-linux.buildInputs
# eclssd-cross-aarch64-linux.buildInputs
# eclssd-cross-pi.buildInputs
];
nativeBuildInputs = [
eclssd.nativeBuildInputs
# eclssd-cross-armv7l-linux.nativeBuildInputs
# eclssd-cross-aarch64-linux.nativeBuildInputs
# eclssd-cross-pi.nativeBuildInputs
];
};
});
nixosModules.default = { config, lib, pkgs, ... }: with lib; let
name = "eclssd";
cfg = config.services.${name};
ssd1680 = "ssd1680";
# cfgCtl = config.programs.eclssctl;
description = "Environmental Controls and Life Support Systems daemon";
in
{
options = with types; {
services.eclssd = {
enable = mkEnableOption name;
i2cdev = mkOption {
type = path;
default = "/dev/i2c-1";
example = "/dev/i2c-1";
description = "The I2C device to use for communication with sensors.";
};
openPorts = mkOption {
type = bool;
default = false;
description = "Whether to open firewall ports for eclssd";
};
onlySensors = mkOption {
type = listOf (enum [
"BME680"
"ENS160"
"PMSA003I"
"SCD30"
"SCD40"
"SCD41"
"SHT41"
"SGP30"
"SEN55"
]);
default = [ ];
description = ''
A list of sensors to explicitly enable, or an empty list to enable all supported sensors.
If this is null, the ECLSS daemon will attempt to use all supported sensors.
'';
};
# Currently this doesn't do anything but I intend to use it for my
# Prometheus scrape config...
location = mkOption {
type = uniq str;
default = "${config.networking.hostname}";
example = "bedroom";
description = "The physical location of this ECLSS sensor.";
};
logging = {
filter = mkOption {
type = separatedString ",";
default = "info";
example = "info,eclss=debug";
description = "`tracing-subscriber` log filtering configuration for eclssd";
};
timestamps = mkEnableOption "timestamps in log output";
colors = mkOption {
type = bool;
default = true;
example = false;
description = "Whether to enable ANSI color codes in log output.";
};
format = mkOption {
type = enum [ "text" "json" "journald" ];
default = "text";
example = "json";
description = "The log output format.";
};
};
server = {
addr = mkOption {
type = uniq str;
default = "0.0.0.0";
example = "127.0.0.1";
description = "The address to bind the server on.";
};
port = mkOption {
type = uniq port;
default = 4200;
example = 4200;
description = "The port to bind the server on.";
};
};
readoutd = {
${ssd1680} = {
enable = mkEnableOption "SSD1680 display support";
};
};
};
};
config = let eclssPkg = self.packages.${pkgs.system}.default; in mkIf cfg.enable (mkMerge [
{
# eclssd user/group. the service requires its own user in order to
# add the "i2c" group.
users = {
users.${name} = {
inherit description;
isSystemUser = true;
group = name;
extraGroups = [ "i2c" ];
};
groups.${name} = { };
};
services.udev.extraRules = ''
SUBSYSTEM=="i2c-dev", TAG+="systemd"
'';
environment.systemPackages = [ eclssPkg ];
systemd.services.${name} =
let
sensorArgs = strings.concatMapStrings (sensor: " --sensor ${sensor}") cfg.onlySensors;
in
{
inherit description;
wantedBy = [ "multi-user.target" ];
after = [ "networking.target" ];
environment = {
ECLSS_LOG = cfg.logging.filter;
ECLSS_LOG_FORMAT = cfg.logging.format;
ECLSS_LOCATION = cfg.location;
};
serviceConfig = {
User = name;
Group = name;
ExecStart = ''${eclssPkg}/bin/${name} \
--i2cdev '${cfg.i2cdev}' \
--listen-addr '${cfg.server.addr}:${toString cfg.server.port}'\
${sensorArgs}
'';
Restart = "on-failure";
RestartSec = "5s";
# only start if the I2C adapter is up.
# ConditionPathExists = "/sys/class/i2c-adapter";
# Ensure that the "API VFS" (i.e. /dev/i2c-n) is mounted for
# the service.
MountAPIVFS = true;
# Ensure the system has access to real hardware devices in
# /dev
PrivateDevices = false;
# Ensure the service has access to the network so that it can
# bind its listener.
PrivateNetwork = false;
StateDirectory = "eclssd";
# Misc hardening --- eclssd shouldn't need any filesystem
# access other than `/dev/i2c-*`.
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
};
};
}
(mkIf cfg.openPorts {
networking.firewall.allowedTCPPorts = [ cfg.server.port ];
})
(mkIf (!cfg.logging.colors) {
systemd.services.${name}.environment = {
NOCOLOR = "true";
};
})
(mkIf (!cfg.logging.timestamps) {
systemd.services.${name}.environment = {
ECLSS_LOG_NO_TIMESTAMPS = "true";
};
})
(mkIf cfg.readoutd.${ssd1680}.enable (
let readoutdName = "eclss-readoutd"; in {
# eclssd user/group. the service requires its own user in order to
# add the "gpio" and "spi" groups.
users = {
users.${readoutdName} = {
inherit description;
isSystemUser = true;
group = readoutdName;
extraGroups = [ "gpio" "spi" ];
};
groups.${readoutdName} = { };
};
systemd.services."${readoutdName}-${ssd1680}" = {
inherit description;
wantedBy = [ "multi-user.target" ];
after = [ "${name}.service" ];
environment = {
ECLSS_LOG = cfg.logging.filter;
ECLSS_LOG_FORMAT = cfg.logging.format;
};
serviceConfig = {
User = readoutdName;
Group = readoutdName;
ExecStart = ''${eclssPkg}/bin/${readoutdName} \
localhost \
--port ${toString cfg.server.port} \
${ssd1680}
'';
Restart = "on-failure";
RestartSec = "5s";
# Ensure that the "API VFS" (i.e. /dev/gpiomem) is mounted for
# the service.
MountAPIVFS = true;
# Ensure the system has access to real hardware devices in
# /dev
PrivateDevices = false;
# Ensure the service has access to the network so that it can
# bind its listener.
PrivateNetwork = false;
StateDirectory = "${readoutdName}-${ssd1680}";
# Misc hardening --- eclssd-readoutd shouldn't need any filesystem
# access other than `/dev/gpiomem` and `/dev/spidev`.
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
};
};
}
))
]);
};
};
}