NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.29k stars 13.54k forks source link

ALSA restore udev rule not working #54387

Open lopsided98 opened 5 years ago

lopsided98 commented 5 years ago

Issue description

The udev rule that is supposed to restore ALSA state from /var/lib/alsa/asound.state does not seem to be working.

alsa-store.service correctly saves the ALSA state to asound.state, but it is not restored on boot. Running udevadm test /sys/devices/pci0000:00/0000:00:1b.0/sound/card0/controlC0 gives this output:

calling: test
version 239
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

Load module index
Created link configuration context.
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/00-path.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/01-md-raid-creating.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/10-dm.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/11-dm-lvm.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/13-dm-disk.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/50-udev-default.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-block.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-cdrom_id.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-drm.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-evdev.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-input-id.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-persistent-alsa.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-persistent-input.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-persistent-storage-tape.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-persistent-storage.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-persistent-v4l.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-sensor.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-serial.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/60-zvol.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/63-md-raid-arrays.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/64-btrfs.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/64-md-raid-assembly.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/69-bcache.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/69-md-clustered-confirm-device.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/69-vdev.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/70-joystick.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/70-mouse.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/70-power-switch.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/70-touchpad.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/70-uaccess.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/71-seat.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/73-seat-late.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/75-net-description.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/75-probe_mtd.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/78-sound-card.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/80-drivers.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/80-net-setup-link.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/80-udisks2.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/89-alsa-ucm.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/90-alsa-restore.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/90-vconsole.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/90-zfs.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/95-dm-notify.rules
Reading rules file: /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/99-local.rules
Reading rules file: /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/99-systemd.rules
rules contain 49152 bytes tokens (4096 * 12 bytes), 19552 bytes strings
2356 strings (35022 bytes), 1600 de-duplicated (16227 bytes), 757 trie nodes used
GROUP 17 /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/50-udev-default.rules:43
IMPORT builtin 'path_id' /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-persistent-alsa.rules:11
LINK 'snd/by-path/pci-0000:00:1b.0' /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/60-persistent-alsa.rules:12
RUN 'uaccess' /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/73-seat-late.rules:17
ATTR '/sys/devices/pci0000:00/0000:00:1b.0/sound/card0/controlC0/../uevent' writing 'change' /nix/store/i2kkikrn30mfcm7v84cb840dnc4bm50c-systemd-239/lib/udev/rules.d/78-sound-card.rules:5
RUN '/nix/store/nc2970g9gzhfiz2v6z10i7jbmbib7qy1-alsa-utils-1.1.7/sbin/alsactl restore $attr{device/number}' /nix/store/0p6zrmkfnnq1c0yx7xfymlwsqjsl7h6d-udev-rules/90-alsa-restore.rules:5
handling device node '/dev/snd/controlC0', devnum=c116:6, mode=0660, uid=0, gid=17
preserve permissions /dev/snd/controlC0, 020660, uid=0, gid=17
preserve already existing symlink '/dev/char/116:6' to '../snd/controlC0'
found 'c116:6' claiming '/run/udev/links/\x2fsnd\x2fby-path\x2fpci-0000:00:1b.0'
creating link '/dev/snd/by-path/pci-0000:00:1b.0' to '/dev/snd/controlC0'
preserve already existing symlink '/dev/snd/by-path/pci-0000:00:1b.0' to '../controlC0'
ACTION=add
DEVLINKS=/dev/snd/by-path/pci-0000:00:1b.0
DEVNAME=/dev/snd/controlC0
DEVPATH=/devices/pci0000:00/0000:00:1b.0/sound/card0/controlC0
ID_PATH=pci-0000:00:1b.0
ID_PATH_TAG=pci-0000_00_1b_0
MAJOR=116
MINOR=6
PATH=/nix/store/1spfmb1dgfl1216bi442kh1fnp153vsf-udev-path/bin:/nix/store/1spfmb1dgfl1216bi442kh1fnp153vsf-udev-path/sbin
SUBSYSTEM=sound
TAGS=:uaccess:
USEC_INITIALIZED=65985748
run: 'uaccess'
run: '/nix/store/nc2970g9gzhfiz2v6z10i7jbmbib7qy1-alsa-utils-1.1.7/sbin/alsactl restore 0'
Unload module index
Unloaded link configuration context.

Manually running /nix/store/nc2970g9gzhfiz2v6z10i7jbmbib7qy1-alsa-utils-1.1.7/sbin/alsactl restore 0 correctly restores the state.

