summaryrefslogtreecommitdiff
path: root/os/core/audio.nix
blob: a7fb5dbd62e09ceaf9f276d18fa890f96f269216 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.os.core.audio;
  toggleMuteNotify = pkgs.writeShellScriptBin "toggle-mute-notify" ''
    IS_MUTED=$(${pkgs.wireplumber}/bin/wpctl get-volume @DEFAULT_AUDIO_SOURCE@ | grep -c "MUTED")

    if [ "$IS_MUTED" -eq 1 ]; then
        ${pkgs.libnotify}/bin/notify-send -a "MuteIndicator" -t 0 -u critical "Microphone Muted" "Mic is currently OFF"
    else
        ${pkgs.mako}/bin/makoctl dismiss -a "MuteIndicator"
    fi
  '';
in
{
  options.os.core.audio = {
    enable = lib.mkEnableOption "audio support";
    disable-devices.enable = lib.mkEnableOption "disables some random devices cluttering up";
  };

  config = lib.mkMerge [
    (lib.mkIf cfg.enable {
      services.pulseaudio.enable = false;
      security.rtkit.enable = true;

      services = {
        pipewire = {
          enable = true;
          audio.enable = true;
          pulse.enable = true;
          alsa = {
            enable = true;
            support32Bit = true;
          };
          jack.enable = true;
          wireplumber = {
            enable = true;
            extraConfig = {
              "99-lock-microphone-gain" = {
                "pulse.rules" = [
                  {
                    matches = [
                      { "application.name" = "~*cord*"; }
                    ];
                    actions = {
                      quirks = [ "no-source-volume" ];
                    };
                  }
                ];
              };
              "99-disable-suspend" = {
                "monitor.alsa.rules" = [
                  {
                    matches = [
                      { "node.name" = "~alsa_input.*"; }
                      { "node.name" = "~alsa_output.*"; }
                    ];
                    actions.update-props = {
                      "session.suspend-timeout-seconds" = 0;
                    };
                  }
                ];
              };
              "10-bluetooth-policy" = {
                "wireplumber.profiles" = {
                  "main" = {
                    "policy.bluetooth" = "enabled";
                  };
                };
                "monitor.bluez.properties" = {
                  "bluez5.roles" = [
                    "a2dp_sink"
                    "a2dp_source"
                    "bap_sink"
                    "bap_source"
                    "hfp_hf"
                    "hsp_hs"
                  ];
                  "bluez5.codecs" = [
                    "sbc"
                    "sbc_xq"
                    "aac"
                    "ldac"
                    "aptx"
                    "aptx_hd"
                  ];
                };
              };
            };
          };
        };
        playerctld.enable = true;
      };

      systemd.user.services.pipewire-quantum = {
        description = "Set strict low-latency PipeWire quantum";
        after = [ "wireplumber.service" ];
        bindsTo = [ "pipewire.service" ];
        wantedBy = [ "wireplumber.service" ];
        serviceConfig = {
          Type = "oneshot";
          ExecStart = [
            "${pkgs.pipewire}/bin/pw-metadata -n settings 0 clock.quantum 512"
            "${pkgs.pipewire}/bin/pw-metadata -n settings 0 clock.min-quantum 512"
          ];
        };
      };

      programs.noisetorch.enable = true;

      systemd.user.services.pipewire-pulse = {
        serviceConfig = {
          Environment = [ "LADSPA_PATH=/tmp" ];
        };
      };

      environment.systemPackages = with pkgs; [
        toggleMuteNotify
        pulsemixer
        pavucontrol
        crosspipe
        alsa-utils
      ];
    })

    (lib.mkIf cfg.disable-devices.enable {
      services.pipewire.wireplumber.extraConfig = {
        "99-disable-useless-devices"."monitor.alsa.rules" = [
          {
            matches = [
              { "device.name" = "~alsa_card.pci-0000_03_00.1*"; }
              { "device.description" = "~USB Audio*"; }
            ];
            actions.update-props = {
              "device.disabled" = true;
            };
          }
        ];
      };
    })
  ];
}