nix-configs/modules/desktop/wpaperd.nix
2025-07-29 03:02:25 +02:00

76 lines
2.5 KiB
Nix

{
pkgs,
config,
lib,
...
}:
let
cfg = config.xyno.desktop.wpaperd;
# wpaperdConf = pkgs.writeText "wpaperd.conf" ''
# [default]
# path = "/home/${config.xyno.system.user.name}/Pictures/backgrounds"
# duration = "10m"
# sorting = "random"
# mode = "center"
# '';
in
{
options.xyno.desktop.wpaperd.enable = lib.mkEnableOption "enable wpaperd wallpaper daemon";
options.xyno.desktop.wpaperd.wantedBy = lib.mkOption {
type = lib.types.str;
default = "niri.service";
};
options.xyno.desktop.wpaperd.package = lib.mkOption {
type = lib.types.package;
default = pkgs.swww;
};
config = lib.mkIf cfg.enable {
environment.systemPackages = [ cfg.package ];
systemd.user.services.swww-daemon = {
unitConfig.PartOf = "graphical-session.target";
unitConfig.After = "graphical-session.target";
unitConfig.Requisite = "graphical-session.target";
serviceConfig.Restart = "on-failure";
wantedBy = [ cfg.wantedBy ];
script = ''
exec ${cfg.package}/bin/swww-daemon'';
};
systemd.user.services.swww-randomize = {
unitConfig.PartOf = "graphical-session.target";
unitConfig.After = "graphical-session.target";
unitConfig.Requisite = "graphical-session.target";
serviceConfig.Restart = "on-failure";
wantedBy = [ "swww-daemon.service" ];
script = ''
export DEFAULT_INTERVAL=300 # In seconds
export DIR=''$HOME/Pictures/backgrounds
# See swww-img(1)
RESIZE_TYPE="crop"
export SWWW_TRANSITION_FPS="120"
export SWWW_TRANSITION="fade"
export SWWW_TRANSITION_DURATION="1"
# export SWWW_TRANSITION_STEP="90"
while true; do
find "''$DIR" -type f \
| while read -r img; do
echo "''$(</dev/urandom tr -dc a-zA-Z0-9 | head -c 8):''$img"
done \
| sort -n | cut -d':' -f2- \
| while read -r img; do
for d in ''$(${cfg.package}/bin/swww query | grep -Po "^[^:]+"); do # see ${cfg.package}/bin/swww-query(1)
# Get next random image for this display, or re-shuffle images
# and pick again if no more unused images are remaining
[ -z "''$img" ] && if read -r img; then true; else break 2; fi
${cfg.package}/bin/swww img --resize "''$RESIZE_TYPE" --outputs "''$d" "''$img"
unset -v img # Each image should only be used once per loop
done
sleep "''${DEFAULT_INTERVAL}"
done
done
'';
# restartTriggers = [wpaperdConf];
};
};
}