From 43020e5b51d814503de1e94ca23e83d489fc372e Mon Sep 17 00:00:00 2001 From: TheCrazyInsanity Date: Wed, 15 Apr 2026 12:40:34 -0400 Subject: [PATCH] random bg (ty gemini) --- machines/latitude/hardware-configuration.nix | 2 +- modules/sway/home.nix | 70 +++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/machines/latitude/hardware-configuration.nix b/machines/latitude/hardware-configuration.nix index 7569dd3..e90be79 100755 --- a/machines/latitude/hardware-configuration.nix +++ b/machines/latitude/hardware-configuration.nix @@ -63,7 +63,7 @@ # Define time delay for hibernation systemd.sleep.settings.Sleep = { - HibernateDelaySec = 900; + HibernateDelaySec = 300; }; # facial recognition diff --git a/modules/sway/home.nix b/modules/sway/home.nix index f682a2b..a0a4e9f 100644 --- a/modules/sway/home.nix +++ b/modules/sway/home.nix @@ -114,7 +114,7 @@ }; }; startup = [ - { command = "swaybg -i ~/.background-image -m fill"; } + # { command = "swaybg -i ~/.background-image -m fill"; } { command = "discord --enable-features=UseOzonePlatform --ozone-platform=wayland"; } { command = "trilium"; } { command = "jami"; } @@ -140,6 +140,73 @@ } ]; }; + extraConfig = '' + exec_always swaymsg output "*" bg ~/.background-image fill + ''; + }; + + # yeah this was written by ai if it wasn't obvious + systemd.user.services.random-background = { + Unit = { + Description = "Update ~/.background_image with a random wallpaper"; + }; + Service = { + Type = "oneshot"; + # pkgs.writeShellScript creates an executable script in the nix store + ExecStart = "${pkgs.writeShellScript "random-bg-script" '' + # Nix uses 'set -e' by default, meaning any command failure kills the script. + # We use native bash features to safely handle finding the image and socket. + + WALLPAPER_DIR="$HOME/wallpapers" + TARGET="$HOME/.background_image" + + if [ ! -d "$WALLPAPER_DIR" ]; then + echo "Wallpaper directory not found." + exit 0 + fi + + # 1. Get a random image safely (|| true prevents set -e crashes if find returns nothing) + RANDOM_IMG=$(${pkgs.findutils}/bin/find "$WALLPAPER_DIR" -type f \( -iname \*.jpg -o -iname \*.png -o -iname \*.jpeg \) 2>/dev/null | ${pkgs.coreutils}/bin/shuf -n 1 || true) + + if [ -n "$RANDOM_IMG" ]; then + # 2. Update symlink + ${pkgs.coreutils}/bin/ln -sf "$RANDOM_IMG" "$TARGET" + + # 3. Safely find the socket using native Bash globbing. + # systemd user services natively provide the correct $XDG_RUNTIME_DIR (/run/user/1000) + shopt -s nullglob + SOCKETS=("$XDG_RUNTIME_DIR"/sway-ipc.*.sock) + + # 4. Check if the bash array found any socket files + if [ ''${#SOCKETS[@]} -gt 0 ]; then + export SWAYSOCK="''${SOCKETS[0]}" + echo "Using socket: $SWAYSOCK" + + # 5. Execute swaymsg + ${pkgs.sway}/bin/swaymsg output "*" bg "$RANDOM_IMG" fill + echo "Updated background to $RANDOM_IMG" + else + echo "Error: Could not find any sway socket in $XDG_RUNTIME_DIR" + exit 1 + fi + else + echo "No images found in $WALLPAPER_DIR" + fi + ''}"; + }; + }; + + systemd.user.timers.random-background = { + Unit = { + Description = "Run random-background service every hour"; + }; + Timer = { + OnCalendar = "minutely"; + Persistent = true; + }; + Install = { + WantedBy = [ "timers.target" ]; + }; }; programs.waybar = { @@ -237,4 +304,5 @@ QT_STYLE_OVERRIDE = "org.kde.breezedark.desktop"; GTK_THEME = "Materia-dark-compact"; }; + }