Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
1334 lines (1216 loc) · 58 KB
/
Copy pathflake.nix
File metadata and controls
1334 lines (1216 loc) · 58 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
description = "VoiceStudio — Electron desktop shell, FastAPI/PyTorch backend, Rust dictation helper";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs =
{ self, nixpkgs }:
let
inherit (nixpkgs) lib;
# VoiceStudio publishes Linux desktop artifacts for x86_64 only
# (docs/install/linux.md, "Requirements"). The Linux shells are offered
# on aarch64 too, because building from source works there; Apple
# Silicon gets a plain toolchain shell.
linuxSystems = [
"x86_64-linux"
"aarch64-linux"
];
devSystems = linuxSystems ++ [ "aarch64-darwin" ];
buildSystems = [ "x86_64-linux" ];
forDevSystems = f: lib.genAttrs devSystems (system: f nixpkgs.legacyPackages.${system});
# ── Source ───────────────────────────────────────────────────────────
# self is the flake's own tree. Stripping .git keeps the store copy small
# and stops git metadata from churning the hash.
src = lib.cleanSourceWith {
src = lib.cleanSource self;
# cleanSource already drops .git and Nix result symlinks, not regular
# result-* source modules. node_modules and .venv are gitignored but
# `nix build path:.` would otherwise copy them.
filter =
path: type:
let
name = baseNameOf (toString path);
in
name != "node_modules"
&& name != ".venv";
};
# Compare real regular modules in the unfiltered tree with the exact
# source consumed by the app build. Skip dependencies before recursion
# and never follow symlinks, including Nix result links.
sourceModules =
let
collect = relative:
lib.concatLists (lib.mapAttrsToList (name: type:
let
child = if relative == "" then name else "${relative}/${name}";
in
if lib.elem name [ ".git" ".svn" "CVS" ".hg" ".jj" ".pijul" "_darcs" "node_modules" ".venv" ] then [ ]
else if type == "directory" then collect child
else lib.optional (
type == "regular"
&& lib.any (extension: lib.hasSuffix extension name)
[ ".js" ".jsx" ".mjs" ".cjs" ".ts" ".tsx" ".mts" ".cts" ]
) child
) (builtins.readDir "${self}/${relative}"));
in
collect "";
missingSourceModules = lib.filter (
relative: !(builtins.pathExists "${src}/${relative}")
) sourceModules;
# Only what `bun install` reads: the root manifest and lockfile, each
# workspace's package.json, the patches named in patchedDependencies and
# any bun/npm config. The node-deps fixed-output derivation is built from
# this, so ordinary source edits cannot reach its pinned hash. Both lists
# are read from package.json, so a new workspace or patch is picked up
# without touching this file.
rootManifest = lib.importJSON ./package.json;
nodeDepsSrc = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions (
[
./package.json
./bun.lock
(lib.fileset.maybeMissing ./bunfig.toml)
(lib.fileset.maybeMissing ./.npmrc)
]
++ map (
ws:
assert lib.assertMsg (!lib.hasInfix "*" ws)
"flake.nix: workspace glob \"${ws}\" in package.json; list workspaces explicitly.";
./. + "/${ws}/package.json"
) (rootManifest.workspaces or [ ])
++ map (patch: ./. + "/${patch}") (lib.attrValues (rootManifest.patchedDependencies or { }))
);
};
# ── libxdo ───────────────────────────────────────────────────────────
# enigo (a dependency of the Rust dictation helper) enables its default
# `xdo` feature, whose src/linux/xdo.rs declares `#[link(name = "xdo")]`,
# so linking the helper needs libxdo.so.
#
# No nixpkgs attribute provides it:
# * `pkgs.xdo` (baskerville/xdo 0.5.7) builds only the `xdo` binary and
# links the library in statically — there is no libxdo.so to link.
# * `pkgs.xdotool` ships the same sources and has an `installlib` target,
# but its install phase installs bin/ and man/ only.
# So build just the library from xdotool's own pinned source, reusing
# nixpkgs' hashes so this tracks upstream automatically.
libxdo =
pkgs:
let
xdotool = pkgs.xdotool;
in
pkgs.stdenv.mkDerivation {
name = "libxdo";
pname = "libxdo";
inherit (xdotool) src version;
nativeBuildInputs = [ pkgs.pkg-config pkgs.perl ];
buildInputs = [
pkgs.libx11
pkgs.libxtst
pkgs.xorgproto
pkgs.libxi
pkgs.libxinerama
pkgs.libxkbcommon
pkgs.libxext
];
strictDeps = true;
dontConfigure = true;
# `installlib` is the upstream target that installs libxdo.so with the
# versioned SONAME plus the unversioned symlink; `installheader`
# installs xdo.h. Using them keeps the SONAME exactly what upstream
# ships, which is what enigo's #[link(name = "xdo")] resolves against.
makeFlags = [
"installlib"
"installheader"
"PREFIX=$(out)"
];
doCheck = false;
meta = {
description = "libxdo shared library (built from the xdotool sources)";
homepage = "https://github.com/jordansissel/xdotool";
license = pkgs.lib.licenses.bsd3;
platforms = pkgs.lib.platforms.linux;
};
};
# ── Toolchain ────────────────────────────────────────────────────────
# Mirrors the native prerequisites in docs/install/linux.md.
#
# The helper links TWO C libraries, not zero:
# * libxdo — enigo 0.3's default feature is ["xdo"], and its
# src/linux/xdo.rs carries #[link(name = "xdo")].
# * libwayland-client — arboard's wayland-data-control, via wayland-sys'
# pkg-config probe.
# libxdo is provided by the derivation above; it belongs on the library
# search path so cargo's linker finds -lxdo.
toolchain =
pkgs:
with pkgs;
[
bun
nodejs_22
uv
python311
cargo
rustc
clippy
rustfmt
cmake
ninja
pkg-config
clang
gnumake
binutils
wayland
openssl
alsa-lib
libx11
libXtst
libxkbcommon
libGL
nss
nspr
at-spi2-atk
libdrm
libgbm
libxshmfence
libpulseaudio
libjack2
dbus
git
curl
ripgrep
which
];
devShell =
pkgs:
pkgs.mkShell {
name = "voicestudio";
nativeBuildInputs = toolchain pkgs;
packages = with pkgs; [
# ffmpeg is one of four tiers in
# backend/services/ffmpeg_utils.py::find_ffmpeg; having it on PATH
# avoids the ~142 MB first-run download from zackees/ffmpeg_bins.
ffmpeg
uv
python311
];
# uv resolves `.python-version` (3.11) against the interpreter already
# in the shell instead of downloading python-build-standalone. The venv
# still lands in the repo's gitignored .venv, so `bun run setup:api`
# behaves exactly as it does on Debian/Fedora/Arch.
UV_PYTHON_PREFERENCE = "only-system";
# PyTorch's wheels ship a C++ runtime dependency they expect the host
# to provide: importing torch, torchvision or torchaudio fails with
# "libstdc++.so.6: cannot open shared object file" without it, and
# NixOS has no /usr/lib to fall back on. libgomp comes from the same
# GCC runtime and is needed by the same extensions.
LD_LIBRARY_PATH = lib.makeLibraryPath [
pkgs.stdenv.cc.cc.lib
pkgs.gnumake
];
# enigo's #[link(name = "xdo")] is resolved by the linker, which does
# not consult pkg-config, so the search path has to be set explicitly
# for `cargo build` of the dictation helper to succeed here.
NIX_LDFLAGS = "-L${libxdo pkgs}/lib -rpath ${libxdo pkgs}/lib";
# `bun run dev` would otherwise launch the generic Electron binary
# the npm package downloads, which needs /lib64/ld-linux-*.so and
# FHS library paths that NixOS lacks. Point the npm package at
# nixpkgs' patched build of the same major instead (electron's
# index.js honours ELECTRON_OVERRIDE_DIST_PATH) and skip the
# download it would never use.
ELECTRON_OVERRIDE_DIST_PATH = "${pkgs.electron_44-bin}/libexec/electron";
ELECTRON_SKIP_BINARY_DOWNLOAD = "1";
shellHook = ''
echo "VoiceStudio dev shell"
echo " 1. bun install"
echo " 2. bun run setup:api # creates .venv via uv"
echo " 3. bun run dev # Electron + supervised backend"
echo ""
echo "Installer production (bun run dist) is NOT supported here: electron-builder"
echo "downloads its own tool binaries from GitHub at build time. For a Nix"
echo "build of the unpacked Linux app use: nix build"
'';
};
# ── FHS environment ──────────────────────────────────────────────────
# The fallback for NixOS hosts without nix-ld: an FHS sandbox in which
# the npm-downloaded Electron, uv's managed CPython and arbitrary
# manylinux wheels find /lib64 and /usr/lib exactly as on other
# distributions. Kept as `.#fhs` so existing instructions keep working.
fhsLibs =
pkgs: with pkgs; [
alsa-lib
at-spi2-atk
at-spi2-core
atk
cairo
cups
dbus
expat
glib
gtk3
libGL
libdrm
libgbm
libpulseaudio
libx11
libxkbcommon
libxcomposite
libxdamage
libxext
libxfixes
libxrandr
libxcb
libxtst
mesa
nspr
nss
openssl
pango
systemd
vulkan-loader
wayland
xdotool
zlib
];
fhsEnv =
pkgs:
pkgs.buildFHSEnv {
name = "voicestudio-fhs";
targetPkgs =
p:
(with p; [
bun
nodejs_22
uv
rustc
cargo
pkg-config
ffmpeg
git
gcc
gnumake
binutils
zsync
])
++ fhsLibs p
# xdotool ships no shared libxdo; the helper links it.
++ [ (libxdo p) ];
# uv must use its own managed CPython, not a Nix store Python.
# No OMNIVOICE_PORT here: an explicit port disables the app's
# fallback when the default backend port is reserved.
profile = ''
export UV_PYTHON_PREFERENCE="only-managed"
'';
# Headers and .pc files for native builds inside the FHS env.
extraOutputsToInstall = [ "dev" ];
runScript = "bash";
};
# macOS needs none of the Linux loader work: the downloaded Electron
# and uv's managed CPython run as they do outside Nix.
darwinShell =
pkgs:
pkgs.mkShell {
name = "voicestudio";
packages = with pkgs; [
bun
nodejs_22
uv
rustc
cargo
pkg-config
ffmpeg
git
apple-sdk
];
};
# ── node dependencies ────────────────────────────────────────────────
# Fixed-output derivations are how Nix expresses "reach the network here,
# and only here, then verify the result by hash". The sandbox itself stays
# hermetic, so no --impure flag is needed anywhere.
#
# First build prints the correct hash; paste it over the outputHash below.
nodeDeps =
{ pkgs }:
pkgs.runCommand "voicestudio-node-deps"
{
nativeBuildInputs = [
pkgs.bun
pkgs.nodejs_22
];
outputHashMode = "recursive";
outputHash = "sha256-bqxnoXhbM2HK+xW+pBOy71WjSuAhsbjbr429b+uT6WU=";
sourceProvenance = null;
} ''
export HOME=$TMPDIR
export BUN_INSTALL="$TMPDIR/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
# Skip the electron postinstall: this build uses nixpkgs' electron
# runtime, and the download is both redundant and a network call we
# would rather not make.
export ELECTRON_SKIP_BINARY_DOWNLOAD=1
# Install into ./source from the dependency manifests alone, then
# drop what bun does not write deterministically:
# * the .cache tree (timestamps and generated binaries);
# * the per-package .bin directories inside the isolated store
# (node_modules/.bun/<pkg>/node_modules/.bin). bun links them
# concurrently, and for peer-dependency cycles (vite-plus and
# oxlint/oxfmt, vitest and @vitest/ui) which links exist varies
# from run to run on identical input, which flipped this
# derivation's hash between PRs. Nothing here runs a dependency
# through them; every consumer calls entrypoints by path.
# The top-level node_modules/.bin trees are deterministic and kept.
install_deps() {
cp -r ${nodeDepsSrc} ./source
chmod -R u+w ./source
(cd ./source && bun install --frozen-lockfile)
rm -rf source/node_modules/.cache source/electron/node_modules/.cache
find source/node_modules/.bun -type d -name .bin -prune -exec rm -rf {} +
}
# Install twice and require identical trees, so any new
# nondeterminism fails here with the differing paths instead of as
# an unexplained hash mismatch on some later, unrelated PR.
install_deps
mv source first
install_deps
if ! diff -r --no-dereference first source > $TMPDIR/determinism.diff 2>&1; then
echo "bun install is not deterministic; differing paths:" >&2
head -50 $TMPDIR/determinism.diff >&2
exit 1
fi
rm -rf first
cd ./source
# bun writes .bin entries as absolute symlinks into the build tree.
# Copy, then re-point any absolute symlink so the result is
# relocatable into a different store path.
mkdir -p $out/electron
# Preserve the directory NAMES bun used. Its isolated layout writes
# relative symlinks that walk out of electron/node_modules and back
# into the root node_modules, so renaming either tree breaks them.
cp -r node_modules $out/node_modules
cp -r electron/node_modules $out/electron/node_modules
chmod -R u+w $out
# bun's .bin shims are JavaScript entrypoints whose first line is
# `#!/usr/bin/env node`. /usr/bin/env exists in neither a Nix sandbox
# nor on NixOS, so the kernel would reject them with "bad
# interpreter". Two fixes are available and the shebang must not name
# an interpreter by store path, because a fixed-output derivation may
# not reference store paths:
#
# * drop the shebang — correct only when the file is run as
# `node <file>`, which is how every consumer below invokes them.
# * rewrite to an absolute path — illegal here, and it would also
# bake an interpreter path into a content-addressed output.
#
# So strip the first line and leave the body byte-exact.
for tree in $out/node_modules $out/electron/node_modules; do
if [ -d "$tree/.bin" ]; then
for shim in "$tree"/.bin/*; do
[ -f "$shim" ] || continue
case $(head -c 2 "$shim") in
'#!')
tail -n +2 "$shim" > "$shim.tmp"
chmod +x "$shim.tmp"
mv "$shim.tmp" "$shim"
;;
esac
done
fi
done
# bun records absolute symlinks into the build tree; make any that
# survived the copy relative to $out so it stays relocatable. The
# source tree's node_modules and electron/node_modules sit at the
# same relative paths under $out, so map the target there and link
# to it relative to the link's own directory (-s keeps the logical
# path bun's layout relies on instead of resolving through links).
prefix=$PWD
find $out -type l | while read -r link; do
target=$(readlink "$link")
case "$target" in
"$prefix"/*)
mapped="$out/''${target#"$prefix"/}"
ln -sfn "$(realpath -ms --relative-to="$(dirname "$link")" "$mapped")" "$link"
if [ ! -e "$link" ]; then
echo "rewritten symlink does not resolve: $link -> $target" >&2
exit 1
fi
;;
esac
done
# Every link must stay inside the two dependency trees. A link to a
# workspace package would resolve to its manifest-only stub here
# rather than to the checkout, so fail instead of shipping it.
find $out -type l | while read -r link; do
case "$(realpath -m "$link")" in
"$out"/node_modules | "$out"/node_modules/* | "$out"/electron/node_modules | "$out"/electron/node_modules/*) ;;
*)
echo "symlink leaves the dependency trees: $link -> $(readlink "$link")" >&2
exit 1
;;
esac
done
'';
# ── omnivoice-tts (OmniVoice GGUF runtime) ───────────────────────────
# The installers ship the runtime build-omnivoice-tts.yml builds with
# scripts/build-omnivoice-tts.sh as resources/bin/, and the backend
# refuses it unless every file matches bin/checksums.sha256. Build the
# same pinned omnivoice.cpp the same way (Vulkan + CPU backends loaded at
# runtime) and write the same manifest.
#
# The rev must equal quant_map.json's runtime_commit_sha (asserted
# below); the ggml rev is that commit's submodule gitlink. When the pin
# moves, update both revs and both hashes.
omnivoiceRuntimeRev = "040c8b344d8c670ce1475194751d119b5ef82c78";
omnivoiceGgmlRev = "d5152bf4348453068aa8de0d479caef40a613b9d";
pinnedRuntimeRev =
(lib.importJSON ./backend/engines/omnivoice_gguf/quant_map.json)._meta.runtime_commit_sha;
omnivoiceTts =
pkgs:
assert lib.assertMsg (omnivoiceRuntimeRev == pinnedRuntimeRev) ''
flake.nix pins omnivoice.cpp ${omnivoiceRuntimeRev}, but
backend/engines/omnivoice_gguf/quant_map.json pins ${pinnedRuntimeRev}.
Update omnivoiceRuntimeRev, omnivoiceGgmlRev and both hashes.
'';
let
ggml = pkgs.fetchFromGitHub {
owner = "ServeurpersoCom";
repo = "ggml";
rev = omnivoiceGgmlRev;
hash = "sha256-qRyucqwOppbd0ZjjrX48Bbdh9yyljYHuUwk3sQFXLnU=";
};
in
pkgs.stdenv.mkDerivation {
pname = "omnivoice-tts";
version = "0-unstable-${builtins.substring 0 7 omnivoiceRuntimeRev}";
src = pkgs.fetchFromGitHub {
owner = "ServeurpersoCom";
repo = "omnivoice.cpp";
rev = omnivoiceRuntimeRev;
hash = "sha256-9XE5WZo55hB4HCXaYV1FknOy0A0yVKqurgfVfaE1GhI=";
};
# GitHub archives leave the ggml submodule as an empty directory.
postUnpack = ''
rmdir "$sourceRoot/ggml"
cp -r ${ggml} "$sourceRoot/ggml"
chmod -R u+w "$sourceRoot/ggml"
'';
nativeBuildInputs = [
pkgs.cmake
(lib.getBin pkgs.shaderc) # glslc
];
buildInputs = [
pkgs.vulkan-headers
pkgs.vulkan-loader
pkgs.spirv-headers
];
# scripts/build-omnivoice-tts.sh's Linux Vulkan configuration.
dontUseCmakeConfigure = true;
buildPhase = ''
runHook preBuild
cmake -B build -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=ON \
-DGGML_BACKEND_DL=ON -DBUILD_SHARED_LIBS=ON \
-DGGML_NATIVE=OFF -DGGML_CPU_ALL_VARIANTS=ON
cmake --build build --config Release -j "$NIX_BUILD_CORES"
runHook postBuild
'';
# Flat layout, as the build script copies it: the binary next to every
# libggml* it loads, so ggml finds its backends beside the executable.
installPhase = ''
runHook preInstall
mkdir -p "$out"
cp -v build/omnivoice-tts "$out/omnivoice-tts-linux-x86_64"
cp -v build/tts-server "$out/tts-server-linux-x86_64"
find build -name 'libggml*.so*' -type f -exec cp -v {} "$out/" \;
find build -name 'libggml*.so*' -type l -exec cp -vL {} "$out/" \;
for file in "$out"/*; do
isELF "$file" || continue
rpath=$(patchelf --print-rpath "$file" | tr ':' '\n' |
{ grep -v -e "^$NIX_BUILD_TOP" -e '^$' || true; } | paste -sd: -)
patchelf --set-rpath "\$ORIGIN''${rpath:+:$rpath}" "$file"
done
runHook postInstall
'';
# The manifest must describe the bytes that ship, so it is written
# after fixup; shrinking the RPATH would drop $ORIGIN.
dontPatchELF = true;
postFixup = ''
(cd "$out" && sha256sum -- * | sort -k2) > "$TMPDIR/checksums.sha256"
mv "$TMPDIR/checksums.sha256" "$out/checksums.sha256"
'';
meta = {
description = "OmniVoice GGUF inference runtime (omnivoice.cpp)";
homepage = "https://github.com/ServeurpersoCom/omnivoice.cpp";
platforms = [ "x86_64-linux" ];
};
};
# ── Package ──────────────────────────────────────────────────────────
mkPackage =
pkgs:
let
electron = pkgs.electron_44-bin;
# electron/package.json pins electron to ^44.3.0; the nixpkgs pinned by
# flake.lock ships electron_44-bin 44.5.1, which satisfies that range
# and is the same upstream binary CI packages. Its dist is already
# patchelf'd (generic.nix sets the interpreter and an RPATH), so the
# copy below needs no loader work — only a rename, because Electron
# derives app.isPackaged from the executable's basename.
electronDist = electron + "/libexec/electron";
nodeDepsDev = nodeDeps { inherit pkgs; };
# The one place the build touches the network: a fixed-output
# derivation that installs the workspace's dependencies from bun.lock.
# Everything after this point is hermetic.
appBundle = pkgs.runCommand "voicestudio-app-bundle"
{
nativeBuildInputs = with pkgs; [ nodejs_22 ];
} ''
cp -r ${src} ./source
chmod -R u+w ./source
sourceRoot="$PWD/source"
cd ./source
export ELECTRON_SKIP_BINARY_DOWNLOAD=1
ln -s ${nodeDepsDev}/node_modules node_modules
ln -s ${nodeDepsDev}/electron/node_modules electron/node_modules
# bun's .bin shims have their shebang stripped in the fixed-output
# derivation (a FOD may not name an interpreter by store path, and
# /usr/bin/env does not exist here), so call the entrypoints
# through node explicitly instead of relying on exec.
electronVite="$PWD/electron/node_modules/electron-vite/bin/electron-vite.js"
viteCli="$PWD/electron/node_modules/vite/bin/vite.js"
# The locale checks the repo's own build script runs first.
node electron/tests/locale-encoding.mjs
node electron/tests/locale-source-keys.mjs
node electron/tests/locale-coverage.mjs
# Both vite configs live in electron/ and resolve their own imports
# (electron-vite, vite, plugins) relative to that directory, so run
# from inside electron/ the way `bun run --cwd electron build` does.
cd electron
# electron/out (main, preload, renderer).
node "$electronVite" build --config electron.vite.config.ts
# ../frontend/dist, the browser UI the backend serves to LAN devices.
node "$viteCli" build --config vite.web.config.ts
cd "$sourceRoot"
mkdir -p "$out/electron" "$out/frontend"
cp -r electron/out "$out/electron/out"
cp -r frontend/dist "$out/frontend/dist"
cp electron/package.json "$out/electron/package.json"
# Vite/Rolldown records source module paths as `//#region`
# comments. With node_modules coming from a store path those
# comments would make the output reference the whole
# voicestudio-node-deps derivation, keeping a duplicate
# node_modules in the runtime closure. The comments are inert, so
# drop the store path and keep the bundle self-contained.
find "$out/electron/out" "$out/frontend/dist" -type f \
\( -name '*.js' -o -name '*.mjs' -o -name '*.cjs' \) -print0 |
xargs -0 -r sed -i "s|${nodeDepsDev}||g"
# Ship only the modules the main process loads at runtime; the
# renderer is bundled and the dev toolchain is build-only.
node "$sourceRoot/scripts/prune-runtime-node-modules.mjs" \
"$sourceRoot" "$out/runtime-node-modules"
# electron/package.json carries the placeholder 0.0.0-electron;
# electron-builder substitutes the real version through
# extraMetadata (electron-builder.config.mjs), and
# tests/test_app_version.py pins the root package.json as the single
# source of truth. Do the same substitution here.
# The rest of extraMetadata matters too: Electron names the
# userData folder after productName (the AppImage's
# ~/.config/VoiceStudio) and takes the Linux app id from
# desktopName.
node --input-type=module -e '
import fs from "node:fs";
import { pathToFileURL } from "node:url";
const [file, root] = process.argv.slice(1);
const load = (rel) => import(pathToFileURL(root + "/" + rel).href);
const { PRODUCT_NAME } = await load("config/app-config.mjs");
const { DISTRIBUTION_TARGET, DISTRIBUTION_PROFILE } = await load("config/distributions.mjs");
const pkg = JSON.parse(fs.readFileSync(file, "utf8"));
Object.assign(pkg, {
version: JSON.parse(fs.readFileSync(root + "/package.json", "utf8")).version,
productName: PRODUCT_NAME,
desktopName: PRODUCT_NAME,
voicestudioDistribution: DISTRIBUTION_TARGET,
voicestudioUpdateOwner: DISTRIBUTION_PROFILE.applicationUpdates,
});
fs.writeFileSync(file, JSON.stringify(pkg, null, 2) + "\n");
' "$out/electron/package.json" "$sourceRoot"
'';
# The same cargo build electron-builder's afterPack hook performs,
# with crates resolved from the store instead of crates.io.
libXdo = libxdo pkgs;
# electron-builder.config.mjs sets syncDesktopName, keeping the
# window identity and the desktop entry on one name. The helper reads
# XDG_DATA_DIRS and never rewrites a system entry
# (native/desktop-bridge/src/wayland_shortcut_core.rs), so installing
# this to $out/share/applications is enough for dictation to bind.
desktopEntry = pkgs.makeDesktopItem {
name = "VoiceStudio";
desktopName = "VoiceStudio";
# makeDesktopItem is its own derivation, so a placeholder here
# would name the desktop item's output, which has no bin/. The
# install phase rewrites this to the package's absolute launcher.
exec = "voicestudio";
icon = "VoiceStudio";
comment = "Local voice cloning studio";
categories = [ "AudioVideo" "Audio" ];
};
desktopBridge = pkgs.rustPlatform.buildRustPackage {
pname = "voicestudio-desktop-bridge";
version = "0.0.0";
src = lib.cleanSource ./native/desktop-bridge;
# Every crate in this lockfile comes from the crates.io registry, and each
# carries a checksum, so importCargoLock resolves them without any
# outputHashes (that option only applies to git dependencies).
cargoLock.lockFile = ./native/desktop-bridge/Cargo.lock;
# nixpkgs' cargo setup hook exports AWS_LC_SYS_USE_SYSTEM=1 for
# every buildRustPackage, demanding a system AWS-LC. The matching
# per-crate override (default-crate-overrides.nix) only feeds the
# legacy buildRustCrate path, so buildRustPackage gets the "use the
# system library" flag without the buildInputs that would provide
# it, and aws-lc-sys (pulled in by reqwest's rustls feature) panics
# in its build script. This tree has no other -sys crate that the
# hook helps, so let aws-lc-sys build its vendored copy instead.
dontCargoUseSystemLibs = true;
nativeBuildInputs = [
pkgs.pkg-config
pkgs.cmake
];
# enigo links libxdo via #[link(name = "xdo")], which the linker
# resolves directly rather than through pkg-config.
NIX_LDFLAGS = "-L${libxdo pkgs}/lib -rpath ${libxdo pkgs}/lib";
buildInputs = [
pkgs.wayland
libXdo
];
meta = {
description = "VoiceStudio dictation and global-shortcut helper";
platforms = pkgs.lib.platforms.linux;
};
};
in
pkgs.stdenv.mkDerivation {
name = "voicestudio";
inherit src;
nativeBuildInputs = [
# The shell wrapper, for its --run hook (the NixOS-only GCC
# runtime path below); the binary wrapper has no equivalent.
pkgs.makeShellWrapper
# nixpkgs' electron_44-bin launcher passes wrapGAppsHook3's
# environment (GSettings schemas, GIO modules, pixbuf loaders,
# XDG_DATA_DIRS); without it GTK file dialogs can abort the app.
pkgs.wrapGAppsHook3
pkgs.patchelf
pkgs.installShellFiles
pkgs.desktop-file-utils
# Deliberately no autoPatchelfHook: the Electron runtime arrives
# already patched from nixpkgs, and the hook would additionally try
# to rewrite the JavaScript .bin shims in node_modules, which are
# not ELF ("cannot find section .dynamic").
pkgs.ffmpeg
pkgs.ripgrep
pkgs.uv
pkgs.git
];
# The Electron runtime from nixpkgs is already patchelf'd with the
# correct interpreter and RPATH, and the dictation helper is patched
# by its own buildRustPackage. Re-running autoPatchelf over the
# unpacked Electron dist would only rewrite identical values.
dontPatchELF = true;
# Same reasoning for stdenv's shebang pass: node_modules/.bin holds
# JavaScript entrypoints, and rewriting their (already stripped)
# shebangs only produces noise on files that are never exec'd here.
dontPatchShebangs = true;
# The strip pass likewise fails on the JavaScript entrypoints in
# node_modules ("patchelf: cannot find section .dynamic"). Debug
# symbols in the shipped app are not worth a failing build.
dontStrip = true;
# stdenv's fixupPhase walks $out and runs patchelf/strip over
# everything, including the JavaScript entrypoints in node_modules,
# which are not ELF. Nothing here needs fixing: the Electron runtime
# arrives patched from nixpkgs, the dictation helper is patched by
# its own buildRustPackage, and the wrapper created below is a shell
# script. mimeinfo.cache is stripped separately.
dontFixup = true;
# What electron_44-bin's own wrapper builds its GApps arguments
# from; gsettings-desktop-schemas supplies the schemas GTK reads.
buildInputs = [
pkgs.glib
pkgs.gtk3
pkgs.gtk4
pkgs.gsettings-desktop-schemas
];
# The wrapper is written by hand below (fixup is off), with the
# arguments the hook collects.
dontWrapGApps = true;
dontConfigure = true;
dontBuild = true;
installPhase = ''
runHook preInstall
staging="$TMPDIR/app"
resources="$staging/resources"
mkdir -p "$staging"
# ── Electron runtime ───────────────────────────────────────────
# Electron derives resources/ and locales/ from its own location,
# and nixpkgs' wrapper resolves the binary relative to itself, so
# the layout has to mirror electron_44-bin exactly:
#
# $out/libexec/electron/… the dist (binary, locales, .so files)
# $out/bin/voicestudio the wrapper
#
# Everything is assembled in the build directory and copied into
# $out once: paths under $out are read-only for anything but
# mkdir/install, so renaming entries in place is not possible.
mkdir -p "$staging/libexec"
cp -r ${electronDist} "$staging/libexec/electron"
chmod -R u+w "$staging"
resources="$staging/libexec/electron/resources"
# ── App payload ───────────────────────────────────────────────
# resources/app mirrors what electron-builder packs into app.asar:
# out/ + package.json. electron-vite's externalizeDepsPlugin leaves
# the main process importing its dependencies at runtime, so the
# runtime node_modules ships as a sibling of app/ (Node resolves it
# by walking up from app/out/main) rather than inside an asar.
mkdir -p "$resources/app"
cp -r ${appBundle}/electron/. "$resources/app/"
# appBundle already reduced bun's isolated layout to the modules
# the main process loads; copy it verbatim so the relative symlinks
# into .bun stay intact.
cp -a ${appBundle}/runtime-node-modules "$resources/node_modules"
# ── extraResources (electron-builder.config.mjs) ──────────────
mkdir -p "$resources/brand"
install -Dm644 electron/build/icons/icon.png "$resources/brand/icon.png"
install -Dm644 electron/build/icons/icon.ico "$resources/brand/icon.ico"
install -Dm644 electron/build/icons/32x32.png "$resources/brand/32x32.png"
install -Dm644 electron/build/icons/tray-recording.png "$resources/brand/tray-recording.png"
install -Dm644 electron/build/icons/icon-macos.png "$resources/brand/icon-macos.png"
install -Dm644 electron/build/icons/trayTemplate.png "$resources/brand/trayTemplate.png"
install -Dm644 electron/build/icons/trayRecordingTemplate.png "$resources/brand/trayRecordingTemplate.png"
# The OmniVoice GGUF runtime with its checksum manifest.
mkdir -p "$resources/bin"
cp ${omnivoiceTts pkgs}/* "$resources/bin/"
cp -r backend "$resources/backend"
cp -r omnivoice "$resources/omnivoice"
mkdir -p "$resources/frontend"
cp -r ${appBundle}/frontend/dist "$resources/frontend/dist"
install -Dm644 pyproject.toml "$resources/pyproject.toml"
install -Dm644 uv.lock "$resources/uv.lock"
install -Dm644 README.md "$resources/README.md"
install -Dm644 LICENSE "$resources/LICENSE"
install -Dm644 LICENSE-NOTICE.md "$resources/LICENSE-NOTICE.md"
install -Dm644 electron/T3CODE-LICENSE.txt "$resources/electron/T3CODE-LICENSE.txt"
install -Dm644 config/app.json "$resources/config/app.json"
install -Dm644 config/analytics.json "$resources/config/analytics.json"
install -Dm644 config/diagnostics.json "$resources/config/diagnostics.json"
# stageRuntimeSources (runtime-project.ts SOURCES) copies these on
# every platform and fails setup when one is missing.
install -Dm644 scripts/windows-rocm-recipe.json "$resources/scripts/windows-rocm-recipe.json"
install -Dm644 scripts/smoke_windows_rocm.py "$resources/scripts/smoke_windows_rocm.py"
# runtime-project.ts looks for <resources>/tools/uv before falling
# back to a download, so ship nixpkgs' uv and skip the installer
# fetch on first run.
install -Dm755 ${pkgs.uv}/bin/uv "$resources/tools/uv"
# backend.ts spawns this from resources/native.
install -Dm755 ${desktopBridge}/bin/voicestudio-desktop-bridge \
"$resources/native/voicestudio-desktop-bridge"
find "$resources" -name '__pycache__' -type d -prune -exec rm -rf {} +
find "$resources" -name '*.pyc' -delete
# ── Desktop integration ───────────────────────────────────────
install -Dm644 ${desktopEntry}/share/applications/VoiceStudio.desktop \
"$out/share/applications/VoiceStudio.desktop"
substituteInPlace "$out/share/applications/VoiceStudio.desktop" \
--replace-fail "Exec=voicestudio" "Exec=$out/bin/voicestudio"
# icon.png is 512x512; ship it at the sizes desktops actually ask
# for rather than re-encoding, which would need imagemagick.
for size in 32 48 64 128 256 512; do
install -Dm644 electron/build/icons/icon.png \
"$out/share/icons/hicolor/$size"x"$size"/apps/VoiceStudio.png
done
# ── Wrapper ───────────────────────────────────────────────────
# The wrapper must exec OUR dist, not electron_44-bin's.
# electron_44-bin/bin/electron is a makeCWrapper stub with its
# exec target baked in as an absolute store path, so copying it
# here silently launches nixpkgs' dist, whose resources/ holds only
# default_app.asar — Electron then pops the "electron <path-to-app>"
# error instead of starting the app.
#
# Wrapping $out/libexec/electron/electron instead also makes Electron
# resolve resources/ from its own location, which is what puts our
# app at resources/app and gives the renderer the process.resourcesPath
# that backend.ts and the native helper are addressed by. No app
# argument is needed, and none is wanted: passing one would leave
# resourcesPath pointing back at the wrong dist.
#
# Electron itself needs no LD_LIBRARY_PATH: nixpkgs patchelf'd the
# binary with an RPATH (see the patchelf call below). The backend
# it spawns does: PyTorch's wheels load libstdc++/libgomp, which
# NixOS has no /usr/lib to provide. Only add the GCC runtime on
# NixOS: on other distributions the host's own libstdc++ serves the
# uv-managed Python, and a newer Nix one could demand glibc symbols
# the host lacks.
#
# The store path is read-only, so Nix, not the in-app updater,
# updates this package (same as the AUR launcher).
#
# This runs in installPhase because dontFixup skips the fixup phase
# that would normally host it.
mkdir -p "$out/bin"
cp -r "$staging/libexec" "$out/libexec"
# Electron derives app.isPackaged from the executable's BASENAME and
# nothing else (App::IsPackaged in
# shell/browser/api/electron_api_app.cc):
#
# return base_name != FILE_PATH_LITERAL("electron");
#
# so a dist whose binary is still called "electron" reports itself
# unpackaged however complete resources/ is. backend.ts then takes
# its development branch and demands a repo checkout with a .venv
# (#resolveSpawnPlan), which cannot work: resources/ lives in a
# read-only store path and the runtime has to be installed under
# app.getPath('userData'). electron-builder renames the binary for
# the same reason; do it here.
mv "$out/libexec/electron/electron" "$out/libexec/electron/voicestudio"
# The copied binary still carries nixpkgs' RUNPATH, whose last entry
# is electron_44-bin's own dist. Point it at ours so the bundled
# .so files resolve inside this output instead of another package's
# store path, and so the package is self-contained.
patchelf --set-rpath \
"$(
patchelf --print-rpath "$out/libexec/electron/voicestudio" |
tr ':' '\n' |
grep -v -e '/libexec/electron$' -e '^$' |
tr '\n' ':'
)$out/libexec/electron" \
"$out/libexec/electron/voicestudio"
patchelf --set-rpath \
"$(patchelf --print-rpath "$out/libexec/electron/chrome_crashpad_handler" |
tr ':' '\n' |
grep -v -e '/libexec/electron$' -e '^$' |
tr '\n' ':'
)$out/libexec/electron" \
"$out/libexec/electron/chrome_crashpad_handler"
# dontFixup skips the preFixup hook that normally collects these.
gappsWrapperArgsHook
makeShellWrapper "$out/libexec/electron/voicestudio" "$out/bin/voicestudio" \
"''${gappsWrapperArgs[@]}" \
--prefix PATH : ${lib.makeBinPath [
pkgs.ffmpeg
pkgs.ripgrep
pkgs.uv
pkgs.git
pkgs.which
]} \
--set-default VOICESTUDIO_DISABLE_UPDATER 1 \
--run 'if [ -e /etc/NIXOS ]; then export LD_LIBRARY_PATH="${
lib.makeLibraryPath [ pkgs.stdenv.cc.cc.lib ]
}''${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"; fi'
runHook postInstall
'';