summaryrefslogtreecommitdiff
path: root/modules/darwin/random-wallpaper.nix
blob: 6b17c0f1ca99644069540769ab2ebe14650a9932 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{ 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";
    };
  };
}