This commit is contained in:
TheCrazyInsanity
2026-05-08 12:41:49 -04:00
parent 998200233c
commit fbbef69fa1
2 changed files with 89 additions and 59 deletions

View File

@@ -61,6 +61,7 @@ in
pkgs.dracula-theme # gtk theme
pkgs.brightnessctl
configure-gtk
pkgs.mpvpaper
];
};

View File

@@ -141,72 +141,101 @@
}
];
};
extraConfig = ''
exec_always swaymsg output "*" bg ~/.background-image fit
'';
};
# 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";
Description = "Universal Wallpaper Rotator (Images, GIFs, Videos)";
After = [ "graphical-session.target" ];
PartOf = [ "graphical-session.target" ];
};
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" fit
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" ];
WantedBy = [ "graphical-session.target" ];
};
Service = {
Type = "simple";
Restart = "on-failure";
RestartSec = 5;
ExecStart = "${pkgs.writeShellScript "wallpaper-rotator" ''
WALLPAPER_DIR="$HOME/wallpapers"
INTERVAL=600 # Time in seconds between wallpaper changes (600 = 10 mins)
# 1. Safely find Wayland and Sway sockets
if [ -z "$WAYLAND_DISPLAY" ]; then
shopt -s nullglob
W_SOCKETS=("$XDG_RUNTIME_DIR"/wayland-[0-9]*)
if [ ''${#W_SOCKETS[@]} -gt 0 ]; then
export WAYLAND_DISPLAY=$(basename "''${W_SOCKETS[0]}")
fi
fi
if [ -z "$SWAYSOCK" ]; then
shopt -s nullglob
S_SOCKETS=("$XDG_RUNTIME_DIR"/sway-ipc.*.sock)
if [ ''${#S_SOCKETS[@]} -gt 0 ]; then
export SWAYSOCK="''${S_SOCKETS[0]}"
fi
fi
# State tracking for mpvpaper
MPV_PID=""
# Clean up mpvpaper gracefully if systemd stops the service
cleanup() {
if [ -n "$MPV_PID" ]; then
kill $MPV_PID 2>/dev/null || true
fi
exit 0
}
trap cleanup SIGINT SIGTERM
# 2. Infinite rotation loop
while true; do
if [ ! -d "$WALLPAPER_DIR" ]; then
echo "Wallpaper directory not found. Waiting..."
${pkgs.coreutils}/bin/sleep 60
continue
fi
# Find images, gifs, and videos
MEDIA=$(${pkgs.findutils}/bin/find "$WALLPAPER_DIR" -type f \( \
-iname \*.jpg -o -iname \*.jpeg -o -iname \*.png -o \
-iname \*.mp4 -o -iname \*.webm -o -iname \*.mkv -o -iname \*.gif \
\) 2>/dev/null | ${pkgs.coreutils}/bin/shuf -n 1 || true)
if [ -n "$MEDIA" ]; then
echo "Selected: $MEDIA"
EXT="''${MEDIA##*.}"
EXT=$(echo "$EXT" | tr '[:upper:]' '[:lower:]') # Convert to lowercase
# If it's a static image, use Sway directly (saves battery/RAM)
if [[ "$EXT" == "jpg" || "$EXT" == "jpeg" || "$EXT" == "png" ]]; then
if [ -n "$MPV_PID" ]; then
kill $MPV_PID 2>/dev/null || true
MPV_PID=""
fi
${pkgs.sway}/bin/swaymsg output "*" bg "$MEDIA" fill
# If it's a video/gif, use mpvpaper
else
if [ -n "$MPV_PID" ]; then
kill $MPV_PID 2>/dev/null || true
fi
# Start mpvpaper in the background and record its process ID
${pkgs.mpvpaper}/bin/mpvpaper -o "loop no-audio" "*" "$MEDIA" &
MPV_PID=$!
fi
else
echo "No media found in $WALLPAPER_DIR."
fi
# Wait for the next rotation
${pkgs.coreutils}/bin/sleep $INTERVAL
done
''}";
};
};