stockholm/krebs/3modules/fetchWallpaper.nix

91 lines
2.2 KiB
Nix
Raw Normal View History

2015-12-12 16:50:33 +00:00
{ config, lib, pkgs, ... }:
2016-10-20 18:54:38 +00:00
with import <stockholm/lib>;
2015-12-12 16:50:33 +00:00
let
cfg = config.krebs.fetchWallpaper;
2015-12-12 16:50:33 +00:00
out = {
options.krebs.fetchWallpaper = api;
2016-02-14 15:43:44 +00:00
config = lib.mkIf cfg.enable imp;
2015-12-12 16:50:33 +00:00
};
api = {
enable = mkEnableOption "fetch wallpaper";
url = mkOption {
type = types.str;
};
timerConfig = mkOption {
type = types.unspecified;
default = {
OnCalendar = "*:00,10,20,30,40,50";
};
};
2017-02-15 23:04:08 +00:00
# TODO find a better default stateDir
2015-12-12 16:50:33 +00:00
stateDir = mkOption {
type = types.str;
2017-02-15 23:04:08 +00:00
default = "./wallpaper";
2015-12-12 16:50:33 +00:00
};
display = mkOption {
type = types.str;
2017-02-15 23:04:08 +00:00
default = ":0";
2015-12-12 16:50:33 +00:00
};
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 = {};
};
2017-01-26 22:59:26 +00:00
maxTime = mkOption {
type = types.int;
default = 0;
description = "Time to wait before download is aborted";
};
2015-12-12 16:50:33 +00:00
};
2016-04-26 23:31:26 +00:00
fetchWallpaperScript = pkgs.writeDash "fetchWallpaper" ''
set -euf
2015-12-12 16:50:33 +00:00
mkdir -p ${shell.escape cfg.stateDir}
cd ${shell.escape cfg.stateDir}
2017-01-26 22:59:26 +00:00
(curl --max-time ${toString cfg.maxTime} -s -o wallpaper.tmp -z wallpaper ${shell.escape cfg.url} && mv wallpaper.tmp wallpaper) || :
2017-02-15 23:04:08 +00:00
feh --no-fehbg --bg-scale wallpaper
2015-12-12 16:50:33 +00:00
'';
imp = {
2017-02-15 23:04:08 +00:00
systemd.user.timers.fetchWallpaper = {
2015-12-12 16:50:33 +00:00
description = "fetch wallpaper timer";
wantedBy = [ "timers.target" ];
timerConfig = cfg.timerConfig;
};
2017-02-15 23:04:08 +00:00
systemd.user.services.fetchWallpaper = {
2015-12-12 16:50:33 +00:00
description = "fetch wallpaper";
2017-02-15 23:04:08 +00:00
after = [ "network.target" "graphical.target" ];
wants = [ "graphical.target" ];
wantedBy = [ "default.target" ];
2015-12-12 16:50:33 +00:00
path = with pkgs; [
curl
feh
2017-02-15 23:04:08 +00:00
coreutils
2015-12-12 16:50:33 +00:00
];
environment = {
DISPLAY = cfg.display;
};
restartIfChanged = true;
serviceConfig = {
Type = "simple";
ExecStart = fetchWallpaperScript;
};
unitConfig = cfg.unitConfig;
2015-12-12 16:50:33 +00:00
};
};
in out