about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/desktops/pipewire/pipewire.nix
blob: 09448833620c68d317de57e76b519615c904ae27 (plain) (blame)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# PipeWire service.
{ config, lib, pkgs, ... }:

with lib;

let
  json = pkgs.formats.json {};
  mapToFiles = location: config: concatMapAttrs (name: value: { "share/pipewire/${location}.conf.d/${name}.conf" = json.generate "${name}" value; }) config;
  extraConfigPkgFromFiles = locations: filesSet: pkgs.runCommand "pipewire-extra-config" { } ''
    mkdir -p ${lib.concatMapStringsSep " " (l: "$out/share/pipewire/${l}.conf.d") locations}
    ${lib.concatMapStringsSep ";" ({name, value}: "ln -s ${value} $out/${name}") (lib.attrsToList filesSet)}
  '';
  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"
  '';

  configPackages = cfg.configPackages;

  extraConfigPkg = extraConfigPkgFromFiles
    [ "pipewire" "client" "client-rt" "jack" "pipewire-pulse" ]
    (
      mapToFiles "pipewire" cfg.extraConfig.pipewire
      // mapToFiles "client" cfg.extraConfig.client
      // mapToFiles "client-rt" cfg.extraConfig.client-rt
      // mapToFiles "jack" cfg.extraConfig.jack
      // mapToFiles "pipewire-pulse" cfg.extraConfig.pipewire-pulse
    );

  configs = pkgs.buildEnv {
    name = "pipewire-configs";
    paths = configPackages
      ++ [ extraConfigPkg ]
      ++ lib.optionals cfg.wireplumber.enable cfg.wireplumber.configPackages;
    pathsToLink = [ "/share/pipewire" ];
  };

  requiredLv2Packages = lib.flatten
    (
      lib.concatMap
      (p:
        lib.attrByPath ["passthru" "requiredLv2Packages"] [] p
      )
      configPackages
    );

  lv2Plugins = pkgs.buildEnv {
    name = "pipewire-lv2-plugins";
    paths = cfg.extraLv2Packages ++ requiredLv2Packages;
    pathsToLink = [ "/lib/lv2" ];
  };
