summaryrefslogtreecommitdiffstats
path: root/configs/pulse.nix
blob: 17c203c7a2e419fdd05427e520a009cae740ef77 (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
{ config, lib, mylib, pkgs, ... }: let
  pkg = pkgs.pulseaudio;
  runDir = "/run/pulse";

  pkgs_i686 = pkgs.pkgsi686Linux;

  support32Bit =
    pkgs.stdenv.isx86_64 &&
    pkgs_i686.alsaLib != null &&
    pkgs_i686.libpulseaudio != null;

  alsaConf = pkgs.writeText "asound.conf" ''
    ctl_type.pulse {
      libs.native = ${pkgs.alsaPlugins}/lib/alsa-lib/libasound_module_ctl_pulse.so;
      ${lib.optionalString support32Bit
        "libs.32Bit = ${pkgs_i686.alsaPlugins}/lib/alsa-lib/libasound_module_ctl_pulse.so;"}
    }
    pcm_type.pulse {
      libs.native = ${pkgs.alsaPlugins}/lib/alsa-lib/libasound_module_pcm_pulse.so;
      ${lib.optionalString support32Bit
        "libs.32Bit = ${pkgs_i686.alsaPlugins}/lib/alsa-lib/libasound_module_pcm_pulse.so;"}
    }
    ctl.!default {
      type pulse
    }
    pcm.!default {
      type pulse
    }
  '';

  clientConf = pkgs.writeText "client.conf" ''
    autospawn=no
    default-server = unix:${runDir}/socket
  '';

  configFile = pkgs.writeText "default.pa" ''
    .include ${pkg}/etc/pulse/default.pa
    load-module ${toString [
      "module-native-protocol-unix"
      "auth-anonymous=1"
      "socket=${runDir}/socket"
    ]}
    ${lib.optionalString (config.krebs.build.host.name == "au") ''
      load-module ${toString [
        "module-native-protocol-tcp"
        "auth-ip-acl=127.0.0.1;10.23.1.0/24"
      ]}
    ''}
    ${lib.optionalString (config.krebs.build.host.name != "au") ''
      load-module ${toString [
        "module-tunnel-sink-new"
        "server=au.hkw"
        "sink_name=au"
        "channels=2"
        "rate=44100"
      ]}
    ''}
  '';
in

{
  environment = {
    etc = {
      "asound.conf".source = alsaConf;
      # XXX mkForce is not strong enough (and neither is mkOverride) to create
      # /etc/pulse/client.conf, see pulseaudio-hack below for a solution.
      #"pulse/client.conf" = mkForce { source = clientConf; };
      #"pulse/client.conf".source = mkForce clientConf;
      "pulse/default.pa".source = configFile;
    };
    systemPackages = [
      pkg
    ] ++ lib.optionals config.services.xserver.enable [
      pkgs.pavucontrol
    ];
  };

  hardware.pulseaudio = {
    inherit support32Bit;
  };

  # Allow PulseAudio to get realtime priority using rtkit.
  security.rtkit.enable = true;

  system.activationScripts.pulseaudio-hack = ''
    ln -fns ${clientConf} /etc/pulse/client.conf
  '';

  systemd.services.pulse = {
    wantedBy = [ "sound.target" ];
    before = [ "sound.target" ];
    environment = {
      PULSE_RUNTIME_PATH = "${runDir}/home";
    };
    serviceConfig = {
      ExecStart = "${pkg}/bin/pulseaudio --exit-idle-time=-1";
      ExecStartPre = pkgs.writeDash "pulse-start" ''
        install -o pulse -g pulse -m 0750 -d ${runDir}
        install -o pulse -g pulse -m 0700 -d ${runDir}/home
      '';
      PermissionsStartOnly = "true";
      User = "pulse";
    };
  };

  # TODO assert that pulse is the only user with "audio" in group/extraGroups
  # otherwise the audio device can be hijacked while the pulse service restarts
  # (e.g. when mpv is running) and then the service will fail.
  users = {
    groups.pulse.gid = config.users.users.pulse.uid;
    users.pulse = {
      uid = mylib.genid_uint31 "pulse";
      group = "pulse";
      extraGroups = [ "audio" ];
      home = "${runDir}/home";
      isSystemUser = true;
    };
  };
}