Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
287 lines (254 loc) · 9.59 KB
/
Copy pathflake.nix
File metadata and controls
287 lines (254 loc) · 9.59 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
{
description = "Voice Mode - Natural voice conversations for AI assistants";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
# nixos-24.05 is the last channel with cudaPackages_11_8 and gcc11.
# CUDA 11.8 supports compute 3.5–8.9, covering Pascal (GTX 10xx) through
# Ada Lovelace (RTX 40xx). Both cudaPackages_11_8 and gcc11 have been
# removed from nixos-unstable, so we need a second nixpkgs input.
nixpkgs-cuda11.url = "github:NixOS/nixpkgs/nixos-24.05";
# Pinned to the latest stable release tag, not main.
whisper-cpp-src = {
url = "github:ggerganov/whisper.cpp/v1.8.3";
flake = false;
};
};
outputs =
{
self,
nixpkgs,
flake-utils,
nixpkgs-cuda11,
whisper-cpp-src,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
lib = pkgs.lib;
isLinuxX86 = system == "x86_64-linux";
whisperVersion = "1.8.3";
# ------------------------------------------------------------------
# whisper.cpp builder
# ------------------------------------------------------------------
#
# Compute capability reference (set cudaArch to match your GPU):
# Pascal (GTX 10xx) → "61"
# Volta (Tesla V100) → "70"
# Turing (RTX 20xx) → "75"
# Ampere (RTX 30xx) → "86"
# Ada (RTX 40xx) → "89"
#
# The build sandbox has no GPU, so CMAKE_CUDA_ARCHITECTURES must be
# set explicitly — auto-detection would fail.
buildWhisperCpp =
{
cudaSupport ? false,
cudaArch ? "61", # Pascal default — override for your GPU
cudaPackages ? null,
hostCompiler ? null,
}:
pkgs.stdenv.mkDerivation {
pname = "whisper-cpp" + lib.optionalString cudaSupport "-cuda";
version = whisperVersion;
src = whisper-cpp-src;
nativeBuildInputs = [
pkgs.cmake
pkgs.pkg-config
]
++ lib.optionals cudaSupport [
cudaPackages.cuda_nvcc
# Adds an RPATH entry so libcuda.so.1 (the userspace driver)
# resolves at runtime. The driver lives outside the nix store
# (/run/opengl-driver/lib on NixOS) and is not available during
# the build. autoPatchelfHook is intentionally omitted — it
# clobbers the driver RPATH, and cmake already handles library
# references correctly for source builds.
pkgs.autoAddDriverRunpath
];
buildInputs = lib.optionals cudaSupport [
cudaPackages.cuda_cudart
cudaPackages.cuda_cccl # CUB + Thrust headers
cudaPackages.libcublas
# libstdc++ from the host compiler — the CUDA runtime links against it.
(lib.getLib hostCompiler.cc)
];
cmakeFlags = lib.optionals cudaSupport [
"-DGGML_CUDA=ON"
"-DCMAKE_CUDA_ARCHITECTURES=${cudaArch}"
"-DCMAKE_CUDA_HOST_COMPILER=${lib.getExe hostCompiler}"
];
# libcuda.so.1 is the userspace driver — only present on the host,
# never in the build sandbox. autoAddDriverRunpath handles runtime
# resolution via /run/opengl-driver/lib RPATH entry.
meta = {
description =
"whisper.cpp speech-to-text engine" + lib.optionalString cudaSupport " (CUDA, compute ${cudaArch})";
homepage = "https://github.com/ggerganov/whisper.cpp";
license = lib.licenses.mit;
platforms = [
"x86_64-linux"
]
++ lib.optionals (!cudaSupport) [
"aarch64-linux"
"aarch64-darwin"
"x86_64-darwin"
];
};
};
# ------------------------------------------------------------------
# whisper-cpp package variants
# ------------------------------------------------------------------
whisper-cpp = buildWhisperCpp { };
cudaPackageSet = lib.optionalAttrs isLinuxX86 (
let
# CUDA 11.8 toolchain — only instantiated on x86_64-linux.
# GCC 11 is required because CUDA 11.8's nvcc rejects GCC >= 12.
# Uses import (not legacyPackages) so we can set allowUnfree for
# the CUDA redistribution packages without affecting the main pkgs.
pkgsCuda11 = import nixpkgs-cuda11 {
inherit system;
config.allowUnfree = true;
};
whisper-cpp-cuda = buildWhisperCpp {
cudaSupport = true;
cudaPackages = pkgsCuda11.cudaPackages_11_8;
hostCompiler = pkgsCuda11.gcc11;
};
in
{
inherit whisper-cpp-cuda;
voice-mode-cuda = mkVoiceMode { whisperPackage = whisper-cpp-cuda; };
}
);
pythonEnv = pkgs.python312.withPackages (
ps: with ps; [
pip
setuptools
wheel
virtualenv
]
);
# Wrapper script that uses uvx with proper environment.
# When whisperPackage is set, its bin/ is added to PATH so that
# VoiceMode's find_whisper_server() discovers whisper-server
# via `which`.
mkVoiceMode =
{
whisperPackage ? null,
}:
pkgs.writeShellScriptBin "voice-mode" ''
export LD_LIBRARY_PATH="${
pkgs.lib.makeLibraryPath [
pkgs.portaudio
pkgs.libpulseaudio
pkgs.alsa-lib
pkgs.stdenv.cc.cc.lib
]
}:$LD_LIBRARY_PATH"
# Add build-time dependencies for compilation
export PKG_CONFIG_PATH="${
pkgs.lib.makeSearchPathOutput "dev" "lib/pkgconfig" [
pkgs.alsa-lib
pkgs.portaudio
pkgs.libpulseaudio
]
}:$PKG_CONFIG_PATH"
# Add development headers to C include path
export CPATH="${
pkgs.lib.makeSearchPathOutput "dev" "include" [
pkgs.alsa-lib
pkgs.portaudio
pkgs.libpulseaudio
]
}:$CPATH"
# Add library paths for linking
export LIBRARY_PATH="${
pkgs.lib.makeLibraryPath [
pkgs.alsa-lib
pkgs.portaudio
pkgs.libpulseaudio
]
}:$LIBRARY_PATH"
# Make sure gcc, pkg-config, and ffmpeg are available
export PATH="${pkgs.gcc}/bin:${pkgs.pkg-config}/bin:${pkgs.ffmpeg}/bin${
lib.optionalString (whisperPackage != null) ":${whisperPackage}/bin"
}:$PATH"
exec ${pkgs.uv}/bin/uvx voice-mode "$@"
'';
voice-mode = mkVoiceMode { };
in
{
packages = {
default = voice-mode;
inherit voice-mode whisper-cpp;
}
// cudaPackageSet;
devShells.default = pkgs.mkShell {
# Build-time dependencies (available during build)
nativeBuildInputs =
with pkgs;
[
pkg-config
gcc
cmake
]
++ pkgs.lib.optionals pkgs.stdenv.isLinux [
alsa-lib.dev # ALSA headers for building simpleaudio
libpulseaudio.dev # PulseAudio headers
];
# Runtime dependencies
buildInputs = with pkgs; [
# Python
pythonEnv
uv
# Audio libraries (runtime)
portaudio
libpulseaudio
alsa-lib
ffmpeg
# Audio tools
pulseaudio
alsa-utils
# Additional tools
git
];
shellHook = ''
echo "Voice Mode NixOS development environment"
echo "Python ${pkgs.python312.version} with uv and audio dependencies"
# Set up library paths
export LD_LIBRARY_PATH="${
pkgs.lib.makeLibraryPath [
pkgs.portaudio
pkgs.libpulseaudio
pkgs.alsa-lib
pkgs.stdenv.cc.cc.lib
]
}:$LD_LIBRARY_PATH"
# Set up pkg-config for build-time compilation
export PKG_CONFIG_PATH="${pkgs.alsa-lib.dev}/lib/pkgconfig:${pkgs.portaudio}/lib/pkgconfig:${pkgs.libpulseaudio.dev}/lib/pkgconfig:$PKG_CONFIG_PATH"
# Add development headers to C include path
export CPATH="${pkgs.alsa-lib.dev}/include:${pkgs.portaudio}/include:${pkgs.libpulseaudio.dev}/include:$CPATH"
# Add library paths for linking
export LIBRARY_PATH="${
pkgs.lib.makeLibraryPath [
pkgs.alsa-lib
pkgs.portaudio
pkgs.libpulseaudio
]
}:$LIBRARY_PATH"
# Create venv if it doesn't exist
if [ ! -d .venv ]; then
echo "Creating virtual environment..."
uv venv
fi
echo ""
echo "To activate the virtual environment, run: source .venv/bin/activate"
echo "Then install voice-mode with: uv pip install -e ."
echo "Or run directly with: uvx voice-mode"
'';
};
}
);
}