mmelodies/nixos/default.nix

102 lines
2.3 KiB
Nix

mmelodies:
{pkgs, config, lib, ...}:
let
cfg = config.services.mmelodies;
in
{
options.services.mmelodies = with lib; {
audioDevice = mkOption {
type = types.str;
default = "0";
};
midiDevice = mkOption {
type = with types; nullOr str;
default = null;
};
hostName = mkOption {
type = types.str;
default = config.networking.hostName;
};
sslCert = mkOption {
type = types.str;
default = "/etc/ssl/snakeoil.crt";
};
sslKey = mkOption {
type = types.str;
default = "/etc/ssl/snakeoil.key";
};
};
config = {
nix.registry.mmelodies.flake = mmelodies;
services.nginx.enable = true;
sound.extraConfig = ''
pcm.!default {
type hw
card ${cfg.audioDevice}
}
ctl.!default {
type hw
card ${cfg.audioDevice}
}
'';
services.nginx.virtualHosts."${cfg.hostName}" = {
addSSL = true;
sslCertificate = cfg.sslCert;
sslCertificateKey = cfg.sslKey;
root = pkgs.mmelodies.frontend;
locations."/api" = {
proxyPass = "http://unix:/run/mmelodies/backend.sock";
proxyWebsockets = true;
};
};
networking.firewall.allowedTCPPorts = [ 80 443 ];
users.users.appuser = {
isSystemUser = true;
group = "audio";
};
systemd.services.mmelodies = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
before = [ "nignx.target" ];
serviceConfig = {
User = "nginx";
Group = "nginx";
Type = "simple";
ExecStart = "${pkgs.mmelodies.backend}/bin/mmelodies -l unix:/run/mmelodies/backend.sock";
Restart = "always";
};
};
systemd.services.phyzzy = {
wantedBy = [ "multi-user.target" ];
postStart = lib.mkIf (! isNull cfg.midiDevice) ''
${pkgs.coreutils}/bin/sleep 5
${pkgs.alsa-utils}/bin/aconnect '${cfg.midiDevice}' 'Pure Data'
'';
serviceConfig = {
User = "appuser";
Group = "audio";
Type = "simple";
ExecStart = "${pkgs.mmelodies.synth}/bin/phyzzy -nogui";
Restart = "always";
CPUSchedulingPolicy = "fifo";
CPUSchedulingPriority = "20";
};
};
systemd.tmpfiles.rules = [
"d /run/mmelodies 0755 nginx nginx 12h -"
];
};
}