stockholm/krebs/3modules/fetchWallpaper.nix

98 lines
2.3 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";
};
};
stateDir = mkOption {
type = types.str;
default = "/var/lib/wallpaper";
2015-12-12 16:50:33 +00:00
};
display = mkOption {
type = types.int;
default = config.services.xserver.display;
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
mkdir -p ${cfg.stateDir}
chmod o+rx ${cfg.stateDir}
cd ${cfg.stateDir}
(curl --max-time ${toString cfg.maxTime} -s -o wallpaper.tmp -z wallpaper.tmp ${shell.escape cfg.url} && cp wallpaper.tmp wallpaper) || :
feh --no-fehbg --bg-scale ${shell.escape cfg.stateDir}/wallpaper
2015-12-12 16:50:33 +00:00
'';
imp = {
users.users.fetchWallpaper = {
name = "fetchWallpaper";
uid = genid "fetchWallpaper";
description = "fetchWallpaper user";
home = cfg.stateDir;
createHome = true;
};
systemd.timers.fetchWallpaper = {
2015-12-12 16:50:33 +00:00
description = "fetch wallpaper timer";
wantedBy = [ "timers.target" ];
timerConfig = cfg.timerConfig;
};
systemd.services.fetchWallpaper = {
2015-12-12 16:50:33 +00:00
description = "fetch wallpaper";
after = [ "network.target" ];
2015-12-12 16:50:33 +00:00
path = with pkgs; [
curl
feh
];
environment = {
URL = cfg.url;
DISPLAY = ":${toString cfg.display}";
2015-12-12 16:50:33 +00:00
};
restartIfChanged = true;
serviceConfig = {
Type = "simple";
ExecStart = fetchWallpaperScript;
User = "fetchWallpaper";
2015-12-12 16:50:33 +00:00
};
unitConfig = cfg.unitConfig;
2015-12-12 16:50:33 +00:00
};
};
in out