Parameterizes nixos config.

This commit is contained in:
Bailey Stevens 2023-10-04 10:19:49 -04:00
parent 21bf7ee40d
commit 2b963ae4ef
2 changed files with 92 additions and 43 deletions

View File

@ -32,7 +32,7 @@
packages = pkgs.mmelodies;
}
) // {
nixosModules.default = import ./nixos;
nixosModules.default = (import ./nixos self);
overlay =
(final: super: {
napalm = import inputs.napalm { pkgs = super; };

View File

@ -1,52 +1,101 @@
{pkgs, config, ...}:
mmelodies:
{pkgs, config, lib, ...}:
let
cfg = config.services.mmelodies;
in
{
services.nginx.enable = true;
services.nginx.virtualHosts."${config.networking.hostName}" = {
addSSL = true;
sslCertificate = "/etc/ssl/snakeoil.crt";
sslCertificateKey = "/etc/ssl/snakeoil.key";
root = pkgs.mmelodies.frontend;
locations."/api" = {
proxyPass = "http://unix:/run/mmelodies/backend.sock";
proxyWebsockets = true;
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;
networking.firewall.allowedTCPPorts = [ 80 443 ];
sound.extraConfig = ''
pcm.!default {
type hw
card ${cfg.audioDevice}
}
ctl.!default {
type hw
card ${cfg.audioDevice}
}
'';
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";
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;
};
};
};
systemd.services.phyzzy = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = "appuser";
Group = "audio";
ExecStart = "${pkgs.mmelodies.synth}/bin/phyzzy -midiindev 1 -nogui";
Restart = "always";
CPUSchedulingPolicy = "fifo";
CPUSchedulingPriority = "20";
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 -"
];
};
environment.systemPackages = with pkgs.mmelodies; [
synth backend
];
systemd.tmpfiles.rules = [
"d /run/mmelodies 0755 nginx nginx 12h -"
];
}