feat(mediabox): wyoming

This commit is contained in:
arcnmx 2024-09-12 17:15:06 -07:00
parent a5966c0724
commit a202d0703f
9 changed files with 283 additions and 2 deletions

72
modules/nixos/wyoming.nix Normal file
View file

@ -0,0 +1,72 @@
let
serverModule = {
config,
lib,
...
}: let
inherit (lib.options) mkOption;
inherit (lib.modules) mkIf mkOptionDefault;
inherit (lib.strings) match toInt;
inherit (lib.lists) elemAt;
in {
options = with lib.types; {
bind = mkOption {
type = str;
readOnly = true;
};
port = mkOption {
type = port;
readOnly = true;
};
};
config = let
matched = match "^tcp://(.*):([0-9]+)$" config.uri;
bind = elemAt matched 0;
port = toInt (elemAt matched 1);
in {
bind = mkIf (matched != null) (mkOptionDefault bind);
port = mkIf (matched != null) (mkOptionDefault port);
};
};
nonServerModule = service: {
config,
lib,
...
} @ args: let
cfg = config.services.wyoming.${service};
module = serverModule (args
// {
name = service;
config = cfg;
});
in {
options.services.wyoming.${service} = module.options;
config.services.wyoming.${service} = module.config;
};
in
{
config,
lib,
...
}: let
inherit (lib.options) mkOption;
inherit (lib.attrsets) genAttrs;
serviceNames = ["piper" "faster-whisper"];
nonServerNames = ["openwakeword" "satellite"];
nonServerServices = map nonServerModule nonServerNames;
in {
imports = nonServerServices;
options.services.wyoming = let
mkServiceOptions = service:
with lib.types; {
servers = mkOption {
type = attrsOf (submodule [serverModule]);
};
};
serverServices = genAttrs serviceNames mkServiceOptions;
in
serverServices
// {
};
}

View file

@ -0,0 +1,60 @@
{
lib,
gensokyo-zone,
...
}: let
inherit (gensokyo-zone.lib) mkAlmostOptionDefault;
inherit (lib.modules) mkIf;
wyomingService = {config, ...}: {
nixos = {
serviceAttrPath = ["services" "wyoming" config.name];
assertions = [
(nixosConfig: let
service = nixosConfig.services.wyoming.${config.name};
cfg = service.servers.${config.id} or service;
in {
assertion = (! cfg ? enable) || (config.enable == cfg.enable);
message = "enable mismatch";
})
(mkIf config.enable (nixosConfig: let
service = nixosConfig.services.wyoming.${config.name};
cfg = service.servers.${config.id} or service;
in {
assertion = ! cfg.enable or false || config.ports.default.port == cfg.port or null;
message = "port mismatch";
}))
];
};
defaults.port.listen = mkAlmostOptionDefault "lan";
ports = {
default = {
transport = "tcp";
};
};
};
in {
config.exports.services = {
faster-whisper = {config, ...}: {
imports = [wyomingService];
displayName = mkAlmostOptionDefault "Wyoming Whisper";
id = mkAlmostOptionDefault "whisper";
ports.default.port = mkAlmostOptionDefault 10300;
};
piper = {config, ...}: {
imports = [wyomingService];
displayName = mkAlmostOptionDefault "Wyoming Piper";
id = mkAlmostOptionDefault "piper";
ports.default.port = mkAlmostOptionDefault 10200;
};
openwakeword = {config, ...}: {
imports = [wyomingService];
displayName = mkAlmostOptionDefault "Wyoming openWakeWord";
ports.default.port = mkAlmostOptionDefault 10400;
};
satellite = {config, ...}: {
imports = [wyomingService];
displayName = mkAlmostOptionDefault "Wyoming Satellite";
ports.default.port = mkAlmostOptionDefault 10700;
};
};
}