wlroots: 0.18.1 -> 0.18.2 (#364488)
[NixPkgs.git] / nixos / modules / services / home-automation / wyoming / faster-whisper.nix
blob69407efba0c6170860b88f583b951ab78f46eab2
2   config,
3   lib,
4   pkgs,
5   ...
6 }:
8 let
9   cfg = config.services.wyoming.faster-whisper;
11   inherit (lib)
12     escapeShellArgs
13     mkOption
14     mkEnableOption
15     mkPackageOption
16     types
17     ;
19   inherit (builtins)
20     toString
21     ;
26   options.services.wyoming.faster-whisper = with types; {
27     package = mkPackageOption pkgs "wyoming-faster-whisper" { };
29     servers = mkOption {
30       default = { };
31       description = ''
32         Attribute set of faster-whisper instances to spawn.
33       '';
34       type = types.attrsOf (
35         types.submodule (
36           { ... }:
37           {
38             options = {
39               enable = mkEnableOption "Wyoming faster-whisper server";
41               model = mkOption {
42                 type = str;
43                 default = "tiny-int8";
44                 example = "Systran/faster-distil-whisper-small.en";
45                 description = ''
46                   Name of the voice model to use.
48                   Check the [2.0.0 release notes](https://github.com/rhasspy/wyoming-faster-whisper/releases/tag/v2.0.0) for possible values.
49                 '';
50               };
52               uri = mkOption {
53                 type = strMatching "^(tcp|unix)://.*$";
54                 example = "tcp://0.0.0.0:10300";
55                 description = ''
56                   URI to bind the wyoming server to.
57                 '';
58               };
60               device = mkOption {
61                 # https://opennmt.net/CTranslate2/python/ctranslate2.models.Whisper.html#
62                 type = types.enum [
63                   "cpu"
64                   "cuda"
65                   "auto"
66                 ];
67                 default = "cpu";
68                 description = ''
69                   Determines the platform faster-whisper is run on. CPU works everywhere, CUDA requires a compatible NVIDIA GPU.
70                 '';
71               };
73               language = mkOption {
74                 type = enum [
75                   # https://github.com/home-assistant/addons/blob/master/whisper/config.yaml#L20
76                   "auto"
77                   "af"
78                   "am"
79                   "ar"
80                   "as"
81                   "az"
82                   "ba"
83                   "be"
84                   "bg"
85                   "bn"
86                   "bo"
87                   "br"
88                   "bs"
89                   "ca"
90                   "cs"
91                   "cy"
92                   "da"
93                   "de"
94                   "el"
95                   "en"
96                   "es"
97                   "et"
98                   "eu"
99                   "fa"
100                   "fi"
101                   "fo"
102                   "fr"
103                   "gl"
104                   "gu"
105                   "ha"
106                   "haw"
107                   "he"
108                   "hi"
109                   "hr"
110                   "ht"
111                   "hu"
112                   "hy"
113                   "id"
114                   "is"
115                   "it"
116                   "ja"
117                   "jw"
118                   "ka"
119                   "kk"
120                   "km"
121                   "kn"
122                   "ko"
123                   "la"
124                   "lb"
125                   "ln"
126                   "lo"
127                   "lt"
128                   "lv"
129                   "mg"
130                   "mi"
131                   "mk"
132                   "ml"
133                   "mn"
134                   "mr"
135                   "ms"
136                   "mt"
137                   "my"
138                   "ne"
139                   "nl"
140                   "nn"
141                   "no"
142                   "oc"
143                   "pa"
144                   "pl"
145                   "ps"
146                   "pt"
147                   "ro"
148                   "ru"
149                   "sa"
150                   "sd"
151                   "si"
152                   "sk"
153                   "sl"
154                   "sn"
155                   "so"
156                   "sq"
157                   "sr"
158                   "su"
159                   "sv"
160                   "sw"
161                   "ta"
162                   "te"
163                   "tg"
164                   "th"
165                   "tk"
166                   "tl"
167                   "tr"
168                   "tt"
169                   "uk"
170                   "ur"
171                   "uz"
172                   "vi"
173                   "yi"
174                   "yo"
175                   "zh"
176                 ];
177                 example = "en";
178                 description = ''
179                   The language used to to parse words and sentences.
180                 '';
181               };
183               beamSize = mkOption {
184                 type = ints.unsigned;
185                 default = 1;
186                 example = 5;
187                 description = ''
188                   The number of beams to use in beam search.
189                 '';
190                 apply = toString;
191               };
193               extraArgs = mkOption {
194                 type = listOf str;
195                 default = [ ];
196                 description = ''
197                   Extra arguments to pass to the server commandline.
198                 '';
199                 apply = escapeShellArgs;
200               };
201             };
202           }
203         )
204       );
205     };
206   };
208   config =
209     let
210       inherit (lib)
211         mapAttrs'
212         mkIf
213         nameValuePair
214         ;
215     in
216     mkIf (cfg.servers != { }) {
217       systemd.services = mapAttrs' (
218         server: options:
219         nameValuePair "wyoming-faster-whisper-${server}" {
220           inherit (options) enable;
221           description = "Wyoming faster-whisper server instance ${server}";
222           wants = [
223             "network-online.target"
224           ];
225           after = [
226             "network-online.target"
227           ];
228           wantedBy = [
229             "multi-user.target"
230           ];
231           # https://github.com/rhasspy/wyoming-faster-whisper/issues/27
232           environment."HF_HUB_CACHE" = "/tmp";
233           serviceConfig = {
234             DynamicUser = true;
235             User = "wyoming-faster-whisper";
236             StateDirectory = "wyoming/faster-whisper";
237             # https://github.com/home-assistant/addons/blob/master/whisper/rootfs/etc/s6-overlay/s6-rc.d/whisper/run
238             ExecStart = ''
239               ${cfg.package}/bin/wyoming-faster-whisper \
240                 --data-dir $STATE_DIRECTORY \
241                 --download-dir $STATE_DIRECTORY \
242                 --uri ${options.uri} \
243                 --device ${options.device} \
244                 --model ${options.model} \
245                 --language ${options.language} \
246                 --beam-size ${options.beamSize} ${options.extraArgs}
247             '';
248             CapabilityBoundingSet = "";
249             DeviceAllow =
250               if
251                 builtins.elem options.device [
252                   "cuda"
253                   "auto"
254                 ]
255               then
256                 [
257                   # https://docs.nvidia.com/dgx/pdf/dgx-os-5-user-guide.pdf
258                   "char-nvidia-uvm"
259                   "char-nvidia-frontend"
260                   "char-nvidia-caps"
261                   "char-nvidiactl"
262                 ]
263               else
264                 "";
265             DevicePolicy = "closed";
266             LockPersonality = true;
267             MemoryDenyWriteExecute = true;
268             PrivateUsers = true;
269             ProtectHome = true;
270             ProtectHostname = true;
271             ProtectKernelLogs = true;
272             ProtectKernelModules = true;
273             ProtectKernelTunables = true;
274             ProtectControlGroups = true;
275             ProtectProc = "invisible";
276             ProcSubset = "pid";
277             RestrictAddressFamilies = [
278               "AF_INET"
279               "AF_INET6"
280               "AF_UNIX"
281             ];
282             RestrictNamespaces = true;
283             RestrictRealtime = true;
284             SystemCallArchitectures = "native";
285             SystemCallFilter = [
286               "@system-service"
287               "~@privileged"
288             ];
289             UMask = "0077";
290           };
291         }
292       ) cfg.servers;
293     };