From be352ef463e160f2f5a6a91eb6b968b4bed07fea Mon Sep 17 00:00:00 2001
From: prescientmoon <git@moonythm.dev>
Date: Thu, 29 Feb 2024 12:44:52 +0100
Subject: [PATCH] Set up bluetooth audio

---
 home/features/desktop/audio.nix               |  4 +++
 home/features/desktop/spotify.nix             |  2 ++
 hosts/nixos/common/optional/bluetooth.nix     |  5 ++++
 hosts/nixos/common/optional/pipewire.nix      | 29 +++++++------------
 .../common/optional/wayland/hyprland.nix      |  1 +
 hosts/nixos/tethys/default.nix                |  1 +
 6 files changed, 24 insertions(+), 18 deletions(-)
 create mode 100644 home/features/desktop/audio.nix
 create mode 100644 hosts/nixos/common/optional/bluetooth.nix

diff --git a/home/features/desktop/audio.nix b/home/features/desktop/audio.nix
new file mode 100644
index 0000000..45da8c3
--- /dev/null
+++ b/home/features/desktop/audio.nix
@@ -0,0 +1,4 @@
+{
+  # Makes bluetooth media controls work
+  services.mpris-proxy.enable = true;
+}
diff --git a/home/features/desktop/spotify.nix b/home/features/desktop/spotify.nix
index 76db852..6e9222a 100644
--- a/home/features/desktop/spotify.nix
+++ b/home/features/desktop/spotify.nix
@@ -22,6 +22,8 @@ let
   });
 in
 {
+  imports = [ ./audio.nix ];
+
   programs.spicetify = {
     enable = true;
 
diff --git a/hosts/nixos/common/optional/bluetooth.nix b/hosts/nixos/common/optional/bluetooth.nix
new file mode 100644
index 0000000..c6ea80a
--- /dev/null
+++ b/hosts/nixos/common/optional/bluetooth.nix
@@ -0,0 +1,5 @@
+# https://nixos.wiki/wiki/Bluetooth
+{
+  hardware.bluetooth.enable = true;
+  hardware.bluetooth.powerOnBoot = true;
+}
diff --git a/hosts/nixos/common/optional/pipewire.nix b/hosts/nixos/common/optional/pipewire.nix
index d87dc45..4a88338 100644
--- a/hosts/nixos/common/optional/pipewire.nix
+++ b/hosts/nixos/common/optional/pipewire.nix
@@ -11,22 +11,15 @@
     jack.enable = true;
   };
 
-  # Volume controls
-  environment.shellAliases =
-    let
-      pactl = "${pkgs.pulseaudio}/bin/pactl";
-      volume = amount: "${pactl} set-sink-volume @DEFAULT_SINK@ ${amount}";
-    in
-    {
-      # Relative 
-      "v-down" = volume "-5%";
-      "v-up" = volume "+5%";
-
-      # Absolute
-      "v-min" = volume "0%";
-      "v-low" = volume "25%";
-      "v-mid" = volume "50%";
-      "v-high" = volume "75%";
-      "v-max" = volume "100%";
-    };
+  # https://nixos.wiki/wiki/PipeWire
+  environment.etc = {
+    "wireplumber/bluetooth.lua.d/51-bluez-config.lua".text = ''
+      bluez_monitor.properties = {
+        ["bluez5.enable-sbc-xq"] = true,
+        ["bluez5.enable-msbc"] = true,
+        ["bluez5.enable-hw-volume"] = true,
+        ["bluez5.headset-roles"] = "[ hsp_hs hsp_ag hfp_hf hfp_ag ]"
+      }
+    '';
+  };
 }
diff --git a/hosts/nixos/common/optional/wayland/hyprland.nix b/hosts/nixos/common/optional/wayland/hyprland.nix
index ad9f273..18ac87e 100644
--- a/hosts/nixos/common/optional/wayland/hyprland.nix
+++ b/hosts/nixos/common/optional/wayland/hyprland.nix
@@ -2,4 +2,5 @@
 { pkgs, ... }: {
   programs.hyprland.enable = true;
   programs.hyprland.package = pkgs.hyprland;
+  services.udev.packages = [ pkgs.swayosd ];
 }
diff --git a/hosts/nixos/tethys/default.nix b/hosts/nixos/tethys/default.nix
index 20e525d..1ce9af1 100644
--- a/hosts/nixos/tethys/default.nix
+++ b/hosts/nixos/tethys/default.nix
@@ -5,6 +5,7 @@
     ../common/users/adrielus.nix
 
     ../common/optional/pipewire.nix
+    ../common/optional/bluetooth.nix
     ../common/optional/greetd.nix
     ../common/optional/quietboot.nix
     ../common/optional/desktop/steam.nix