{ bild ? import ../Biz/Bild.nix {}
}:

# This represents a single ship running in a container.

let
  # bootstrap & startup process, expects two files:
  # - /ship.name: containing a ship name like 'sampel-palnet'
  # - /ship.key: containing the key
  urbit-start = bild.pkgs.writeScript "urbit-start" ''
    #!${bild.pkgs.runtimeShell}
    set -xeuo pipefail

    ship=$(cat /ship.name)
    pier="/$ship"

    if [ -d "$pier" ]; then
      # pack pier if it exists, to save space
      /bin/urbit-worker pack "$pier"
    else
      # otherwise, setup
      /bin/urbit -c "$pier" -k /ship.key -x
    fi

    /bin/urbit -tq "$pier"
  '';
in bild.image {
  name = "urbit-ship";
  tag = "latest";

  fromImage = null; # scratch

  contents = bild.pkgs.urbit;

  config = {
    Cmd = [
      "${urbit-start}"
    ];
    WorkingDir = "/";
    Volumes = {
      "/" = { };
    };
  };
}