···
[ "sound" "enableOSSEmulation" ]
[ "hardware" "alsa" "enableOSSEmulation" ]
89
-
(lib.mkRenamedOptionModule
90
-
[ "sound" "extraConfig" ]
91
-
[ "hardware" "alsa" "config" ])
89
+
(lib.mkRenamedOptionModule [ "sound" "extraConfig" ] [ "hardware" "alsa" "config" ])
options.hardware.alsa = {
···
example = lib.literalExpression ''
firefox = { device = "front"; maxVolume = -25.0; };
···
289
-
config = lib.mkIf cfg.enable {
287
+
options.hardware.alsa.enablePersistence = lib.mkOption {
288
+
type = lib.types.bool;
289
+
defaultText = lib.literalExpression "config.hardware.alsa.enable";
290
+
default = config.hardware.alsa.enable;
292
+
Whether to enable ALSA sound card state saving on shutdown.
293
+
This is generally not necessary if you're using an external sound server.
291
-
# Disable sound servers enabled by default and,
292
-
# if the user enabled one manually, cause a conflict.
293
-
services.pipewire.enable = false;
294
-
hardware.pulseaudio.enable = false;
297
+
config = lib.mkMerge [
296
-
hardware.alsa.config =
300
-
pcm.!default fromenv
299
+
(lib.mkIf cfg.enable {
300
+
# Disable sound servers enabled by default and,
301
+
# if the user enabled one manually, cause a conflict.
302
+
services.pipewire.enable = false;
303
+
services.pulseaudio.enable = false;
302
-
# Read the capture and playback device from
303
-
# the ALSA_AUDIO_IN, ALSA_AUDIO_OUT variables
310
-
vars [ ALSA_AUDIO_OUT ]
311
-
default pcm.sysdefault
305
+
hardware.alsa.config =
309
+
pcm.!default fromenv
311
+
# Read the capture and playback device from
312
+
# the ALSA_AUDIO_IN, ALSA_AUDIO_OUT variables
319
+
vars [ ALSA_AUDIO_OUT ]
320
+
default pcm.sysdefault
318
-
vars [ ALSA_AUDIO_IN ]
319
-
default pcm.sysdefault
327
+
vars [ ALSA_AUDIO_IN ]
328
+
default pcm.sysdefault
324
-
(lib.optional cfg.enableRecorder ''
325
-
pcm.!default "splitter:fromenv,recorder"
333
+
(lib.optional cfg.enableRecorder ''
334
+
pcm.!default "splitter:fromenv,recorder"
327
-
# Send audio to two stereo devices
330
-
@args.A.type string
331
-
@args.B.type string
335
-
route_policy "duplicate"
340
-
slaves.a.channels 2
341
-
slaves.b.channels 2
343
-
{ slave a channel 0 }
344
-
{ slave a channel 1 }
345
-
{ slave b channel 0 }
346
-
{ slave b channel 1 }
336
+
# Send audio to two stereo devices
339
+
@args.A.type string
340
+
@args.B.type string
344
+
route_policy "duplicate"
349
+
slaves.a.channels 2
350
+
slaves.b.channels 2
352
+
{ slave a channel 0 }
353
+
{ slave a channel 1 }
354
+
{ slave b channel 0 }
355
+
{ slave b channel 1 }
353
-
# Device which records and plays back audio
360
-
slave.pcm "hw:loopback,1,0"
361
-
slave.period_size 1024
362
-
slave.buffer_size 8192
368
-
slave.pcm "hw:loopback,0,0"
369
-
slave.period_size 1024
370
-
slave.buffer_size 8192
362
+
# Device which records and plays back audio
369
+
slave.pcm "hw:loopback,1,0"
370
+
slave.period_size 1024
371
+
slave.buffer_size 8192
377
+
slave.pcm "hw:loopback,0,0"
378
+
slave.period_size 1024
379
+
slave.buffer_size 8192
374
-
(lib.mapAttrsToList mkControl cfg.controls)
375
-
(lib.mapAttrsToList (n: v: "pcm.${n} ${quote v}") cfg.deviceAliases)
378
-
lib.mkBefore (lib.concatStringsSep "\n" (lib.flatten conf));
383
+
(lib.mapAttrsToList mkControl cfg.controls)
384
+
(lib.mapAttrsToList (n: v: "pcm.${n} ${quote v}") cfg.deviceAliases)
387
+
lib.mkBefore (lib.concatStringsSep "\n" (lib.flatten conf));
380
-
hardware.alsa.cardAliases = lib.mkIf cfg.enableRecorder {
381
-
loopback.driver = "snd_aloop";
389
+
hardware.alsa.cardAliases = lib.mkIf cfg.enableRecorder {
390
+
loopback.driver = "snd_aloop";
385
-
# Set default PCM devices
386
-
environment.sessionVariables = defaultDeviceVars;
387
-
systemd.globalEnvironment = defaultDeviceVars;
394
+
# Set default PCM devices
395
+
environment.sessionVariables = defaultDeviceVars;
396
+
systemd.globalEnvironment = defaultDeviceVars;
389
-
environment.etc."asound.conf".text = cfg.config;
398
+
environment.etc."asound.conf".text = cfg.config;
391
-
boot.kernelModules =
393
-
++ lib.optionals cfg.enableOSSEmulation [ "snd_pcm_oss" "snd_mixer_oss" ]
394
-
++ lib.optionals cfg.enableRecorder [ "snd_aloop" ];
400
+
boot.kernelModules =
402
+
++ lib.optionals cfg.enableOSSEmulation [
406
+
++ lib.optionals cfg.enableRecorder [ "snd_aloop" ];
396
-
# Assign names to the sound cards
397
-
boot.extraModprobeConfig = lib.concatStringsSep "\n" cardsConfig;
408
+
# Assign names to the sound cards
409
+
boot.extraModprobeConfig = lib.concatStringsSep "\n" cardsConfig;
399
-
# Provide alsamixer, aplay, arecord, etc.
400
-
environment.systemPackages = [ pkgs.alsa-utils ];
411
+
# Provide alsamixer, aplay, arecord, etc.
412
+
environment.systemPackages = [ pkgs.alsa-utils ];
402
-
# Install udev rules for restoring card settings on boot
403
-
services.udev.extraRules = ''
404
-
ACTION=="add", SUBSYSTEM=="sound", KERNEL=="controlC*", KERNELS!="card*", GOTO="alsa_restore_go"
405
-
GOTO="alsa_restore_end"
415
+
(lib.mkIf config.hardware.alsa.enablePersistence {
407
-
LABEL="alsa_restore_go"
408
-
TEST!="/etc/alsa/state-daemon.conf", RUN+="${alsactl} restore -gU $attr{device/number}"
409
-
TEST=="/etc/alsa/state-daemon.conf", RUN+="${alsactl} nrestore -gU $attr{device/number}"
410
-
LABEL="alsa_restore_end"
417
+
# Install udev rules for restoring card settings on boot
418
+
services.udev.extraRules = ''
419
+
ACTION=="add", SUBSYSTEM=="sound", KERNEL=="controlC*", KERNELS!="card*", GOTO="alsa_restore_go"
420
+
GOTO="alsa_restore_end"
413
-
# Service to store/restore the sound card settings
414
-
systemd.services.alsa-store = {
415
-
description = "Store Sound Card State";
416
-
wantedBy = [ "multi-user.target" ];
417
-
restartIfChanged = false;
419
-
RequiresMountsFor = "/var/lib/alsa";
420
-
ConditionVirtualization = "!systemd-nspawn";
424
-
RemainAfterExit = true;
425
-
StateDirectory = "alsa";
426
-
# Note: the service should never be restated, otherwise any
427
-
# setting changed between the last `store` and now will be lost.
428
-
# To prevent NixOS from starting it in case it has failed we
429
-
# expand the exit codes considered successful
430
-
SuccessExitStatus = [ 0 99 ];
431
-
ExecStart = "${alsactl} restore -gU";
432
-
ExecStop = "${alsactl} store -gU";
422
+
LABEL="alsa_restore_go"
423
+
TEST!="/etc/alsa/state-daemon.conf", RUN+="${alsactl} restore -gU $attr{device/number}"
424
+
TEST=="/etc/alsa/state-daemon.conf", RUN+="${alsactl} nrestore -gU $attr{device/number}"
425
+
LABEL="alsa_restore_end"
428
+
# Service to store/restore the sound card settings
429
+
systemd.services.alsa-store = {
430
+
description = "Store Sound Card State";
431
+
wantedBy = [ "multi-user.target" ];
432
+
restartIfChanged = false;
434
+
RequiresMountsFor = "/var/lib/alsa";
435
+
ConditionVirtualization = "!systemd-nspawn";
439
+
RemainAfterExit = true;
440
+
StateDirectory = "alsa";
441
+
# Note: the service should never be restated, otherwise any
442
+
# setting changed between the last `store` and now will be lost.
443
+
# To prevent NixOS from starting it in case it has failed we
444
+
# expand the exit codes considered successful
445
+
SuccessExitStatus = [
449
+
ExecStart = "${alsactl} restore -gU";
450
+
ExecStop = "${alsactl} store -gU";
meta.maintainers = with lib.maintainers; [ rnhmjoj ];