-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
155 lines (138 loc) · 4.25 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
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
crane.url = "github:ipetkov/crane/v0.19.0";
esp-dev = {
url = "github:mirrexagon/nixpkgs-esp-dev";
inputs.nixpkgs.follows = "nixpkgs";
};
treefmt-nix.url = "github:numtide/treefmt-nix";
};
outputs =
inputs@{
self,
nixpkgs,
flake-parts,
fenix,
esp-dev,
crane,
...
}:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
inputs.treefmt-nix.flakeModule
];
flake = {
overlays.default = import ./nix/overlay.nix;
};
systems = [ "x86_64-linux" ];
perSystem =
{
config,
self',
system,
...
}:
let
overlays = [
fenix.overlays.default
esp-dev.overlays.default
self.overlays.default
];
pkgs = import nixpkgs { inherit system overlays; };
rustToolchain =
with fenix.packages.${system};
combine [
pkgs.rust-esp
pkgs.rust-src-esp
];
craneLib = crane.mkLib pkgs;
craneToolchain = craneLib.overrideToolchain rustToolchain;
src = craneLib.cleanCargoSource ./.;
commonArgs = {
inherit src;
cargoVendorDir = craneLib.vendorMultipleCargoDeps {
cargoLockList = [
./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!
"${pkgs.rust-src-esp}/lib/rustlib/src/rust/Cargo.lock"
];
};
strictDeps = true;
doCheck = false;
dontCheck = true;
dontPatchELF = true;
cargoExtraArgs = "-Zbuild-std=core,alloc --target xtensa-esp32-none-elf";
buildInputs = with pkgs; [
openssl
pkg-config
esp-idf-esp32-with-clang
];
# FIXME: this is a hack for cargo to find our linker
CARGO_TARGET_XTENSA_ESP32_NONE_ELF_LINKER =
let
deps = pkgs.esp-idf-esp32-with-clang.propagatedBuildInputs;
idfName = pkgs.esp-idf-esp32-with-clang.name;
targetPkg = builtins.head (builtins.filter (p: p.name == "esp-clang-${idfName}") deps);
in
"${targetPkg}/bin/xtensa-esp32-elf-ld";
};
cargoArtifacts = craneToolchain.buildDepsOnly commonArgs;
crate = craneToolchain.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
}
);
in
{
devShells.default =
with pkgs;
mkShell {
buildInputs = [
openssl
pkg-config
esp-idf-esp32-with-clang
rustToolchain
cargo-generate
cargo-espflash
];
};
treefmt = {
projectRootFile = "Cargo.toml";
programs = {
nixfmt.enable = true;
rustfmt.enable = true;
};
};
packages.default = crate;
apps.default = {
type = "app";
program = pkgs.lib.getExe (
pkgs.writeShellApplication {
name = "espflash-run";
runtimeInputs = [
crate
pkgs.espflash
];
text = ''
espflash flash --monitor "${crate}/bin/${crate.pname}"
'';
}
);
};
};
};
}