modules/darwin/random-wallpaper.nix

ce371efd802e63b08c16a43735f539df0d324c18 · 1.5 KB · 45 lines raw

1 { config, lib, pkgs, ... }:
2 let
3 cfg = config.local.randomWallpaper;
4 script = pkgs.writeShellApplication {
5 name = "set-random-wallpaper";
6 runtimeInputs = [ pkgs.coreutils pkgs.findutils ];
7 text = ''
8 set -euo pipefail
9 img=$(find "${cfg.directory}" \
10 -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) \
11 | shuf -n1)
12 [ -n "$img" ] || exit 0
13 #/usr/bin/osascript -e "tell application \"System Events\" to tell every desktop to set picture to \"$img\""
14 #desktoppr "$img"
15 URL="file://$img"
16 PLIST="${config.users.users.${config.system.primaryUser}.home}/Library/Application Support/com.apple.wallpaper/Store/Index.plist"
17 /usr/libexec/PlistBuddy -c "set AllSpacesAndDisplays:Desktop:Content:Choices:0:Files:0:relative $URL" "$PLIST"
18 /usr/bin/killall WallpaperAgent 2>/dev/null || true
19 '';
20 };
21 in
22 {
23 options.local.randomWallpaper = {
24 enable = lib.mkEnableOption "Random Rotating Wallpaper";
25 directory = lib.mkOption {
26 type = lib.types.str;
27 description = "Folder containing images";
28 };
29 interval = lib.mkOption {
30 type = lib.types.int;
31 default = 1800;
32 description = "Seconds between wallpaper changes";
33 };
34 };
35
36 config = lib.mkIf cfg.enable {
37 launchd.user.agents.random-wallpaper.serviceConfig = {
38 Label = "org.rschanz.wallpaperDaemon";
39 ProgramArguments = [ "${script}/bin/set-random-wallpaper" ];
40 StartInterval = cfg.interval;
41 RunAtLoad = true;
42 ProcessType = "Background";
43 };
44 };
45 }