summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/fetchWallpaper.nix
blob: 3d6a285c3fe835ab678349f68c32b58d5239302a (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
90
{ config, lib, pkgs, ... }:

with import <stockholm/lib>;

let
  cfg = config.krebs.fetchWallpaper;

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

  api = {
    enable = mkEnableOption "fetch wallpaper";
    url = mkOption {
      type = types.str;
    };
    timerConfig = mkOption {
      type = types.unspecified;
      default = {
        OnCalendar = "*:00,10,20,30,40,50";
      };
    };
    # TODO find a better default stateDir
    stateDir = mkOption {
      type = types.str;
      default = "$HOME/wallpaper";
    };
    display = mkOption {
      type = types.str;
      default = ":0";
    };
    unitConfig = mkOption {
      type = types.attrsOf types.str;
      description = "Extra unit configuration for fetchWallpaper to define conditions and assertions for the unit";
      example = literalExample ''
        # do not start when running on umts
        { ConditionPathExists = "!/var/run/ppp0.pid"; }
      '';
      default = {};
    };
    maxTime = mkOption {
      type = types.int;
      default = 0;
      description = "Time to wait before download is aborted";
    };
  };

  fetchWallpaperScript = pkgs.writeDash "fetchWallpaper" ''
    set -euf

    mkdir -p ${cfg.stateDir}
    cd ${cfg.stateDir}
    (curl --max-time ${toString cfg.maxTime} -s -o wallpaper.tmp -z wallpaper ${shell.escape cfg.url} && mv wallpaper.tmp wallpaper) || :
    feh --no-fehbg --bg-scale wallpaper
  '';

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

      timerConfig = cfg.timerConfig;
    };
    systemd.user.services.fetchWallpaper = {
      description = "fetch wallpaper";
      after = [ "graphical.target" ];
      wants = [ "graphical.target" ];
      wantedBy = [ "default.target" ];

      path = with pkgs; [
        curl
        feh
        coreutils
      ];

      environment = {
        DISPLAY = cfg.display;
      };
      restartIfChanged = true;

      serviceConfig = {
        Type = "simple";
        ExecStart = fetchWallpaperScript;
      };

      unitConfig = cfg.unitConfig;
    };
  };
in out