Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
327 lines (298 loc) · 11.8 KB
/
Copy pathflake.nix
File metadata and controls
327 lines (298 loc) · 11.8 KB
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
{
description = "Rust bindings and safe API for libghostty";
nixConfig = {
extra-substituters = ["https://ghostty.cachix.org"];
extra-trusted-public-keys = ["ghostty.cachix.org-1:QB389yTa6gTyneehvqG58y0WnHjQOqgnA+wBnpWWxns="];
};
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/release-26.05";
flake-utils.url = "github:numtide/flake-utils";
crane.url = "github:ipetkov/crane";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
zig = {
url = "github:mitchellh/zig-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
ghostty = {
url = "github:aislopware/ghostty/95b7cc6f9fe571434ab88ba5c2ff02a44e3fa0e7";
};
};
outputs = {
nixpkgs,
flake-utils,
crane,
rust-overlay,
zig,
ghostty,
...
}:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
rustVersion = "1.90.0";
# crane's devShell puts this toolchain's cargo/rustc ahead of the
# devToolchain in packages, so it must carry the cross targets too or
# its sysroot (without their std) shadows the devToolchain's.
buildToolchain = pkgs.rust-bin.stable.${rustVersion}.minimal.override {
targets = rustTargets;
};
rustTargets =
pkgs.lib.optionals pkgs.stdenv.isLinux [
"x86_64-unknown-linux-gnu"
"x86_64-unknown-linux-musl"
]
++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
# iOS builds go through the vendored zig xcframework path and
# need the target's std so the dev shell can cargo-check them.
"aarch64-apple-ios"
"aarch64-apple-ios-sim"
];
checkToolchain = pkgs.rust-bin.stable.${rustVersion}.default.override {
extensions = ["clippy" "rustfmt"];
};
devToolchain = pkgs.rust-bin.stable.${rustVersion}.default.override {
extensions = ["rust-src" "rust-std" "clippy" "rustfmt" "rust-analyzer"];
targets = rustTargets;
};
miriToolchain = pkgs.rust-bin.selectLatestNightlyWith (
toolchain:
toolchain.default.override {
extensions = ["rust-src" "rust-std" "miri"];
targets = rustTargets;
}
);
craneLib = (crane.mkLib pkgs).overrideToolchain buildToolchain;
craneCheckLib = (crane.mkLib pkgs).overrideToolchain checkToolchain;
unfilteredRoot = ./.;
zigPkg = zig.packages.${system}."0.16.0";
ghosttyLib = ghostty.packages.${system}.libghostty-vt;
miriCommand = pkgs.writeShellApplication {
name = "libghostty-miri";
runtimeInputs = [miriToolchain];
text = ''
cargo miri test --locked -p libghostty-vt-sys --lib
cargo miri test --locked -p libghostty-vt --lib alloc::tests::
# Issue #74 soundness regression tests (both clipboard findings
# live in this module). Miri aborts the test process at the first
# UB it finds, so a broken tree reports only the first failing test.
cargo miri test --locked -p libghostty-vt --lib miri_soundness
'';
};
src = pkgs.lib.fileset.toSource {
root = unfilteredRoot;
fileset = pkgs.lib.fileset.unions [
(craneLib.fileset.commonCargoSources unfilteredRoot)
(pkgs.lib.fileset.fileFilter (
file:
file.hasExt "h"
|| file.hasExt "zig"
|| file.hasExt "zon"
|| file.hasExt "md"
|| file.hasExt "ttf"
) unfilteredRoot)
];
};
commonArgs =
{
pname = "libghostty-rs";
version = "0.2.1";
inherit src;
strictDeps = true;
# No workspace member enables the optional `png` feature, so turn it
# on here; otherwise the PNG decoder is never type-checked, linted,
# doc-checked or tested in CI. Setting it in the shared arguments
# also builds the dependency artifacts with it, so the checks
# below don't each rebuild png and its dependencies.
cargoExtraArgs = "--locked --features libghostty-vt-sys/pkg-config --features libghostty-vt/png";
nativeBuildInputs = [
pkgs.pkg-config
pkgs.clang
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.cctools
pkgs.xcbuild
];
buildInputs =
[
ghosttyLib
pkgs.libclang
pkgs.openssl
]
++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.apple-sdk
pkgs.libiconv
];
}
// pkgs.lib.optionalAttrs pkgs.stdenv.isDarwin {
DEVELOPER_DIR = "${pkgs.apple-sdk}";
SDKROOT = "${pkgs.apple-sdk.sdkroot}";
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
application = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
}
);
in {
packages.default = application;
checks = {
default = application;
cargo-check = craneLib.mkCargoDerivation (
commonArgs
// {
inherit cargoArtifacts;
pnameSuffix = "-check";
buildPhaseCargoCommand = "cargoWithProfile check ${commonArgs.cargoExtraArgs} --workspace --all-targets";
}
);
cargo-clippy = craneCheckLib.cargoClippy (
commonArgs
// {
inherit cargoArtifacts;
# Without denying warnings, clippy findings never fail CI.
cargoClippyExtraArgs = "--workspace --all-targets -- --deny warnings";
}
);
cargo-doc = craneCheckLib.cargoDoc (
commonArgs
// {
inherit cargoArtifacts;
cargoDocExtraArgs = "--workspace --no-deps";
RUSTDOCFLAGS = "-D warnings";
}
);
# Regenerate the bindings from the pinned Ghostty headers and fail if
# they differ from the checked-in ones. GHOSTTY_INCLUDE_DIR is used
# instead of GHOSTTY_SOURCE_DIR so that the build script keeps linking
# the prebuilt library rather than building Ghostty from source.
#
# The headers come from the flake input, but users build against
# the commit pinned in build.rs. Nothing else ties the two together,
# so check that they agree first; otherwise bumping only one of them
# would leave this check green while comparing the wrong headers.
bindings-fresh = craneCheckLib.mkCargoDerivation (
commonArgs
// {
# The shared deps are built without the bindgen-tool feature and in
# the release profile, so they would be unpacked and then ignored.
# A dev build of just the generator is quicker than a release one.
cargoArtifacts = null;
pnameSuffix = "-bindings";
GHOSTTY_INCLUDE_DIR = "${ghostty}/include";
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
buildPhaseCargoCommand = ''
if ! grep -qF 'const GHOSTTY_COMMIT: &str = "${ghostty.rev}";' \
crates/libghostty-vt-sys/build.rs; then
echo "error: the Ghostty pin in flake.nix (${ghostty.rev}) does not" >&2
echo "match GHOSTTY_COMMIT in crates/libghostty-vt-sys/build.rs." >&2
echo "Bump both to the same commit." >&2
exit 1
fi
# Not commonArgs.cargoExtraArgs: it also enables libghostty-vt
# features, which cargo rejects with only the -sys crate selected.
cargo run --locked --features libghostty-vt-sys/pkg-config -p libghostty-vt-sys \
--features libghostty-vt-sys/bindgen-tool --bin gen-bindings
cargo fmt -p libghostty-vt-sys
if ! diff -u ${src}/crates/libghostty-vt-sys/src/bindings.rs \
crates/libghostty-vt-sys/src/bindings.rs; then
echo "error: crates/libghostty-vt-sys/src/bindings.rs is out of date." >&2
echo "Regenerate it from the dev shell with:" >&2
echo " cargo run -p libghostty-vt-sys --features bindgen-tool --bin gen-bindings && cargo fmt -p libghostty-vt-sys" >&2
exit 1
fi
'';
doInstallCargoArtifacts = false;
installPhaseCommand = "touch $out";
}
);
cargo-fmt = craneCheckLib.cargoFmt {
pname = "libghostty-rs";
version = "0.2.1";
inherit src;
};
cargo-test = craneLib.cargoTest (
commonArgs
// {
inherit cargoArtifacts;
cargoTestExtraArgs = "--workspace --all-targets";
}
);
# `--all-targets` does not include doctests, so run them separately.
# Only for the safe crate: the -sys bindings carry C header comments
# that rustdoc would otherwise try to compile as Rust.
cargo-doctest = craneLib.cargoTest (
commonArgs
// {
inherit cargoArtifacts;
pnameSuffix = "-doctest";
cargoTestExtraArgs = "-p libghostty-vt --doc";
}
);
# Upstream ghostty exercises both link modes in CI, so run the test
# suite against the shared library as well. Cargo adds the emitted
# link-search paths to the test binaries' library path, so the
# ghostty .so is found at runtime.
cargo-test-dynamic = craneLib.cargoTest (
commonArgs
// {
inherit cargoArtifacts;
cargoExtraArgs = "${commonArgs.cargoExtraArgs} --features libghostty-vt-sys/link-dynamic";
cargoTestExtraArgs = "--workspace --all-targets";
}
);
};
apps.miri = flake-utils.lib.mkApp {
drv = miriCommand;
};
devShells.default = craneLib.devShell {
packages = [
devToolchain
zigPkg
pkgs.clang
pkgs.libclang
pkgs.pkg-config
pkgs.openssl
pkgs.cmake
pkgs.ninja
] ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [
pkgs.libx11
pkgs.libxcursor
pkgs.libxrandr
pkgs.libxinerama
pkgs.libxi
pkgs.libGL
pkgs.libxkbcommon
pkgs.wayland
];
shellHook = ''
export LIBCLANG_PATH=${pkgs.libclang.lib}/lib
'' + pkgs.lib.optionalString pkgs.stdenv.hostPlatform.isDarwin ''
# Unset Nix Darwin SDK env vars and remove the xcbuild
# xcrun wrapper so Zig's SDK detection uses the real
# system xcrun/xcode-select.
unset SDKROOT
unset DEVELOPER_DIR
export PATH=$(echo "$PATH" | tr ':' '\n' | grep -v xcbuild | tr '\n' ':')
'' + pkgs.lib.optionalString pkgs.stdenv.hostPlatform.isLinux ''
# Make Ghostling able to find libGL on Linux.
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${pkgs.lib.makeLibraryPath [
pkgs.libglvnd
pkgs.wayland
pkgs.libx11
pkgs.libxkbcommon
pkgs.libxi
]}"
'';
};
devShells.miri = pkgs.mkShell {
packages = [miriToolchain];
};
}
);
}