{ pkgs, zen-browser, lib, config, ... }:

let
  # tex = (pkgs.texlive.combine { inherit (pkgs.texlive)
  #     scheme-medium
  #     lettre
  #     wallpaper
  #     lastpage
  #     hyphenat
  #     moresize
  #     fontawesome5
  #     montserrat
  #     titlesec
  #     ly1
  #     footmisc
  #     titling
  #     frpseudocode
  #     ebproof
  #     comment
  #     ucs;
  #   });
  tex = pkgs.texliveFull;
in {
  users.users.lyes.packages = with pkgs; [
    # Web
    zen-browser.packages."${system}".default
    dropbox
    filezilla
    organicmaps

    # Messaging
    discord
    vesktop
    whatsapp-for-linux
    # signal-desktop
    element-desktop
    fractal
    zoom-us
    # cinny-desktop

    # Image
    gimp
    inkscape

    # Video
    mpv
    syncplay
    obs-studio
    yt-dlp
    # handbrake
    (kodi.withPackages(kodiPkgs: with kodiPkgs; [
      youtube
      sponsorblock
      pvr-iptvsimple
    ]))
    # stremio   # insecure dependency

    # Audio
    helvum
    local.quadcastrgb
    euphonica
    local.KhinsiderDownloader

    # Games
    heroic
    # cemu
    prismlauncher
    # vvvvvv
    ryubing
    dolphin-emu
    parsec-bin
    # unstable.suyu
    # suyu
    # factorio

    # Reading
    # calibre

    # Note taking
    tex
    pandoc
    unstable.typst
    # typst
    tinymist
    zotero
    folio

    # Planification
    # planify
    dosage-tracker

    # Programming
    git
    git-lfs
    # vscode
    vscodium
    # zed-editor
    gcc
    rustup
    python3
    ocaml
    ocamlPackages.ocaml-lsp
    ocamlPackages.ocamlformat
    ocamlPackages.odoc
    opam
    dune_3
    ledit
    nodejs
    nil
    nixd
    nixfmt-rfc-style
    protege # LogIA Course
    haskellPackages.Agda
    agdaPackages.standard-library
    elan
    coq
    # rocq-core
    # rocqPackages.stdlib
    coqPackages.coq
    coqPackages.stdlib
    # coqPackages.HoTT
    coqPackages.equations
    coqPackages.metarocq
    coqPackages.vscoq-language-server
    numworks-udev-rules

    # Containers & VMs
    toolbox
    distrobox

    # Virtualization
    qemu
    virt-manager

    # Utilities
    ghostty
    via

    # Proton
    protonmail-bridge-gui
    protonmail-desktop
    protonvpn-gui
    proton-pass

    # Multimedia
    pipewire.dev
    # pulseaudio
    libopus
  ];

  # system.extraDependencies = with pkgs; [
  #   factorio.src
  # ];

  # Flatpaks
  services.flatpak = lib.mkIf (config?services.flatpak.packages) {
    packages = [
      # Messaging
      "org.signal.Signal"

      # Utilities
      "org.gnome.World.PikaBackup"

      # Customization
      "com.github.tchx84.Flatseal"

      # Games
      "com.usebottles.bottles"
    ];

    overrides = {
      "org.signal.Signal".Context = {
        "env" = [
          "SIGNAL_PASSWORD_STORE=gnome-libsecret"
        ];
      };
    };
  };

  nixpkgs.overlays = [
    # Version pins
    (final: prev: {
      # factorio = prev.factorio.overrideAttrs (super: {
      #   version = "2.0.42";
      #   # src.name = "factorio_alpha_x64-2.0.42.tar.xz";
      #   # src.sha256 = "1zq6wcqkmn9bzys27v0jlk9m9m1jhaai1mybdv8hz7p2si4l76n9";
      #   src = "/nix/store/b9y0dwgxa6hpddrd8nn0g2fizxl3xss7-factorio_alpha_x64-2.0.42.tar.xz";
      # });
      # factorio = prev.factorio.overrideAttrs (super: {
      #   username = "ntlyes";
      #   token = "";
      #   src =
      #     (pkgs.fetchurl {
      #       name = super.src.name;
      #       url = super.src.url;
      #       sha256 = "sha256-83GCCvULfgEHBBLN3HUlw9jJR5jwQau2XsEo5PFeboc=";
      #     });
      #   preHook =
      #     ''
      #       echo -n "ntlyes" > username
      #       cp ${config.age.secrets.factorio.path} token
      #     '';
      #     # (lib.overrideDerivation
      #     #   (fetchurl {
      #     #     name = "alpha";
      #     #     url = "";
      #     #     inherit name url sha256;
      #     #     curlOptsList = [
      #     #       "--get"
      #     #       "--data-urlencode"
      #     #       "username@username"
      #     #       "--data-urlencode"
      #     #       "token@token"
      #     #     ];
      #     #   })
      #     #   (_: {
      #     #     # This preHook hides the credentials from /proc
      #     #     preHook =
      #     #       if username != "" && token != "" then
      #     #         ''
      #     #           echo -n "${username}" >username
      #     #           echo -n "${token}"    >token
      #     #         ''
      #     #       else
      #     #         ''
      #     #           # Deliberately failing since username/token was not provided, so we can't fetch.
      #     #           # We can't use builtins.throw since we want the result to be used if the tar is in the store already.
      #     #           exit 1
      #     #         '';
      #     #     failureHook = ''
      #     #       cat <<EOF
      #     #       ${helpMsg}
      #     #       EOF
      #     #     '';
      #     #   })
      #     # );
      # });

      # dolphin-emu = prev.dolphin-emu.overrideAttrs (super: {
      #   version = "2407";
      #   commit = "b92e354389bb7c0bd114a8631b8af110d3cb3a14";

      #   src = pkgs.fetchFromGitHub {
      #     owner = "dolphin-emu";
      #     repo = "dolphin";
      #     rev = "heads/refs/tags/${final.dolphin-emu.version}";
      #     hash = "sha256-8W4KyIj+rhDkWnQogjpzlEJVo3HJenfpWKimSyMGN7c=";
      #     fetchSubmodules = true;
      #   };

      #   cmakeFlags = [
      #     "-DDISTRIBUTOR=NixOS"
      #     "-DDOLPHIN_WC_BRANCH=${final.dolphin-emu.src.rev}"
      #     "-DDOLPHIN_WC_DESCRIBE=${final.dolphin-emu.version}"
      #     "-DDOLPHIN_WC_REVISION=${final.dolphin-emu.commit}"
      #   ];
      # });

      # mesa = final.mesa-24-2-4.mesa;
      # mesa = prev.mesa.overrideAttrs (super: {
      #   version = "24.2.4";

      #   src = pkgs.fetchFromGitLab {
      #     domain = "gitlab.freedesktop.org";
      #     owner = "mesa";
      #     repo = "mesa";
      #     rev = "mesa-24.2.4";
      #     hash = "sha256-pgyvgMHImWO+b4vpCCe4+zOI98XCqcG8NRWpIcImGUk=";
      #   };
      # });
    })
  ];
}
