-
Notifications
You must be signed in to change notification settings - Fork 20
/
flake.nix
244 lines (224 loc) · 8.3 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
{
description = "CLI utility for Drone, an Embedded Operating System";
inputs = {
utils.url = "github:numtide/flake-utils";
nixpkgs.url = "nixpkgs/nixos-22.05";
naersk = {
url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
openocd = {
type = "git";
url = "git://git.code.sf.net/p/openocd/code";
shallow = true;
submodules = true;
ref = "v0.11.0";
rev = "f342aac0845a69d591ad39a025d74e9c765f6420";
flake = false;
};
};
outputs = { self, utils, nixpkgs, naersk, fenix, openocd }:
(utils.lib.eachDefaultSystem (system:
let
rustChannel = {
channel = "1.65";
sha256 = "DzNEaW724O8/B8844tt5AVHmSjSQ3cmzlU4BP90oRlY=";
};
rustFmtChannel = {
channel = "nightly";
date = "2022-11-12";
sha256 = "NZrKSshDgITZuDSffP89NpZl/pQlblc7arXatkV+O9A=";
};
pkgs = nixpkgs.legacyPackages.${system};
buildInputs = with pkgs; [
hidapi
libftdi1
libusb1
];
nativeBuildInputs = with pkgs; [
clang
];
rustToolchain = with fenix.packages.${system}; combine
(with toolchainOf rustChannel; [
rustc
cargo
clippy
rust-src
]);
rustFmt = (fenix.packages.${system}.toolchainOf rustFmtChannel).rustfmt;
rustAnalyzer = fenix.packages.${system}.rust-analyzer;
naersk-lib = naersk.lib.${system}.override {
cargo = rustToolchain;
rustc = rustToolchain;
};
libopenocd = { patches ? null, configureFlags ? [ ] }: pkgs.stdenv.mkDerivation {
name = "libopenocd";
src = openocd;
nativeBuildInputs = with pkgs; [
autoconf
automake
libtool
pkg-config
which
];
inherit buildInputs;
patches = (pkgs.lib.optionals (builtins.isNull patches) [
# Patch is upstream, so can be removed when OpenOCD 0.12.0 or later is released.
(pkgs.fetchpatch {
url = "https://github.com/openocd-org/openocd/commit/cff0e417da58adef1ceef9a63a99412c2cc87ff3.patch";
sha256 = "Xxzf5miWy4S34sbQq8VQdAbY/oqGyhL/AJxiEPRuj3Q=";
})
]) ++ (pkgs.lib.optionals (!builtins.isNull patches) patches);
preConfigure = ''
SKIP_SUBMODULE=1 ./bootstrap
'';
configureFlags = [ "--disable-werror" ] ++ configureFlags;
buildPhase = ''
make --jobs=$NIX_BUILD_CORES
'';
postInstall = ''
mkdir -p $out/lib
cp src/.libs/*.a jimtcl/*.a $out/lib
mkdir -p $out/include
cd src
find -name '*.h' -exec install -Dm 444 '{}' $out/include/'{}' \;
find helper -name '*.h' -exec ln -s '{}' $out/include \;
cp openocd.c startup_tcl.inc $out/include
cd ../jimtcl
find -name '*.h' -exec install -Dm 444 '{}' $out/include/'{}' \;
cd ..
cp config.h $out/include
rm -r $out/bin $out/share/info $out/share/man
rm -r $out/share/openocd/contrib $out/share/openocd/OpenULINK
'';
};
env = libopenocdArgs:
let libopenocdPkg = libopenocd libopenocdArgs; in
{
OPENOCD_LIB = "${libopenocdPkg}/lib";
OPENOCD_INCLUDE = "${libopenocdPkg}/include";
OPENOCD_SCRIPTS = "${libopenocdPkg}/share/openocd/scripts";
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
RUST_SRC_PATH = "${rustToolchain}/lib/rustlib/src/rust/library";
};
package = pkgs.lib.makeOverridable
(libopenocdArgs: naersk-lib.buildPackage ({
src = ./.;
inherit buildInputs;
inherit nativeBuildInputs;
version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version;
postInstall = ''
mkdir -p $out/share/openocd
ln -s $OPENOCD_SCRIPTS $out/share/openocd
'';
} // (env libopenocdArgs)))
{ };
cargoRdme = (
pkgs.rustPlatform.buildRustPackage rec {
name = "cargo-rdme";
src = pkgs.fetchFromGitHub {
owner = "orium";
repo = name;
rev = "v0.7.3";
sha256 = "qzit/uYkyWiOqpO5sHYo2hKJvOhovcO+oVbq/Bo2HsI=";
};
cargoSha256 = "lbyLVmSLNt4mt6hQbJnCuNL1Y1/2E/81sVpLYOkv7w8=";
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [ pkgs.openssl ];
doCheck = false;
});
checkAll = pkgs.writeShellScriptBin "check-all" ''
set -ex
cargo rdme --check
cargo fmt --all --check
cargo clippy --workspace -- --deny warnings
cargo test --workspace --exclude drone-openocd
RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --workspace
'';
updateVersions = pkgs.writeShellScriptBin "update-versions" ''
sed -i "s/\(api\.drone-os\.com\/drone\/\)[0-9]\+\(\.[0-9]\+\)\+/\1$(echo $1 | sed 's/\(.*\)\.[0-9]\+/\1/')/" \
Cargo.toml config/Cargo.toml stream/Cargo.toml openocd/Cargo.toml
sed -i "/\[.*\]/h;/version = \".*\"/{x;s/\[workspace.package\]/version = \"$1\"/;t;x}" \
Cargo.toml
sed -i "/\[.*\]/h;/version = \"=.*\"/{x;s/\[.*drone-.*\]/version = \"=$1\"/;t;x}" \
Cargo.toml
sed -i "s/\(drone-.* = { version = \"\).*\(\"\)/\1$1\2/" \
project_template_*/Cargo.toml
sed -i "s/\(drone-os\/drone\/v\).*\(\";\)/\1$1\2/" \
project_template_*/flake.nix
'';
publishCrates = pkgs.writeShellScriptBin "publish-crates" ''
cd stream && cargo publish
sleep 30
cd config && cargo publish
cd openocd && cargo publish
sleep 30
cargo publish
'';
publishDocs = pkgs.writeShellScriptBin "publish-docs" ''
dir=$(sed -n 's/.*api\.drone-os\.com\/\(.*\/.*\)\/.*\/"/\1/;T;p' Cargo.toml) \
&& rm -rf ../drone-api/$dir \
&& cp -rT target/doc ../drone-api/$dir \
&& echo '<!DOCTYPE html><meta http-equiv="refresh" content="0; URL=./drone">' > ../drone-api/$dir/index.html \
&& cd ../drone-api && git add $dir && git commit -m "Docs for $dir"
'';
shell = pkgs.mkShell ({
name = "native";
inherit buildInputs;
nativeBuildInputs = nativeBuildInputs ++ [
rustToolchain
rustFmt
rustAnalyzer
cargoRdme
checkAll
updateVersions
publishCrates
publishDocs
];
} // (env { }));
in
{
packages = {
drone = package;
default = package;
};
devShells = {
native = shell;
default = shell;
};
}
)) // {
templates =
let
welcomeText = ''
# Initialized a new Drone project
## Next steps
* Run **git init && git add -A** to initialize a git repository
* Edit **Cargo.toml** and **src/main.rs** to change the default project name
* Edit **flake.nix** to configure your cross-compilation toolchain
* Edit **probe.tcl** to configure your debug probe
* Edit **layout.toml** to configure your microcontroller memory layout
* Edit **Cargo.toml** to add more Drone packages for your microcontroller
* Run **direnv allow** (if you have `direnv` installed on your host) or **nix
develop** to load the project's Nix shell
'';
in
rec {
stm32 = {
path = ./project-templates/stm32;
description = "STM32 Drone project template";
inherit welcomeText;
};
raspberrypi-pico = {
path = ./project-templates/raspberrypi-pico;
description = "Raspberry Pi Pico Drone project template";
inherit welcomeText;
};
default = raspberrypi-pico;
};
};
}