in {
  meta.maintainers = teams.freedesktop.members ++ [ lib.maintainers.k900 ];

  ###### interface
  options = {
    services.pipewire = {
      enable = mkEnableOption (lib.mdDoc "PipeWire service");

      package = mkPackageOption pkgs "pipewire" { };

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

      audio = {
        enable = lib.mkOption {
          type = lib.types.bool;
          # this is for backwards compatibility
          default = cfg.alsa.enable || cfg.jack.enable || cfg.pulse.enable;
          defaultText = lib.literalExpression "config.services.pipewire.alsa.enable || config.services.pipewire.jack.enable || config.services.pipewire.pulse.enable";
          description = lib.mdDoc "Whether to use PipeWire as the primary sound server";
        };
      };

      alsa = {
        enable = mkEnableOption (lib.mdDoc "ALSA support");
        support32Bit = mkEnableOption (lib.mdDoc "32-bit ALSA support on 64-bit systems");
      };

      jack = {
        enable = mkEnableOption (lib.mdDoc "JACK audio emulation");
      };

      pulse = {
        enable = mkEnableOption (lib.mdDoc "PulseAudio server emulation");
      };

      systemWide = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = lib.mdDoc ''
          If true, a system-wide PipeWire service and socket is enabled
          allowing all users in the "pipewire" group to use it simultaneously.
          If false, then user units are used instead, restricting access to
          only one user.

          Enabling system-wide PipeWire is however not recommended and disabled
          by default according to
          https://github.com/PipeWire/pipewire/blob/master/NEWS
        '';
      };

      extraConfig = {
        pipewire = mkOption {
          type = lib.types.attrsOf json.type;
          default = {};
          example = {
            "10-clock-rate" = {
              "context.properties" = {
                "default.clock.rate" = 44100;
              };
            };
            "11-no-upmixing" = {
              "stream.properties" = {
                "channelmix.upmix" = false;
              };
            };
          };
          description = lib.mdDoc ''
            Additional configuration for the PipeWire server.

            Every item in this attrset becomes a separate drop-in file in `/etc/pipewire/pipewire.conf.d`.

            See `man pipewire.conf` for details, and [the PipeWire wiki][wiki] for examples.

            See also:
            - [PipeWire wiki - virtual devices][wiki-virtual-device] for creating virtual devices or remapping channels
            - [PipeWire wiki - filter-chain][wiki-filter-chain] for creating more complex processing pipelines
            - [PipeWire wiki - network][wiki-network] for streaming audio over a network

            [wiki]: https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Config-PipeWire
            [wiki-virtual-device]: https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Virtual-Devices
            [wiki-filter-chain]: https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Filter-Chain
            [wiki-network]: https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Network
          '';
        };
        client = mkOption {
          type = lib.types.attrsOf json.type;
          default = {};
          example = {
            "10-no-resample" = {
              "stream.properties" = {
                "resample.disable" = true;
              };
            };
          };
          description = lib.mdDoc ''
            Additional configuration for the PipeWire client library, used by most applications.

            Every item in this attrset becomes a separate drop-in file in `/etc/pipewire/client.conf.d`.

            See the [PipeWire wiki][wiki] for examples.

            [wiki]: https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Config-client
          '';
        };
        client-rt = mkOption {
          type = lib.types.attrsOf json.type;
          default = {};
          example = {
            "10-alsa-linear-volume" = {
              "alsa.properties" = {
                "alsa.volume-method" = "linear";
              };
            };
          };
          description = lib.mdDoc ''
            Additional configuration for the PipeWire client library, used by real-time applications and legacy ALSA clients.

            Every item in this attrset becomes a separate drop-in file in `/etc/pipewire/client-rt.conf.d`.

            See the [PipeWire wiki][wiki] for examples of general configuration, and [PipeWire wiki - ALSA][wiki-alsa] for ALSA clients.

            [wiki]: https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Config-client
            [wiki-alsa]: https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Config-ALSA
          '';
        };
        jack = mkOption {
          type = lib.types.attrsOf json.type;
          default = {};
          example = {
            "20-hide-midi" = {
              "jack.properties" = {
                "jack.show-midi" = false;
              };
            };
          };
          description = lib.mdDoc ''
            Additional configuration for the PipeWire JACK server and client library.

            Every item in this attrset becomes a separate drop-in file in `/etc/pipewire/jack.conf.d`.

            See the [PipeWire wiki][wiki] for examples.

            [wiki]: https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Config-JACK
          '';
        };
        pipewire-pulse = mkOption {
          type = lib.types.attrsOf json.type;
          default = {};
          example = {
            "15-force-s16-info" = {
              "pulse.rules" = [{
                matches = [
                  { "application.process.binary" = "my-broken-app"; }
                ];
                actions = {
                  quirks = [ "force-s16-info" ];
                };
              }];
            };
          };
          description = lib.mdDoc ''
            Additional configuration for the PipeWire PulseAudio server.

            Every item in this attrset becomes a separate drop-in file in `/etc/pipewire/pipewire-pulse.conf.d`.

            See `man pipewire-pulse.conf` for details, and [the PipeWire wiki][wiki] for examples.

            See also:
            - [PipeWire wiki - PulseAudio tricks guide][wiki-tricks] for more examples.

            [wiki]: https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Config-PulseAudio
            [wiki-tricks]: https://gitlab.freedesktop.org/pipewire/pipewire/-/wikis/Guide-PulseAudio-Tricks
          '';
        };
      };

      configPackages = lib.mkOption {
        type = lib.types.listOf lib.types.package;
        default = [];
        description = lib.mdDoc ''
          List of packages that provide PipeWire configuration, in the form of
          `share/pipewire/*/*.conf` files.

          LV2 dependencies will be picked up from config packages automatically
          via `passthru.requiredLv2Packages`.
        '';
      };

      extraLv2Packages = lib.mkOption {
        type = lib.types.listOf lib.types.package;
        default = [];
        example = lib.literalExpression "[ pkgs.lsp-plugins ]";
        description = lib.mdDoc ''
          List of packages that provide LV2 plugins in `lib/lv2` that should
          be made available to PipeWire for [filter chains][wiki-filter-chain].

          Config packages have their required LV2 plugins added automatically,
          so they don't need to be specified here. Config packages need to set
          `passthru.requiredLv2Packages` for this to work.

          [wiki-filter-chain]: https://docs.pipewire.org/page_module_filter_chain.html
        '';
      };
    };
  };

  imports = [
    (lib.mkRemovedOptionModule ["services" "pipewire" "config"] ''
      Overriding default PipeWire configuration through NixOS options never worked correctly and is no longer supported.
      Please create drop-in configuration files via `services.pipewire.extraConfig` instead.
    '')
    (lib.mkRemovedOptionModule ["services" "pipewire" "media-session"] ''
      pipewire-media-session is no longer supported upstream and has been removed.
      Please switch to `services.pipewire.wireplumber` instead.
    '')
  ];

  ###### implementation
  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = cfg.audio.enable -> !config.hardware.pulseaudio.enable;
        message = "Using PipeWire as the sound server conflicts with 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";
      }
      {
        # JACK intentionally not checked, as PW-on-JACK setups are a thing that some people may want
        assertion = (cfg.alsa.enable || cfg.pulse.enable) -> cfg.audio.enable;
        message = "Using PipeWire's ALSA/PulseAudio compatibility layers requires running PipeWire as the sound server. Set `services.pipewire.audio.enable` to true.";
      }
      {
        assertion = builtins.length
          (builtins.attrNames
            (
              lib.filterAttrs
                (name: value:
                  lib.hasPrefix "pipewire/" name || name == "pipewire"
                )
                config.environment.etc
            )) == 1;
        message = "Using `environment.etc.\"pipewire<...>\"` directly is no longer supported in 24.05. Use `services.pipewire.extraConfig` or `services.pipewire.configPackages` instead.";
      }
    ];

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

    systemd.packages = [ cfg.package ];

    # PipeWire depends on DBUS but doesn't list it. Without this booting
    # into a terminal results in the service crashing with an error.
    systemd.services.pipewire.bindsTo = [ "dbus.service" ];
    systemd.user.services.pipewire.bindsTo = [ "dbus.service" ];

    # Enable either system or user units.  Note that for pipewire-pulse there
    # are only user units, which work in both cases.
    systemd.sockets.pipewire.enable = cfg.systemWide;
    systemd.services.pipewire.enable = cfg.systemWide;
    systemd.user.sockets.pipewire.enable = !cfg.systemWide;
    systemd.user.services.pipewire.enable = !cfg.systemWide;

    systemd.services.pipewire.environment.LV2_PATH = lib.mkIf cfg.systemWide "${lv2Plugins}/lib/lv2";
    systemd.user.services.pipewire.environment.LV2_PATH = lib.mkIf (!cfg.systemWide) "${lv2Plugins}/lib/lv2";

    # Mask pw-pulse if it's not wanted
    systemd.user.services.pipewire-pulse.enable = cfg.pulse.enable;
    systemd.user.sockets.pipewire-pulse.enable = cfg.pulse.enable;

    systemd.sockets.pipewire.wantedBy = lib.mkIf cfg.socketActivation [ "sockets.target" ];
    systemd.user.sockets.pipewire.wantedBy = lib.mkIf cfg.socketActivation [ "sockets.target" ];
    systemd.user.sockets.pipewire-pulse.wantedBy = lib.mkIf cfg.socketActivation [ "sockets.target" ];

    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/alsa-lib/libasound_module_pcm_pipewire.so ;
            ${optionalString enable32BitAlsaPlugins
              "libs.32Bit = ${pkgs.pkgsi686Linux.pipewire}/lib/alsa-lib/libasound_module_pcm_pipewire.so ;"}
          }
          ctl_type.pipewire {
            libs.native = ${cfg.package}/lib/alsa-lib/libasound_module_ctl_pipewire.so ;
            ${optionalString enable32BitAlsaPlugins
              "libs.32Bit = ${pkgs.pkgsi686Linux.pipewire}/lib/alsa-lib/libasound_module_ctl_pipewire.so ;"}
          }
        '';
      };

      "alsa/conf.d/50-pipewire.conf" = mkIf cfg.alsa.enable {
        source = "${cfg.package}/share/alsa/alsa.conf.d/50-pipewire.conf";
      };

      "alsa/conf.d/99-pipewire-default.conf" = mkIf cfg.alsa.enable {
        source = "${cfg.package}/share/alsa/alsa.conf.d/99-pipewire-default.conf";
      };
      pipewire.source = "${configs}/share/pipewire";
    };

    environment.sessionVariables.LD_LIBRARY_PATH =
      lib.mkIf cfg.jack.enable [ "${cfg.package.jack}/lib" ];

    users = lib.mkIf cfg.systemWide {
      users.pipewire = {
        uid = config.ids.uids.pipewire;
        group = "pipewire";
        extraGroups = [
          "audio"
          "video"
        ] ++ lib.optional config.security.rtkit.enable "rtkit";
        description = "PipeWire system service user";
        isSystemUser = true;
        home = "/var/lib/pipewire";
        createHome = true;
      };
      groups.pipewire.gid = config.ids.gids.pipewire;
    };
  };
}