Sound only works while pavucontrol is open

I recently installed NixOS with gnome-wayland on a desktop, and sound usually doesn’t play, except if I open pavucontrol in a nix-shell. Then sound plays, but a few seconds after closing the window, the sound begins to glitch out then go silent.

It seems like pavucontrol is changing some routing that only lasts as long as the window is open. I tried checking the pacmd list-sinks and there are some differences while the window is open:

 3 sink(s) available.
   * index: 0
        name: <alsa_output.usb-MOTU_M4_M4MA0A90BO-00.analog-surround-40>
        driver: <module-alsa-card.c>
        flags: HARDWARE DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
-       state: IDLE
+       state: RUNNING
        suspend cause: (none)
        priority: 9049
        volume: front-left: 43245 /  66% / -10.83 dB,   front-right: 43245 /  66% / -10.83 dB,   rear-left: 43245 /  66% / -10.83 dB,   rear-right: 43245 /  66% / -10.83 dB
                balance 0.00
        base volume: 65536 / 100% / 0.00 dB
        volume steps: 65537
        muted: no
-       current latency: 81.08 ms
-       max request: 56 KiB
-       max rewind: 56 KiB
+       current latency: 46.73 ms
+       max request: 27 KiB
+       max rewind: 27 KiB
        monitor source: 0
-       sample spec: s32le 4ch 48000Hz
+       sample spec: s32le 4ch 44100Hz
        channel map: front-left,front-right,rear-left,rear-right
                     Surround 4.0
-       used by: 0
-       linked by: 1
-       configured latency: 75.00 ms; range is 0.50 .. 1365.33 ms
+       used by: 1
+       linked by: 3
+       configured latency: 40.00 ms; range is 0.50 .. 1486.08 ms
        card: 0 <alsa_card.usb-MOTU_M4_M4MA0A90BO-00>
        module: 6

and then the rest is the same between them.

What might the issue be and how can I get sound to work reliably?

I have

sound.enable = true;
hardware.pulseaudio.enable = true;

in my config and my user is in the audio group.

I hate to be that guy, but I just recommend switching to Pipewire with Pulseaudio emulation. In my experience this fixes pretty much all problems that people have with sound.

Quoting from Headphone volume resets itself to 100% - #2 by hmenke

{
  hardware.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
  };
}

I do not recommend using the Jack emulation ( services.pipewire.jack.enable = true ) at this point because it uses LD_LIBRARY_PATH which breaks some applications (Evolution, epiphany: Webkitgtk bubblewrapper crashes when LD_LIBRARY path points into /run/current-system/** (the case on a pipewire system with jack emulation) · Issue #110468 · NixOS/nixpkgs · GitHub).

3 Likes