summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/fetchWallpaper.nix
blob: a3eddcc27b95fc7d68769a6a75361b8a13262da0 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.krebs.fetchWallpaper;

  out = {
    options.krebs.fetchWallpaper = api;
    config = mkIf cfg.enable imp;
  };

  api = {
    enable = mkEnableOption "fetch wallpaper";
    predicate = mkOption {
      type = with types; nullOr path;
      default = null;
    };
    url = mkOption {
      type = types.str;
    };
    timerConfig = mkOption {
      type = types.unspecified;
      default = {
        OnCalendar = "*:00,10,20,30,40,50";
      };
    };
    stateDir = mkOption {
      type = types.str;
      default = "/tmp/wallpaper";
    };
    display = mkOption {
      type = types.str;
      default = ":11";
    };
  };

  fetchWallpaperScript = pkgs.writeScript "fetchWallpaper" ''
    #! ${pkgs.bash}/bin/bash
    ${if (cfg.predicate == null) then "" else ''
      ${cfg.predicate}
      if [ $? -ne 0 ]; then
        echo "predicate failed"
        exit 23
      fi
    ''}
    mkdir -p ${shell.escape cfg.stateDir}
    curl -s -o ${shell.escape cfg.stateDir}/wallpaper -z ${shell.escape cfg.stateDir}/wallpaper ${shell.escape cfg.url}
    feh --no-fehbg --bg-scale ${shell.escape cfg.stateDir}/wallpaper
  '';

  imp = {
    users.extraUsers.fetchWallpaper = {
      name = "fetchWallpaper";
      uid = 3332383611; #genid fetchWallpaper
      description = "fetchWallpaper user";
      home = "/var/empty";
    };

    systemd.timers.fetchWallpaper = {
      description = "fetch wallpaper timer";
      wantedBy = [ "timers.target" ];

      timerConfig = cfg.timerConfig;
    };
    systemd.services.fetchWallpaper = {
      description = "fetch wallpaper";
      after = [ "network.target" ];

      path = with pkgs; [
        curl
        feh
      ];

      environment = {
        URL = cfg.url;
        DISPLAY = cfg.display;
      };

      restartIfChanged = true;

      serviceConfig = {
        Type = "simple";
        ExecStart = fetchWallpaperScript;
        User = "fetchWallpaper";
      };
    };
  };
in out