From 08297376a85a1719518507e54fca9de954d2376a Mon Sep 17 00:00:00 2001 From: Petri Hienonen Date: Thu, 23 May 2024 13:56:00 +0300 Subject: Agenix configuration --- home/wallpapers/default.nix | 87 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 home/wallpapers/default.nix (limited to 'home/wallpapers/default.nix') diff --git a/home/wallpapers/default.nix b/home/wallpapers/default.nix new file mode 100644 index 0000000..458f711 --- /dev/null +++ b/home/wallpapers/default.nix @@ -0,0 +1,87 @@ +{ config, pkgs, ... }: +let + wallpaperScript = pkgs.writeShellScript "update-wallpaper.sh" '' + set -euo pipefail + + if [ -z "''${UNSPLASH_ACCESS_KEY:-}" ]; then + echo "Error: UNSPLASH_ACCESS_KEY is not set" + 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"; + After = [ "graphical-session.target" ]; + }; + + Service = { + Environment = [ + "UNSPLASH_ACCESS_KEY=$(${pkgs.coreutils}/bin/cat ${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"; + }; + Timer = { + OnBootSec = "2min"; + OnUnitActiveSec = "3h"; + Persistent = true; + }; + Install = { + WantedBy = [ "timers.target" ]; + }; + }; +} -- cgit v1.2.3-70-g09d2