Please help me with getting a systemd service to work

Hello dear NixOS wizards and happy new year

I try to run a appimage of an application called espanso.
I’m aware that this app there is also a package and service on Nix available: Link.
The problem with it is that the Nix version is 0.7.3 while the most recent version of espanso is 2.1.8 and they are pretty different by now. So I try running it with appimage.

The software works fine so far after installing the appimage-run package.

I can run the app with cd ~/opt && appimage-run Espanso.AppImage start --unmanaged

I have the latest NixOS version 22.11 installed

Here is what I tried with systemd so far:

systemd.services.espanso-unmanaged = {
  script = ''
    cd /home/-/opt && appimage-run Espanso.AppImage start --unmanaged
  '';
  wantedBy = [ "graphical-session.target" ];
  partOf = [ "graphical-session.target" ];
};

when boot up fresh and check the service with “systemctl status espanso-unmanaged” there will be this error:

○ espanso-unmanaged.service
     Loaded: loaded (/etc/systemd/system/espanso-unmanaged.service; enabled; pr>
     Active: inactive (dead)

after starting the service there will be this error message:

× espanso-unmanaged.service
     Loaded: loaded (/etc/systemd/system/espanso-unmanaged.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Sun 2023-01-01 10:54:50 CET; 7min ago
   Duration: 23ms
   Process: 3064 ExecStart=/nix/store/hl1qbcgdh2j5hqf086amlhq3hp8vyci1-unit-script-espanso-unmanaged-start/bin/espanso-unmanaged-start (code=exited, status=127)
   Main PID: 3064 (code=exited, status=127)
         IP: 0B in, 0B out
        CPU: 7ms

Jan 01 10:54:50 MacBookPro systemd[1]: Started espanso-unmanaged.service.
Jan 01 10:54:50 MacBookPro espanso-unmanaged-start[3066]: /nix/store/hl1qbcgdh2j5hqf086amlhq3hp8vyci1-unit-script-espanso-unmanaged-start/bin/espanso-unmanaged-start: line 3: appimage-run: command not found
Jan 01 10:54:50 MacBookPro systemd[1]: espanso-unmanaged.service: Main process exited, code=exited, status=127/n/a
Jan 01 10:54:50 MacBookPro systemd[1]: espanso-unmanaged.service: Failed with result 'exit-code'.

I guess it’s mainly this problem:

line 3: appimage-run: command not found

“which appimage-run” gives me /run/current-system/sw/bin/appimage-run, when I use this in the systemd service I get the same error message.

What can I do to solve this?

Thank you for your answer in advance.

Systemd service scripts have an empty environment by default. You need to include the package that contains appimage-run (presumably the package of the same name?) in the path option on the service.

1 Like

You can also interpolate it into the script. Something like:

script = ''
    cd /home/-/opt && ${wherever_appimage_package_comes_from}/bin/appimage-run Espanso.AppImage start --unmanaged
  ''; 

If appimage-run also depends on PATH for dependencies, you may be better off adding them all to the service’s path option as Justin’s suggests…

1 Like

Hello Justinas, thank you for your answer!

I changed it like so, plus I found out that it is not working as root:

systemd.user.services.espanso-unmanaged = {
  script = ''
    cd /home/-/opt/ && ${pkgs.appimage-run}/bin/appimage-run Espanso.AppImage start --unmanaged
  '';
  wantedBy = [ "graphical-session.target" ];
  partOf = [ "graphical-session.target" ];
};

now I get this systemctl status:

$ systemctl status --user espanso-unmanaged | fold

○ espanso-unmanaged.service
     Loaded: loaded (/home/-/.config/systemd/user/espanso-unmanaged.service; 
enabled; preset: enabled)
     Active: inactive (dead) since Mon 2023-01-02 13:46:56 CET; 5min ago
   Duration: 1.352s
    Process: 1136 ExecStart=/nix/store/k2igi60b81kn16mxvm9yhd1rs8nilgpb-unit-scr
ipt-espanso-unmanaged-start/bin/espanso-unmanaged-start (code=exited, status=0/S
UCCESS)
   Main PID: 1136 (code=exited, status=0/SUCCESS)
        CPU: 544ms

Jan 02 13:46:55 MacBookPro systemd[1084]: Started espanso-unmanaged.service.
Jan 02 13:46:55 MacBookPro espanso-unmanaged-start[1140]: Espanso.AppImage insta
lled in /home/-/.cache/appimage-run/ad95210fcbcdf626624bb0f464839c5470a12f924
3719492cd6a43607fdfdb70
Jan 02 13:46:55 MacBookPro espanso-unmanaged-start[1140]: /home/-/.cache/appi
mage-run/ad95210fcbcdf626624bb0f464839c5470a12f9243719492cd6a43607fdfdb70/AppRun
: /usr/lib/libtiff.so.5: no version information available (required by /home/-/.cache/appimage-run/ad95210fcbcdf626624bb0f464839c5470a12f9243719492cd6a43607f
dfdb70/usr/bin/../lib/libwx_gtk3u_core-3.0.so.0)
Jan 02 13:46:56 MacBookPro espanso-unmanaged-start[1140]: espanso started correc
tly!

espanso started correctly!

It seems that everything now works! But still espanso is not running when I try to use it …
Maybe something is wrong with the wantedBy section of the systemd service?