{
  lib,
  stdenv,
  fetchurl,
  fetchpatch,
  makeWrapper,
  fetchFromGitHub,
  dpkg,
  glib,
  gnutar,
  gtk3-x11,
  luajit,
  sdcv,
  sdl3,
  openssl,
  writeScript,
}:

let
  version = "2026.07.1";

  # LuaJIT with table.pack/unpack support for KOReader
  # https://github.com/koreader/koreader-base/tree/master/thirdparty/luajit
  luajit_koreader = luajit.overrideAttrs (old: {
    patches = (old.patches or [ ]) ++ [
      (fetchpatch {
        url = "https://raw.githubusercontent.com/koreader/koreader-base/master/thirdparty/luajit/koreader-luajit-enable-table_pack.patch";
        hash = "sha256-tvx7eRoSwnumqK6H7+2RCAKRDFJtaRY/2mRPjy30fJA=";
      })
    ];
  });

  src_repo = fetchFromGitHub {
    repo = "koreader";
    owner = "koreader";
    tag = "v${version}";
    fetchSubmodules = true;
    hash = "sha256-395G5bu0gs0oKOc/CfKNeal66vgXBDgHSpopoQ0GRzw=";
  };
in
stdenv.mkDerivation {
  pname = "koreader";
  inherit version;

  __structuredAttrs = true;
  strictDeps = true;

  src =
    let
      selectSystem =
        attrs:
        attrs.${stdenv.hostPlatform.system} or (throw "Unsupported system: ${stdenv.hostPlatform.system}");
      arch = selectSystem {
        aarch64-linux = "arm64";
        armv7l-linux = "armhf";
        x86_64-linux = "amd64";
      };
    in
    fetchurl {
      url = "https://github.com/koreader/koreader/releases/download/v${version}/koreader_${version}-1_${arch}.deb";
      hash = selectSystem {
        aarch64-linux = "sha256-swwNcmquMjnO31pFNkKZbqU8Kx7Rtwq2ebT12XK8yvQ=";
        armv7l-linux = "sha256-TpMm9smcDff7A4BkWguWuOwDAJXQITk/HGmDgDIRtrw=";
        x86_64-linux = "sha256-CWmnfC7Dy4nVf7diwRvsOvE12W/gkE2235h49Hq0f4k=";
      };
    };

  nativeBuildInputs = [
    dpkg
    makeWrapper
  ];

  buildInputs = [
    glib
    gnutar
    gtk3-x11
    luajit_koreader
    sdcv
    sdl3
    openssl
  ];

  installPhase = ''
    runHook preInstall

    cp --recursive usr $out
  ''
  # Link required binaries
  + ''
    ln -sf ${luajit_koreader}/bin/luajit $out/lib/koreader/luajit
    ln -sf ${sdcv}/bin/sdcv $out/lib/koreader/sdcv
    ln -sf ${gnutar}/bin/tar $out/lib/koreader/tar
  ''
  # Link SSL/network libraries
  + ''
    ln -sf ${lib.getLib openssl}/lib/libcrypto.so.3 $out/lib/koreader/libs/libcrypto.so.1.1
    ln -sf ${lib.getLib openssl}/lib/libssl.so.3 $out/lib/koreader/libs/libssl.so.1.1
    ln -sf ${lib.getLib sdl3}/lib/libSDL3.so.0 $out/lib/koreader/libs/libSDL3.so.0
  ''
  # Copy fonts
  + ''
    cp -r ${src_repo}/resources/fonts/* $out/lib/koreader/fonts/
  ''
  # Remove broken symlinks
  + ''
    find $out -xtype l -print -delete
  ''
  + ''
    wrapProgram $out/bin/koreader --prefix LD_LIBRARY_PATH : $out/lib/koreader/libs:${
      lib.makeLibraryPath [
        gtk3-x11
        sdl3
        glib
        stdenv.cc.cc
        openssl.out
      ]
    }

    runHook postInstall
  '';

  passthru = {
    inherit src_repo luajit_koreader;
    updateScript = writeScript "update-koreader" ''
      #!/usr/bin/env nix-shell
      #!nix-shell -i bash -p nix curl jq nix-update common-updater-scripts
      set -eou pipefail
      version=$(nix eval --raw --file . koreader.version)
      nix-update koreader
      latestVersion=$(nix eval --raw --file . koreader.version)
      if [[ "$latestVersion" == "$version" ]]; then
        exit 0
      fi
      update-source-version koreader $latestVersion --source-key=src_repo --ignore-same-version
      systems=$(nix eval --json -f . koreader.meta.platforms | jq --raw-output '.[]')
      for system in $systems; do
        hash=$(nix --extra-experimental-features nix-command hash convert --to sri --hash-algo sha256 $(nix-prefetch-url $(nix eval --raw -f . koreader.src.url --system "$system")))
        update-source-version koreader $latestVersion $hash --system=$system --ignore-same-version --ignore-same-hash
      done
    '';
  };

  meta = {
    homepage = "https://github.com/koreader/koreader";
    changelog = "https://github.com/koreader/koreader/releases/tag/v${version}";
    description = "Ebook reader application supporting PDF, DjVu, EPUB, FB2 and many more formats, running on Cervantes, Kindle, Kobo, PocketBook and Android devices";
    mainProgram = "koreader";
    sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
    platforms = [
      "aarch64-linux"
      "armv7l-linux"
      "x86_64-linux"
    ];
    license = lib.licenses.agpl3Only;
    maintainers = with lib.maintainers; [
      contrun
      liberodark
    ];
  };
}
