{ config, pkgs, ... }: let wallpaperScript = pkgs.writeShellScript "update-wallpaper.sh" '' set -euo pipefail CREDENTIAL_FILE="$CREDENTIALS_DIRECTORY/login_token" UNSPLASH_ACCESS_KEY=$("${pkgs.coreutils}/bin/cat" "$CREDENTIAL_FILE") if [ -z "$UNSPLASH_ACCESS_KEY" ]; then echo "Error: UNSPLASH_ACCESS_KEY is empty" exit 1 fi WALLPAPER_DIR="$HOME/.local/share/wallpapers" "${pkgs.coreutils}/bin/mkdir" -p "$WALLPAPER_DIR" TMPFILE="$("${pkgs.coreutils}/bin/mktemp")" echo "Fetching random Unsplash image URL..." URL=$("${pkgs.curl}/bin/curl" -s \ "https://api.unsplash.com/photos/random?query=nature,landscape&orientation=landscape&client_id=$UNSPLASH_ACCESS_KEY" \ | "${pkgs.jq}/bin/jq" -r '.urls.raw') if [ -z "$URL" ] || [ "$URL" = "null" ]; then echo "Error: could not retrieve Unsplash URL" exit 1 fi EXT=$("${pkgs.coreutils}/bin/basename" "$URL" | "${pkgs.gawk}/bin/awk" -F. '{print tolower($NF)}') [[ "$EXT" =~ ^(jpg|jpeg|png)$ ]] || EXT="jpg" TMPFILE_EXT="$TMPFILE.$EXT" OUTFILE="$WALLPAPER_DIR/$("${pkgs.coreutils}/bin/date" +%Y%m%d-%H%M%S).webp" echo "Downloading original Unsplash image..." "${pkgs.curl}/bin/curl" -L --fail --retry 3 -o "$TMPFILE_EXT" "$URL" echo "Converting to WebP..." "${pkgs.libwebp}/bin/cwebp" -mt -preset photo -hint photo -metadata all -pass 10 -quiet -q 80 -m 6 "$TMPFILE_EXT" -o "$OUTFILE" "${pkgs.coreutils}/bin/rm" -f "$TMPFILE_EXT" echo "Selecting a wallpaper different from current..." CURRENT_WALL=$("${pkgs.hyprland}/bin/hyprctl" hyprpaper listloaded | "${pkgs.gawk}/bin/awk" '{print $NF}' || true) NEW_WALL=$("${pkgs.findutils}/bin/find" "$WALLPAPER_DIR" -type f ! -name "$("${pkgs.coreutils}/bin/basename" "$CURRENT_WALL")" | "${pkgs.coreutils}/bin/shuf" -n 1) echo "Reloading hyprpaper with: $NEW_WALL" "${pkgs.hyprland}/bin/hyprctl" hyprpaper reload ,"$NEW_WALL" ''; in { # Ensure required packages are available home.packages = with pkgs; [ coreutils curl findutils hyprland jq libwebp ]; systemd.user.services."wallpaper-fetch" = { Unit = { Description = "Fetch and update 4K nature wallpaper for Hyprpaper"; }; Service = { LoadCredential = [ "login_token:${config.age.secrets.unsplash_access_key.path}" ]; Type = "oneshot"; ExecStart = "${wallpaperScript}"; }; }; # Timer to run periodically (every 3 hours) systemd.user.timers."wallpaper-fetch" = { Unit = { Description = "Periodic Unsplash wallpaper fetch timer"; After = [ "graphical-session.target" ]; Wants = [ "network-online.target" ]; }; Timer = { OnBootSec = "2min"; OnUnitActiveSec = "3h"; Persistent = true; }; Install = { WantedBy = [ "timers.target" ]; }; }; }