-
Notifications
You must be signed in to change notification settings - Fork 1
/
flake.nix
403 lines (367 loc) · 13.4 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
{
description = "IOT automation for people who think like programmers";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
crane.url = "github:ipetkov/crane";
poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
flockenzeit.url = "github:balsoft/flockenzeit";
devenv.url = "github:cachix/devenv";
};
outputs = inputs @ {
self,
nixpkgs,
flake-utils,
rust-overlay,
crane,
poetry2nix,
nixpkgs-unstable,
flockenzeit,
devenv,
}:
flake-utils.lib.eachSystem ["x86_64-linux" "aarch64-linux"] (system: let
# pkgs_arm = nixpkgs.legacyPackages."aarch64-linux";
# pkgs = import nixpkgs {
# inherit system;
# crossSystem = "aarch64-linux";
# overlays = [
# (import rust-overlay)
# (self: super: {
# inherit (pkgs_arm)
# openssl protobuf fontconfig freetype xorg mesa wayland
# libxkbcommon;
# })
# ];
# };
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
pkgs_unstable = nixpkgs-unstable.legacyPackages.${system};
nodejs = pkgs.nodejs_20;
p2n = import poetry2nix {inherit pkgs;};
poetry_env = p2n.mkPoetryEnv {
python = pkgs.python3;
projectDir = ./robotica-tokio/python;
overrides = p2n.defaultPoetryOverrides.extend (self: super: {
x-wr-timezone = super.x-wr-timezone.overridePythonAttrs (old: {
buildInputs = (old.buildInputs or []) ++ [super.setuptools];
});
recurring-ical-events = super.recurring-ical-events.overridePythonAttrs (old: {
buildInputs = (old.buildInputs or []) ++ [super.setuptools];
});
});
};
rustPlatform = pkgs.rust-bin.stable.latest.default.override {
targets = ["wasm32-unknown-unknown"];
extensions = ["rust-src"];
};
craneLib = (crane.mkLib pkgs).overrideToolchain rustPlatform;
nodePackages = pkgs.buildNpmPackage {
name = "robotica-frontend";
src = ./robotica-frontend;
# npmDepsHash = "sha256-1bhWY/wOCYq0J5AYm9Mp9M7DfGCMpp7wBtHNWqV0+5c=";
# npmDepsHash = pkgs.lib.fakeHash;
npmDepsHash = builtins.readFile ./npm-deps-hash;
dontNpmBuild = true;
inherit nodejs;
installPhase = ''
mkdir $out
cp -r node_modules $out
ln -s $out/node_modules/.bin $out/bin
'';
};
build_env = {
BUILD_DATE = with flockenzeit.lib.splitSecondsSinceEpoch {} self.lastModified; "${F}T${T}${Z}";
VCS_REF = "${self.rev or "dirty"}";
};
robotica-frontend = let
common = {
src = ./.;
pname = "robotica-frontend";
version = "0.0.0";
cargoExtraArgs = "-p robotica-frontend";
nativeBuildInputs = with pkgs; [pkg-config];
buildInputs = with pkgs; [
# openssl python3
protobuf
];
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
doCheck = false;
};
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly common;
# Run clippy (and deny all warnings) on the crate source.
clippy = craneLib.cargoClippy ({
inherit cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings";
}
// common);
# Build the actual crate itself.
pkg = craneLib.buildPackage ({
inherit cargoArtifacts;
doCheck = false;
}
// common
// build_env);
in {
clippy = clippy;
pkg = pkg;
};
robotica-frontend-bindgen = pkgs.stdenv.mkDerivation {
name = "robotica-frontend-bindgen";
src = ./robotica-frontend;
buildPhase = ''
${pkgs.wasm-bindgen-cli}/bin/wasm-bindgen \
--target bundler \
--out-dir pkg \
--omit-default-module-path \
${robotica-frontend.pkg}/lib/robotica_frontend.wasm
ln -s ${nodePackages}/node_modules ./node_modules
export PATH="${nodejs}/bin:${nodePackages}/bin:$PATH"
webpack
'';
installPhase = ''
mkdir $out
cp -rv dist/* $out/
'';
};
brian-backend = let
common = {
src = ./.;
pname = "brian-backend";
version = "0.0.0";
cargoExtraArgs = "-p brian-backend";
nativeBuildInputs = with pkgs; [pkg-config];
buildInputs = with pkgs; [openssl python3 protobuf];
# See https://github.com/ipetkov/crane/issues/414#issuecomment-1860852084
# for possible work around if this is required in the future.
# installCargoArtifactsMode = "use-zstd";
};
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly common;
# Run clippy (and deny all warnings) on the crate source.
clippy = craneLib.cargoClippy ({
inherit cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings";
}
// common);
# Next, we want to run the tests and collect code-coverage, _but only if
# the clippy checks pass_ so we do not waste any extra cycles.
coverage =
craneLib.cargoTarpaulin ({cargoArtifacts = clippy;} // common);
# Build the actual crate itself.
pkg = craneLib.buildPackage ({
inherit cargoArtifacts;
doCheck = true;
# CARGO_LOG = "cargo::core::compiler::fingerprint=info";
}
// common
// build_env);
wrapper = pkgs.writeShellScriptBin "brian-backend" ''
export PATH="${poetry_env}/bin:$PATH"
exec ${pkg}/bin/brian-backend "$@"
'';
in {
clippy = clippy;
coverage = coverage;
pkg = wrapper;
};
robotica-freeswitch = let
common = {
src = ./.;
pname = "robotica-freeswitch";
version = "0.0.0";
cargoExtraArgs = "-p robotica-freeswitch";
nativeBuildInputs = with pkgs; [pkg-config];
buildInputs = with pkgs; [openssl python3 protobuf];
};
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly common;
# Run clippy (and deny all warnings) on the crate source.
clippy = craneLib.cargoClippy ({
inherit cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings";
}
// common);
# Next, we want to run the tests and collect code-coverage, _but only if
# the clippy checks pass_ so we do not waste any extra cycles.
coverage =
craneLib.cargoTarpaulin ({cargoArtifacts = clippy;} // common);
# Build the actual crate itself.
pkg = craneLib.buildPackage ({
inherit cargoArtifacts;
doCheck = true;
}
// common
// build_env);
wrapper = pkgs.writeShellScriptBin "robotica-freeswitch" ''
export PATH="${poetry_env}/bin:$PATH"
exec ${pkg}/bin/robotica-freeswitch "$@"
'';
in {
clippy = clippy;
coverage = coverage;
pkg = wrapper;
};
robotica-slint = let
common = {
src = ./.;
pname = "robotica-slint";
version = "0.0.0";
cargoExtraArgs = "-p robotica-slint";
nativeBuildInputs = with pkgs; [pkg-config];
buildInputs = with pkgs; [
openssl
protobuf
fontconfig
freetype
xorg.libxcb
xorg.libX11
xorg.libXcursor
xorg.libXrandr
xorg.libXi
mesa
wayland
libxkbcommon
];
};
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly common;
# Run clippy (and deny all warnings) on the crate source.
clippy = craneLib.cargoClippy ({
inherit cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings";
}
// common);
# Next, we want to run the tests and collect code-coverage, _but only if
# the clippy checks pass_ so we do not waste any extra cycles.
coverage =
craneLib.cargoTarpaulin ({cargoArtifacts = clippy;} // common);
libPath = pkgs.lib.makeLibraryPath [
pkgs.libGL
pkgs.libxkbcommon
pkgs.dbus.lib
pkgs.wayland
pkgs.fontconfig
];
# Build the actual crate itself.
pkg = craneLib.buildPackage ({
inherit cargoArtifacts;
doCheck = true;
postFixup = pkgs.lib.optional pkgs.stdenv.isLinux ''
rpath=$(patchelf --print-rpath $out/bin/robotica-slint)
patchelf --set-rpath "$rpath:${libPath}" $out/bin/robotica-slint
'';
}
// common
// build_env);
in {
clippy = clippy;
coverage = coverage;
pkg = pkg;
};
devShell = devenv.lib.mkShell {
inherit inputs pkgs;
modules = [
{
packages = [
pkgs.poetry
poetry_env
pkgs_unstable.rust-analyzer
pkgs.pkg-config
pkgs.openssl
pkgs.protobuf
pkgs.fontconfig
pkgs.freetype
nodejs
pkgs.wasm-pack
pkgs.wasm-bindgen-cli
pkgs.slint-lsp
rustPlatform
pkgs.cargo-expand
# https://github.com/NixOS/nixpkgs/issues/156890
pkgs.binaryen
nodePackages
pkgs.prefetch-npm-deps
pkgs.gcc
pkgs.sqlx-cli
pkgs.influxdb2
pkgs.mosquitto
];
enterShell = ''
export ROBOTICA_DEBUG=true
export PYTHONPATH="${poetry_env}/lib/python3.11/site-packages"
export CONFIG_FILE="$PWD/robotica-backend.yaml"
export SLINT_CONFIG_FILE="$PWD/robotica-slint.yaml"
export STATIC_PATH="robotica-frontend/dist"
export DATABASE_URL="postgresql://robotica:your_secure_password_here@localhost/robotica"
'';
processes.mqtt = {
exec = "${pkgs.mosquitto}/bin/mosquitto -c mosquitto.conf";
};
processes.influxdb = {exec = "${pkgs.influxdb2}/bin/influxd";};
services.postgres = {
enable = true;
package = pkgs.postgresql_15.withPackages (ps: [ps.postgis]);
listen_addresses = "127.0.0.1";
initialDatabases = [{name = "robotica";}];
initialScript = ''
\c robotica;
CREATE USER robotica with encrypted password 'your_secure_password_here';
GRANT ALL PRIVILEGES ON DATABASE robotica TO robotica;
-- GRANT ALL ON SCHEMA public TO robotica;
ALTER USER robotica WITH SUPERUSER;
'';
};
}
];
};
in {
# Disable coverage checks as broken since Rust 1.77:
# Mar 27 05:16:41.964 ERROR cargo_tarpaulin::test_loader: Error parsing debug information from binary: An I/O error occurred while reading.
# Mar 27 05:16:41.964 WARN cargo_tarpaulin::test_loader: Stripping symbol information can prevent tarpaulin from working. If you want to do this pass `--engine=llvm`
# Mar 27 05:16:41.965 ERROR cargo_tarpaulin: Error while parsing binary or DWARF info.
# Error: "Error while parsing binary or DWARF info."
checks = {
robotica-slint-clippy = robotica-slint.clippy;
# robotica-slint-coverage = robotica-slint.coverage;
robotica-slint = robotica-slint.pkg;
robotica-frontend-clippy = robotica-frontend.clippy;
robotica-frontend = robotica-frontend.pkg;
brian-backend-clippy = brian-backend.clippy;
# brian-backend-coverage = brian-backend.coverage;
brian-backend = brian-backend.pkg;
robotica-freeswitch-clippy = robotica-freeswitch.clippy;
# freeswitch-coverage = robotica-freeswitch.coverage;
robotica-freeswitch = robotica-freeswitch.pkg;
};
devShells.default = devShell;
packages = {
robotica-frontend = robotica-frontend-bindgen;
brian-backend = brian-backend.pkg;
robotica-slint = robotica-slint.pkg;
robotica-freeswitch = robotica-freeswitch.pkg;
devenv-up = devShell.config.procfileScript;
};
})
// {
nixosModules = {
robotica-backend =
import ./modules/robotica-backend.nix {inherit self;};
robotica-slint =
import ./modules/robotica-slint.nix {inherit self;};
robotica-freeswitch =
import ./modules/robotica-freeswitch.nix {inherit self;};
};
};
}