about summary refs log tree commit diff
path: root/modules/nixos-apple-silicon/apple-silicon-support/modules/sound/default.nix
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2023-12-26 17:21:50 +0100
committerAlyssa Ross <hi@alyssa.is>2024-01-02 17:29:12 +0100
commit3a559792110a75c0a56c8cc21cc9e1353b27351f (patch)
tree7c8efd00586abedf7a11e3f089c6a9138f1046dc /modules/nixos-apple-silicon/apple-silicon-support/modules/sound/default.nix
parentdd691ab6c1fabf1a4915cfe397a5b173e10f77af (diff)
parent9f81761270e6343d00b99d3e07e1feee8f3ba4b5 (diff)
downloadnixlib-3a559792110a75c0a56c8cc21cc9e1353b27351f.tar
nixlib-3a559792110a75c0a56c8cc21cc9e1353b27351f.tar.gz
nixlib-3a559792110a75c0a56c8cc21cc9e1353b27351f.tar.bz2
nixlib-3a559792110a75c0a56c8cc21cc9e1353b27351f.tar.lz
nixlib-3a559792110a75c0a56c8cc21cc9e1353b27351f.tar.xz
nixlib-3a559792110a75c0a56c8cc21cc9e1353b27351f.tar.zst
nixlib-3a559792110a75c0a56c8cc21cc9e1353b27351f.zip
Merge https://github.com/tpwrules/nixos-apple-silicon
Diffstat (limited to 'modules/nixos-apple-silicon/apple-silicon-support/modules/sound/default.nix')
-rw-r--r--modules/nixos-apple-silicon/apple-silicon-support/modules/sound/default.nix61
1 files changed, 55 insertions, 6 deletions
diff --git a/modules/nixos-apple-silicon/apple-silicon-support/modules/sound/default.nix b/modules/nixos-apple-silicon/apple-silicon-support/modules/sound/default.nix
index 5713f1aa18ec..e1b45a8dcbcf 100644
--- a/modules/nixos-apple-silicon/apple-silicon-support/modules/sound/default.nix
+++ b/modules/nixos-apple-silicon/apple-silicon-support/modules/sound/default.nix
@@ -1,20 +1,69 @@
 { config, pkgs, lib, ... }:
 
 {
+  imports = [
+    # disable pulseaudio as the Asahi sound infrastructure can't use it.
+    # if we disable it only if setupAsahiSound is enabled, then infinite
+    # recursion results as pulseaudio enables config.sound by default.
+    { config.hardware.pulseaudio.enable = false; }
+  ];
+
   options.hardware.asahi = {
-    setupAlsaUcm = lib.mkOption {
+    setupAsahiSound = lib.mkOption {
       type = lib.types.bool;
       default = config.sound.enable;
       description = ''
-	Enable the Asahi-specific ALSA UCM2 configs in the global environment
-        so that headphone jack input and output work properly.
+        Set up the Asahi DSP components so that the speakers and headphone jack
+        work properly and safely.
       '';
     };
   };
 
-  config = lib.mkIf config.hardware.asahi.setupAlsaUcm {
-    environment.variables = {
-      ALSA_CONFIG_UCM2 = "${pkgs.alsa-ucm-conf-asahi}/share/alsa/ucm2";
+  config = let
+    asahi-audio = pkgs.asahi-audio; # the asahi-audio we use
+
+    lsp-plugins = pkgs.lsp-plugins; # the lsp-plugins we use
+
+    lsp-plugins-is-patched = (lsp-plugins.overrideAttrs (old: {
+      passthru = (old.passthru or {}) // {
+        lsp-plugins-is-patched = builtins.elem "58c3f985f009c84347fa91236f164a9e47aafa93.patch"
+          (builtins.map (p: p.name) (old.patches or []));
+      };
+    })).lsp-plugins-is-patched;
+
+    lsp-plugins-is-safe = (pkgs.lib.versionAtLeast lsp-plugins.version "1.2.14") || lsp-plugins-is-patched;
+  in lib.mkIf config.hardware.asahi.setupAsahiSound {
+    # enable pipewire to run real-time and avoid audible glitches
+    security.rtkit.enable = true;
+    # set up pipewire with the supported capabilities (instead of pulseaudio)
+    services.pipewire = {
+      enable = true;
+
+      alsa.enable = true;
+      wireplumber.enable = true;
+      pulse.enable = true;
     };
+
+    # enable speakersafetyd to protect speakers
+    systemd.packages = lib.mkAssert lsp-plugins-is-safe
+      "lsp-plugins is unpatched/outdated and speakers cannot be safely enabled"
+      [ pkgs.speakersafetyd ];
+    services.udev.packages = [ pkgs.speakersafetyd ];
+
+    # set up enivronment so that asahi-audio and UCM configs are used
+    environment.etc = builtins.listToAttrs (builtins.map
+      (f: { name = f; value = { source = "${asahi-audio}/share/${f}"; }; })
+      asahi-audio.providedConfigFiles);
+    environment.variables.ALSA_CONFIG_UCM2 = "${pkgs.alsa-ucm-conf-asahi}/share/alsa/ucm2";
+
+    # set up pipewire and wireplumber to use asahi-audio configs and plugins
+    systemd.user.services.pipewire.environment.ALSA_CONFIG_UCM2 = config.environment.variables.ALSA_CONFIG_UCM2;
+    systemd.user.services.wireplumber.environment.ALSA_CONFIG_UCM2 = config.environment.variables.ALSA_CONFIG_UCM2;
+    systemd.user.services.pipewire.environment.LV2_PATH = let
+      lv2Plugins = [ lsp-plugins pkgs.bankstown-lv2 ];
+    in lib.makeSearchPath "lib/lv2" lv2Plugins;
+    systemd.user.services.wireplumber.environment.LV2_PATH = let
+      lv2Plugins = [ lsp-plugins pkgs.bankstown-lv2 ];
+    in lib.makeSearchPath "lib/lv2" lv2Plugins;
   };
 }