blob: 5203cffe74e8dda6db5dcb02e5a953c5d3fa7929 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
{ 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" ];
};
};
}
|