summaryrefslogtreecommitdiffstats
path: root/tv/2configs/pulse.nix
blob: 3db3532d5f0bf08cf647058c21a429079fbae0da (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
{ config, lib, pkgs, ... }:

let
  pkg = pkgs.pulseaudioLight;
  runDir = "/run/pulse";

  alsaConf = pkgs.writeText "asound.conf" ''
    ctl_type.pulse {
      libs.native = ${pkgs.alsaPlugins}/lib/alsa-lib/libasound_module_ctl_pulse.so;
    }
    pcm_type.pulse {
      libs.native = ${pkgs.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"
    ]}
  '';
in

{
  systemd.tmpfiles.rules = [
    "d ${runDir} 0750 pulse pulse - -"
    "d ${runDir}/home 0700 pulse pulse - -"
  ];

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

  environment = {
    etc = {
      "asound.conf".source = alsaConf;
      #"pulse/client.conf" = lib.mkForce { source = clientConf; };
      "pulse/default.pa".source = configFile;
    };
    systemPackages = [ pkg ];
  };

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

  systemd.services.pulse = {
    wantedBy = [ "sound.target" ];
    before = [ "sound.target" ];
    environment = {
      PULSE_RUNTIME_PATH = "${runDir}/home";
      #DISPLAY = ":${toString config.services.xserver.display}";
    };
    serviceConfig = {
      ExecStart = "${pkg}/bin/pulseaudio";
      User = "pulse";
    };
  };

  users = {
    groups.pulse.gid = config.users.users.pulse.uid;
    users.pulse = {
      uid = genid "pulse";
      group = "pulse";
      extraGroups = [ "audio" ];
      home = "${runDir}/home";
    };
  };
}