2014-09-11 03:54:00 +00:00
|
|
|
{ config, lib, pkgs, pkgs_i686, ... }:
|
2011-07-26 01:51:56 +00:00
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
with pkgs;
|
2014-05-05 18:58:51 +00:00
|
|
|
with lib;
|
2013-02-28 18:40:07 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.hardware.pulseaudio;
|
|
|
|
|
2014-04-21 09:50:49 +00:00
|
|
|
systemWide = cfg.enable && cfg.systemWide;
|
|
|
|
nonSystemWide = cfg.enable && !cfg.systemWide;
|
2016-07-03 22:32:04 +00: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 09:50:49 +00:00
|
|
|
|
2014-09-11 03:54:00 +00:00
|
|
|
# Forces 32bit pulseaudio and alsaPlugins to be built/supported for apps
|
|
|
|
# using 32bit alsa on 64bit linux.
|
2015-06-10 18:48:52 +00:00
|
|
|
enable32BitAlsaPlugins = cfg.support32Bit && stdenv.isx86_64 && (pkgs_i686.alsaLib != null && pkgs_i686.libpulseaudio != null);
|
2014-09-11 03:54:00 +00:00
|
|
|
|
2016-07-03 22:32:04 +00: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 17:34:33 +00:00
|
|
|
([ "module-native-protocol-tcp" ] ++ allAnon ++ ipAnon))}
|
2016-07-03 22:32:04 +00:00
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-09-02 11:16:13 +00:00
|
|
|
ids = config.ids;
|
2013-02-28 18:40:07 +00:00
|
|
|
|
2014-09-02 11:16:13 +00:00
|
|
|
uid = ids.uids.pulseaudio;
|
|
|
|
gid = ids.gids.pulseaudio;
|
|
|
|
|
|
|
|
stateDir = "/var/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" ''
|
2014-04-21 09:50:49 +00:00
|
|
|
autospawn=${if nonSystemWide then "yes" else "no"}
|
2016-07-03 22:32:04 +00:00
|
|
|
${optionalString nonSystemWide "daemon-binary=${binary}"}
|
2016-06-25 12:34:34 +00: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 03:54:00 +00:00
|
|
|
alsaConf = writeText "asound.conf" (''
|
2013-02-28 18:40:07 +00:00
|
|
|
pcm_type.pulse {
|
2014-09-11 03:54:00 +00:00
|
|
|
libs.native = ${pkgs.alsaPlugins}/lib/alsa-lib/libasound_module_pcm_pulse.so ;
|
|
|
|
${lib.optionalString enable32BitAlsaPlugins
|
|
|
|
"libs.32Bit = ${pkgs_i686.alsaPlugins}/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 {
|
2016-07-03 22:32:04 +00:00
|
|
|
libs.native = ${pkgs.alsaPlugins}/lib/alsa-lib/libasound_module_ctl_pulse.so ;
|
2014-09-11 03:54:00 +00:00
|
|
|
${lib.optionalString enable32BitAlsaPlugins
|
|
|
|
"libs.32Bit = ${pkgs_i686.alsaPlugins}/lib/alsa-lib/libasound_module_ctl_pulse.so ;"}
|
2013-02-28 18:40:07 +00:00
|
|
|
}
|
|
|
|
ctl.!default {
|
|
|
|
type pulse
|
|
|
|
}
|
2014-09-11 03:54:00 +00:00
|
|
|
'');
|
2013-02-28 18:40:07 +00:00
|
|
|
|
|
|
|
in {
|
2011-07-26 01:51:56 +00:00
|
|
|
|
|
|
|
options = {
|
2011-09-14 18:20:50 +00: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;
|
|
|
|
description = ''
|
|
|
|
Whether to enable the PulseAudio sound server.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
systemWide = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
If false, a PulseAudio server is launched automatically for
|
|
|
|
each user that tries to use the sound system. The server runs
|
2013-08-10 21:07:13 +00:00
|
|
|
with user privileges. This is the recommended and most secure
|
2013-02-28 18:40:07 +00:00
|
|
|
way to use PulseAudio. If true, one system-wide PulseAudio
|
|
|
|
server is launched on boot, running as the user "pulse".
|
|
|
|
Please read the PulseAudio documentation for more details.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-06-10 18:48:52 +00:00
|
|
|
support32Bit = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2015-06-11 20:44:57 +00:00
|
|
|
description = ''
|
|
|
|
Whether to include the 32-bit pulseaudio libraries in the systemn or not.
|
|
|
|
This is only useful on 64-bit systems and currently limited to x86_64-linux.
|
|
|
|
'';
|
2015-06-10 18:48:52 +00:00
|
|
|
};
|
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
configFile = mkOption {
|
2016-07-03 22:32:04 +00:00
|
|
|
type = types.nullOr types.path;
|
2013-02-28 18:40:07 +00:00
|
|
|
description = ''
|
2016-07-03 22:32:04 +00: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 22:32:04 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Literal string to append to <literal>configFile</literal>
|
|
|
|
and the config file generated by the pulseaudio module.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-06-25 12:34:34 +00:00
|
|
|
extraClientConf = mkOption {
|
2016-07-03 22:32:04 +00:00
|
|
|
type = types.lines;
|
2016-06-25 12:34:34 +00:00
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
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;
|
2016-04-28 13:42:16 +00:00
|
|
|
default = pulseaudioLight;
|
|
|
|
defaultText = "pkgs.pulseaudioLight";
|
|
|
|
example = literalExample "pkgs.pulseaudioFull";
|
2013-02-28 18:40:07 +00:00
|
|
|
description = ''
|
2015-06-04 10:57:12 +00:00
|
|
|
The PulseAudio derivation to use. This can be used to enable
|
|
|
|
features (such as JACK support, Bluetooth) via the
|
|
|
|
<literal>pulseaudioFull</literal> package.
|
2013-02-28 18:40:07 +00:00
|
|
|
'';
|
|
|
|
};
|
2014-04-21 09:50:49 +00:00
|
|
|
|
|
|
|
daemon = {
|
|
|
|
logLevel = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "notice";
|
|
|
|
description = ''
|
|
|
|
The log level that the system-wide pulseaudio daemon should use,
|
|
|
|
if activated.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2016-07-03 22:32:04 +00: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 = [];
|
|
|
|
example = literalExample ''[ "127.0.0.1" "192.168.1.0/24" ]'';
|
|
|
|
description = ''
|
|
|
|
A list of IP subnets that are allowed to stream to the server.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2011-08-01 13:26:51 +00:00
|
|
|
};
|
|
|
|
|
2011-07-26 01:51:56 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2011-07-26 01:51:56 +00:00
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
config = mkMerge [
|
|
|
|
{
|
|
|
|
environment.etc = singleton {
|
|
|
|
target = "pulse/client.conf";
|
|
|
|
source = clientConf;
|
|
|
|
};
|
2014-09-18 14:12:36 +00:00
|
|
|
|
2016-07-03 22:32:04 +00: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 22:32:04 +00:00
|
|
|
environment.systemPackages = [ overriddenPackage ];
|
2013-02-28 18:40:07 +00:00
|
|
|
|
|
|
|
environment.etc = singleton {
|
|
|
|
target = "asound.conf";
|
|
|
|
source = alsaConf;
|
|
|
|
};
|
|
|
|
|
|
|
|
# Allow PulseAudio to get realtime priority using rtkit.
|
|
|
|
security.rtkit.enable = true;
|
2016-07-03 22:32:04 +00: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 09:50:49 +00:00
|
|
|
(mkIf nonSystemWide {
|
2013-02-28 18:40:07 +00:00
|
|
|
environment.etc = singleton {
|
|
|
|
target = "pulse/default.pa";
|
2016-07-03 22:32:04 +00:00
|
|
|
source = myConfigFile;
|
2013-02-28 18:40:07 +00:00
|
|
|
};
|
2016-04-02 15:05:21 +00:00
|
|
|
|
|
|
|
systemd.user = {
|
|
|
|
services.pulseaudio = {
|
|
|
|
description = "PulseAudio Server";
|
|
|
|
# NixOS doesn't support "Also" so we bring it in manually
|
|
|
|
wantedBy = [ "default.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "notify";
|
2016-07-03 22:32:04 +00:00
|
|
|
ExecStart = binaryNoDaemon;
|
2016-04-02 15:05:21 +00:00
|
|
|
Restart = "on-failure";
|
2016-07-20 23:18:06 +00:00
|
|
|
RestartSec = "500ms";
|
2016-04-02 15:05:21 +00:00
|
|
|
};
|
2016-07-06 02:17:14 +00:00
|
|
|
environment = { DISPLAY = ":${toString config.services.xserver.display}"; };
|
2016-07-03 22:32:04 +00:00
|
|
|
restartIfChanged = true;
|
2016-04-02 15:05:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
sockets.pulseaudio = {
|
|
|
|
description = "PulseAudio Socket";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
socketConfig = {
|
|
|
|
Priority = 6;
|
|
|
|
Backlog = 5;
|
|
|
|
ListenStream = "%t/pulse/native";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2013-02-28 18:40:07 +00:00
|
|
|
})
|
|
|
|
|
2014-04-21 09:50:49 +00:00
|
|
|
(mkIf systemWide {
|
2013-02-28 18:40:07 +00:00
|
|
|
users.extraUsers.pulse = {
|
|
|
|
# For some reason, PulseAudio wants UID == GID.
|
|
|
|
uid = assert uid == gid; uid;
|
|
|
|
group = "pulse";
|
|
|
|
extraGroups = [ "audio" ];
|
|
|
|
description = "PulseAudio system service user";
|
2014-09-02 11:16:13 +00:00
|
|
|
home = stateDir;
|
|
|
|
createHome = true;
|
2013-02-28 18:40:07 +00:00
|
|
|
};
|
2013-10-30 16:37:45 +00:00
|
|
|
|
2013-02-28 18:40:07 +00:00
|
|
|
users.extraGroups.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 09:50:49 +00:00
|
|
|
environment.PULSE_RUNTIME_PATH = stateDir;
|
|
|
|
serviceConfig = {
|
2016-04-02 15:05:21 +00:00
|
|
|
Type = "notify";
|
2016-07-03 22:32:04 +00:00
|
|
|
ExecStart = "${binaryNoDaemon} --log-level=${cfg.daemon.logLevel} --system -n --file=${myConfigFile}";
|
2016-04-02 15:05:21 +00:00
|
|
|
Restart = "on-failure";
|
2016-07-20 23:18:06 +00:00
|
|
|
RestartSec = "500ms";
|
2014-04-21 09:50:49 +00:00
|
|
|
};
|
2013-02-28 18:40:07 +00:00
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
2011-07-26 01:51:56 +00:00
|
|
|
|
|
|
|
}
|