From fedc713d8201b4e997cbde1f576087a7d34c8303 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Fri, 19 Jul 2024 09:58:45 -0400 Subject: [PATCH] home/hyprland: move scripts to scripts.nix --- .../desktop/hyprland/default.nix | 159 +----------------- .../desktop/hyprland/scripts.nix | 114 +++++++++++++ homeManagerModules/desktop/hyprland/vars.nix | 49 ++++++ 3 files changed, 172 insertions(+), 150 deletions(-) create mode 100644 homeManagerModules/desktop/hyprland/scripts.nix diff --git a/homeManagerModules/desktop/hyprland/default.nix b/homeManagerModules/desktop/hyprland/default.nix index 6a025381..8be0567d 100644 --- a/homeManagerModules/desktop/hyprland/default.nix +++ b/homeManagerModules/desktop/hyprland/default.nix @@ -12,95 +12,28 @@ in { hyprland.extraConfig = let inherit - (import ./vars.nix) + (import ./vars.nix {inherit config lib pkgs;}) + brightness + defaultApps defaultWorkspaces layerRules + media modifier + screenshot + volume windowManagerBinds windowRules ; + inherit (import ./scripts.nix {inherit config lib pkgs;}) clamshell idleD tablet wallpaperD; # Hyprland desktop utilities hyprnome = lib.getExe pkgs.hyprnome; - hyprctl = lib.getExe' config.wayland.windowManager.hyprland.package "hyprctl"; - - # Default apps - defaultApps = { - browser = lib.getExe cfg.defaultApps.webBrowser; - editor = lib.getExe cfg.defaultApps.editor; - fileManager = lib.getExe cfg.defaultApps.fileManager; - launcher = lib.getExe pkgs.fuzzel; - lock = lib.getExe pkgs.swaylock; - logout = lib.getExe pkgs.wlogout; - terminal = lib.getExe cfg.defaultApps.terminal; - virtKeyboard = lib.getExe' pkgs.squeekboard "squeekboard"; - }; - - wallpaperd = - if cfg.desktop.hyprland.randomWallpaper - then - pkgs.writers.writeRuby "hyprland-randomWallpaper" {} '' - require 'fileutils' - - directory = "${config.xdg.dataHome}/backgrounds/" - hyprctl = "${lib.getExe' config.wayland.windowManager.hyprland.package "hyprctl"}" - current_pids = {} - known_monitors = {} - last_update_time = {} - - update_interval = 900 # 15 minutes in seconds - - sleep 1 - - if Dir.exist?(directory) - loop do - outputs = IO.popen([hyprctl, 'monitors']).read - active_monitors = outputs.each_line.map { |line| line.split[1] if line.include?('Monitor') }.compact - - added_monitors = active_monitors - known_monitors.keys - removed_monitors = known_monitors.keys - active_monitors - - # Handle newly added monitors - added_monitors.each do |monitor| - random_background = Dir.glob(File.join(directory, '*.{png,jpg}')).sample - pid = spawn("/nix/store/azvrjs0k5ap90dcqw280xrbqlm6nkibv-swaybg-1.2.1/bin/swaybg", '-o', monitor, '-i', random_background, '-m', 'fill') - current_pids[monitor] = pid - last_update_time[monitor] = Time.now - known_monitors[monitor] = random_background - end - - # Remove wallpapers from removed monitors - removed_monitors.each do |monitor| - Process.kill('TERM', current_pids[monitor]) if current_pids[monitor] - current_pids.delete(monitor) - last_update_time.delete(monitor) - known_monitors.delete(monitor) - end - - # Update wallpapers after the set interval - active_monitors.each do |monitor| - if Time.now - last_update_time[monitor] >= update_interval - random_background = Dir.glob(File.join(directory, '*.{png,jpg}')).sample - pid = spawn("${lib.getExe pkgs.swaybg}", '-o', monitor, '-i', random_background, '-m', 'fill') - sleep 1 - Process.kill('TERM', current_pids[monitor]) if current_pids[monitor] - current_pids[monitor] = pid - last_update_time[monitor] = Time.now - known_monitors[monitor] = random_background - end - end - - sleep 5 # Check for monitor changes and update intervals every 5 seconds - end - end - '' - else "${lib.getExe pkgs.swaybg} -i ${cfg.theme.wallpaper}"; startupApps = [ - wallpaperd + wallpaperD (lib.getExe pkgs.waybar) - idled + idleD (lib.getExe pkgs.wayland-pipewire-idle-inhibit) (lib.getExe' pkgs.blueman "blueman-applet") (lib.getExe' pkgs.networkmanagerapplet "nm-applet") @@ -111,80 +44,6 @@ in { ] ++ lib.lists.optional (cfg.desktop.hyprland.redShift) "${lib.getExe pkgs.gammastep} -l 33.74:-84.38"; - - screenshot = rec { - bin = lib.getExe pkgs.hyprshot; - folder = "${config.xdg.userDirs.pictures}/screenshots"; - screen = "${bin} -m output -o ${folder}"; - region = "${bin} -m region -o ${folder}"; - }; - - clamshell = pkgs.writeShellScript "hyprland-clamshell" '' - NUM_MONITORS=$(${hyprctl} monitors all | grep Monitor | wc --lines) - if [ "$1" == "on" ]; then - if [ $NUM_MONITORS -gt 1 ]; then - ${hyprctl} keyword monitor "eDP-1, disable" - fi - elif [ "$1" == "off" ]; then - ${ - lib.strings.concatMapStringsSep "\n" - (monitor: ''${hyprctl} keyword monitor "${monitor}"'') - cfg.desktop.hyprland.laptopMonitors - } - fi - ''; - - tablet = pkgs.writeShellScript "hyprland-tablet" '' - STATE=`${lib.getExe pkgs.dconf} read /org/gnome/desktop/a11y/applications/screen-keyboard-enabled` - - if [ $STATE -z ] || [ $STATE == "false" ]; then - if ! [ `pgrep -f ${defaultApps.virtKeyboard}` ]; then - ${defaultApps.virtKeyboard} & - fi - ${lib.getExe pkgs.dconf} write /org/gnome/desktop/a11y/applications/screen-keyboard-enabled true - elif [ $STATE == "true" ]; then - ${lib.getExe pkgs.dconf} write /org/gnome/desktop/a11y/applications/screen-keyboard-enabled false - fi - ''; - - # Media/hardware commands - brightness = rec { - bin = lib.getExe' pkgs.swayosd "swayosd-client"; - up = "${bin} --brightness=raise"; - down = "${bin} --brightness=lower"; - }; - - volume = rec { - bin = lib.getExe' pkgs.swayosd "swayosd-client"; - up = "${bin} --output-volume=raise"; - down = "${bin} --output-volume=lower"; - mute = "${bin} --output-volume=mute-toggle"; - micMute = "${bin} --input-volume=mute-toggle"; - }; - - media = rec { - bin = lib.getExe pkgs.playerctl; - play = "${bin} play-pause"; - paus = "${bin} pause"; - next = "${bin} next"; - prev = "${bin} previous"; - }; - - idled = pkgs.writeShellScript "hyprland-idled" '' - ${lib.getExe pkgs.swayidle} -w \ - before-sleep '${media.paus}' \ - before-sleep '${defaultApps.lock}' \ - timeout 240 '${lib.getExe pkgs.brightnessctl} -s set 10' \ - resume '${lib.getExe pkgs.brightnessctl} -r' \ - timeout 300 '${defaultApps.lock}' \ - timeout 330 '${hyprctl} dispatch dpms off' \ - resume '${hyprctl} dispatch dpms on' \ - ${ - if cfg.desktop.hyprland.autoSuspend - then ''timeout 900 'sleep 2 && ${lib.getExe' pkgs.systemd "systemctl"} suspend' \'' - else ''\'' - } - ''; in '' ${ lib.strings.concatMapStringsSep "\n" diff --git a/homeManagerModules/desktop/hyprland/scripts.nix b/homeManagerModules/desktop/hyprland/scripts.nix new file mode 100644 index 00000000..8cf4af85 --- /dev/null +++ b/homeManagerModules/desktop/hyprland/scripts.nix @@ -0,0 +1,114 @@ +{ + config, + lib, + pkgs, + ... +}: let + cfg = config.ar.home; + hyprctl = lib.getExe' config.wayland.windowManager.hyprland.package "hyprctl"; + inherit (import ./vars.nix {inherit config lib pkgs;}) defaultApps media; +in { + clamshell = pkgs.writeShellScript "hyprland-clamshell" '' + NUM_MONITORS=$(${hyprctl} monitors all | grep Monitor | wc --lines) + if [ "$1" == "on" ]; then + if [ $NUM_MONITORS -gt 1 ]; then + ${hyprctl} keyword monitor "eDP-1, disable" + fi + elif [ "$1" == "off" ]; then + ${ + lib.strings.concatMapStringsSep "${hyprctl}\n" + (monitor: ''${hyprctl} keyword monitor "${monitor}"'') + cfg.desktop.hyprland.laptopMonitors + } + fi + ''; + + idleD = pkgs.writeShellScript "hyprland-idled" '' + ${lib.getExe pkgs.swayidle} -w \ + before-sleep '${media.paus}' \ + before-sleep '${defaultApps.lock}' \ + timeout 240 '${lib.getExe pkgs.brightnessctl} -s set 10' \ + resume '${lib.getExe pkgs.brightnessctl} -r' \ + timeout 300 '${defaultApps.lock}' \ + timeout 330 '${hyprctl} dispatch dpms off' \ + resume '${hyprctl} dispatch dpms on' \ + ${ + if cfg.desktop.hyprland.autoSuspend + then ''timeout 900 'sleep 2 && ${lib.getExe' pkgs.systemd "systemctl"} suspend' \'' + else ''\'' + } + ''; + + tablet = pkgs.writeShellScript "hyprland-tablet" '' + STATE=`${lib.getExe pkgs.dconf} read /org/gnome/desktop/a11y/applications/screen-keyboard-enabled` + + if [ $STATE -z ] || [ $STATE == "false" ]; then + if ! [ `pgrep -f ${defaultApps.virtKeyboard}` ]; then + ${defaultApps.virtKeyboard} & + fi + ${lib.getExe pkgs.dconf} write /org/gnome/desktop/a11y/applications/screen-keyboard-enabled true + elif [ $STATE == "true" ]; then + ${lib.getExe pkgs.dconf} write /org/gnome/desktop/a11y/applications/screen-keyboard-enabled false + fi + ''; + + wallpaperD = + if cfg.desktop.hyprland.randomWallpaper + then + pkgs.writers.writeRuby "hyprland-randomWallpaper" {} '' + require 'fileutils' + + directory = "${config.xdg.dataHome}/backgrounds/" + hyprctl = "${hyprctl}" + current_pids = {} + known_monitors = {} + last_update_time = {} + + update_interval = 900 # 15 minutes in seconds + + sleep 1 + + if Dir.exist?(directory) + loop do + outputs = IO.popen([hyprctl, 'monitors']).read + active_monitors = outputs.each_line.map { |line| line.split[1] if line.include?('Monitor') }.compact + + added_monitors = active_monitors - known_monitors.keys + removed_monitors = known_monitors.keys - active_monitors + + # Handle newly added monitors + added_monitors.each do |monitor| + random_background = Dir.glob(File.join(directory, '*.{png,jpg}')).sample + pid = spawn("/nix/store/azvrjs0k5ap90dcqw280xrbqlm6nkibv-swaybg-1.2.1/bin/swaybg", '-o', monitor, '-i', random_background, '-m', 'fill') + current_pids[monitor] = pid + last_update_time[monitor] = Time.now + known_monitors[monitor] = random_background + end + + # Remove wallpapers from removed monitors + removed_monitors.each do |monitor| + Process.kill('TERM', current_pids[monitor]) if current_pids[monitor] + current_pids.delete(monitor) + last_update_time.delete(monitor) + known_monitors.delete(monitor) + end + + # Update wallpapers after the set interval + active_monitors.each do |monitor| + if Time.now - last_update_time[monitor] >= update_interval + random_background = Dir.glob(File.join(directory, '*.{png,jpg}')).sample + pid = spawn("${lib.getExe pkgs.swaybg}", '-o', monitor, '-i', random_background, '-m', 'fill') + sleep 1 + Process.kill('TERM', current_pids[monitor]) if current_pids[monitor] + current_pids[monitor] = pid + last_update_time[monitor] = Time.now + known_monitors[monitor] = random_background + end + end + + sleep 5 # Check for monitor changes and update intervals every 5 seconds + end + end + '' + else "${lib.getExe pkgs.swaybg} -i ${cfg.theme.wallpaper}"; +} diff --git a/homeManagerModules/desktop/hyprland/vars.nix b/homeManagerModules/desktop/hyprland/vars.nix index 5cbf5580..20d5c327 100644 --- a/homeManagerModules/desktop/hyprland/vars.nix +++ b/homeManagerModules/desktop/hyprland/vars.nix @@ -1,4 +1,30 @@ { + config, + lib, + pkgs, + ... +}: let + cfg = config.ar.home; +in { + # Media/hardware commands + brightness = rec { + bin = lib.getExe' pkgs.swayosd "swayosd-client"; + up = "${bin} --brightness=raise"; + down = "${bin} --brightness=lower"; + }; + + # Default apps + defaultApps = { + browser = lib.getExe cfg.defaultApps.webBrowser; + editor = lib.getExe cfg.defaultApps.editor; + fileManager = lib.getExe cfg.defaultApps.fileManager; + launcher = lib.getExe pkgs.fuzzel; + lock = lib.getExe pkgs.swaylock; + logout = lib.getExe pkgs.wlogout; + terminal = lib.getExe cfg.defaultApps.terminal; + virtKeyboard = lib.getExe' pkgs.squeekboard "squeekboard"; + }; + defaultWorkspaces = [1 2 3 4 5 6 7 8 9]; layerRules = '' @@ -13,8 +39,31 @@ layerrule = ignorezero, swayosd ''; + media = rec { + bin = lib.getExe pkgs.playerctl; + play = "${bin} play-pause"; + paus = "${bin} pause"; + next = "${bin} next"; + prev = "${bin} previous"; + }; + modifier = "SUPER"; + screenshot = rec { + bin = lib.getExe pkgs.hyprshot; + folder = "${config.xdg.userDirs.pictures}/screenshots"; + screen = "${bin} -m output -o ${folder}"; + region = "${bin} -m region -o ${folder}"; + }; + + volume = rec { + bin = lib.getExe' pkgs.swayosd "swayosd-client"; + up = "${bin} --output-volume=raise"; + down = "${bin} --output-volume=lower"; + mute = "${bin} --output-volume=mute-toggle"; + micMute = "${bin} --input-volume=mute-toggle"; + }; + windowManagerBinds = { down = "d"; left = "l";