-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflake.nix
More file actions
351 lines (304 loc) · 11 KB
/
Copy pathflake.nix
File metadata and controls
351 lines (304 loc) · 11 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
{
description = "VOICEVOX CLI";
nixConfig = {
extra-substituters = [
"https://voicevox-cli.cachix.org"
"https://nix-community.cachix.org"
];
extra-trusted-public-keys = [
"voicevox-cli.cachix.org-1:mgBVkErTVM4g1h08Bz86D73qhB4Jew/+JQ4iCjaPzj0="
"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
];
};
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
crane.url = "github:ipetkov/crane";
};
outputs =
{
self,
nixpkgs,
flake-utils,
fenix,
crane,
}:
let
systems = [ "aarch64-darwin" ];
in
flake-utils.lib.eachSystem systems (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
lib = pkgs.lib;
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
version = cargoToml.package.version;
# Fenix stable toolchain
rustToolchain = fenix.packages.${system}.stable;
# Minimal toolchain for builds and checks (no rust-src/rust-docs)
buildToolchain = fenix.packages.${system}.combine [
rustToolchain.rustc
rustToolchain.cargo
rustToolchain.rustfmt
rustToolchain.clippy
];
# Crane library with minimal fenix toolchain
craneLib = (crane.mkLib pkgs).overrideToolchain buildToolchain;
# Source filtering
src = lib.cleanSourceWith {
src = craneLib.cleanCargoSource ./.;
filter =
path: type:
let
baseName = baseNameOf path;
in
!(
(type == "directory" && lib.hasSuffix "-extract" baseName)
|| (type == "regular" && lib.hasSuffix ".tar.gz" baseName)
);
};
# ONNX Runtime library search path for build.rs (voicevox-ort-sys).
# Actual library is loaded at runtime via dlopen (load-dynamic),
# so only the path needs to exist at build time.
onnxruntimeLibDir = pkgs.runCommand "onnxruntime-lib" { } ''
mkdir -p $out/lib
'';
# Vendor cargo dependencies (git deps fetched at eval time)
cargoVendorDir = craneLib.vendorCargoDeps {
inherit src;
overrideVendorGitCheckout =
ps: drv:
# VOICEVOX/ort is a workspace with excluded members (backends,
# examples, tests) whose Cargo.toml files confuse crane's
# package discovery. Vendor the two needed crates manually.
if lib.any (p: p.name == "ort") ps then
let
pkg = name: (lib.findFirst (p: p.name == name) null ps);
dir = p: "${p.name}-${p.version}";
ortPkg = pkg "ort";
sysPkg = pkg "ort-sys";
in
assert ortPkg != null && sysPkg != null;
drv.overrideAttrs {
installPhase =
let
ort = dir ortPkg;
sys = dir sysPkg;
in
''
mkdir -p $out
# Root crate (copy without workspace members)
cp -r . $out/${ort}
rm -rf $out/${ort}/{ort-sys,backends,examples,tests}
echo '{"files":{}}' > $out/${ort}/.cargo-checksum.json
# ort-sys sub-crate
cp -r ort-sys $out/${sys}
echo '{"files":{}}' > $out/${sys}/.cargo-checksum.json
'';
}
else
drv;
};
# Shared build arguments for all crane derivations
commonArgs = {
inherit src cargoVendorDir version;
pname = "voicevox-cli";
strictDeps = true;
doCheck = false;
cargoExtraArgs = "--locked --all-features";
CARGO_NET_OFFLINE = true;
ORT_LIB_LOCATION = "${onnxruntimeLibDir}";
preConfigure = ''
export HOME=$PWD/build-home
mkdir -p $HOME
'';
preBuild = ''
export GIT_SSL_CAINFO="${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
export SSL_CERT_FILE="${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
'';
nativeBuildInputs = with pkgs; [
pkg-config
cmake
gnumake
autoconf
automake
libtool
git
cacert
];
};
# Build only cargo dependencies (shared by build, clippy, tests)
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# Voice models and resources downloader
voicevoxDownloader = pkgs.fetchurl {
url = "https://github.com/VOICEVOX/voicevox_core/releases/download/0.16.3/download-osx-arm64";
hash = "sha256-7GMosxM4HRDAix6BImNP5Q5PNpWJYEvMLNApKjNht+k=";
};
voicevoxResources = pkgs.stdenv.mkDerivation {
name = "voicevox-resources";
dontUnpack = true;
installPhase = ''
mkdir -p $out/bin
cp ${voicevoxDownloader} $out/bin/voicevox-download
chmod +x $out/bin/voicevox-download
'';
};
packageMeta = {
description = "VOICEVOX CLI for Apple Silicon - Dynamic voice detection system";
homepage = "https://github.com/usabarashi/voicevox-cli";
license = with lib.licenses; [
mit
asl20
];
platforms = systems;
};
# Final package
voicevoxCli = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
postInstall = ''
cp ${voicevoxResources}/bin/voicevox-download $out/bin/
install -m755 ${./scripts/voicevox-setup.sh} $out/bin/voicevox-setup
install -m644 ${./VOICEVOX.md} $out/bin/VOICEVOX.md
'';
meta = packageMeta;
}
);
# Development utility: reset daemon state
voicevoxResetWrapper = pkgs.writeShellScriptBin "voicevox-reset" (
builtins.readFile ./scripts/voicevox-reset.sh
);
mkApp = program: {
type = "app";
inherit program;
};
appBins = [
"voicevox-say"
"voicevox-daemon"
"voicevox-mcp-server"
];
appAttrs = lib.genAttrs appBins (bin: mkApp "${voicevoxCli}/bin/${bin}");
in
{
packages = rec {
default = voicevoxCli;
voicevox-cli = default;
voicevox-say = default;
inherit voicevoxResources;
};
checks = {
# Code formatting
formatting = craneLib.cargoFmt {
inherit src;
};
# Shell script syntax validation
scripts =
pkgs.runCommand "check-scripts"
{
nativeBuildInputs = with pkgs; [
bash
gnused
gnugrep
];
src = lib.cleanSourceWith {
src = ./.;
filter =
path: type:
let
baseName = baseNameOf path;
in
(type == "directory" && baseName == "scripts")
|| (type == "regular" && lib.hasSuffix ".sh" baseName);
};
}
''
test -f $src/scripts/voicevox-setup.sh || (echo "Missing voicevox-setup.sh" && exit 1)
for script in $src/scripts/*.sh; do
if [ -f "$script" ]; then
echo "Validating: $(basename "$script")"
if grep -q '@@.*@@' "$script"; then
sed 's/@@[^@]*@@/placeholder/g' "$script" | bash -n
else
bash -n "$script"
fi
fi
done
touch $out
'';
# Build verification
build = voicevoxCli;
# Static analysis (reuses cargoArtifacts)
clippy = craneLib.cargoClippy (
commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- -D warnings";
}
);
# Test suite (reuses cargoArtifacts)
tests = craneLib.cargoTest (
commonArgs
// {
inherit cargoArtifacts;
doCheck = true;
}
);
};
apps = appAttrs // {
default = appAttrs.voicevox-say;
};
devShells.default = craneLib.devShell {
checks = self.checks.${system};
# ONNX Runtime library search path (actual library loaded at runtime via dlopen)
ORT_LIB_LOCATION = "${onnxruntimeLibDir}";
packages = with pkgs; [
rustToolchain.rust-analyzer
cargo-audit
# Build tools
pkg-config
cmake
gcc
# Development utilities
voicevoxResetWrapper
# TLC model checker for modeling/*.tla
tlaplus
];
shellHook = ''
# Work around Nix Darwin cc-wrapper linker conflicts when host/build SDK env vars differ.
unset DEVELOPER_DIR_FOR_BUILD
unset SDKROOT_FOR_BUILD
unset NIX_APPLE_SDK_VERSION_FOR_BUILD
# Create project-home directory for CARGO_HOME
mkdir -p .project-home
export CARGO_HOME="$PWD/.project-home/.cargo"
echo "VOICEVOX CLI Development Environment (Apple Silicon)"
echo "Available commands:"
echo " cargo build --bin voicevox-say - Build client"
echo " cargo build --bin voicevox-daemon - Build daemon"
echo " cargo run --bin voicevox-say - Run client"
echo " nix build - Build with Nix"
echo " nix flake check - Run formatting/scripts/build/clippy/tests"
echo " nix run - Run voicevox-say directly"
echo " voicevox-reset - Reset daemon state (kill processes + remove socket)"
echo " cargo test - Also works in this shell (FOR_BUILD SDK vars sanitized)"
echo " cargo kani - Run Kani proofs (install: cargo install --locked kani-verifier && cargo kani setup)"
echo " tlc -deadlock -config X.cfg X.tla - Run TLC model checker (in modeling/)"
echo ""
echo "Dynamic voice detection system - no hardcoded voice names"
'';
};
}
)
// {
overlays.default = final: _prev: {
voicevox-cli = (self.packages.${final.system} or self.packages.aarch64-darwin).voicevox-cli;
voicevox-say = final.voicevox-cli;
};
overlays.voicevox-cli = self.overlays.default;
};
}