From f69d233c394eb9c997a6ea460537a746359578cb Mon Sep 17 00:00:00 2001 From: Noah Masur <7386960+nmasur@users.noreply.github.com> Date: Wed, 22 Mar 2023 21:15:41 -0400 Subject: [PATCH] switch audio with rofi and add extraLib --- flake.nix | 1 + modules/darwin/utilities.nix | 17 ++--------------- modules/nixos/graphical/default.nix | 4 ++++ modules/nixos/graphical/i3.nix | 2 ++ modules/nixos/graphical/rofi.nix | 11 ++++++++++- modules/nixos/graphical/rofi/pulse-sink.sh | 15 +++++++++++++++ overlays/lib.nix | 16 ++++++++++++++++ 7 files changed, 50 insertions(+), 16 deletions(-) create mode 100755 modules/nixos/graphical/rofi/pulse-sink.sh create mode 100644 overlays/lib.nix diff --git a/flake.nix b/flake.nix index 812aa8d..a0a47f5 100644 --- a/flake.nix +++ b/flake.nix @@ -121,6 +121,7 @@ inputs.nur.overlay inputs.nix2vim.overlay (import ./overlays/neovim-plugins.nix inputs) + (import ./overlays/lib.nix) (import ./overlays/calibre-web.nix) ]; diff --git a/modules/darwin/utilities.nix b/modules/darwin/utilities.nix index 763dba7..1021ef6 100644 --- a/modules/darwin/utilities.nix +++ b/modules/darwin/utilities.nix @@ -1,19 +1,6 @@ { config, pkgs, lib, ... }: -let - - # Quickly package shell scripts with their dependencies - # From https://discourse.nixos.org/t/how-to-create-a-script-with-dependencies/7970/6 - mkScript = { name, file, env ? [ ] }: - pkgs.writeScriptBin name '' - for i in ${lib.concatStringsSep " " env}; do - export PATH="$i/bin:$PATH" - done - - exec ${pkgs.bash}/bin/bash ${file} $@ - ''; - -in { +{ home-manager.users.${config.user} = lib.mkIf pkgs.stdenv.isDarwin { @@ -32,7 +19,7 @@ in { consul noti # Create notifications programmatically ipcalc # Make IP network calculations - (mkScript { + (pkgs.extraLib.mkScript { name = "ocr"; file = ../../modules/common/shell/bash/scripts/ocr.sh; env = [ tesseract ]; diff --git a/modules/nixos/graphical/default.nix b/modules/nixos/graphical/default.nix index 9d6c8e6..13c3b1a 100644 --- a/modules/nixos/graphical/default.nix +++ b/modules/nixos/graphical/default.nix @@ -17,6 +17,10 @@ type = lib.types.str; description = "Command to use for choosing windows"; }; + audioSwitchCommand = lib.mkOption { + type = lib.types.str; + description = "Command to use for switching audio sink"; + }; toggleBarCommand = lib.mkOption { type = lib.types.str; description = "Command to hide and show the status bar."; diff --git a/modules/nixos/graphical/i3.nix b/modules/nixos/graphical/i3.nix index d41e3be..6874ba5 100644 --- a/modules/nixos/graphical/i3.nix +++ b/modules/nixos/graphical/i3.nix @@ -112,6 +112,8 @@ in { "exec --no-startup-id ${config.launcherCommand}"; "${modifier}+Shift+s" = "exec --no-startup-id ${config.systemdSearch}"; + "${modifier}+Shift+a" = + "exec --no-startup-id ${config.audioSwitchCommand}"; "Mod1+Tab" = "exec --no-startup-id ${config.altTabCommand}"; "${modifier}+Shift+c" = "reload"; "${modifier}+Shift+r" = "restart"; diff --git a/modules/nixos/graphical/rofi.nix b/modules/nixos/graphical/rofi.nix index 3b4ec9d..a0e75d6 100644 --- a/modules/nixos/graphical/rofi.nix +++ b/modules/nixos/graphical/rofi.nix @@ -155,7 +155,16 @@ altTabCommand = "${ config.home-manager.users.${config.user}.programs.rofi.finalPackage }/bin/rofi -show window -modi window"; - + audioSwitchCommand = "${ + (pkgs.extraLib.mkScript { + name = "switch-audio"; + file = ./rofi/pulse-sink.sh; + env = [ + pkgs.ponymix + config.home-manager.users.${config.user}.programs.rofi.finalPackage + ]; + }) + }/bin/switch-audio"; }; } diff --git a/modules/nixos/graphical/rofi/pulse-sink.sh b/modules/nixos/graphical/rofi/pulse-sink.sh new file mode 100755 index 0000000..1bb09c0 --- /dev/null +++ b/modules/nixos/graphical/rofi/pulse-sink.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +# Credit: https://gist.github.com/Nervengift/844a597104631c36513c + +sink=$( + ponymix -t sink list | + awk '/^sink/ {s=$1" "$2;getline;gsub(/^ +/,"",$0);print s" "$0}' | + rofi -dmenu -p 'pulseaudio sink:' -location 6 -width 100 | + grep -Po '[0-9]+(?=:)' +) && + ponymix set-default -d "$sink" && + for input in $(ponymix list -t sink-input | grep -Po '[0-9]+(?=:)'); do + echo "$input -> $sink" + ponymix -t sink-input -d "$input" move "$sink" + done diff --git a/overlays/lib.nix b/overlays/lib.nix new file mode 100644 index 0000000..9e60c89 --- /dev/null +++ b/overlays/lib.nix @@ -0,0 +1,16 @@ +_final: prev: { + extraLib = prev.lib // { + + # Quickly package shell scripts with their dependencies + # From https://discourse.nixos.org/t/how-to-create-a-script-with-dependencies/7970/6 + mkScript = { name, file, env ? [ ] }: + prev.pkgs.writeScriptBin name '' + for i in ${prev.lib.concatStringsSep " " env}; do + export PATH="$i/bin:$PATH" + done + + exec ${prev.pkgs.bash}/bin/bash ${file} $@ + ''; + }; + +}