Unverified Commit 4843518a authored by rnhmjoj's avatar rnhmjoj
Browse files

nixos/alsa: reintroduce hardware.alsa.enablePersistence

parent af0cd203
Loading
Loading
Loading
Loading
+155 −135
Original line number Diff line number Diff line
@@ -86,9 +86,7 @@ in
      [ "sound" "enableOSSEmulation" ]
      [ "hardware" "alsa" "enableOSSEmulation" ]
    )
    (lib.mkRenamedOptionModule
      [ "sound" "extraConfig" ]
      [ "hardware" "alsa" "config" ])
    (lib.mkRenamedOptionModule [ "sound" "extraConfig" ] [ "hardware" "alsa" "config" ])
  ];

  options.hardware.alsa = {
@@ -286,12 +284,23 @@ in

  };

  config = lib.mkIf cfg.enable {
  options.hardware.alsa.enablePersistence = lib.mkOption {
    type = lib.types.bool;
    defaultText = lib.literalExpression "config.hardware.alsa.enable";
    default = config.hardware.alsa.enable;
    description = ''
      Whether to enable ALSA sound card state saving on shutdown.
      This is generally not necessary if you're using an external sound server.
    '';
  };

  config = lib.mkMerge [

    (lib.mkIf cfg.enable {
      # Disable sound servers enabled by default and,
      # if the user enabled one manually, cause a conflict.
      services.pipewire.enable = false;
    hardware.pulseaudio.enable = false;
      services.pulseaudio.enable = false;

      hardware.alsa.config =
        let
@@ -390,7 +399,10 @@ in

      boot.kernelModules =
        [ ]
      ++ lib.optionals cfg.enableOSSEmulation [ "snd_pcm_oss" "snd_mixer_oss" ]
        ++ lib.optionals cfg.enableOSSEmulation [
          "snd_pcm_oss"
          "snd_mixer_oss"
        ]
        ++ lib.optionals cfg.enableRecorder [ "snd_aloop" ];

      # Assign names to the sound cards
@@ -398,6 +410,9 @@ in

      # Provide alsamixer, aplay, arecord, etc.
      environment.systemPackages = [ pkgs.alsa-utils ];
    })

    (lib.mkIf config.hardware.alsa.enablePersistence {

      # Install udev rules for restoring card settings on boot
      services.udev.extraRules = ''
@@ -427,12 +442,17 @@ in
          # setting changed between the last `store` and now will be lost.
          # To prevent NixOS from starting it in case it has failed we
          # expand the exit codes considered successful
        SuccessExitStatus = [ 0 99 ];
          SuccessExitStatus = [
            0
            99
          ];
          ExecStart = "${alsactl} restore -gU";
          ExecStop = "${alsactl} store -gU";
        };
      };
  };
    })

  ];

  meta.maintainers = with lib.maintainers; [ rnhmjoj ];