{ pkgs, self }:
let
  version = self.shortRev or self.dirtyShortRev or "unstable-${self.lastModified}";
  dockerTag = self.rev or self.dirtyRev or "unstable-${self.lastModified}";
  mkDocker = type: { server, public }:
    let
      PORT = 3000;
    in
    pkgs.dockerTools.${type} {
      name = "registry.fly.io/alanpearce-eu";
      tag = dockerTag;
      contents = [
        (pkgs.writeTextDir "config.toml" (builtins.readFile ./../config.toml))
        public
      ];
      config = {
        Cmd = [ "${server}/bin/server" ];
        Env = [
          "PRODUCTION=true"
          "LISTEN_ADDRESS=::"
          "PORT=${builtins.toString PORT}"
        ];
        ExposedPorts = {
          "${builtins.toString PORT}/tcp" = { };
        };
      };
    };
  mkDockerStream = mkDocker "streamLayeredImage";
  mkDockerImage = mkDocker "buildLayeredImage";
in
with pkgs; rec {
  default = server;
  builder = buildGoApplication {
    pname = "website-builder";
    inherit version;
    CGO_ENABLED = 0;
    src = with lib.fileset; toSource {
      root = ./..;
      fileset = unions [
        ./../go.mod
        ./../go.sum
        ./../cmd/build
        ./../internal
      ];
    };
    modules = ./gomod2nix.toml;
    subPackages = [ "cmd/build" ];
  };
  public = runCommandLocal "build"
    {
      src = with lib.fileset; toSource {
        root = ./..;
        fileset = unions [
          ./../config.toml
          ./../content
          ./../static
          ./../templates
        ];
      };
    } ''
    ${builder}/bin/build -s $src -d $out/public
  '';
  server = buildGoApplication {
    pname = "server";
    inherit version;
    CGO_ENABLED = 0;
    src = with lib.fileset; toSource {
      root = ./..;
      fileset = unions [
        ./../go.mod
        ./../go.sum
        ./../cmd/server
        ./../internal
      ];
    };
    modules = ./gomod2nix.toml;
    ldflags = [ "-s" "-w" "-X" "main.Commit=${version}" ];
  };
  docker-stream = mkDockerStream { inherit server public; };
  docker-stream-aarch64-linux = mkDockerStream {
    inherit public; server = (self.packages.aarch64-linux.server);
  };
  docker-stream-x86_64-linux = mkDockerStream {
    inherit public; server = (self.packages.x86_64-linux.server);
  };
  docker-image = mkDockerImage { inherit server public; };
  docker-image-aarch64-linux = mkDockerImage {
    inherit public; server = (self.packages.aarch64-linux.server);
  };
  docker-image-x86_64-linux = mkDockerImage {
    inherit public; server = (self.packages.x86_64-linux.server);
  };
}