Is there a universal way to enable a service auto restart?

For example: the service autossh seems have no option to make it restart when failed.

While in other distributions, there is an option in the .service file to enable it always restart when failed.


You always have the possibility to overwrite systemd files directly, even though it might not look the prettiest.
In autossh’s case, you’ll want to write something like:$NAME.serviceConfig.Restart = lib.mkForce "always";

With $NAME replaced by the name you’ve given to the session. lib.mkForce will let your declaration take priority over the defaults.
You might also want to include something like RestartSec = 3

1 Like

Hi, and thank you, but following error appears:

error: cannot coerce a set to a string, at /nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/nixos/modules/system/boot/systemd-lib.nix:65:8

which seems to complain that the type of lib.mkForce "always" is not compatiable to the required type of Restart.

Which nixpkgs version are you on? Because I’ve fixed this a while ago.

Edit: Apparently I didn’t backport this to NixOS 18.03, which I just did. Thanks for the implicit reminder.


Hi, my nixos version is:

$> nixos-version 
18.03.133072.10b979ff213 (Impala)

Okay, then it should be part of an upcoming channel update after the next evaluation, so I’d guess you could update the channel in around 6 hours and what @ldesgoui wrote above should work.

1 Like

Thank you very much.:grin: