aboutsummaryrefslogtreecommitdiff
path: root/infra/libkookie/nixpkgs/nixos/modules/services/desktops/pipewire/pipewire.nix
blob: 044120de7c719fb5dcc19a4820e429ce031b34b4 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# pipewire service.
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.pipewire;
  enable32BitAlsaPlugins = cfg.alsa.support32Bit
                           && pkgs.stdenv.isx86_64
                           && pkgs.pkgsi686Linux.pipewire != null;

  # The package doesn't output to $out/lib/pipewire directly so that the
  # overlays can use the outputs to replace the originals in FHS environments.
  #
  # This doesn't work in general because of missing development information.
  jack-libs = pkgs.runCommand "jack-libs" {} ''
    mkdir -p "$out/lib"
    ln -s "${cfg.package.jack}/lib" "$out/lib/pipewire"
  '';

  # Helpers for generating the pipewire JSON config file
  mkSPAValueString = v:
  if builtins.isList v then "[${lib.concatMapStringsSep " " mkSPAValueString v}]"
  else if lib.types.attrs.check v then
    "{${lib.concatStringsSep " " (mkSPAKeyValue v)}}"
  else lib.generators.mkValueStringDefault { } v;

  mkSPAKeyValue = attrs: map (def: def.content) (
  lib.sortProperties
    (
      lib.mapAttrsToList
        (k: v: lib.mkOrder (v._priority or 1000) "${lib.escape [ "=" ] k} = ${mkSPAValueString (v._content or v)}")
        attrs
    )
  );

  toSPAJSON = attrs: lib.concatStringsSep "\n" (mkSPAKeyValue attrs);
