Should I use /run/current-system/sw/bin/x or ${pkgs.package}/x in scripts?

I am writing a postStart script for a systemd service where I need to execute a program, specifically nextcloud-occ.

I can refer to it by /run/current-system/sw/bin/nextcloud-occ or by ${pkgs.nextcloud28}/occ in my script.

To my surprise they don’t yield the same path and only the former works. In many scripts for other packages I saw people using the latter, though. Which one should be preferred and why?

The former is a wrapper around the latter that is generated by the nexcloud nixos-module and also placed in environment.systemPackages for convenience. It essentially ensures that occ can find the configuration and is run as the correct user.

In general, I would avoid referring to stuff via /run/current-system/sw/bin/foo as it just silently fails to work if nothing in environment.systemPackages provides foo. Just In this special case the derivation cannot provide a script that is useful without a suitable wrapper. The wrapper can also be accessed via ${}/bin/nextcloud-occ.