{ 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\"" ''; }; 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"; }; }; }