modules/darwin/random-wallpaper.nix
b33f5b96e8b659a28e29d7df3fa63669b1e7b316
· 1.2 KB · 40 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 | ''; |
| 15 | }; |
| 16 | in |
| 17 | { |
| 18 | options.local.randomWallpaper = { |
| 19 | enable = lib.mkEnableOption "Random Rotating Wallpaper"; |
| 20 | directory = lib.mkOption { |
| 21 | type = lib.types.str; |
| 22 | description = "Folder containing images"; |
| 23 | }; |
| 24 | interval = lib.mkOption { |
| 25 | type = lib.types.int; |
| 26 | default = 1800; |
| 27 | description = "Seconds between wallpaper changes"; |
| 28 | }; |
| 29 | }; |
| 30 | |
| 31 | config = lib.mkIf cfg.enable { |
| 32 | launchd.user.agents.random-wallpaper.serviceConfig = { |
| 33 | Label = "org.rschanz.wallpaperDaemon"; |
| 34 | ProgramArguments = [ "${script}/bin/set-random-wallpaper" ]; |
| 35 | StartInterval = cfg.interval; |
| 36 | RunAtLoad = true; |
| 37 | ProcessType = "Background"; |
| 38 | }; |
| 39 | }; |
| 40 | } |