2 { config, lib, pkgs, ... }:
8 inherit (pkgs) alsa-utils;
10 pulseaudioEnabled = config.hardware.pulseaudio.enable;
16 (mkRenamedOptionModule [ "sound" "enableMediaKeys" ] [ "sound" "mediaKeys" "enable" ])
28 description = lib.mdDoc ''
29 Whether to enable ALSA sound.
33 enableOSSEmulation = mkOption {
36 description = lib.mdDoc ''
37 Whether to enable ALSA OSS emulation (with certain cards sound mixing may not work!).
41 extraConfig = mkOption {
47 description = lib.mdDoc ''
48 Set addition configuration for system-wide alsa.
57 description = lib.mdDoc ''
58 Whether to enable volume and capture control with keyboard media keys.
60 You want to leave this disabled if you run a desktop environment
61 like KDE, Gnome, Xfce, etc, as those handle such things themselves.
62 You might want to enable this if you run a minimalistic desktop
63 environment or work from bare linux ttys/framebuffers.
65 Enabling this will turn on {option}`services.actkbd`.
69 volumeStep = mkOption {
73 description = lib.mdDoc ''
74 The value by which to increment/decrement volume on media keys.
76 See amixer(1) for allowed values.
89 config = mkIf config.sound.enable {
91 environment.systemPackages = [ alsa-utils ];
93 environment.etc = mkIf (!pulseaudioEnabled && config.sound.extraConfig != "")
94 { "asound.conf".text = config.sound.extraConfig; };
96 # ALSA provides a udev rule for restoring volume settings.
97 services.udev.packages = [ alsa-utils ];
99 boot.kernelModules = optional config.sound.enableOSSEmulation "snd_pcm_oss";
101 systemd.services.alsa-store =
102 { description = "Store Sound Card State";
103 wantedBy = [ "multi-user.target" ];
104 unitConfig.RequiresMountsFor = "/var/lib/alsa";
105 unitConfig.ConditionVirtualization = "!systemd-nspawn";
108 RemainAfterExit = true;
109 ExecStart = "${pkgs.coreutils}/bin/mkdir -p /var/lib/alsa";
110 ExecStop = "${alsa-utils}/sbin/alsactl store --ignore";
114 services.actkbd = mkIf config.sound.mediaKeys.enable {
118 { keys = [ 113 ]; events = [ "key" ]; command = "${alsa-utils}/bin/amixer -q set Master toggle"; }
120 # "Lower Volume" media key
121 { keys = [ 114 ]; events = [ "key" "rep" ]; command = "${alsa-utils}/bin/amixer -q set Master ${config.sound.mediaKeys.volumeStep}- unmute"; }
123 # "Raise Volume" media key
124 { keys = [ 115 ]; events = [ "key" "rep" ]; command = "${alsa-utils}/bin/amixer -q set Master ${config.sound.mediaKeys.volumeStep}+ unmute"; }
126 # "Mic Mute" media key
127 { keys = [ 190 ]; events = [ "key" ]; command = "${alsa-utils}/bin/amixer -q set Capture toggle"; }