{ inputs ? import ../inputs.nix }:

self: super:

let
  unstable = inputs.nixpkgs-unstable;
  crs-tools = self.callPackage ./crs-tools.nix {};
  rustPlatform = self.makeRustPlatform {
    cargo = self.rust-bin.stable.latest.minimal;
    rustc = self.rust-bin.stable.latest.minimal;
  };
  radicle = import ./radicle.nix;
in
{
  beam = super.beam_nox;

  git-annex-remote-remarkable2 = self.callPackage
    ({ rustPlatform, fetchgit, ... }:
      rustPlatform.buildRustPackage rec {
        name = "git-annex-remote-remarkable2";
        src = fetchgit {
          url = "https://stuebinm.eu/git/git-annex-remote-remarkable2";
          rev = "refs/heads/main";
          hash = "sha256-ZDoZFFADti3ZwLZqeP/F3BbW+Csuprw6+1LBBQIjnlo=";
        };
        cargoLock.lockFile = "${src.outPath}/Cargo.lock";
      }) { };

  #### packages which are actually in use somewhere ####

  galmon-core = self.callPackage ./galmon.nix {};
  galmon-full = self.callPackage ./galmon.nix { buildAll = true; };

  akkoma-fe = super.akkoma-frontends.akkoma-fe.overrideAttrs (old: {
    patches = [ ./patches/akkoma-fe-toki-pona.patch ];
  });

  akkoma = super.akkoma.overrideAttrs (old: {
    patches = [ ./patches/akkoma-toki-pona.patch ];
  });

  cgit = super.cgit.overrideAttrs (old: {
    patches = [ ./patches/cgit-main-instead-of-master.patch ];
  });

  almanac = self.rustPlatform.buildRustPackage rec {
    pname = "almanac";
    version = "git";
    src = inputs.almanac;
    cargoLock.lockFile = "${src}/Cargo.lock";
  };

  mergiraf = self.rustPlatform.buildRustPackage rec {
    pname = "mergiraf";
    version = "git";
    src = self.fetchgit {
      url = "https://codeberg.org/mergiraf/mergiraf";
      rev = "v0.2.0";
      hash = "sha256-egtX2daCbPXi5rX2OxbEZA/VI9R4HMj5LRKbUEBFo+E=";
    };
    # TODO: probably unnecessary once we are on nixpkgs 24.11; earlier version
    # hardcode --frozen here, but cargo wants --offline.
    # the postBuild hook is necessary to set up things for the install hook
    buildPhase = ''
      cargo build -j $NIX_BUILD_CORES \
       --target x86_64-unknown-linux-gnu \
       --release \
       --offline

      runHook postBuild
    '';
    # no idea why this didn't work with either test or build inputs ..
    preCheck = ''
      export PATH=$PATH:${self.gitMinimal}/bin
    '';
    cargoLock = {
      lockFile = "${src}/Cargo.lock";
      outputHashes = {
        "tree-sitter-go-0.23.1" = "sha256-elPqkvVYs0vADOuN/umDteWP5hqcXhQAoSkqYDtTxaU=";
        "tree-sitter-xml-0.7.0" = "sha256-RTWvOUAs3Uql9DKsP1jf9FZZHaZORE40GXd+6g6RQZw=";
        "tree-sitter-yaml-0.6.1" = "sha256-gS+SjOnGl/86U9VV/y1ca7naYIe7DAOvOv++jCRLTKo=";
      };
    };
  };

  rustex = rustPlatform.buildRustPackage rec {
    pname = "rustex";
    version = "git";
    src = "${self.fetchFromGitHub {
      owner = "slatex";
      repo = "RusTeX";
      rev = "74f9d13d4a88e32a32c5d8b10e5ae1a2442a7a92";
      sha256 = "sha256-SOnqb/ptMoPI+G96nI/dQHOsvjlaDa6YSBEjy77kRdY=";
    }}/rustex";
    postPatch = ''
      ln -s ${./patches/rustex-cargo.lock} Cargo.lock
    '';
    cargoLock.lockFile = ./patches/rustex-cargo.lock;
    doCheck = false;
  };

  transport_validator = rustPlatform.buildRustPackage rec {
    pname = "transport-validator";
    version = "git";
    src = self.fetchFromGitHub {
      owner = "etalab";
      repo = pname;
      rev = "f50bee8af83af04805b1b4fcca334152d382c276";
      hash = "sha256-MB66OkSldL0Uy9N0aNOWr293R1Wisu/r0IZNC08m/Ps=";
    };

    # i patch the Cargo.toml to have it optionally not depend on actix
    buildNoDefaultFeatures = true;
    patches = [ ./patches/transport-validator-without-daemon.patch ];

    cargoLock.lockFile = "${src}/Cargo.lock";
    # binary name is `main` for some reason
    postInstall = ''
      mv $out/bin/main $out/bin/transport-validator
    '';

    meta.mainProgram = "transport-validator";
  };

  kijetesantakaluotokieni = self.rustPlatform.buildRustPackage rec {
    pname = "kijetesantakaluotokieni";
    version = "git";
    src = self.fetchgit {
      url = "https://git.2ki.xyz/spiders/kijetesantakaluotokieni";
      rev = "7d9ea828f42ffd9fb6826ea4cbf7677b08222064";
      sha256 = "sha256-PEp+K5mRNUR6yWLrpGpiX8qvaPQIibA17NQwXUiC2lk=";
    };
    cargoLock.lockFile = "${src}/Cargo.lock";
  };

  showrt = self.rustPlatform.buildRustPackage rec {
    pname = "showrt";
    version = "git";
    src = inputs.showrt;
    nativeBuildInputs = [ self.protobuf ];
    cargoLock.lockFile = "${src}/Cargo.lock";
  };

  isabelle-utils = self.rustPlatform.buildRustPackage rec {
    pname = "isabelle-utils";
    version = "git";
    src = inputs.isabelle-utils;
    cargoLock.lockFile = "${src}/Cargo.lock";
    doCheck = false;
  };

  isabat = import "${inputs.isabelle-utils}/isabat.nix" {
    pkgs = self;
    isabelle-rust-utils = self.isabelle-utils;
  };

  ntfy-matrix-bot = rustPlatform.buildRustPackage rec {
    pname = "ntfy-matrix-bot";
    version = "0.10";
    src = inputs.ntfy-matrix-bot;
    cargoLock.lockFile = "${src}/Cargo.lock";
    doCheck = false;
    meta.mainProgram = "ntfy-matrix-bot";
  };

  element-web-unwrapped = super.element-web-unwrapped.overrideAttrs {
    patches = [ ./patches/element-css.patch ];
    # replace the emoji fonts with their older version, which i liked
    # more. also patch the javascript's assumptions about box height
    # in the left side bar so that hiding overflow will work correctly
    # should be .mx_RoomTile's height + 12px
    # TODO: once --replace-fail is in stable use that instead
    preBuild = let
      TwemojiMozilla-colr = self.fetchurl {
        url = "https://github.com/matrix-org/matrix-react-sdk/raw/a465b1659f1f7763a1a5afcd56b7aa8513c57342/res/fonts/Twemoji_Mozilla/TwemojiMozilla-colr.woff2";
        hash = "sha256-TS2DTfotWc/C+7dKv9TYEnxognfTQ3/l8uPD5Ptozbs=";
      };
      TwemojiMozilla-sbix = self.fetchurl {
        url = "https://github.com/matrix-org/matrix-react-sdk/raw/a465b1659f1f7763a1a5afcd56b7aa8513c57342/res/fonts/Twemoji_Mozilla/TwemojiMozilla-sbix.woff2";
        hash = "sha256-sbZGHcRiscCWrelyLQav6Q1a0x5RQInSNh/XwFlZHj8=";
      };
    in ''
      ln -sf ${TwemojiMozilla-colr} \
        res/fonts/Twemoji_Mozilla/TwemojiMozilla-colr.woff2
      ln -sf ${TwemojiMozilla-sbix} \
        res/fonts/Twemoji_Mozilla/TwemojiMozilla-sbix.woff2
      substituteInPlace src/stores/room-list/ListLayout.ts \
        --replace "TILE_HEIGHT_PX = 44" "TILE_HEIGHT_PX = 32"
    '';
  };

  bahnhof-name =
    let
      haskellPkgs = self.haskellPackages.override (old: {
        overrides = self': super': with self.haskell.lib; {
          fuzzyfind = unmarkBroken (doJailbreak super'.fuzzyfind);
          fuzzyset = unmarkBroken (doJailbreak super'.fuzzyset);
        };
      });
      pkg = { mkDerivation, base, bytestring, cassava, containers
            , fuzzyfind, fuzzyset, http-client, http-client-openssl, http-types
            , lib, stm, text, time, vector, wai, wai-extra, warp
            }:
              mkDerivation {
                pname = "bahnhof.name";
                version = "0.2.0.0";
                src = inputs.bahnhof-name;
                isLibrary = false;
                isExecutable = true;
                executableHaskellDepends = [
                  base bytestring cassava containers fuzzyfind fuzzyset http-client
                  http-client-openssl http-types stm text time vector wai wai-extra
                  warp
                ];
                mainProgram = "bahnhof-name";
                license = lib.licenses.eupl12;
                postInstall = ''
                  cp -r $src/data $out
                '';
              };
    in
      haskellPkgs.callPackage pkg {};

  nomsring = super.haskellPackages.callPackage ./nomsring {};

  apply-config = self.callPackage ./apply-config {};

  #### sporadically maintained / updated ####

  hikari_unstable = (unstable.hikari.overrideAttrs (old: {
    src = /home/stuebinm/clones/hikari;
    buildInputs = old.buildInputs ++ [ self.pandoc self.xorg.xcbutilwm.dev ]; #self.libdrm self.libdrm.dev ];
    makeFlags = with self.lib;
      [ "PREFIX=$(out)" "WITH_POSIX_C_SOURCE=YES"
        "WITH_GAMMACONTROL=YES" "WITH_LAYERSHELL=YES" "WITH_SCREENCOPY=YES" ];
  })).override { wlroots = unstable.wlroots_0_16; stdenv = self.clangStdenv; };
  twelf = super.twelf.overrideAttrs (old:  {
    src = self.fetchFromGitHub {
      owner = "k4rtik";
      repo = "twelf";
      rev = "c1bec0d0b9fa506e36bb364b1765191b159e6c4c";
      hash = "sha256-sh/yMlCvMMq6GBqAKO+V0Bcyp38zB7I/3H8a0fxqOos=";
    };
  });

  hikari = self.callPackage ./hikari.nix { wlroots = self.wlroots_0_15; };

  wlroots_0_15 = (self.wlroots_0_16.overrideAttrs rec {
    version = "0.15.1";
    src = self.fetchFromGitLab {
      domain = "gitlab.freedesktop.org";
      owner = "wlroots";
      repo = "wlroots";
      rev = version;
      hash = "sha256-MFR38UuB/wW7J9ODDUOfgTzKLse0SSMIRYTpEaEdRwM=";
    };
  });

  travelynx = self.callPackage ./travelynx.nix {};



  #### packaged mostly as shitpost / to play around with ####

  bookwyrm = (self.callPackage ./bookwyrm.nix {
    python = super.python312.override ({
      packageOverrides = self: super: {
        django-sass-processor = self.callPackage ./python/django-sass-processor.nix {};
        django-imagekit = self.callPackage ./python/django-imagekit.nix {};
        django-pgtrigger = self.callPackage ./python/django-pgtrigger.nix {};
        s3-tar = self.callPackage ./python/s3-tar.nix {};
        bw-file-resubmit = self.callPackage ./python/bw-file-resubmit.nix {};
      };
    });
  });

  # currently broken because poetry2nix was moved out of nixpkgs
  # glitchtip = (self.callPackage ./glitchtip.nix {
  #   python = super.python310.override ({
  #     packageOverrides = self: super: {
  #       django = super.django_4;
  #     };
  #   });
  # });

  crs-tracker = crs-tools.tracker;
  crs-php = crs-tools.php;

  matrix-to = self.callPackage ./matrix-to.nix {};

  heartwood = self.callPackage radicle.heartwood { inherit (rustPlatform) buildRustPackage; };
  radicle-interface = self.callPackage radicle.interface {};
  radicle-tui = self.callPackage radicle.tui { inherit (rustPlatform) buildRustPackage; };

  inweb = self.callPackage ({stdenv, gnumake, fetchFromGitHub}: stdenv.mkDerivation rec {
    pname = "inweb";
    version = "v7.2.0";
    src = fetchFromGitHub {
      repo = "inweb";
      owner = "ganelson";
      rev = version;
      hash = "sha256-kgyMzqGyZ1aihx8f9OCFOd2dlAUhUp98ni4rcgNrPq0=";
    };

    buildPhase = ''
      ln -s . inweb
      bash inweb/scripts/first.sh linux
    '';

    installPhase = ''
      mkdir -p $out/bin
      cp Tangled/inweb $out/bin
    '';
  }) {};

  mollysocket = self.callPackage ./mollysocket.nix { };
}