-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
399 lines (330 loc) · 11.7 KB
/
flake.nix
File metadata and controls
399 lines (330 loc) · 11.7 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
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
# From https://github.com/nix-community/naersk/blob/master/examples/cross-windows/flake.nix under MIT
{
description = "Qint TeamSpeak client";
inputs = {
fenix.url = "github:nix-community/fenix";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
naersk.inputs.nixpkgs.follows = "nixpkgs";
fenix.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = {
self,
nixpkgs,
naersk,
fenix,
flake-utils,
}:
flake-utils.lib.eachSystem ["x86_64-linux"] (system: let
pkgs = (import nixpkgs) {
inherit system;
};
lib = pkgs.lib;
native-toolchain = with fenix.packages.${system};
combine [
minimal.rustc
minimal.cargo
complete.clippy
latest.rustfmt
];
mingw-toolchain = with fenix.packages.${system};
combine [
minimal.rustc
minimal.cargo
targets.x86_64-pc-windows-gnu.latest.rust-std
];
naersk-lib = naersk.lib.${system}.override {
cargo = native-toolchain;
rustc = native-toolchain;
};
naersk-lib-win = naersk.lib.${system}.override {
cargo = mingw-toolchain;
rustc = mingw-toolchain;
};
sdlVersion = "2.26.5";
sdl-mingw = pkgs.fetchurl {
url = "https://www.libsdl.org/release/SDL2-devel-${sdlVersion}-mingw.tar.gz";
hash = "sha256-sO/fq5+qrrS0nVFFEriV0W/a/XBJYDKhMr2fS5jw9ME=";
};
run-libpaths = with pkgs; [
SDL2
cairo
gdk-pixbuf
glib
gtk3
libayatana-appindicator
libsoup_3
openssl
webkitgtk_4_1
];
defaultBuildArgs = {
pname = "qint";
root = ./.;
gitSubmodules = true;
strictDeps = true;
nativeBuildInputs = with pkgs; [
cmake
perl
pkg-config
cargo-tauri
];
# Deny warnings and clippy warnings
RUSTFLAGS = ["-Dwarnings"];
};
defaultLinuxBuildArgs =
defaultBuildArgs
// {
nativeBuildInputs =
defaultBuildArgs.nativeBuildInputs
++ (with pkgs; [
llvmPackages_latest.clang
llvmPackages_latest.lld
wrapGAppsHook4
# autoPatchelfHook # Make SDL2 available after build
]);
buildInputs = with pkgs;
[
dbus
glib-networking
gtksourceview
libopus
zlib
]
++ run-libpaths;
# Needed so bindgen can find libclang.so
LIBCLANG_PATH = "${pkgs.llvmPackages_latest.libclang.lib}/lib";
doCheck = true;
cargoTestOptions = opts: opts ++ ["--" "--skip" "web::tests"];
cargoTestCommands = tests:
tests
++ [
# TODO Run clippy lints
#"cargo $cargo_options clippy --all-targets"
# Check formatting
"cargo $cargo_options fmt --all --check"
];
};
node_modules = pkgs.stdenv.mkDerivation (finalAttrs: {
pname = "qint-frontend-node_modules";
version = "1.0.0";
outputHash = "sha256-egfWdejkeq+7ugcGl54CWylmkYbhzCC1M45Ehswq8+M=";
outputHashAlgo = "sha256";
outputHashMode = "recursive";
src = "${self}/frontend";
nativeBuildInputs = with pkgs; [
bun
];
dontConfigure = true;
dontFixup = true;
buildPhase = ''
runHook preBuild
bun install --no-progress --frozen-lockfile
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out
cp -R ./node_modules $out
runHook postInstall
'';
});
build-frontend = book_events:
pkgs.runCommand "build-qint-frontend" {
nativeBuildInputs = with pkgs; [bun nodejs];
src = ./frontend;
} ''
cp -r "$src/." .
ln -s ${node_modules}/node_modules ./
chmod -R +w .
cp ${book_events} src/book_events.ts
# bun run build
node node_modules/.bin/rsbuild build
mv dist $out
'';
win-pkg = naersk-lib-win.buildPackage (defaultBuildArgs
// {
depsBuildBuild = with pkgs; [
pkgsCross.mingwW64.stdenv.cc
pkgsCross.mingwW64.windows.pthreads
];
/*
nativeBuildInputs = defaultBuildArgs.nativeBuildInputs ++ (with pkgs; [
# We need Wine to run tests:
wineWowPackages.stable
]);
*/
# Tells Cargo that we're building for Windows.
# (https://doc.rust-lang.org/cargo/reference/config.html#buildtarget)
CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu";
# Fix some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
# when using C dependencies
TARGET_CC = "x86_64-w64-mingw32-gcc";
TARGET_CXX = "x86_64-w64-mingw32-g++";
# Tells Cargo that it should use Wine to run tests.
# (https://doc.rust-lang.org/cargo/reference/config.html#targettriplerunner)
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUNNER = pkgs.writeShellScript "wine-wrapper" ''
export WINEPREFIX="$(mktemp -d)"
exec wine64 $@
'';
# Fix undefined reference to `__stack_chk_fail' and mingw ld segmentation fault.
RUSTFLAGS = "-C link-args=-lssp -C link-args=-s";
overrideMain = oldAttrs:
oldAttrs
// {
# Production path
FRONTEND_PATH = "./ui/";
postPatch = ''
substituteInPlace src-tauri/tauri.conf.json \
--replace-fail ../frontend/dist ${frontend}
ln -s /build/source /build/dummy-src
'';
# Extract sdl
preBuild = ''
${pkgs.gnutar}/bin/tar xf ${sdl-mingw}
mkdir -p proxy-codegen/gnu-mingw/{lib,dll}/64
mv SDL2-${sdlVersion}/x86_64-w64-mingw32/lib/*.a proxy-codegen/gnu-mingw/lib/64/
mv SDL2-${sdlVersion}/x86_64-w64-mingw32/bin/SDL2.dll proxy-codegen/gnu-mingw/dll/64/
'';
# Tauri build
postBuild = ''
# Is overwritten during build
chmod u+w target/x86_64-pc-windows-gnu/release/WebView2Loader.dll
pushd src-tauri
cargo tauri build --no-bundle --target "$CARGO_BUILD_TARGET"
popd
'';
postInstall = ''
install -t $out/bin target/x86_64-pc-windows-gnu/release/build/webview2-com-sys-*/out/x64/WebView2Loader.dll
'';
};
});
qint = naersk-lib.buildPackage (defaultLinuxBuildArgs
// {
pname = "qint";
nativeBuildInputs = defaultLinuxBuildArgs.nativeBuildInputs ++ (with pkgs; [copyDesktopItems makeWrapper]);
# Only set for main derivation
overrideMain = oldAttrs:
oldAttrs
// {
postPatch = ''
substituteInPlace src-tauri/tauri.conf.json \
--replace-fail ../frontend/dist ${frontend}
ln -s /build/source /build/dummy-src
'';
postBuild = ''
pushd src-tauri
cargo tauri build --no-bundle
popd
'';
postFixup = ''
wrapProgram $out/bin/qint \
--set __NV_DISABLE_EXPLICIT_SYNC 1 \
--set SDL_AUDIO_DRIVER "pulseaudio" \
--prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath run-libpaths}
wrapProgram $out/bin/webapp \
--set SDL_AUDIO_DRIVER "pulseaudio" \
--prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath run-libpaths}
'';
postInstall = ''
install -Dm644 assets/128x128.png $out/share/icons/hicolor/128x128/apps/qint.png
install -Dm644 assets/128x128@2x.png $out/share/icons/hicolor/256x256/apps/qint.png
'';
FRONTEND_PATH = frontend;
# For tests
LD_LIBRARY_PATH = lib.makeLibraryPath run-libpaths;
RUST_BACKTRACE = "short";
};
desktopItems = [
(pkgs.makeDesktopItem
{
name = "Qint";
exec = "qint";
icon = "qint";
comment = "Mit Qint wird alles besser!";
desktopName = "Qint";
categories = ["Development"];
type = "Application";
})
];
});
# Generate just book_events.ts
book_events = naersk-lib.buildPackage (defaultLinuxBuildArgs
// {
cargoBuildOptions = opts: opts ++ ["--package" "proxy-codegen"];
doCheck = false;
overrideMain = oldAttrs:
oldAttrs
// {
installPhase = ''
mkdir -p $out
cp frontend/src/book_events.ts $out/
'';
};
});
frontend = build-frontend "${book_events}/book_events.ts";
in rec {
packages.default = packages.qint;
packages.node_modules = node_modules;
packages.frontend = frontend;
packages.builtDeps = pkgs.runCommand "qint-dependencies" {} ''
cat > $out <<EOF
${lib.concatStringsSep "," qint.builtDependencies}
${lib.concatStringsSep "," book_events.builtDependencies}
${lib.concatStringsSep "," win-pkg.builtDependencies}
EOF
'';
packages.qint = qint;
# The rust compiler is internally a cross compiler, so a single
# toolchain can be used to compile multiple targets. In a hermetic
# build system like nix flakes, there's effectively one package for
# every permutation of the supported hosts and targets.
# i.e.: nix build .#packages.x86_64-linux.x86_64-pc-windows-gnu
# where x86_64-linux is the host and x86_64-pc-windows-gnu is the
# target
packages.win = win-pkg;
packages.win-webapp-package = pkgs.runCommand "qint-win" {} ''
mkdir -p $out
mkdir -p Qint
cp ${win-pkg}/bin/webapp.exe Qint/
cp -ar ${frontend}/ Qint/ui
# Add SDL
${pkgs.gnutar}/bin/tar xf ${sdl-mingw}
mv SDL2-${sdlVersion}/x86_64-w64-mingw32/bin/SDL2.dll Qint/
${pkgs.zip}/bin/zip -r $out/Qint-webapp.zip Qint
'';
packages.win-tauri-package = pkgs.runCommand "qint-win" {} ''
mkdir -p $out
mkdir -p Qint
cp ${win-pkg}/bin/qint.exe Qint/
# Add SDL
${pkgs.gnutar}/bin/tar xf ${sdl-mingw}
mv SDL2-${sdlVersion}/x86_64-w64-mingw32/bin/SDL2.dll Qint/
# Add webview
cp ${win-pkg}/bin/WebView2Loader.dll Qint/
${pkgs.zip}/bin/zip -r $out/Qint.zip Qint
'';
apps.default = apps.qint;
apps.qint = flake-utils.lib.mkApp {
name = "qint";
drv = packages.qint;
};
apps.webapp = flake-utils.lib.mkApp {
name = "webapp";
drv = packages.qint;
};
checks.typos = pkgs.runCommand "check-typos" {} ''
cd ${self}
${lib.getExe pkgs.typos}
mkdir -p $out
'';
checks.build = packages.qint;
})
// {
nixosModules.default = {pkgs, ...}: {
environment.systemPackages = [
self.packages.${pkgs.stdenv.hostPlatform.system}.default
];
};
};
}