{
  description = "SSH honeypot with frontend";

  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

  outputs =
    { self, nixpkgs }:
    let
      allSystems = [
        "x86_64-linux"
        "aarch64-linux"
        "x86_64-darwin"
        "aarch64-darwin"
      ];
      forAllSystems =
        f:
        nixpkgs.lib.genAttrs allSystems (
          system:
          f {
            pkgs = import nixpkgs { inherit system; };
          }
        );
    in
    {
      overlays.default = final: prev: {
        apiary = self.packages.${prev.system}.default;
      };

      packages = forAllSystems (
        { pkgs }:
        {
          default = self.packages.${pkgs.system}.apiary;
          apiary =
            let
              src = pkgs.lib.sourceFilesBySuffices ./. [
                "go.mod"
                "go.sum"
                ".go"
              ];
              version = pkgs.lib.strings.removePrefix "v" (
                builtins.elemAt (pkgs.lib.strings.split "\"" (
                  pkgs.lib.lists.findFirst (x: pkgs.lib.strings.hasInfix "Version" x) null (
                    pkgs.lib.strings.splitString "\n" (builtins.readFile ./version.go)
                  )
                )) 2
              );
              rev = self.rev or "";
              geoDb = pkgs.fetchFromGitHub {
                owner = "geoacumen";
                repo = "geoacumen-country";
                rev = "5f770af620465f40427c3f421446a7b7845e2699";
                sha256 = "sha256-t3ELkhAbJC40z6r6wJeQI4Kutfw26fRg6n7a6FmhvkA=";
              };

              frontend = pkgs.buildNpmPackage {
                inherit version;
                name = "apiary-frontend";

                src = ./frontend;

                npmDepsHash = "sha256-7gjPEiQAMOpteLwHT4AIY9QLpDozpXb7xy0bDiOpDf4=";

                installPhase = ''
                  mkdir -p $out
                  cp -r dist/* $out
                '';
              };
            in
            pkgs.buildGoModule {
              inherit version;
              pname = "apiary";
              src = src;
              prePatch = ''
                cp ${geoDb}/Geoacumen-Country.mmdb honeypot/ssh
                mkdir -p web/frontend/dist
                cp -r ${frontend}/* web/frontend/dist
              '';
              vendorHash = "sha256-g2KFDCb/D+EEluyJxLe4IHHvwNWmnYv0Ye2qZArmYzo=";
              ldflags = [ "-X git.t-juice.club/torjus/apiary.Build=${rev}" ];
              tags = [
                "embed"
              ];
            };

          tarball =
            let
              version = self.packages.${pkgs.system}.apiary.version;
            in
            pkgs.stdenv.mkDerivation {
              name = "apiary-tarballs-${version}";
              phases = [ "installPhase" ];
              installPhase = ''
                mkdir -p $out
                mkdir apiary
                cp ${self.packages.${pkgs.system}.apiary}/bin/apiary apiary/apiary-${pkgs.system}-${version}
                tar cvzf $out/apiary-${pkgs.system}-${version}.tar.gz apiary
                pushd apiary
                sha256sum apiary-${pkgs.system}-${version} > apiary-${pkgs.system}-${version}.sha256sum
                popd
                cp apiary/apiary-${pkgs.system}-${version}.sha256sum $out
              '';
            };
        }
      );
      devShells = forAllSystems (
        { pkgs }:
        {
          default = pkgs.mkShell {
            packages = with pkgs; [
              go
              golangci-lint
            ];
          };
          frontend = pkgs.mkShell {
            packages = with pkgs; [
              nodejs
              yarn
            ];
          };
        }
      );
    };
}
