Systemd and variety

I’m trying to set a slideshow via home manager using variety. But it seems that systemd isnt picking up the executable:

{pkgs,config,...}:
{
  systemd.user.services = {
    wallpaper-slideshow = {
      Unit = {
        Description = "Sets up the wallpaper slideshow";
      };
      Install = {
        # WantedBy = [ "multi-user.target" ];
        WantedBy = [ "default.target" ];
      };
      Service = {
        Type = "exec";
        ExecStart = "${pkgs.writeShellApplication 
          { name = "wallpaper-slideshow"; 
            runtimeInputs = [pkgs.variety pkgs.libappindicator ]; 
            text = builtins.readFile ./scripts/wallpaper.sh;}
        }/bin/wallpaper-slideshow";
        Restart = "no";
        Environment = [
          "BACKGROUNDS=${config.home.sessionVariables.BACKGROUNDS}"
        ];
      };
      
    };
  };
}

The contents of wallpaper.sh is as follows:

#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash
while true
do
  for filename in "$BACKGROUNDS"/*; do 
    echo "$filename"
    variety --set "$filename"
    sleep 10s
  done
done

After switching, restarting the service, and doing systemctl --user status wallpaper-slideshow.service I get the following logs:

dic 31 01:21:22 nixos systemd[1368]: Starting Sets up the wallpaper slideshow...
dic 31 01:21:22 nixos systemd[1368]: Started Sets up the wallpaper slideshow.
dic 31 01:21:22 nixos wallpaper-slideshow[63486]: /home/dan/.config/home-manager/rio/background_pics/cg01_07A.png
dic 31 01:21:23 nixos wallpaper-slideshow[63488]: WARNING: 2024-12-31 01:21:23,197: create_menu() 'Variety Slideshow is not installed. This is an optional extension adding pan-and-zoom slideshows to Variety: see https://github.>
dic 31 01:21:23 nixos .variety-wrappe[63488]: gtk_widget_get_scale_factor: assertion 'GTK_IS_WIDGET (widget)' failed

I checked that the output of writeShellApplication indeed appends the variety bin folder to the path variable. Moreover, the bash script generated also works if I execute it myself. Any idea of what might be happening?

It looks like the variety executable is being found; it’s just not functioning correctly in the service context. I expect it’s missing some environment variable or something. The internet is full of ‘can’t get GUI application to launch from systemd’ questions, with varying degrees of success depending on the application’s specific requirements and one’s tolerance for hacks.

1 Like

Not an ideal solution but a workaround.

Variety doesnt seem to return when calling set. So we must finish it manually instead, for example, using a timeout. The following script sets the wallpaper and then waits 10minutes before killing variety.

#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash
RANDOM_FILE=$(find "$BACKGROUNDS" -type f | shuf -n 1 | tr -d '\n')
variety --set="$RANDOM_FILE" &
sleep 10m
kill $!

After killing the script, you can let systemd restart it by passing restart=always, thus changing the wallpaper every time it dies:

systemd.user.services = {
    wallpaper-slideshow-random = {
      Unit = {
        Description = "Sets up the wallpaper slideshow randomly";
      };
      Install = {
        WantedBy = [ "default.target" ];
      };
      Service = {
        Type = "exec";
        ExecStart = "${pkgs.writeShellApplication 
          { name = "wallpaper-slideshow-random"; 
            # runtimeInputs = [pkgs.variety ]; 
            text = builtins.readFile ./scripts/random_wallpaper.sh;}
        }/bin/wallpaper-slideshow-random";
        Restart = "always";
        Environment = [
          "BACKGROUNDS=${config.home.sessionVariables.BACKGROUNDS}"
        ];
      };
    };
  };

There should be a way of killing and restarting from bash. But i found no success :frowning: (the timeout utility seems to kill the whole script).