about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/audio/wyoming/piper.nix
blob: 2828fdf0789219579ff8b72daf81f2ae46a79bb0 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{ config
, lib
, pkgs
, ...
}:

let
  cfg = config.services.wyoming.piper;

  inherit (lib)
    escapeShellArgs
    mkOption
    mdDoc
    mkEnableOption
    mkPackageOption
    types
    ;

  inherit (builtins)
    toString
    ;

in

{
  meta.buildDocsInSandbox = false;

  options.services.wyoming.piper = with types; {
    package = mkPackageOption pkgs "wyoming-piper" { };

    servers = mkOption {
      default = {};
      description = mdDoc ''
        Attribute set of piper instances to spawn.
      '';
      type = types.attrsOf (types.submodule (
        { ... }: {
          options = {
            enable = mkEnableOption (mdDoc "Wyoming Piper server");

            piper = mkPackageOption pkgs "piper-tts" { };

            voice = mkOption {
              type = str;
              example = "en-us-ryan-medium";
              description = mdDoc ''
                Name of the voice model to use. See the following website for samples:
                https://rhasspy.github.io/piper-samples/
              '';
            };

            uri = mkOption {
              type = strMatching "^(tcp|unix)://.*$";
              example = "tcp://0.0.0.0:10200";
              description = mdDoc ''
                URI to bind the wyoming server to.
              '';
            };

            speaker = mkOption {
              type = ints.unsigned;
              default = 0;
              description = mdDoc ''
                ID of a specific speaker in a multi-speaker model.
              '';
              apply = toString;
            };

            noiseScale = mkOption {
              type = float;
              default = 0.667;
              description = mdDoc ''
                Generator noise value.
              '';
              apply = toString;
            };

            noiseWidth = mkOption {
              type = float;
              default = 0.333;
              description = mdDoc ''
                Phoneme width noise value.
              '';
              apply = toString;
            };

            lengthScale = mkOption {
              type = float;
              default = 1.0;
              description = mdDoc ''
                Phoneme length value.
              '';
              apply = toString;
            };

            extraArgs = mkOption {
              type = listOf str;
              default = [ ];
              description = mdDoc ''
                Extra arguments to pass to the server commandline.
              '';
              apply = escapeShellArgs;
            };
          };
        }
      ));
    };
  };

  config = let
    inherit (lib)
      mapAttrs'
      mkIf
      nameValuePair
    ;
  in mkIf (cfg.servers != {}) {
    systemd.services = mapAttrs' (server: options:
      nameValuePair "wyoming-piper-${server}" {
        inherit (options) enable;
        description = "Wyoming Piper server instance ${server}";
        after = [
          "network-online.target"
        ];
        wantedBy = [
          "multi-user.target"
        ];
        serviceConfig = {
          DynamicUser = true;
          User = "wyoming-piper";
          StateDirectory = "wyoming/piper";
          # https://github.com/home-assistant/addons/blob/master/piper/rootfs/etc/s6-overlay/s6-rc.d/piper/run
          ExecStart = ''
            ${cfg.package}/bin/wyoming-piper \
              --data-dir $STATE_DIRECTORY \
              --download-dir $STATE_DIRECTORY \
              --uri ${options.uri} \
              --piper ${options.piper}/bin/piper \
              --voice ${options.voice} \
              --speaker ${options.speaker} \
              --length-scale ${options.lengthScale} \
              --noise-scale ${options.noiseScale} \
              --noise-w ${options.noiseWidth} ${options.extraArgs}
          '';
          CapabilityBoundingSet = "";
          DeviceAllow = "";
          DevicePolicy = "closed";
          LockPersonality = true;
          MemoryDenyWriteExecute = true;
          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"
            "AF_UNIX"
          ];
          RestrictNamespaces = true;
          RestrictRealtime = true;
          SystemCallArchitectures = "native";
          SystemCallFilter = [
            "@system-service"
            "~@privileged"
          ];
          UMask = "0077";
        };
      }) cfg.servers;
  };
}