How can i delay a systemd service?

Good morning,
i am curious how i can delay a service with nix. Usually i assign the ip address to my ssh configuration, and it make fail at boot; this does happen to any distro i used before. So i usually modify the ssh.service, and it solve the problem.

I added this lines to my .nix file, the rebuild don’t give me any error, but seem not working as expected.

systemd.services.openssh.wants = [ "network-online.target" ];
systemd.services.openssh.after = [ "network-online.target" ];

Guess i’m missing something.

Regards and merry xmas.

You could try to restart on failure a couple of times. That’s how I do it for the automated daily updates

systemd.services.nixos-upgrade = {
    preStart = "${pkgs.host}/bin/host firecat53.net";  # Check network connectivity
    serviceConfig = {
      Restart = "on-failure";
      RestartSec = "120";
    };
    unitConfig = {
      StartLimitIntervalSec = 600;
      StartLimitBurst = 2;
    };
    after = ["flake-update.service"];
    wants = ["flake-update.service"];
    path = [pkgs.host];
  };

Tried with “extraConfig” but seem is not the options for restart. serviceConfig is not on openssh.

Those aren’t ssh module options. They are options for the ssh service systemd.services.openssh.

I tried a similar solution

systemd.services.openssh = {
152       preStart = "${pkgs.host}/bin/host firecat53.net";  # Check network connectivity
153     serviceConfig = {
154       Restart = "on-failure";
155       RestartSec = "120";
156     };
157     unitConfig = {
158       StartLimitIntervalSec = 600;
159       StartLimitBurst = 2;
160     };
161     after = ["network-online.target"];
162     wants = ["network-online.target"];
163     path = [pkgs.host];  
164  };  

Build not fail, but ssh still down after a reboot; have to manually restart it. Don’t know if the preStart line fit my configuration with XFCE and network manager.

First of all, NixOS does not manage your services directly, it instructs systemd to do it. This is therefore a purely systemd-specific question and has nothing to do with Nix. I’d recommend you to ask in generic Linux forums how to achieve your goal and then here if you need help transferring the generic solution into a NixOS declaration.

Please clarify what exactly you mean by that, it is not clear to me.

“modify” in what way?

Did those lines come into effect in the relevant unit file? I’m pretty sure the ssh daemon service is sshd.service on NixOS, not openssh.service.


Generally speaking, you should explicitly depend on the specific things you need to have “up” by encapsulating the “things you need up” as systemd units.

If this is related to network configuration, you probably want to use systemd-networkd to configure the network because it automatically generates .link units on which you can then make other units (such as sshd) depend.

1 Like

The problem with OpenSSH and Network Manager always been the same. If specify the listen IP address, and Network Manager is not up yet, it fail to start. For solve this problem usually i add 2 lines to the sshd.service in the systemd folder (not always the same location in every distro)


After=network-online.target
Wants=network-online.target

This is how i set up the service:

154   services.openssh = {
155   enable = true;
156   ports = [ 5xxxx3 ];
157
158   settings = {
159     ListenAddress = "192.168.1.x"; 
160     AddressFamily =  "inet";  
161     
162     PasswordAuthentication = true;
163     AllowUsers = ["xxxxxx@192.168.1.0/24"]; # Allows all users by default. Can be [ "user1" "user2" ]
164     UseDns = true;
165     X11Forwarding = false;
166     PermitRootLogin = "prohibit-password"; # "yes", "without-password", "prohibit-password", "forced-commands-only", "no"
167   };

Edit: could related to this?

May I ask why you’re using NetworkManager? Typically, you only want that on desktop systems and even there it’s generally pretty …meh.

That modification should work just as it did on other distros though. Please try to apply it to the correct service (systemd.services.sshd).

I use NixOS on a laptop, and the XFCE image use Network Manager… take note that it sucks, not knew.

Edit: the solution was easy:

systemd.services.sshd = {
171    requires = [ "network-online.target" ];
172    after = [ "network-online.target" ];
173    wants = [ "network-online.target" ];
174     };