summaryrefslogtreecommitdiff
path: root/custom/trivmix-service.nix
blob: 9a8ed504bd5a72be27b86b35770892155c5b1d60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.trivmix;

  trivmix = pkgs.haskellPackages.callPackage ./trivmix {};

  mixerModule = {
    options = {
      connectIn = mkOption {
        type = with types; coercedTo str singleton (listOf str);
        default = [];
        description = "Ports to connect mixer input to";
      };

      connectOut = mkOption {
        type = with types; coercedTo str singleton (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";
      };

      balance = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = "Volumes of mixers are multiplied with their balances, this is the name of the file that contains the factor for this mixer";
      };

      initialBalance = mkOption {
        type = types.str;
        default = "1";
        description = "Initial balance";
      };

      initial = mkOption {
        type = types.str;
        default = "1";
        description = "Initial volume";
      };
    };
  };

  service = name: mixerCfg: with mixerCfg; let
    connectScript = pkgs.writeScript "connect" ''
      #!${pkgs.stdenv.shell}

      ${concatMapStringsSep "\n" (port: "jack_connect ${port} $1") connectIn}
      ${concatMapStringsSep "\n" (port: "jack_connect $2 ${port}") connectOut}
      ${onConnect}
    '';

    connect = (connectOut != []) || (connectIn != []) || (onConnect != []);

    mixerDeps = filter (x: any (hasPrefix (x + ":")) connectIn || any (hasPrefix (x + ":")) connectOut) (attrNames cfg);

    trivmixArgs = let
        dirName = if isNull group then name else group;
      in [ "--fps" cfg.fps "--interval" cfg.interval "--client" name "--level" initial "--initial-balance" initialBalance ]
               ++ optionals connect ["--run" connectScript] 
               ++ optionals (adjustable && (! isNull balance)) ["--balance" "/dev/shm/mix/${dirName}/${balance}"]
               ++ optional (adjustable && isNull group) "/dev/shm/mix/${name}/level"
               ++ optional (! isNull group) "/dev/shm/mix/${group}/level";
  in {
    wantedBy = [ "sound.target" ];
    requires = [ "jack.service" ] ++ map (n: n + ".service") mixerDeps;
    after = [ "jack.service" ] ++ map (n: n + ".service") mixerDeps;
    path = with pkgs; [ jack2Full trivmix ];
    serviceConfig = {
      Type = "notify";
      User = "jack";
      Group = "audio";
      Nice = "-10";
      LimitRTPRIO = "95:95";
      LimitMEMLOCK = "infinity";
    };
    script = ''
      exec -a trivmix -- trivmix ${escapeShellArgs trivmixArgs}
    '';
  };
in {
  options.services.trivmix = {
    mixers = mkOption {
      type = types.attrsOf (types.submodule mixerModule);
      default = {};
      description = "Definition of mixers";
    };

    fps = mkOption {
      type = types.str;
      default = "200";
    };

    interval = mkOption {
      type = types.str;
      default = "0.2";
    };
  };

  config = mkIf (cfg.mixers != {}) {
    environment.systemPackages = [ trivmix ];
  
    systemd.services = mapAttrs service cfg.mixers;
  };
}