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 --- custom/trivmix-service.nix | 94 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 29 deletions(-) (limited to 'custom/trivmix-service.nix') 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