mbp
This commit is contained in:
173
modules/nixos/wallpaper/default.nix
Normal file
173
modules/nixos/wallpaper/default.nix
Normal file
@@ -0,0 +1,173 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
namespace,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.${namespace}.wallpaper;
|
||||
|
||||
wallpaper-command = if cfg.source == "nasa" then "nasa-wallpaper" else "bing-wallpaper";
|
||||
|
||||
# System activation script to ensure wallpaper is available early in boot
|
||||
wallpaper-activation-script = ''
|
||||
# Create wallpaper directory if it doesn't exist
|
||||
mkdir -p ${cfg.dir}
|
||||
|
||||
# Copy default wallpaper as fallback if it doesn't exist
|
||||
if [ ! -f ${cfg.dir}/fallback.jpg ]; then
|
||||
cp ${cfg.defaultWallpaper} ${cfg.dir}/fallback.jpg
|
||||
fi
|
||||
|
||||
# If no current wallpaper exists, use the fallback
|
||||
if [ ! -f ${cfg.dir}/current.jpg ]; then
|
||||
cp ${cfg.dir}/fallback.jpg ${cfg.dir}/current.jpg
|
||||
fi
|
||||
|
||||
# Create symlink for Plymouth and display manager
|
||||
ln -sf ${cfg.dir}/current.jpg /run/wallpaper.jpg
|
||||
'';
|
||||
|
||||
bing-wallpaper = pkgs.writeScriptBin "bing-wallpaper" ''
|
||||
# Directory to store wallpapers
|
||||
WALLPAPER_DIR="${cfg.dir}"
|
||||
IMG_PATH="$WALLPAPER_DIR/current.jpg"
|
||||
FALLBACK_PATH="$WALLPAPER_DIR/fallback.jpg"
|
||||
|
||||
# Ensure directory exists
|
||||
mkdir -p "$WALLPAPER_DIR"
|
||||
|
||||
# Update symlink
|
||||
ln -sf "$IMG_PATH" /run/wallpaper.jpg
|
||||
|
||||
# Try to download new wallpaper
|
||||
if ${lib.getExe pkgs.curl} -s --connect-timeout 5 --max-time 10 "https://www.bing.com" > /dev/null; then
|
||||
URL=$(${lib.getExe pkgs.curl} -s "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1" | \
|
||||
${lib.getExe pkgs.jq} -r '.images[0].url')
|
||||
FULL_URL="https://www.bing.com$URL"
|
||||
if ${lib.getExe pkgs.curl} -s -o "$IMG_PATH.tmp" "$FULL_URL"; then
|
||||
mv "$IMG_PATH.tmp" "$IMG_PATH"
|
||||
echo "Downloaded $FULL_URL to $IMG_PATH successfully"
|
||||
else
|
||||
echo "Failed to download Bing wallpaper, using previous or fallback"
|
||||
if [ ! -f "$IMG_PATH" ] && [ -f "$FALLBACK_PATH" ]; then
|
||||
cp "$FALLBACK_PATH" "$IMG_PATH"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo "Network unavailable, using previous or fallback wallpaper"
|
||||
if [ ! -f "$IMG_PATH" ] && [ -f "$FALLBACK_PATH" ]; then
|
||||
cp "$FALLBACK_PATH" "$IMG_PATH"
|
||||
fi
|
||||
fi
|
||||
'';
|
||||
|
||||
nasa-wallpaper = pkgs.writeScriptBin "nasa-wallpaper" ''
|
||||
# Directory to store wallpapers
|
||||
WALLPAPER_DIR="${cfg.dir}"
|
||||
IMG_PATH="$WALLPAPER_DIR/current.jpg"
|
||||
FALLBACK_PATH="$WALLPAPER_DIR/fallback.jpg"
|
||||
|
||||
# Ensure directory exists
|
||||
mkdir -p "$WALLPAPER_DIR"
|
||||
|
||||
# Update symlink
|
||||
ln -sf "$IMG_PATH" /run/wallpaper.jpg
|
||||
|
||||
# Try to download new wallpaper
|
||||
if ${lib.getExe pkgs.curl} -s --connect-timeout 5 --max-time 10 "https://api.nasa.gov" > /dev/null; then
|
||||
APOD_URL="https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"
|
||||
IMAGE_URL=$(${lib.getExe pkgs.curl} -s "$APOD_URL" | ${lib.getExe pkgs.jq} -r '.hdurl // .url')
|
||||
if ${lib.getExe pkgs.curl} -s -o "$IMG_PATH.tmp" "$IMAGE_URL"; then
|
||||
mv "$IMG_PATH.tmp" "$IMG_PATH"
|
||||
echo "Downloaded $IMAGE_URL to $IMG_PATH successfully"
|
||||
else
|
||||
echo "Failed to download NASA wallpaper, using previous or fallback"
|
||||
if [ ! -f "$IMG_PATH" ] && [ -f "$FALLBACK_PATH" ]; then
|
||||
cp "$FALLBACK_PATH" "$IMG_PATH"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo "Network unavailable, using previous or fallback wallpaper"
|
||||
if [ ! -f "$IMG_PATH" ] && [ -f "$FALLBACK_PATH" ]; then
|
||||
cp "$FALLBACK_PATH" "$IMG_PATH"
|
||||
fi
|
||||
fi
|
||||
'';
|
||||
|
||||
reloadScript = lib.optionalString (cfg.reloadCommand != null) cfg.reloadCommand;
|
||||
in
|
||||
{
|
||||
imports = [ ./options.nix ];
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
environment.systemPackages = [
|
||||
bing-wallpaper
|
||||
nasa-wallpaper
|
||||
];
|
||||
|
||||
# Ensure wallpaper directory and symlink exist before the display manager starts
|
||||
system.activationScripts.wallpaper = wallpaper-activation-script;
|
||||
|
||||
systemd = {
|
||||
services = {
|
||||
# System-level: download on boot after network is available
|
||||
preload-wallpaper = {
|
||||
enable = true;
|
||||
wants = [ "network-online.target" ];
|
||||
after = [ "network-online.target" ];
|
||||
path = [
|
||||
pkgs.bash
|
||||
pkgs.jq
|
||||
pkgs.curl
|
||||
bing-wallpaper
|
||||
nasa-wallpaper
|
||||
];
|
||||
script = ''
|
||||
${wallpaper-command}
|
||||
'';
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
TimeoutSec = "10s";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
user = {
|
||||
services = {
|
||||
# User-level: daily refresh + optional DE hot-reload
|
||||
reload-wallpaper = {
|
||||
enable = true;
|
||||
path = [
|
||||
pkgs.bash
|
||||
pkgs.jq
|
||||
pkgs.curl
|
||||
bing-wallpaper
|
||||
nasa-wallpaper
|
||||
];
|
||||
script = ''
|
||||
${wallpaper-command}
|
||||
${reloadScript}
|
||||
'';
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
timers = {
|
||||
reload-wallpaper = {
|
||||
description = "Timer for reload-wallpaper";
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = {
|
||||
OnCalendar = "daily";
|
||||
Persistent = true;
|
||||
Unit = "reload-wallpaper.service";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user