os/modules/audio/default.nix
Benjamin Bädorf bb53317daf
Add spotify as an audio option
Adds a `config.pub-solar.audio.spotify` option that when enabled
installs and configures `spotifyd` as a systemd daemon and `spotify-tui`
as the terminal-based UI.

After enabling, run `spt` in the terminal to open the UI.
2022-08-10 22:52:17 +02:00

107 lines
3.3 KiB
Nix

{ lib, config, pkgs, ... }:
with lib;
let
psCfg = config.pub-solar;
cfg = config.pub-solar.audio;
xdg = config.home-manager.users."${psCfg.user.name}".xdg;
in
{
options.pub-solar.audio = {
enable = mkEnableOption "Life in highs and lows";
mopidy.enable = mkEnableOption "Life with mopidy";
spotify.enable = mkEnableOption "Life in DRM";
spotify.username = mkOption {
description = "Spotify login username or email";
type = types.str;
example = "yourname@example.com";
default = "";
};
bluetooth.enable = mkEnableOption "Life with bluetooth";
};
config = mkIf cfg.enable {
users.users = pkgs.lib.setAttrByPath [ psCfg.user.name ] {
extraGroups = [ "audio" ];
};
home-manager = with pkgs; pkgs.lib.setAttrByPath [ "users" psCfg.user.name ] {
home.packages = [
# easyeffects, e.g. for microphone noise filtering
easyeffects
mu
pavucontrol
pa_applet
playerctl
# Needed for pactl cmd, until pw-cli is more mature (vol up/down hotkeys?)
pulseaudio
vimpc
] ++ (if cfg.spotify.enable then [ pkgs.spotify-tui ] else [ ]);
xdg.configFile."vimpc/vimpcrc".source = ./.config/vimpc/vimpcrc;
systemd.user.services.easyeffects = import ./easyeffects.service.nix pkgs;
services.spotifyd = mkIf cfg.spotify.enable {
enable = true;
settings = {
global = {
username = cfg.spotify.username;
password_cmd = "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1001/bus ${pkgs.libsecret}/bin/secret-tool lookup spotify password";
bitrate = 320;
volume_normalisation = true;
no_audio_cache = false;
max_cache_size = 1000000000;
};
};
};
};
# Enable sound using pipewire-pulse
services.pipewire = {
enable = true;
alsa.enable = true;
alsa.support32Bit = true;
pulse.enable = true;
config.pipewire = {
context.default.clock = {
allowed-rates = [ 44100 48000 88200 96000 ];
rate = 44100;
};
};
config.pipewire-pulse = builtins.fromJSON (builtins.readFile ./pipewire-pulse.conf.json);
# Bluetooth configuration for pipewire
media-session.config.bluez-monitor.rules = mkIf cfg.bluetooth.enable [
{
# Matches all cards
matches = [{ "device.name" = "~bluez_card.*"; }];
actions = {
"update-props" = {
"bluez5.reconnect-profiles" = [ "hfp_hf" "hsp_hs" "a2dp_sink" ];
# mSBC is not expected to work on all headset + adapter combinations.
"bluez5.msbc-support" = true;
};
};
}
{
matches = [
# Matches all sources
{ "node.name" = "~bluez_input.*"; }
# Matches all outputs
{ "node.name" = "~bluez_output.*"; }
];
actions = {
"node.pause-on-idle" = false;
};
}
];
};
# Enable bluetooth
hardware.bluetooth.enable = mkIf cfg.bluetooth.enable true;
services.blueman.enable = mkIf cfg.bluetooth.enable true;
# Enable audio server & client
services.mopidy = mkIf cfg.mopidy.enable ((import ./mopidy.nix) pkgs);
};
}