From 27a77c6ddd1bc29d56675da90bfef699e9c2f12a Mon Sep 17 00:00:00 2001 From: Adithya Nair Date: Thu, 16 Mar 2023 23:36:17 +0530 Subject: [PATCH] add script to change power profile --- home/scripts.nix | 165 ++++++++++++++++++++++------------- home/sway/waybar/default.nix | 14 ++- home/sway/waybar/style.css | 3 +- 3 files changed, 120 insertions(+), 62 deletions(-) diff --git a/home/scripts.nix b/home/scripts.nix index 9312142..c8f5738 100644 --- a/home/scripts.nix +++ b/home/scripts.nix @@ -1,67 +1,114 @@ -{ pkgs, ... }: +{ osConfig, pkgs, ... }: { - xdg.configFile."scripts/power_menu.sh" = { - text = '' - #!/bin/sh + xdg.configFile = { + "scripts/power_menu.sh" = { + executable = true; + text = '' + #!/bin/sh - set -eu + set -eu - chpower() { - case "$1" in - "") - ;; - Shutdown) - exec systemctl poweroff - ;; - Reboot) - exec systemctl reboot - ;; - Hibernate) - exec systemctl hibernate + chpower() { + case "$1" in + "") + ;; + Shutdown) + exec systemctl poweroff + ;; + Reboot) + exec systemctl reboot + ;; + Hibernate) + exec systemctl hibernate + ;; + Logout) + swaymsg exit + ;; + *) + ${pkgs.libnotify}/bin/notify-send -t 1500 -u low "Invalid Option" + ;; + esac + } + + OPTIONS="Shutdown\nReboot\nHibernate\nLogout" + + chpower "$(printf "%b" "$OPTIONS" | sort | ${pkgs.rofi-wayland}/bin/rofi -dmenu -p "Power Menu")" + ''; + }; + + "scripts/volume_up.sh" = { + executable = true; + text = '' + #!/bin/sh + + set -eu + + ${pkgs.wireplumber}/bin/wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 + [ $(${pkgs.wireplumber}/bin/wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk -F': ' '{print $2}' | sed 's/\.//') -lt 100 ] && ${pkgs.wireplumber}/bin/wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+ + ''; + }; + + "scripts/tmux_sessions.sh" = { + executable = true; + text = '' + #!/bin/sh + + set -eu + + SESSION="$(${pkgs.tmux}/bin/tmux list-sessions -F "(#{session_attached}) #S [#{pane_current_command} in #{pane_current_path}] #{pane_title}" | sort | ${pkgs.rofi-wayland}/bin/rofi -dmenu -p "Running TMUX Sessions" | awk '{print $2}')" + case "$SESSION" in + "") + ;; + *) + ${pkgs.kitty}/bin/kitty ${pkgs.tmux}/bin/tmux -u attach-session -dEt "$SESSION" + ;; + esac''; + }; + "scripts/power_profile.sh" = + let + sudo = "/run/wrappers/bin/sudo"; + cpupower = "${osConfig.boot.kernelPackages.cpupower}/bin/cpupower"; + powerprofilesctl = "${pkgs.power-profiles-daemon}/bin/powerprofilesctl"; + in { + executable = true; + text = '' + #!/bin/sh + + set -eu + + POWER_PROFILE_FILE="$HOME/.cache/power_profile" + POWER_PROFILE="powersave" + + if [ -f "$POWER_PROFILE_FILE" ]; then + POWER_PROFILE="$(<$POWER_PROFILE_FILE)" + fi + + case "$1" in + "toggle") + if [ "$POWER_PROFILE" == "powersave" ]; then + ${sudo} ${cpupower} frequency-set --governor performance > /dev/null + ${powerprofilesctl} set performance + POWER_PROFILE="performance" + elif [ "$POWER_PROFILE" == "performance" ]; then + ${sudo} ${cpupower} frequency-set --governor powersave > /dev/null + ${powerprofilesctl} set power-saver + POWER_PROFILE="powersave" + fi + echo $POWER_PROFILE > $POWER_PROFILE_FILE ;; - Logout) - swaymsg exit - ;; - *) - ${pkgs.libnotify}/bin/notify-send -t 1500 -u low "Invalid Option" - ;; - esac - } + "icon") + if [ "$POWER_PROFILE" == "powersave" ]; then + echo "󰌪" + elif [ "$POWER_PROFILE" == "performance" ]; then + echo "󰓅" + fi + ;; + *) + ;; + esac - OPTIONS="Shutdown\nReboot\nHibernate\nLogout" - - chpower "$(printf "%b" "$OPTIONS" | sort | ${pkgs.rofi-wayland}/bin/rofi -dmenu -p "Power Menu")" - ''; - executable = true; - }; - - xdg.configFile."scripts/volume_up.sh" = { - text = '' - #!/bin/sh - - set -eu - - ${pkgs.wireplumber}/bin/wpctl set-mute @DEFAULT_AUDIO_SINK@ 0 - [ $(${pkgs.wireplumber}/bin/wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk -F': ' '{print $2}' | sed 's/\.//') -lt 100 ] && ${pkgs.wireplumber}/bin/wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+ - ''; - executable = true; - }; - - xdg.configFile."scripts/tmux_sessions.sh" = { - text = '' - #!/bin/sh - - set -eu - - SESSION="$(${pkgs.tmux}/bin/tmux list-sessions -F "(#{session_attached}) #S [#{pane_current_command} in #{pane_current_path}] #{pane_title}" | sort | ${pkgs.rofi-wayland}/bin/rofi -dmenu -p "Running TMUX Sessions" | awk '{print $2}')" - case "$SESSION" in - "") - ;; - *) - ${pkgs.kitty}/bin/kitty ${pkgs.tmux}/bin/tmux -u attach-session -dEt "$SESSION" - ;; - esac''; - executable = true; + ''; + }; }; } diff --git a/home/sway/waybar/default.nix b/home/sway/waybar/default.nix index 2ecf0b2..42d3759 100644 --- a/home/sway/waybar/default.nix +++ b/home/sway/waybar/default.nix @@ -1,4 +1,4 @@ -{ pkgs, ... }: +{ config, pkgs, ... }: { programs.waybar.enable = true; @@ -12,7 +12,7 @@ height = 28; modules-left = [ "sway/workspaces" "sway/window" "sway/mode" ]; modules-center = [ ]; - modules-right = [ "tray" "idle_inhibitor" "network" "bluetooth" "pulseaudio" "backlight" "battery" "clock" ]; + modules-right = [ "tray" "custom/power_profile" "idle_inhibitor" "network" "bluetooth" "pulseaudio" "backlight" "battery" "clock" ]; "sway/mode" = { "format" = "{}"; }; @@ -88,6 +88,16 @@ "tray" = { "spacing" = 4; }; + "custom/power_profile" = + let + script = "${config.xdg.configHome}/scripts/power_profile.sh"; + in { + exec = "${script} icon"; + on-click = "${script} toggle"; + format = "{}"; + tooltip = false; + interval = "10"; + }; }; }; } diff --git a/home/sway/waybar/style.css b/home/sway/waybar/style.css index 122de45..e51a9e5 100644 --- a/home/sway/waybar/style.css +++ b/home/sway/waybar/style.css @@ -58,7 +58,8 @@ window#waybar.solo { #tray, #mode, #idle_inhibitor, -#bluetooth { +#bluetooth, +#custom-power_profile { padding: 0 10px; color: @foreground; background-color: @background;