Unverified Commit 68891efe authored by Leah Amelia Chen's avatar Leah Amelia Chen Committed by GitHub
Browse files

marytts: init at 5.2.1-unstable-2024-10-09, nixos/marytts: init (#351933)

* marytts: init at 5.2.1-unstable-2024-10-09

* nixos/marytts: init module

* nixos/marytts: add tests
parent 94b32163
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@

- [Omnom](https://github.com/asciimoo/omnom), a webpage bookmarking and snapshotting service. Available as [services.omnom](options.html#opt-services.omnom.enable).

- [MaryTTS](https://github.com/marytts/marytts), an open-source, multilingual text-to-speech synthesis system written in pure Java. Available as [services.marytts](options.html#opt-services.marytts).

- [Traccar](https://www.traccar.org/), a modern GPS Tracking Platform. Available as [services.traccar](#opt-services.traccar.enable).

- [crab-hole](https://github.com/LuckyTurtleDev/crab-hole), a cross platform Pi-hole clone written in Rust using hickory-dns/trust-dns. Available as [services.crab-hole](#opt-services.crab-hole.enable).
+1 −0
Original line number Diff line number Diff line
@@ -395,6 +395,7 @@
  ./services/audio/jack.nix
  ./services/audio/jmusicbot.nix
  ./services/audio/liquidsoap.nix
  ./services/audio/marytts.nix
  ./services/audio/mopidy.nix
  ./services/audio/mpd.nix
  ./services/audio/mpdscribble.nix
+184 −0
Original line number Diff line number Diff line
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.marytts;
  format = pkgs.formats.javaProperties { };
in
{
  options.services.marytts = {
    enable = lib.mkEnableOption "MaryTTS";

    settings = lib.mkOption {
      type = lib.types.submodule {
        freeformType = format.type;
      };
      default = { };
      description = ''
        Settings for MaryTTS.

        See the [default settings](https://github.com/marytts/marytts/blob/master/marytts-runtime/conf/marybase.config)
        for a list of possible keys.
      '';
    };

    package = lib.mkPackageOption pkgs "marytts" { };

    basePath = lib.mkOption {
      type = lib.types.path;
      default = "/var/lib/marytts";
      description = ''
        The base path in which MaryTTS runs.
      '';
    };

    port = lib.mkOption {
      type = lib.types.port;
      default = 59125;
      description = ''
        Port to bind the MaryTTS server to.
      '';
    };

    openFirewall = lib.mkOption {
      type = lib.types.bool;
      default = false;
      example = true;
      description = ''
        Whether to open the port in the firewall for MaryTTS.
      '';
    };

    voices = lib.mkOption {
      type = lib.types.listOf lib.types.path;
      default = [ ];
      example = lib.literalExpression ''
        [
          (pkgs.fetchzip {
            url = "https://github.com/marytts/voice-bits1-hsmm/releases/download/v5.2/voice-bits1-hsmm-5.2.zip";
            hash = "sha256-1nK+qZxjumMev7z5lgKr660NCKH5FDwvZ9sw/YYYeaA=";
          })
        ]
      '';
      description = ''
        Paths to the JAR files that contain additional voices for MaryTTS.

        Voices are automatically detected by MaryTTS, so there is no need to alter
        your config to make use of new voices.
      '';
    };

    userDictionaries = lib.mkOption {
      type = lib.types.listOf lib.types.path;
      default = [ ];
      example = lib.literalExpression ''
        [
          (pkgs.writeTextFile {
            name = "userdict-en_US";
            destination = "/userdict-en_US.txt";
            text = '''
              Nixpkgs | n I k s - ' p { - k @ - dZ @ s
            ''';
          })
        ]
      '';
      description = ''
        Paths to the user dictionary files for MaryTTS.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    services.marytts.settings = {
      "mary.base" = lib.mkDefault cfg.basePath;
      "socket.port" = lib.mkDefault cfg.port;
    };

    environment.systemPackages = [ cfg.package ];

    systemd.services.marytts = {
      description = "MaryTTS server instance";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      # FIXME: MaryTTS's config loading mechanism appears to be horrendously broken
      # and it doesn't seem to actually read config files outside of precompiled JAR files.
      # Using system properties directly works for now, but this is really ugly.
      script = ''
        ${lib.getExe pkgs.marytts} -classpath "${cfg.basePath}/lib/*:${cfg.package}/lib/*" ${
          lib.concatStringsSep " " (lib.mapAttrsToList (n: v: ''-D${n}="${v}"'') cfg.settings)
        }
      '';

      restartTriggers = cfg.voices ++ cfg.userDictionaries;

      serviceConfig = {
        DynamicUser = true;
        User = "marytts";
        RuntimeDirectory = "marytts";
        StateDirectory = "marytts";
        Restart = "on-failure";
        RestartSec = 5;
        TimeoutSec = 20;

        # Hardening
        ProtectClock = true;
        ProtectKernelLogs = true;
        ProtectControlGroups = true;
        ProtectKernelModules = true;
        ProtectHostname = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectHome = true;
        ProcSubset = "pid";

        PrivateTmp = true;
        PrivateNetwork = false;
        PrivateUsers = cfg.port >= 1024;
        PrivateDevices = true;

        RestrictRealtime = true;
        RestrictNamespaces = true;
        RestrictAddressFamilies = [
          "AF_INET"
          "AF_INET6"
        ];

        MemoryDenyWriteExecute = false; # Java does not like w^x :(
        LockPersonality = true;
        AmbientCapabilities = lib.optional (cfg.port < 1024) "CAP_NET_BIND_SERVICE";
        CapabilityBoundingSet = "";
        SystemCallArchitectures = "native";
        SystemCallFilter = [
          "@system-service"
          "~@resources"
          "~@privileged"
        ];
        UMask = "0027";
      };
    };

    systemd.tmpfiles.settings."10-marytts" = {
      "${cfg.basePath}/lib"."L+".argument = "${pkgs.symlinkJoin {
        name = "marytts-lib";

        # Put user paths before default ones so that user ones have priority
        paths = cfg.voices ++ [ "${cfg.package}/lib" ];
      }}";

      "${cfg.basePath}/user-dictionaries"."L+".argument = "${pkgs.symlinkJoin {
        name = "marytts-user-dictionaries";

        # Put user paths before default ones so that user ones have priority
        paths = cfg.userDictionaries ++ [ "${cfg.package}/user-dictionaries" ];
      }}";
    };

    networking.firewall = lib.mkIf cfg.openFirewall {
      allowedTCPPorts = [ cfg.port ];
    };
  };
}
+1 −0
Original line number Diff line number Diff line
@@ -579,6 +579,7 @@ in {
  mailman = handleTest ./mailman.nix {};
  man = handleTest ./man.nix {};
  mariadb-galera = handleTest ./mysql/mariadb-galera.nix {};
  marytts = handleTest ./marytts.nix {};
  mastodon = pkgs.recurseIntoAttrs (handleTest ./web-apps/mastodon { inherit handleTestOn; });
  pixelfed = discoverTests (import ./web-apps/pixelfed { inherit handleTestOn; });
  mate = handleTest ./mate.nix {};
+87 −0
Original line number Diff line number Diff line
import ./make-test-python.nix (
  { lib, ... }:
  let
    port = 59126;
  in
  {
    name = "marytts";
    meta.maintainers = with lib.maintainers; [ pluiedev ];

    nodes.machine =
      { pkgs, ... }:
      {
        networking.firewall.enable = false;
        networking.useDHCP = false;

        services.marytts = {
          enable = true;
          inherit port;

          voices = [
            (pkgs.fetchzip {
              url = "https://github.com/marytts/voice-bits1-hsmm/releases/download/v5.2/voice-bits1-hsmm-5.2.zip";
              hash = "sha256-1nK+qZxjumMev7z5lgKr660NCKH5FDwvZ9sw/YYYeaA=";
            })
          ];

          userDictionaries = [
            (pkgs.writeTextFile {
              name = "userdict-en_US.txt";
              destination = "/userdict-en_US.txt";
              text = ''
                amogus | @ - ' m @U - g @ s
                Nixpkgs | n I k s - ' p { - k @ - dZ @ s
              '';
            })
          ];
        };
      };

    testScript = ''
      from xml.etree import ElementTree
      from urllib.parse import urlencode

      machine.wait_for_unit("marytts.service")

      with subtest("Checking health of MaryTTS server"):
        machine.wait_for_open_port(${toString port})
        assert 'Mary TTS server' in machine.succeed("curl 'localhost:${toString port}/version'")

      with subtest("Generating example MaryXML"):
        query = urlencode({
          'datatype': 'RAWMARYXML',
          'locale': 'en_US',
        })
        xml = machine.succeed(f"curl 'localhost:${toString port}/exampletext?{query}'")
        root = ElementTree.fromstring(xml)
        text = " ".join(root.itertext()).strip()
        assert text == "Welcome to the world of speech synthesis!"

      with subtest("Detecting custom voice"):
        assert "bits1-hsmm" in machine.succeed("curl 'localhost:${toString port}/voices'")

      with subtest("Finding user dictionary"):
        query = urlencode({
          'INPUT_TEXT': 'amogus',
          'INPUT_TYPE': 'TEXT',
          'OUTPUT_TYPE': 'PHONEMES',
          'LOCALE': 'en_US',
        })
        phonemes = machine.succeed(f"curl 'localhost:${toString port}/process?{query}'")
        phonemes_tree = ElementTree.fromstring(phonemes)
        print([i.get('ph') for i in phonemes_tree.iter('{http://mary.dfki.de/2002/MaryXML}t')])
        assert ["@ - ' m @U - g @ s"] == [i.get('ph') for i in phonemes_tree.iter('{http://mary.dfki.de/2002/MaryXML}t')]

      with subtest("Synthesizing"):
        query = urlencode({
          'INPUT_TEXT': 'Nixpkgs is a collection of over 100,000 software packages that can be installed with the Nix package manager.',
          'INPUT_TYPE': 'TEXT',
          'OUTPUT_TYPE': 'AUDIO',
          'AUDIO': 'WAVE_FILE',
          'LOCALE': 'en_US',
        })
        machine.succeed(f"curl 'localhost:${toString port}/process?{query}' -o ./audio.wav")
        machine.copy_from_vm("./audio.wav")
    '';
  }
)
Loading