Steps to reproduce

  1. Change mixer volumes
  2. Reboot
  3. Observe that volumes are not preserved

Technical details

stale[bot] commented 4 years ago

Thank you for your contributions.

This has been automatically marked as stale because it has had no activity for 180 days.

If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.

Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse.
  3. Ask on the #nixos channel on irc.freenode.net.
gadtfly commented 3 years ago

I'm currently grappling with this.

gadtfly commented 3 years ago

I did some very crude printf-debugging and found that my system is executing the first RUN+="..." command in etc/udev/rule.d/90-alsa-restore.rules sometime during system boot, so my previous comment may be a bit of a red herring.

How about: does something else need to be in place before alsactl restore will work?

lopsided98 commented 3 years ago

I just tested a machine running about a month old version of nixpkgs master and I can't reproduce this issue anymore. @gadtfly What nixpkgs version are you testing?

gadtfly commented 3 years ago

Thanks for checking.

To be completely honest, I'm not sure how to determine that or what precisely that means.

I installed this system from the 21.05 Graphical ISO some time around June 30th, I haven't explicitly done anything to do with channels, and I've run nixos-rebuild switch a bunch times while connected to the internet. My nixpkgs version is in whatever state that would leave me in.

gadtfly commented 3 years ago

Here's where I'm at with this issue on my system:

gadtfly commented 3 years ago

Here's the workaround that I'm trying:

systemd.user.services.alsa-restore-fix = {
  after = [ "pulseaudio.service" ];
  wantedBy = [ "pulseaudio.service" ];
  script = "${pkgs.alsaUtils}/bin/amixer -c1 sset 'Speaker' 39";
};

I am not particularly familiar with writing systemd units so I have no idea if this is the "right" way to do this, but it seems to work.

I couldn't (sufficiently easily) figure out how to get a system-wide service to trigger on PulseAudio start (which happens per-user), so this is a user service.

I couldn't (sufficiently easily) figure out how to get alsactl restore (which requires root permissions) to work in a user service, so I'm just setting the volume directly.

39 is 100% on this thing (the default it kept resetting to was 10, which is 26%). The intention was always to manage the volume with PulseAudio. This seems to work so far.

lopsided98 commented 3 years ago

Yeah, I'm pretty sure that is a different issue than mine, unless perhaps PulseAudio was implicitly enabled somewhere in my config. My machines that had this issue were headless, and did not intentionally enable PulseAudio.

I would guess that PulseAudio is trying to manage the volume itself, and there might be some way of persistently adjusting it from within the PulseAudio settings.

gadtfly commented 3 years ago

I'm starting to suspect that the root problem for me is that PulseAudio is not hooking itself in to my audio card's volume in the way it may be supposed to (I don't know, I'm not particularly famliar with PulseAudio either).

You're right that does seem to be a different issue.

Thank you for your help!

stale[bot] commented 2 years ago

I marked this as stale due to inactivity. → More info

mastarija commented 2 years ago

Any update on this? I'm having problems with alsa settings not being restored on startup.

Baitinq commented 1 year ago

Having this problem too

lykso commented 10 months ago

I am also having this problem. I found a workaround, but my machine is a little unusual so YMMV. You'll likely want to leave out line 138, at any rate, as that's specific to my particular setup.

The workaround, minus line 138, for your convenience:

services.udev.extraRules = ''
ACTION=="add", SUBSYSTEM=="sound", KERNEL=="controlC*", KERNELS!="card*", TEST="${pkgs.alsa-utils}", TEST="/var/lib/alsa/asound.state, GOTO="alsa_restore_go", GOTO="alsa_restore_end"

LABEL="alsa_restore_go"
TEST!="/etc/alsa/state-daemon.conf",RUN+="${pkgs.alsa-utils}/bin/alsactl restore $attr{device/number}"
TEST=="/etc/alsa/state-daemon.conf",RUN+="${pkgs.alsa-utils}/bin/alsactl nrestore $attr{device/number}"

LABEL="alsa_restore_end"
'';
tupo2 commented 9 months ago

Other workaround if PulseAudio is installed:

Edit the file /usr/share/pulseaudio/alsa-mixer/paths/analog-output.conf.common (on Debian) Add or modify those element examples to not be altered by PulseAudio:

[Element Master]
switch = ignore
volume = ignore

[Element PCM]
switch = ignore
volume = ignore

[Element Front]
switch = ignore
volume = ignore

There is an equivalent for PipeWire.