in {

  meta = {
    maintainers = teams.freedesktop.members;
  };

  ###### interface
  options = {
    services.pipewire = {
      enable = mkEnableOption "pipewire service";

      package = mkOption {
        type = types.package;
        default = pkgs.pipewire;
        defaultText = "pkgs.pipewire";
        example = literalExample "pkgs.pipewire";
        description = ''
          The pipewire derivation to use.
        '';
      };

      socketActivation = mkOption {
        default = true;
        type = types.bool;
        description = ''
          Automatically run pipewire when connections are made to the pipewire socket.
        '';
      };

      config = mkOption {
        type = types.attrs;
        description = ''
          Configuration for the pipewire daemon.
        '';
        default = {
          properties = {
            ## set-prop is used to configure properties in the system
            #
            # "library.name.system" = "support/libspa-support";
            # "context.data-loop.library.name.system" = "support/libspa-support";
            "link.max-buffers" = 16; # version < 3 clients can't handle more than 16
            #"mem.allow-mlock" = false;
            #"mem.mlock-all" = true;
            ## https://gitlab.freedesktop.org/pipewire/pipewire/-/blob/master/src/pipewire/pipewire.h#L93
            #"log.level" = 2; # 5 is trace, which is verbose as hell, default is 2 which is warnings, 4 is debug output, 3 is info

            ## Properties for the DSP configuration
            #
            #"default.clock.rate" = 48000;
            #"default.clock.quantum" = 1024;
            #"default.clock.min-quantum" = 32;
            #"default.clock.max-quantum" = 8192;
            #"default.video.width" = 640;
            #"default.video.height" = 480;
            #"default.video.rate.num" = 25;
            #"default.video.rate.denom" = 1;
          };

          spa-libs = {
            ## add-spa-lib <factory-name regex> <library-name>
            #
            # used to find spa factory names. It maps an spa factory name
            # regular expression to a library name that should contain
            # that factory.
            #
            "audio.convert*" = "audioconvert/libspa-audioconvert";
            "api.alsa.*" = "alsa/libspa-alsa";
            "api.v4l2.*" = "v4l2/libspa-v4l2";
            "api.libcamera.*" = "libcamera/libspa-libcamera";
            "api.bluez5.*" = "bluez5/libspa-bluez5";
            "api.vulkan.*" = "vulkan/libspa-vulkan";
            "api.jack.*" = "jack/libspa-jack";
            "support.*" = "support/libspa-support";
            # "videotestsrc" = "videotestsrc/libspa-videotestsrc";
            # "audiotestsrc" = "audiotestsrc/libspa-audiotestsrc";
          };

          modules = {
            ##  <module-name> = { [args = "<key>=<value> ..."]
            #                     [flags = ifexists] }
            #                     [flags = [ifexists]|[nofail]}
            #
            # Loads a module with the given parameters.
            # If ifexists is given, the module is ignoed when it is not found.
            # If nofail is given, module initialization failures are ignored.
            #
            libpipewire-module-rtkit = {
              args = {
                #rt.prio = 20;
                #rt.time.soft = 200000;
                #rt.time.hard = 200000;
                #nice.level = -11;
              };
              flags = "ifexists|nofail";
            };
            libpipewire-module-protocol-native = { _priority = -100; _content = "null"; };
            libpipewire-module-profiler = "null";
            libpipewire-module-metadata = "null";
            libpipewire-module-spa-device-factory = "null";
            libpipewire-module-spa-node-factory = "null";
            libpipewire-module-client-node = "null";
            libpipewire-module-client-device = "null";
            libpipewire-module-portal = "null";
            libpipewire-module-access = {
              args.access = {
                allowed = ["${builtins.unsafeDiscardStringContext cfg.sessionManagerExecutable}"];
                rejected = [];
                restricted = [];
                force = "flatpak";
              };
            };
            libpipewire-module-adapter = "null";
            libpipewire-module-link-factory = "null";
            libpipewire-module-session-manager = "null";
          };

          objects = {
            ## create-object [-nofail] <factory-name> [<key>=<value> ...]
            #
            # Creates an object from a PipeWire factory with the given parameters.
            # If -nofail is given, errors are ignored (and no object is created)
            #
          };


          exec = {
            ## exec <program-name>
            #
            # Execute the given program. This is usually used to start the
            # session manager. run the session manager with -h for options
            #
            "${builtins.unsafeDiscardStringContext cfg.sessionManagerExecutable}" = { args = "\"${lib.concatStringsSep " " cfg.sessionManagerArguments}\""; };
          };
        };
      };

      sessionManagerExecutable = mkOption {
        type = types.str;
        default = "";
        example = literalExample ''${pkgs.pipewire.mediaSession}/bin/pipewire-media-session'';
        description = ''
          Path to the session manager executable.
        '';
      };

      sessionManagerArguments = mkOption {
        type = types.listOf types.str;
        default = [];
        example = literalExample ''["-p" "bluez5.msbc-support=true"]'';
        description = ''
          Arguments passed to the pipewire session manager.
        '';
      };

      alsa = {
        enable = mkEnableOption "ALSA support";
        support32Bit = mkEnableOption "32-bit ALSA support on 64-bit systems";
      };

      jack = {
        enable = mkEnableOption "JACK audio emulation";
      };

      pulse = {
        enable = mkEnableOption "PulseAudio server emulation";
      };
    };
  };


  ###### implementation
  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = cfg.pulse.enable -> !config.hardware.pulseaudio.enable;
        message = "PipeWire based PulseAudio server emulation replaces PulseAudio. This option requires `hardware.pulseaudio.enable` to be set to false";
      }
      {
        assertion = cfg.jack.enable -> !config.services.jack.jackd.enable;
        message = "PipeWire based JACK emulation doesn't use the JACK service. This option requires `services.jack.jackd.enable` to be set to false";
      }
    ];

    environment.systemPackages = [ cfg.package ]
                                 ++ lib.optional cfg.jack.enable jack-libs;

    systemd.packages = [ cfg.package ]
                       ++ lib.optional cfg.pulse.enable cfg.package.pulse;

    # PipeWire depends on DBUS but doesn't list it. Without this booting
    # into a terminal results in the service crashing with an error.
    systemd.user.sockets.pipewire.wantedBy = lib.mkIf cfg.socketActivation [ "sockets.target" ];
    systemd.user.sockets.pipewire-pulse.wantedBy = lib.mkIf (cfg.socketActivation && cfg.pulse.enable) ["sockets.target"];
    systemd.user.services.pipewire.bindsTo = [ "dbus.service" ];
    services.udev.packages = [ cfg.package ];

    # If any paths are updated here they must also be updated in the package test.
    environment.etc."alsa/conf.d/49-pipewire-modules.conf" = mkIf cfg.alsa.enable {
      text = ''
        pcm_type.pipewire {
          libs.native = ${cfg.package.lib}/lib/alsa-lib/libasound_module_pcm_pipewire.so ;
          ${optionalString enable32BitAlsaPlugins
            "libs.32Bit = ${pkgs.pkgsi686Linux.pipewire.lib}/lib/alsa-lib/libasound_module_pcm_pipewire.so ;"}
        }
        ctl_type.pipewire {
          libs.native = ${cfg.package.lib}/lib/alsa-lib/libasound_module_ctl_pipewire.so ;
          ${optionalString enable32BitAlsaPlugins
            "libs.32Bit = ${pkgs.pkgsi686Linux.pipewire.lib}/lib/alsa-lib/libasound_module_ctl_pipewire.so ;"}
        }
      '';
    };
    environment.etc."alsa/conf.d/50-pipewire.conf" = mkIf cfg.alsa.enable {
      source = "${cfg.package}/share/alsa/alsa.conf.d/50-pipewire.conf";
    };
    environment.etc."alsa/conf.d/99-pipewire-default.conf" = mkIf cfg.alsa.enable {
      source = "${cfg.package}/share/alsa/alsa.conf.d/99-pipewire-default.conf";
    };

    environment.sessionVariables.LD_LIBRARY_PATH =
      lib.optional cfg.jack.enable "/run/current-system/sw/lib/pipewire";

    # https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/464#note_723554
    systemd.user.services.pipewire.environment = {
      "PIPEWIRE_LINK_PASSIVE" = "1";
      "PIPEWIRE_CONFIG_FILE" = pkgs.writeText "pipewire.conf" (toSPAJSON cfg.config);
    };
  };
}