From 076a87e9784635b679ab337b74ebc617c4e2dd3d Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 3 Sep 2017 22:03:35 +0200 Subject: Much fancier trivmix-service --- bragi.nix | 33 ++++++++-------- custom/trivmix-service.nix | 94 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 82 insertions(+), 45 deletions(-) diff --git a/bragi.nix b/bragi.nix index 4d61b61a..40c71def 100644 --- a/bragi.nix +++ b/bragi.nix @@ -1,8 +1,6 @@ { config, pkgs, ... }: let - trivmixService = opts: (pkgs.callPackage ./custom/trivmix-service.nix opts).out; - thermoprint-servant = (pkgs.callPackage ./custom/thermoprint {}).thermoprint-servant; inherit (pkgs) lib; in rec { imports = @@ -11,6 +9,7 @@ in rec { ./custom/zsh.nix ./users.nix ./custom/unit-status-mail.nix + ./custom/trivmix-service.nix ]; boot.loader.grub.enable = true; @@ -145,26 +144,28 @@ in rec { }; }; - systemd.services."mpdmix0" = trivmixService { name = "mpdmix0"; connectOut = "outnode0:in"; group = "mpd"; initial = "-35dB"; }; - systemd.services."mpdmix1" = trivmixService { name = "mpdmix1"; connectOut = "outnode1:in"; group = "mpd"; initial = "-35dB"; }; + services.trivmix = { + mpdmix0 = { connectOut = "outnode0:in"; group = "mpd"; initial = "-35dB"; }; + mpdmix1 = { connectOut = "outnode1:in"; group = "mpd"; initial = "-35dB"; }; - systemd.services."passmix0" = trivmixService { name = "passmix0"; connectOut = "outnode0:in"; connectIn = "system:capture_5"; group = "vali_out"; initial = "-5dB"; }; - systemd.services."passmix1" = trivmixService { name = "passmix1"; connectOut = "outnode1:in"; connectIn = "system:capture_6"; group = "vali_out"; initial = "-5dB"; }; + passmix0 = { connectOut = "outnode0:in"; connectIn = "system:capture_5"; group = "vali_out"; initial = "-5dB"; }; + passmix1 = { connectOut = "outnode1:in"; connectIn = "system:capture_6"; group = "vali_out"; initial = "-5dB"; }; - systemd.services."passmix2" = trivmixService { name = "passmix2"; connectOut = "system:playback_5"; connectIn = "system:capture_1"; group = "mic_out"; initial = "1"; }; - systemd.services."passmix3" = trivmixService { name = "passmix3"; connectOut = "system:playback_6"; connectIn = "system:capture_1"; group = "mic_out"; initial = "1"; }; + passmix2 = { connectOut = "system:playback_5"; connectIn = "system:capture_1"; group = "mic_out"; initial = "1"; }; + passmix3 = { connectOut = "system:playback_6"; connectIn = "system:capture_1"; group = "mic_out"; initial = "1"; }; - systemd.services."passmix4" = trivmixService { name = "passmix4"; connectOut = "outnode0:in"; connectIn = "system:capture_7"; group = "hel_out"; initial = "-5dB"; }; - systemd.services."passmix5" = trivmixService { name = "passmix5"; connectOut = "outnode1:in"; connectIn = "system:capture_8"; group = "hel_out"; initial = "-5dB"; }; + passmix4 = { connectOut = "outnode0:in"; connectIn = "system:capture_7"; group = "hel_out"; initial = "-5dB"; }; + passmix5 = { connectOut = "outnode1:in"; connectIn = "system:capture_8"; group = "hel_out"; initial = "-5dB"; }; - systemd.services."outnode0" = trivmixService { name = "outnode0"; initial = "1"; }; - systemd.services."outnode1" = trivmixService { name = "outnode1"; initial = "1"; }; + outnode0 = { initial = "1"; }; + outnode1 = { initial = "1"; }; - systemd.services."headphones0" = trivmixService { name = "heaphones0"; connectOut = "system:playback_3"; connectIn = "outnode0:out"; group = "headphones"; initial = "1"; }; - systemd.services."headphones1" = trivmixService { name = "heaphones1"; connectOut = "system:playback_4"; connectIn = "outnode1:out"; group = "headphones"; initial = "1"; }; + headphones0 = { connectOut = "system:playback_3"; connectIn = "outnode0:out"; group = "headphones"; initial = "1"; }; + headphones1 = { connectOut = "system:playback_4"; connectIn = "outnode1:out"; group = "headphones"; initial = "1"; }; - systemd.services."speakers0" = trivmixService { name = "speakers0"; connectOut = "system:playback_7"; connectIn = "outnode0:out"; group = "speakers"; initial = "0"; }; - systemd.services."speakers1" = trivmixService { name = "speakers1"; connectOut = "system:playback_8"; connectIn = "outnode1:out"; group = "speakers"; initial = "0"; }; + speakers0 = { connectOut = "system:playback_7"; connectIn = "outnode0:out"; group = "speakers"; initial = "0"; }; + speakers1 = { connectOut = "system:playback_8"; connectIn = "outnode1:out"; group = "speakers"; initial = "0"; }; + }; services.mpd = { enable = true; diff --git a/custom/trivmix-service.nix b/custom/trivmix-service.nix index 44044504..e8cb5072 100644 --- a/custom/trivmix-service.nix +++ b/custom/trivmix-service.nix @@ -1,40 +1,69 @@ -{ name -, run ? null -, connectOut ? null -, connectIn ? null -, group ? null -, initial ? null -, adjustable ? true -, trivmix -, stdenv -, makeWrapper -, jack2Full -, coreutils -, writeScript -}: +{ config, lib, pkgs, ... }: + +with lib; let - connect = (! isNull connectOut) || (! isNull connectIn); - connectScript = writeScript "connect" '' - #!${stdenv.shell} + cfg = config.services.trivmix; - PATH=${jack2Full}/bin:$PATH + mixerModule = { + connectIn = mkOption { + type = with types; either str (listOf str); + default = []; + description = "Ports to connect mixer input to"; + }; - ${optionalString (! isNull connectIn) "jack_connect ${connectIn} $1"} - ${optionalString (! isNull connectOut) "jack_connect $2 ${connectOut}"} - ''; - inherit (stdenv.lib) optionalString; -in { - out = { + connectOut = mkOption { + type = with types; either str (listOf str); + default = []; + description = "Ports to connect mixer output to"; + }; + + onConnect = mkOption { + type = types.lines; + default = ""; + description = "Additional shell commands to run after connections are set up"; + }; + + adjustable = mkOption { + type = types.bool; + default = true; + description = "Should the volume on this mixer be adjustable?"; + }; + + group = mkOption { + type = types.nullOr types.str; + default = null; + description = "Volumes of mixers that share a group name are synchronised"; + }; + + initial = mkOption { + type = types.str; + default = "1"; + description = "Initial volume"; + }; + }; + + service = name: mixerCfg: with mixerCfg; let + connectIn = if isList mixerCfg.connectIn then mixerCfg.connectIn else singleton mixerCfg.connectIn; + connectOut = if isList mixerCfg.connectOut then mixerCfg.connectOut else singleton mixerCfg.connectOut; + + connectScript = '' + #!${pkgs.stdenv.shell} + + ${concatMapStringsSep "\n" (port: "jack_connect ${port} $1") connectIn} + ${concatMapStringsSep "\n" (port: "jack_connect $2 ${port}") connectOut} + ${onConnect} + ''; + + connect = (connectOut != []) || (connectIn != []) || (onConnect != []); + in { wantedBy = [ "sound.target" ]; requires = [ "jack.service" ]; - before = [ "mpd.service" ]; serviceConfig = { Type = "simple"; - ExecStart = ''${trivmix}/bin/trivmix --client ${name} \ + ExecStart = ''${pkgs.trivmix}/bin/trivmix --client ${name} \ ${optionalString connect "--run ${connectScript}"} \ - ${optionalString (! isNull run) "--run ${run}"} \ - ${optionalString (! isNull initial) "--level ${initial}"} \ + "--level ${initial}" \ ${optionalString (adjustable && isNull group) "/dev/shm/mix/${name}/level"} \ ${optionalString (! isNull group) "/dev/shm/mix/${group}/level"} ''; @@ -43,6 +72,13 @@ in { Nice = "-10"; LimitRTPRIO = "95:95"; LimitMEMLOCK = "infinity"; - }; }; +in { + options.services.trivmix = mkOption { + type = types.attrsOf (types.submodule mixerModule); + default = []; + description = "Definition of mixers"; + }; + + config.systemd.services = mapAttrs service cfg; } -- cgit v1.2.3