2018-11-03 05:55:57 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2011-07-26 02:51:56 +01:00
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
with pkgs;
|
2014-05-05 19:58:51 +01:00
|
|
|
with lib;
|
2013-02-28 18:40:07 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.hardware.pulseaudio;
|
2017-06-30 23:01:58 +01:00
|
|
|
alsaCfg = config.sound;
|
2013-02-28 18:40:07 +00:00
|
|
|
|
2014-04-21 10:50:49 +01:00
|
|
|
systemWide = cfg.enable && cfg.systemWide;
|
|
|
|
nonSystemWide = cfg.enable && !cfg.systemWide;
|
2016-07-03 23:32:04 +01:00
|
|
|
hasZeroconf = let z = cfg.zeroconf; in z.publish.enable || z.discovery.enable;
|
|
|
|
|
|
|
|
overriddenPackage = cfg.package.override
|
|
|
|
(optionalAttrs hasZeroconf { zeroconfSupport = true; });
|
|
|
|
binary = "${getBin overriddenPackage}/bin/pulseaudio";
|
|
|
|
binaryNoDaemon = "${binary} --daemonize=no";
|
2014-04-21 10:50:49 +01:00
|
|
|
|
2021-06-10 08:27:31 +01:00
|
|
|
# Forces 32bit pulseaudio and alsa-plugins to be built/supported for apps
|
2014-09-11 04:54:00 +01:00
|
|
|
# using 32bit alsa on 64bit linux.
|
2021-06-10 08:27:31 +01:00
|
|
|
enable32BitAlsaPlugins = cfg.support32Bit && stdenv.isx86_64 && (pkgs.pkgsi686Linux.alsa-lib != null && pkgs.pkgsi686Linux.libpulseaudio != null);
|
2014-09-11 04:54:00 +01:00
|
|
|
|
2016-07-03 23:32:04 +01:00
|
|
|
|
|
|
|
myConfigFile =
|
|
|
|
let
|
|
|
|
addModuleIf = cond: mod: optionalString cond "load-module ${mod}";
|
|
|
|
allAnon = optional cfg.tcp.anonymousClients.allowAll "auth-anonymous=1";
|
|
|
|
ipAnon = let a = cfg.tcp.anonymousClients.allowedIpRanges;
|
|
|
|
in optional (a != []) ''auth-ip-acl=${concatStringsSep ";" a}'';
|
|
|
|
in writeTextFile {
|
|
|
|
name = "default.pa";
|
|
|
|
text = ''
|
|
|
|
.include ${cfg.configFile}
|
|
|
|
${addModuleIf cfg.zeroconf.publish.enable "module-zeroconf-publish"}
|
|
|
|
${addModuleIf cfg.zeroconf.discovery.enable "module-zeroconf-discover"}
|
|
|
|
${addModuleIf cfg.tcp.enable (concatStringsSep " "
|
2016-08-25 18:34:33 +01:00
|
|
|
([ "module-native-protocol-tcp" ] ++ allAnon ++ ipAnon))}
|
2020-07-18 12:32:48 +01:00
|
|
|
${addModuleIf config.services.jack.jackd.enable "module-jack-sink"}
|
|
|
|
${addModuleIf config.services.jack.jackd.enable "module-jack-source"}
|
2016-07-03 23:32:04 +01:00
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-09-02 12:16:13 +01:00
|
|
|
ids = config.ids;
|
2013-02-28 18:40:07 +00:00
|
|
|
|
2014-09-02 12:16:13 +01:00
|
|
|
uid = ids.uids.pulseaudio;
|
|
|
|
gid = ids.gids.pulseaudio;
|
|
|
|
|
2017-11-27 07:49:38 +00:00
|
|
|
stateDir = "/run/pulse";
|
2013-02-28 18:40:07 +00:00
|
|
|
|
|
|
|
# Create pulse/client.conf even if PulseAudio is disabled so
|
|
|
|
# that we can disable the autospawn feature in programs that
|
|
|
|
# are built with PulseAudio support (like KDE).
|
|
|
|
clientConf = writeText "client.conf" ''
|
2019-10-12 01:02:25 +01:00
|
|
|
autospawn=no
|
2016-06-25 13:34:34 +01:00
|
|
|
${cfg.extraClientConf}
|
2013-02-28 18:40:07 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
# Write an /etc/asound.conf that causes all ALSA applications to
|
|
|
|
# be re-routed to the PulseAudio server through ALSA's Pulse
|
|
|
|
# plugin.
|
2014-09-11 04:54:00 +01:00
|
|
|
alsaConf = writeText "asound.conf" (''
|
2013-02-28 18:40:07 +00:00
|
|
|
pcm_type.pulse {
|
2021-06-10 08:27:31 +01:00
|
|
|
libs.native = ${pkgs.alsa-plugins}/lib/alsa-lib/libasound_module_pcm_pulse.so ;
|
2014-09-11 04:54:00 +01:00
|
|
|
${lib.optionalString enable32BitAlsaPlugins
|
2021-06-10 08:27:31 +01:00
|
|
|
"libs.32Bit = ${pkgs.pkgsi686Linux.alsa-plugins}/lib/alsa-lib/libasound_module_pcm_pulse.so ;"}
|
2013-02-28 18:40:07 +00:00
|
|
|
}
|
|
|
|
pcm.!default {
|
|
|
|
type pulse
|
|
|
|
hint.description "Default Audio Device (via PulseAudio)"
|
|
|
|
}
|
|
|
|
ctl_type.pulse {
|
2021-06-10 08:27:31 +01:00
|
|
|
libs.native = ${pkgs.alsa-plugins}/lib/alsa-lib/libasound_module_ctl_pulse.so ;
|
2014-09-11 04:54:00 +01:00
|
|
|
${lib.optionalString enable32BitAlsaPlugins
|
2021-06-10 08:27:31 +01:00
|
|
|
"libs.32Bit = ${pkgs.pkgsi686Linux.alsa-plugins}/lib/alsa-lib/libasound_module_ctl_pulse.so ;"}
|
2013-02-28 18:40:07 +00:00
|
|
|
}
|
|
|
|
ctl.!default {
|
|
|
|
type pulse
|
|
|
|
}
|
2017-06-30 23:01:58 +01:00
|
|
|
${alsaCfg.extraConfig}
|
2014-09-11 04:54:00 +01:00
|
|
|
'');
|
2013-02-28 18:40:07 +00:00
|
|
|
|
|
|
|
in {
|
2011-07-26 02:51:56 +01:00
|
|
|
|
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
hardware.pulseaudio = {
|
|
|
|
enable = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.bool;
|
2013-02-28 18:40:07 +00:00
|
|
|
default = false;
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc ''
|
2013-02-28 18:40:07 +00:00
|
|
|
Whether to enable the PulseAudio sound server.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
systemWide = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc ''
|
2013-02-28 18:40:07 +00:00
|
|
|
If false, a PulseAudio server is launched automatically for
|
|
|
|
each user that tries to use the sound system. The server runs
|
2019-10-30 15:58:57 +00:00
|
|
|
with user privileges. If true, one system-wide PulseAudio
|
2017-11-30 06:37:01 +00:00
|
|
|
server is launched on boot, running as the user "pulse", and
|
|
|
|
only users in the "audio" group will have access to the server.
|
2013-02-28 18:40:07 +00:00
|
|
|
Please read the PulseAudio documentation for more details.
|
2019-10-30 15:58:57 +00:00
|
|
|
|
|
|
|
Don't enable this option unless you know what you are doing.
|
2013-02-28 18:40:07 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-06-10 19:48:52 +01:00
|
|
|
support32Bit = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc ''
|
2017-01-26 18:00:43 +00:00
|
|
|
Whether to include the 32-bit pulseaudio libraries in the system or not.
|
2015-06-11 21:44:57 +01:00
|
|
|
This is only useful on 64-bit systems and currently limited to x86_64-linux.
|
|
|
|
'';
|
2015-06-10 19:48:52 +01:00
|
|
|
};
|
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
configFile = mkOption {
|
2016-07-03 23:32:04 +01:00
|
|
|
type = types.nullOr types.path;
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-07-03 23:32:04 +01:00
|
|
|
The path to the default configuration options the PulseAudio server
|
2013-02-28 18:40:07 +00:00
|
|
|
should use. By default, the "default.pa" configuration
|
|
|
|
from the PulseAudio distribution is used.
|
2013-10-30 16:37:45 +00:00
|
|
|
'';
|
2013-02-28 18:40:07 +00:00
|
|
|
};
|
2013-10-30 16:37:45 +00:00
|
|
|
|
2016-07-03 23:32:04 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Literal string to append to `configFile`
|
2016-07-03 23:32:04 +01:00
|
|
|
and the config file generated by the pulseaudio module.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-06-25 13:34:34 +01:00
|
|
|
extraClientConf = mkOption {
|
2016-07-03 23:32:04 +01:00
|
|
|
type = types.lines;
|
2016-06-25 13:34:34 +01:00
|
|
|
default = "";
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-06-25 13:34:34 +01:00
|
|
|
Extra configuration appended to pulse/client.conf file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
package = mkOption {
|
2014-02-27 12:22:04 +00:00
|
|
|
type = types.package;
|
2020-07-18 12:32:48 +01:00
|
|
|
default = if config.services.jack.jackd.enable
|
|
|
|
then pkgs.pulseaudioFull
|
|
|
|
else pkgs.pulseaudio;
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression "pkgs.pulseaudio";
|
|
|
|
example = literalExpression "pkgs.pulseaudioFull";
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc ''
|
2015-06-04 11:57:12 +01:00
|
|
|
The PulseAudio derivation to use. This can be used to enable
|
|
|
|
features (such as JACK support, Bluetooth) via the
|
2022-07-19 14:05:45 +01:00
|
|
|
`pulseaudioFull` package.
|
2013-02-28 18:40:07 +00:00
|
|
|
'';
|
|
|
|
};
|
2014-04-21 10:50:49 +01:00
|
|
|
|
2018-10-17 20:05:46 +01:00
|
|
|
extraModules = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [];
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression "[ pkgs.pulseaudio-modules-bt ]";
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-10-17 20:05:46 +01:00
|
|
|
Extra pulseaudio modules to use. This is intended for out-of-tree
|
|
|
|
pulseaudio modules like extra bluetooth codecs.
|
|
|
|
|
|
|
|
Extra modules take precedence over built-in pulseaudio modules.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-04-21 10:50:49 +01:00
|
|
|
daemon = {
|
|
|
|
logLevel = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "notice";
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc ''
|
2014-04-21 10:50:49 +01:00
|
|
|
The log level that the system-wide pulseaudio daemon should use,
|
|
|
|
if activated.
|
|
|
|
'';
|
|
|
|
};
|
2017-01-14 21:58:16 +00:00
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
type = types.attrsOf types.unspecified;
|
|
|
|
default = {};
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc "Config of the pulse daemon. See `man pulse-daemon.conf`.";
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''{ realtime-scheduling = "yes"; }'';
|
2017-01-14 21:58:16 +00:00
|
|
|
};
|
2014-04-21 10:50:49 +01:00
|
|
|
};
|
2016-07-03 23:32:04 +01:00
|
|
|
|
|
|
|
zeroconf = {
|
|
|
|
discovery.enable =
|
|
|
|
mkEnableOption "discovery of pulseaudio sinks in the local network";
|
|
|
|
publish.enable =
|
|
|
|
mkEnableOption "publishing the pulseaudio sink in the local network";
|
|
|
|
};
|
|
|
|
|
|
|
|
# TODO: enable by default?
|
|
|
|
tcp = {
|
|
|
|
enable = mkEnableOption "tcp streaming support";
|
|
|
|
|
|
|
|
anonymousClients = {
|
|
|
|
allowAll = mkEnableOption "all anonymous clients to stream to the server";
|
|
|
|
allowedIpRanges = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''[ "127.0.0.1" "192.168.1.0/24" ]'';
|
2022-07-19 14:05:45 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-07-03 23:32:04 +01:00
|
|
|
A list of IP subnets that are allowed to stream to the server.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2011-08-01 14:26:51 +01:00
|
|
|
};
|
|
|
|
|
2011-07-26 02:51:56 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2011-07-26 02:51:56 +01:00
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
config = mkMerge [
|
|
|
|
{
|
2019-09-14 18:51:29 +01:00
|
|
|
environment.etc = {
|
|
|
|
"pulse/client.conf".source = clientConf;
|
2013-02-28 18:40:07 +00:00
|
|
|
};
|
2014-09-18 15:12:36 +01:00
|
|
|
|
2016-07-03 23:32:04 +01:00
|
|
|
hardware.pulseaudio.configFile = mkDefault "${getBin overriddenPackage}/etc/pulse/default.pa";
|
2013-02-28 18:40:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(mkIf cfg.enable {
|
2016-07-03 23:32:04 +01:00
|
|
|
environment.systemPackages = [ overriddenPackage ];
|
2013-02-28 18:40:07 +00:00
|
|
|
|
2018-04-14 19:09:04 +01:00
|
|
|
sound.enable = true;
|
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
environment.etc = {
|
|
|
|
"asound.conf".source = alsaConf;
|
2017-01-14 21:58:16 +00:00
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
"pulse/daemon.conf".source = writeText "daemon.conf"
|
|
|
|
(lib.generators.toKeyValue {} cfg.daemon.config);
|
2017-11-27 07:49:38 +00:00
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
"openal/alsoft.conf".source = writeText "alsoft.conf" "drivers=pulse";
|
2017-11-27 07:49:38 +00:00
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
"libao.conf".source = writeText "libao.conf" "default_driver=pulse";
|
|
|
|
};
|
2013-02-28 18:40:07 +00:00
|
|
|
|
2019-01-27 19:24:12 +00:00
|
|
|
# Disable flat volumes to enable relative ones
|
|
|
|
hardware.pulseaudio.daemon.config.flat-volumes = mkDefault "no";
|
|
|
|
|
2019-05-31 16:04:33 +01:00
|
|
|
# Upstream defaults to speex-float-1 which results in audible artifacts
|
|
|
|
hardware.pulseaudio.daemon.config.resample-method = mkDefault "speex-float-5";
|
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
# Allow PulseAudio to get realtime priority using rtkit.
|
|
|
|
security.rtkit.enable = true;
|
2016-07-03 23:32:04 +01:00
|
|
|
|
2017-08-03 02:17:19 +01:00
|
|
|
systemd.packages = [ overriddenPackage ];
|
2020-02-07 21:50:07 +00:00
|
|
|
|
|
|
|
# PulseAudio is packaged with udev rules to handle various audio device quirks
|
|
|
|
services.udev.packages = [ overriddenPackage ];
|
2016-07-03 23:32:04 +01:00
|
|
|
})
|
|
|
|
|
2018-10-17 20:05:46 +01:00
|
|
|
(mkIf (cfg.extraModules != []) {
|
|
|
|
hardware.pulseaudio.daemon.config.dl-search-path = let
|
|
|
|
overriddenModules = builtins.map
|
|
|
|
(drv: drv.override { pulseaudio = overriddenPackage; })
|
|
|
|
cfg.extraModules;
|
|
|
|
modulePaths = builtins.map
|
2020-10-26 01:18:26 +00:00
|
|
|
(drv: "${drv}/${overriddenPackage.pulseDir}/modules")
|
2018-10-17 20:05:46 +01:00
|
|
|
# User-provided extra modules take precedence
|
|
|
|
(overriddenModules ++ [ overriddenPackage ]);
|
|
|
|
in lib.concatStringsSep ":" modulePaths;
|
|
|
|
})
|
|
|
|
|
2016-07-03 23:32:04 +01:00
|
|
|
(mkIf hasZeroconf {
|
|
|
|
services.avahi.enable = true;
|
|
|
|
})
|
|
|
|
(mkIf cfg.zeroconf.publish.enable {
|
|
|
|
services.avahi.publish.enable = true;
|
|
|
|
services.avahi.publish.userServices = true;
|
2013-02-28 18:40:07 +00:00
|
|
|
})
|
|
|
|
|
2014-04-21 10:50:49 +01:00
|
|
|
(mkIf nonSystemWide {
|
2019-09-14 18:51:29 +01:00
|
|
|
environment.etc = {
|
|
|
|
"pulse/default.pa".source = myConfigFile;
|
2013-02-28 18:40:07 +00:00
|
|
|
};
|
2016-04-02 16:05:21 +01:00
|
|
|
systemd.user = {
|
|
|
|
services.pulseaudio = {
|
2017-05-25 18:33:13 +01:00
|
|
|
restartIfChanged = true;
|
2016-04-02 16:05:21 +01:00
|
|
|
serviceConfig = {
|
2016-07-21 00:18:06 +01:00
|
|
|
RestartSec = "500ms";
|
2017-05-25 18:33:13 +01:00
|
|
|
PassEnvironment = "DISPLAY";
|
2016-04-02 16:05:21 +01:00
|
|
|
};
|
2020-07-18 12:32:48 +01:00
|
|
|
} // optionalAttrs config.services.jack.jackd.enable {
|
|
|
|
environment.JACK_PROMISCUOUS_SERVER = "jackaudio";
|
2017-05-25 18:33:13 +01:00
|
|
|
};
|
|
|
|
sockets.pulseaudio = {
|
|
|
|
wantedBy = [ "sockets.target" ];
|
2016-04-02 16:05:21 +01:00
|
|
|
};
|
|
|
|
};
|
2013-02-28 18:40:07 +00:00
|
|
|
})
|
|
|
|
|
2014-04-21 10:50:49 +01:00
|
|
|
(mkIf systemWide {
|
2018-06-30 00:58:35 +01:00
|
|
|
users.users.pulse = {
|
2013-02-28 18:40:07 +00:00
|
|
|
# For some reason, PulseAudio wants UID == GID.
|
|
|
|
uid = assert uid == gid; uid;
|
|
|
|
group = "pulse";
|
|
|
|
extraGroups = [ "audio" ];
|
|
|
|
description = "PulseAudio system service user";
|
2014-09-02 12:16:13 +01:00
|
|
|
home = stateDir;
|
|
|
|
createHome = true;
|
2021-03-07 13:54:00 +00:00
|
|
|
isSystemUser = true;
|
2013-02-28 18:40:07 +00:00
|
|
|
};
|
2013-10-30 16:37:45 +00:00
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.groups.pulse.gid = gid;
|
2013-10-30 16:37:45 +00:00
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
systemd.services.pulseaudio = {
|
2013-11-09 19:06:01 +00:00
|
|
|
description = "PulseAudio System-Wide Server";
|
2013-02-28 18:40:07 +00:00
|
|
|
wantedBy = [ "sound.target" ];
|
|
|
|
before = [ "sound.target" ];
|
2014-04-21 10:50:49 +01:00
|
|
|
environment.PULSE_RUNTIME_PATH = stateDir;
|
|
|
|
serviceConfig = {
|
2016-04-02 16:05:21 +01:00
|
|
|
Type = "notify";
|
2016-07-03 23:32:04 +01:00
|
|
|
ExecStart = "${binaryNoDaemon} --log-level=${cfg.daemon.logLevel} --system -n --file=${myConfigFile}";
|
2016-04-02 16:05:21 +01:00
|
|
|
Restart = "on-failure";
|
2016-07-21 00:18:06 +01:00
|
|
|
RestartSec = "500ms";
|
2014-04-21 10:50:49 +01:00
|
|
|
};
|
2013-02-28 18:40:07 +00:00
|
|
|
};
|
2016-12-04 21:05:15 +00:00
|
|
|
|
|
|
|
environment.variables.PULSE_COOKIE = "${stateDir}/.config/pulse/cookie";
|
2013-02-28 18:40:07 +00:00
|
|
|
})
|
|
|
|
];
|
2011-07-26 02:51:56 +01:00
|
|
|
|
|
|
|
}
|