{ ... }: {
  flake.nixosModules.systemAudio =
    { config, lib, ... }:
    let
      cfg = config.chiasson.system.audio;
    in
    {
      options.chiasson.system.audio = {
        enable = lib.mkEnableOption ''
          PipeWire (ALSA + Pulse), RTKit, optional WirePlumber tweak to kill idle suspend pop on devices.
        '';

        rtkit.enable = lib.mkOption {
          type = lib.types.bool;
          default = true;
          description = "Enable `security.rtkit.enable` (recommended with PipeWire).";
        };

        pipewire = {
          alsa = {
            enable = lib.mkOption {
              type = lib.types.bool;
              default = true;
              description = "PipeWire ALSA support.";
            };
            support32Bit = lib.mkOption {
              type = lib.types.bool;
              default = true;
              description = "32-bit ALSA support on x86_64.";
            };
          };
          pulse.enable = lib.mkOption {
            type = lib.types.bool;
            default = true;
            description = "PipeWire PulseAudio compatibility (`pulseaudio` clients).";
          };
          jack.enable = lib.mkOption {
            type = lib.types.bool;
            default = false;
            description = "PipeWire JACK support (e.g. pro-audio / low-latency apps).";
          };
        };

        preventIdleSuspend = lib.mkOption {
          type = lib.types.bool;
          default = true;
          description = ''
            WirePlumber + clock hints so ALSA nodes do not sleep (~0.5s glitch on resume otherwise).
          '';
        };
      };

      config = lib.mkIf cfg.enable (lib.mkMerge [
        {
          services.pulseaudio.enable = false;
          security.rtkit.enable = cfg.rtkit.enable;

          services.pipewire = lib.mkMerge [
            {
              enable = true;
              alsa.enable = cfg.pipewire.alsa.enable;
              alsa.support32Bit = cfg.pipewire.alsa.support32Bit;
              pulse.enable = cfg.pipewire.pulse.enable;
            }
            (lib.mkIf cfg.pipewire.jack.enable {
              jack.enable = true;
            })
            (lib.mkIf cfg.preventIdleSuspend {
              extraConfig.pipewire = {
                "context.properties" = {
                  "default.clock.quantum" = 1024;
                  "default.clock.min-quantum" = 32;
                  "default.clock.max-quantum" = 8192;
                };
              };
            })
          ];
        }
        (lib.mkIf cfg.preventIdleSuspend {
          environment.etc."wireplumber/wireplumber.conf.d/99-prevent-suspend.conf".text = ''
            monitor.alsa.rules = [
              {
                matches = [
                  { node.name = "~alsa_output.*" }
                  { node.name = "~alsa_input.*" }
                ]
                actions = {
                  update-props = {
                    session.suspend-timeout-seconds = 0
                  }
                }
              }
            ]
          '';
        })
      ]);
    };
}
