-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
448 lines (421 loc) · 17.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
{
inputs = {
advisory-db = {
flake = false;
url = "github:rustsec/advisory-db";
};
crane.url = "github:ipetkov/crane";
flake-utils.url = "github:numtide/flake-utils";
nix-update-scripts = {
url = "github:jwillikers/nix-update-scripts";
inputs = {
flake-utils.follows = "flake-utils";
nixpkgs.follows = "nixpkgs";
pre-commit-hooks.follows = "pre-commit-hooks";
treefmt-nix.follows = "treefmt-nix";
};
};
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
pre-commit-hooks = {
url = "github:cachix/pre-commit-hooks.nix";
inputs = {
nixpkgs.follows = "nixpkgs";
nixpkgs-stable.follows = "nixpkgs";
};
};
rust-overlay = {
inputs = {
nixpkgs.follows = "nixpkgs";
};
url = "github:oxalica/rust-overlay";
};
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
# deadnix: skip
self,
advisory-db,
crane,
flake-utils,
nix-update-scripts,
nixpkgs,
pre-commit-hooks,
rust-overlay,
treefmt-nix,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
defaultBoard = "attiny85";
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit overlays system;
};
commonNativeBuildInputs = with pkgs; [
asciidoctor
clippy
fish
just
lychee
nil
nushell
];
pre-commit = pre-commit-hooks.lib.${system}.run (
import ./pre-commit-hooks.nix { inherit pkgs treefmtEval; }
);
treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
boards = {
attiny85 = rec {
# The development shell requires the GCC AVR toolchain to be available.
# Thus, this cross-compilation configuration here does the trick.
avrCrossPkgs = import nixpkgs {
inherit overlays system;
config.allowUnsupportedSystem = true;
crossSystem = {
inherit overlays;
config = "avr";
# todo Configure Rust for the proper cross-compilation target when that can be done for target without std.
# Perhaps once AVR is stabilized for Rust this will be possible?
# rust.rustcTarget = "avr-unknown-none-attiny85";
# rust.platform = nixpkgs.lib.importJSON "${./boards/attiny85/avr-unknown-none-attiny85.json}";
# rustc = {
# config = "avr-unknown-none-attiny85";
# platform = nixpkgs.lib.importJSON "${./boards/attiny85/avr-unknown-none-attiny85.json}";
# };
};
};
rustToolchainFor =
p:
(p.rust-bin.fromRustupToolchainFile ./boards/attiny85/rust-toolchain.toml).override {
# Remove the avr-unknown-none-attiny85.json file from the list of targets for the Rust toolchain.
# Nix doesn't really support target JSON files specified in the toolchain and even if it did, it won't be ablet to build a toolchain for AVR.
# The AVR toolchain is unstable and does not include std.
targets = [ p.stdenv.hostPlatform.rust.rustcTarget ];
};
rustToolchain = rustToolchainFor pkgs;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchainFor;
nativeBuildInputs =
with pkgs;
[
avrdude
avrCrossPkgs.avrlibc
avrCrossPkgs.buildPackages.binutils
avrCrossPkgs.buildPackages.gcc
cargo-binutils
# ravedude
]
++ commonNativeBuildInputs;
commonArgs = {
pname = "pwm-fan-controller-attiny85";
strictDeps = true;
doCheck = false;
src = pkgs.lib.cleanSourceWith {
src = ./boards/attiny85;
# Don't remove the avr-unknown-none-attiny85.json file from the sources.
# See https://github.com/ipetkov/crane/issues/444
filter =
path: type:
(craneLib.filterCargoSources path type)
|| (builtins.baseNameOf path == "avr-unknown-none-attiny85.json");
};
cargoVendorDir = craneLib.vendorMultipleCargoDeps {
inherit (craneLib.findCargoFiles commonArgs.src) cargoConfigs;
cargoLockList = [
./boards/attiny85/Cargo.lock
# Unfortunately this approach requires IFD (import-from-derivation)
# otherwise Nix will refuse to read the Cargo.lock from our toolchain
# (unless we build with `--impure`).
#
# Another way around this is to manually copy the rustlib `Cargo.lock`
# to the repo and import it with `./path/to/rustlib/Cargo.lock` which
# will avoid IFD entirely but will require manually keeping the file
# up to date!
"${rustToolchain.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/Cargo.lock"
];
};
cargoExtraArgs = "--target avr-unknown-none-attiny85.json -Z build-std=core";
inherit nativeBuildInputs;
extraDummyScript = ''
cp --archive ${./boards/attiny85/avr-unknown-none-attiny85.json} $out/avr-unknown-none-attiny85.json
rm --force --recursive $out/src/bin/crane-dummy-*
'';
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
packages.pwm-fan-controller = pkgs.callPackage ./boards/attiny85/package.nix {
inherit cargoArtifacts commonArgs craneLib;
inherit (avrCrossPkgs) stdenv;
};
apps.flash = {
type = "app";
program = builtins.toString (
pkgs.writers.writeNu "flash-avrdude" ''
^${pkgs.lib.getExe pkgs.avrdude} -c USBtiny -B 4 -p attiny85 -U flash:w:${
self.packages.${system}.pwm-fan-controller-attiny85
}/bin/pwm-fan-controller-attiny85.hex:i
''
);
};
devShell = craneLib.devShell {
env = {
# Required by rust-analyzer
# todo Check if I actually need this.
RUST_SRC_PATH = "${rustToolchain.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/library";
};
packages =
nativeBuildInputs
++ [
treefmtEval.config.build.wrapper
# Make formatters available for IDE's.
(pkgs.lib.attrValues treefmtEval.config.build.programs)
]
++ pre-commit.enabledPackages;
inherit (pre-commit) shellHook;
};
};
pico = rec {
craneLib = (crane.mkLib pkgs).overrideToolchain (
p: p.rust-bin.fromRustupToolchainFile ./boards/pico/rust-toolchain.toml
);
commonArgs = {
pname = "pwm-fan-controller-pico";
strictDeps = true;
doCheck = false;
src = pkgs.lib.cleanSourceWith {
src = ./boards/pico;
# Don't remove the memory.x linker script file from the sources.
filter =
path: type: (craneLib.filterCargoSources path type) || (builtins.baseNameOf path == "memory.x");
};
# Need to make the memory.x linker script available to the dummy crate.
# See https://github.com/ipetkov/crane/issues/444
# Should I symlink instead of copy?
# ln --symbolic ${./memory.x} memory.x
CARGO_TARGET_THUMBV6M_NONE_EABI_RUSTFLAGS = "-C link-arg=--library-path=.";
extraDummyScript = ''
cp --archive ${./boards/pico/memory.x} $out/memory.x
rm --force --recursive $out/src/bin/crane-dummy-*
'';
cargoExtraArgs = "--target thumbv6m-none-eabi";
nativeBuildInputs = with pkgs; [
flip-link
];
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
apps = {
flash = {
type = "app";
program = builtins.toString (
pkgs.writers.writeNu "flash-elf2uf2-rs" ''
^${pkgs.lib.getExe pkgs.elf2uf2-rs} --deploy ${
self.packages.${system}.pwm-fan-controller-pico
}/bin/pwm-fan-controller-pico
''
);
};
run = {
type = "app";
program = builtins.toString (
pkgs.writers.writeNu "run-probe-rs" ''
${pkgs.lib.getExe pkgs.probe-rs} run --chip RP2040 --protocol swd ${
self.packages.${system}.pwm-fan-controller-pico
}/bin/pwm-fan-controller-pico
''
);
};
};
devShell = craneLib.devShell {
packages =
with pkgs;
[
elf2uf2-rs
flip-link
probe-rs
treefmtEval.config.build.wrapper
# Make formatters available for IDE's.
(pkgs.lib.attrValues treefmtEval.config.build.programs)
]
++ commonArgs.nativeBuildInputs
++ commonNativeBuildInputs
++ pre-commit.enabledPackages;
inherit (pre-commit) shellHook;
};
packages.pwm-fan-controller = pkgs.callPackage ./boards/pico/package.nix {
inherit commonArgs cargoArtifacts craneLib;
};
};
qt-py-ch32v203 = rec {
craneLib = (crane.mkLib pkgs).overrideToolchain (
p: p.rust-bin.fromRustupToolchainFile ./boards/qt-py-ch32v203/rust-toolchain.toml
);
commonArgs = {
pname = "pwm-fan-controller-qt-py-ch32v203";
strictDeps = true;
doCheck = false;
src = pkgs.lib.cleanSourceWith {
src = ./boards/qt-py-ch32v203;
# Don't remove the memory.x linker script file from the sources.
filter =
path: type: (craneLib.filterCargoSources path type) || (builtins.baseNameOf path == "memory.x");
};
# Need to make the memory.x linker script available to the dummy crate.
# See https://github.com/ipetkov/crane/issues/444
# Should I symlink instead of copy?
# ln --symbolic ${./memory.x} memory.x
CARGO_TARGET_RISCV32IMAC_UNKNOWN_NONE_ELF_RUSTFLAGS = "-C link-arg=--library-path=.";
extraDummyScript = ''
cp --archive ${./boards/qt-py-ch32v203/memory.x} $out/memory.x
rm --force --recursive $out/src/bin/crane-dummy-*
'';
cargoExtraArgs = "--target riscv32imac-unknown-none-elf";
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
apps.flash = {
type = "app";
program = builtins.toString (
pkgs.writers.writeNu "flash-wchisp" ''
^${pkgs.lib.getExe pkgs.wchisp} flash ${
self.packages.${system}.pwm-fan-controller-qt-py-ch32v203
}/bin/pwm-fan-controller-qt-py-ch32v203
''
);
};
devShell = craneLib.devShell {
packages =
with pkgs;
[
wchisp
# probe-rs
treefmtEval.config.build.wrapper
# Make formatters available for IDE's.
(pkgs.lib.attrValues treefmtEval.config.build.programs)
]
++ commonNativeBuildInputs
++ pre-commit.enabledPackages;
inherit (pre-commit) shellHook;
};
packages.pwm-fan-controller = pkgs.callPackage ./boards/qt-py-ch32v203/package.nix {
inherit (boards.qt-py-ch32v203) cargoArtifacts;
inherit (boards.qt-py-ch32v203) commonArgs;
inherit (boards.qt-py-ch32v203) craneLib;
};
};
};
in
{
apps = pkgs.lib.attrsets.genAttrs (builtins.attrNames boards) (board: boards.${board}.apps) // {
inherit (nix-update-scripts.apps.${system}) update-nix-direnv update-nixos-release;
default = self.apps.${system}.${defaultBoard}.flash;
};
checks = {
# attiny85-pwm-fan-controller = boards.attiny85.pwm-fan-controller;
# pico-pwm-fan-controller = boards.pico.pwm-fan-controller;
# qt-py-ch32v203-pwm-fan-controller = boards.qt-py-ch32v203.pwm-fan-controller;
attiny85-clippy = boards.attiny85.craneLib.cargoClippy (
boards.attiny85.commonArgs
// {
inherit (boards.attiny85) cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}
);
pico-clippy = boards.pico.craneLib.cargoClippy (
boards.pico.commonArgs
// {
inherit (boards.pico) cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}
);
qt-py-ch32v203-clippy = boards.qt-py-ch32v203.craneLib.cargoClippy (
boards.qt-py-ch32v203.commonArgs
// {
inherit (boards.qt-py-ch32v203) cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}
);
# attiny85.doc = craneLib.cargoDoc (boards.attiny85.commonArgs // {
# cargoArtifacts = boards.attiny85.cargoArtifacts;
# });
# pico.doc = craneLib.cargoDoc (boards.pico.commonArgs // {
# cargoArtifacts = boards.pico.cargoArtifacts;
# });
# qt-py-ch32v203.doc = craneLib.cargoDoc (boards.qt-py-ch32v203.commonArgs // {
# cargoArtifacts = boards.qt-py-ch32v203.cargoArtifacts;
# });
# attiny85.rustfmt = craneLib.cargoFmt {
# src = boards.attiny85.src;
# };
# pico.rustfmt = craneLib.cargoFmt {
# src = boards.pico.src;
# };
# qt-py-ch32v203.rustfmt = craneLib.cargoFmt {
# src = boards.qt-py-ch32v203.src;
# };
# attiny85.toml-fmt = craneLib.taploFmt {
# src = pkgs.lib.sources.sourceFilesBySuffices src [ ".toml" ];
# # taplo arguments can be further customized below as needed
# # taploExtraArgs = "--config ./taplo.toml";
# };
# Audit dependencies
attiny85-audit = boards.attiny85.craneLib.cargoAudit {
inherit advisory-db;
inherit (boards.attiny85.commonArgs) src;
};
pico-audit = boards.pico.craneLib.cargoAudit {
inherit advisory-db;
inherit (boards.pico.commonArgs) src;
};
qt-py-ch32v203-audit = boards.qt-py-ch32v203.craneLib.cargoAudit {
inherit advisory-db;
inherit (boards.qt-py-ch32v203.commonArgs) src;
};
# Audit licenses
attiny85-deny = boards.attiny85.craneLib.cargoDeny {
inherit (boards.attiny85.commonArgs) src;
};
pico-deny = boards.pico.craneLib.cargoDeny {
inherit (boards.pico.commonArgs) src;
};
qt-py-ch32v203-deny = boards.qt-py-ch32v203.craneLib.cargoDeny {
inherit (boards.qt-py-ch32v203.commonArgs) src;
};
# Run tests with cargo-nextest
# Consider setting `doCheck = false` on `my-crate` if you do not want
# the tests to run twice
# attiny85.nextest = craneLib.cargoNextest (boards.attiny85.commonArgs // {
# cargoArtifacts = boards.attiny85.cargoArtifacts;
# partitions = 1;
# partitionType = "count";
# });
};
devShells =
pkgs.lib.attrsets.genAttrs (builtins.attrNames boards) (board: boards.${board}.devShell)
// {
default = pkgs.mkShell {
nativeBuildInputs =
commonNativeBuildInputs
++ [
treefmtEval.config.build.wrapper
# Make formatters available for IDE's.
(builtins.attrValues treefmtEval.config.build.programs)
]
++ pre-commit.enabledPackages;
inherit (pre-commit) shellHook;
};
};
formatter = treefmtEval.config.build.wrapper;
packages =
pkgs.lib.attrsets.genAttrs (builtins.attrNames boards) (
board: boards.${board}.packages.pwm-fan-controller
)
// {
default = self.packages.${system}.${defaultBoard};
# attiny85-llvm-coverage = craneLibLLvmTools.cargoLlvmCov (boards.attiny85.commonArgs // {
# cargoArtifacts = boards.attiny85.cargoArtifacts;
# });
};
}
);
}