{ config, lib, pkgs, ... }: let cfg = config.local.randomWallpaper; script = pkgs.writeShellApplication { name = "set-random-wallpaper"; runtimeInputs = [ pkgs.coreutils pkgs.findutils ]; text = '' set -euo pipefail img=$(find "${cfg.directory}" \ -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) \ | shuf -n1) [ -n "$img" ] || exit 0 #/usr/bin/osascript -e "tell application \"System Events\" to tell every desktop to set picture to \"$img\"" #desktoppr "$img" URL="file://$img" PLIST="${config.users.users.${config.system.primaryUser}.home}/Library/Application Support/com.apple.wallpaper/Store/Index.plist" /usr/libexec/PlistBuddy -c "set AllSpacesAndDisplays:Desktop:Content:Choices:0:Files:0:relative $URL" "$PLIST" /usr/bin/killall WallpaperAgent 2>/dev/null || true ''; }; in { options.local.randomWallpaper = { enable = lib.mkEnableOption "Random Rotating Wallpaper"; directory = lib.mkOption { type = lib.types.str; description = "Folder containing images"; }; interval = lib.mkOption { type = lib.types.int; default = 1800; description = "Seconds between wallpaper changes"; }; }; config = lib.mkIf cfg.enable { launchd.user.agents.random-wallpaper.serviceConfig = { Label = "org.rschanz.wallpaperDaemon"; ProgramArguments = [ "${script}/bin/set-random-wallpaper" ]; StartInterval = cfg.interval; RunAtLoad = true; ProcessType = "Background"; }; }; }