about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/audio/tts.nix
blob: 1a355c8ee39f5961db8256ca59ce3f6bd2b024d9 (plain) (blame)
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
{ config
, lib
, pkgs
, ...
}:

let
  cfg = config.services.tts;
in

{
  options.services.tts = let
    inherit (lib) literalExpression mkOption mdDoc mkEnableOption types;
  in  {
    servers = mkOption {
      type = types.attrsOf (types.submodule (
        { ... }: {
          options = {
            enable = mkEnableOption (mdDoc "Coqui TTS server");

            port = mkOption {
              type = types.port;
              example = 5000;
              description = mdDoc ''
                Port to bind the TTS server to.
              '';
            };

            model = mkOption {
              type = types.nullOr types.str;
              default = "tts_models/en/ljspeech/tacotron2-DDC";
              example = null;
              description = mdDoc ''
                Name of the model to download and use for speech synthesis.

                Check `tts-server --list_models` for possible values.

                Set to `null` to use a custom model.
              '';
            };

            useCuda = mkOption {
              type = types.bool;
              default = false;
              example = true;
              description = mdDoc ''
                Whether to offload computation onto a CUDA compatible GPU.
              '';
            };

            extraArgs = mkOption {
              type = types.listOf types.str;
              default = [];
              description = mdDoc ''
                Extra arguments to pass to the server commandline.
              '';
            };
          };
        }
      ));
      default = {};
      example = literalExpression ''
        {
          english = {
            port = 5300;
            model = "tts_models/en/ljspeech/tacotron2-DDC";
          };
          german = {
            port = 5301;
            model = "tts_models/de/thorsten/tacotron2-DDC";
          };
          dutch = {
            port = 5302;
            model = "tts_models/nl/mai/tacotron2-DDC";
          };
        }
      '';
      description = mdDoc ''
        TTS server instances.
      '';
    };
  };

  config = let
    inherit (lib) mkIf mapAttrs' nameValuePair optionalString concatMapStringsSep escapeShellArgs;
  in mkIf (cfg.servers != {}) {
    systemd.services = mapAttrs' (server: options:
      nameValuePair "tts-${server}" {
        description = "Coqui TTS server instance ${server}";
        after = [
          "network-online.target"
        ];
        wantedBy = [
          "multi-user.target"
        ];
        path = with pkgs; [
          espeak-ng
        ];
        environment.HOME = "/var/lib/tts";
        serviceConfig = {
          DynamicUser = true;
          User = "tts";
          StateDirectory = "tts";
          ExecStart = "${pkgs.tts}/bin/tts-server --port ${toString options.port}"
            + optionalString (options.model != null) " --model_name ${options.model}"
            + optionalString (options.useCuda) " --use_cuda"
            + (concatMapStringsSep " " escapeShellArgs options.extraArgs);
          CapabilityBoundingSet = "";
          DeviceAllow = if options.useCuda then [
            # https://docs.nvidia.com/dgx/pdf/dgx-os-5-user-guide.pdf
            "/dev/nvidia1"
            "/dev/nvidia2"
            "/dev/nvidia3"
            "/dev/nvidia4"
            "/dev/nvidia-caps/nvidia-cap1"
            "/dev/nvidia-caps/nvidia-cap2"
            "/dev/nvidiactl"
            "/dev/nvidia-modeset"
            "/dev/nvidia-uvm"
            "/dev/nvidia-uvm-tools"
          ] else "";
          DevicePolicy = "closed";
          LockPersonality = true;
          # jit via numba->llvmpipe
          MemoryDenyWriteExecute = false;
          PrivateDevices = true;
          PrivateUsers = true;
          ProtectHome = true;
          ProtectHostname = true;
          ProtectKernelLogs = true;
          ProtectKernelModules = true;
          ProtectKernelTunables = true;
          ProtectControlGroups = true;
          ProtectProc = "invisible";
          ProcSubset = "pid";
          RestrictAddressFamilies = [
            "AF_INET"
            "AF_INET6"
          ];
          RestrictNamespaces = true;
          RestrictRealtime = true;
          SystemCallArchitectures = "native";
          SystemCallFilter = [
            "@system-service"
            "~@privileged"
          ];
          UMask = "0077";
        };
      }) cfg.servers;
  };
}