IHP 1.1 is out now!

Running IHP as a systemd service on NixOS

Lillo PRO

Hi! This might be mostly systemd related, but I'm trying to run IHP as a systemd service on a NixOS webserver, eventually on DigitalOcean for a service I am making.

Currently testing locally on my NixOS machine with a fresh IHP project.

In a nix configuration, I have this declaration, that apparently runs without error after a nixos-rebuild:

 systemd.services.ship = {
    description = "My IHP server";
    enable = true;
    after = [ "network.target" "postgresql.service" ];
    wantedBy = [ "multi-user.target" ];
    environment = {
      DATABASE_URL = "postgres://lillo:userpassword@localhost:5432/ship";
    };
    serviceConfig = {
      Type = "simple";
      User = "lillo";
      ExecStartPre = [
        ''${pkgs.bash}/bin/bash -c cd /home/lillo/kode/ihp-test && nix-shell --run "make build/bin/RunUnoptimizedProdServer" && nix-shell --run "make static/prod.css static/prod.js"''
      ];
      ExecStart = ''
        ${pkgs.bash}/bin/bash -c cd /home/lillo/kode/ihp-test && nix-shell --run build/bin/RunProdServer
      '';
    };
  };

Problem is that running it through the systemd declaration, the process seems to immediately exit after the ExecStart script, which is undesired behaviour as I want the server to be running :)

As you see below when I run systemctl status ship.service, the RunProdServer process runs and exits without failure, but is in status inactive (dead):

○ ship.service - My IHP server
     Loaded: loaded (/etc/systemd/system/ship.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Fri 2022-04-08 07:03:18 CEST; 5s ago
    Process: 229927 ExecStartPre=/nix/store/dznl6xxx7bp9xynmsbrjjba9x726m3f9-bash-5.1-p8/bin/bash -c cd /home/lillo/kode/ihp-test && nix-shell --run make build/bin/RunUnoptimizedProdServer && nix-shell --run make static/prod.css>
    Process: 229928 ExecStart=/nix/store/dznl6xxx7bp9xynmsbrjjba9x726m3f9-bash-5.1-p8/bin/bash -c cd /home/lillo/kode/ihp-test && nix-shell --run build/bin/RunProdServer (code=exited, status=0/SUCCESS)
   Main PID: 229928 (code=exited, status=0/SUCCESS)
         IP: 0B in, 0B out
        CPU: 3ms

Apr 08 07:03:18 kodeFant systemd[1]: Starting My IHP server...
Apr 08 07:03:18 kodeFant systemd[1]: Started My IHP server.
Apr 08 07:03:18 kodeFant systemd[1]: ship.service: Deactivated successfully.

Running the ExecStartPre and ExecStart scripts via my own console works as expected.

Any ideas on where it could be failing would be highly appreciated :)

Lillo PRO

It was this script that was failing.

 ExecStart = ''
        ${pkgs.bash}/bin/bash -c cd /home/lillo/kode/ihp-test && nix-shell --run build/bin/RunProdServer
      '';

The cd command apparently sent an exit code that terminated the process.

This worked fine:

      ExecStart = ''
        /home/lillo/kode/ihp-test/build/bin/RunProdServer
      '';