# { pkgs ? import <nixpkgs> {} }:

# let

#   fenix = import "${
#   fetchTarball "https://github.com/nix-community/fenix/archive/main.tar.gz"
#   }/packages.nix";

# in

# pkgs.mkShell {
#   shellHook = ''export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${pkgs.lib.makeLibraryPath [
#     pkgs.alsaLib
#     pkgs.udev
#     pkgs.vulkan-loader
#   ]}"'';

#   buildInputs = with pkgs; [
#     (
#       with fenix;
#       combine (
#         with default; [
#           cargo
#           clippy-preview
#           latest.rust-src
#           rust-analyzer
#           rust-std
#           rustc
#           rustfmt-preview
#         ]
#       )
#     )
#     cargo-edit
#     cargo-watch

#     lld
#     clang

#     # # bevy-specific deps (from https://github.com/bevyengine/bevy/blob/main/docs/linux_dependencies.md)
#     pkgconfig
#     udev
#     alsaLib
#     lutris
#     x11
#     xorg.libXcursor
#     xorg.libXrandr
#     xorg.libXi
#     vulkan-tools
#     vulkan-headers
#     vulkan-loader
#     vulkan-validation-layers
#   ];

# }
{
  description = "Minimal Rust development environment for Bevy project";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    rust-overlay.url = "github:oxalica/rust-overlay";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        overlays = [ (import rust-overlay) ];
        pkgs = import nixpkgs { inherit system overlays; };
        rustToolchain = pkgs.rust-bin.stable.latest.default.override {
          extensions = [ "rust-src" "rust-analyzer" "clippy" ];
        };
      in {
        devShells.default = pkgs.mkShell {
          nativeBuildInputs = with pkgs; [
            pkg-config
            gobject-introspection
            cargo
            cargo-tauri
            nodejs
          ];
          buildInputs = with pkgs; [
            rustToolchain
            clang
            llvmPackages_latest.bintools
            udev
            alsa-lib
            vulkan-loader
            xorg.libX11
            xorg.libXcursor
            xorg.libXi
            xorg.libXrandr
            libxkbcommon
            wayland
            glibc.dev
            glib.dev
            dioxus-cli
            at-spi2-atk
            atkmm
            cairo
            gdk-pixbuf
            glib
            gtk3
            harfbuzz
            librsvg
            libsoup_3
            pango
            webkitgtk_4_1
            openssl
            xdotool
          ];

          shellHook = ''
            export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin
            export LD_LIBRARY_PATH=${
              pkgs.lib.makeLibraryPath [
                pkgs.vulkan-loader
                pkgs.libxkbcommon
                pkgs.wayland
                pkgs.alsa-lib
                pkgs.udev
              ]
            }:$LD_LIBRARY_PATH
            export LIBCLANG_PATH="${pkgs.llvmPackages_latest.libclang.lib}/lib"
            export BINDGEN_EXTRA_CLANG_ARGS="-I${pkgs.glibc.dev}/include -I${pkgs.llvmPackages_latest.libclang.lib}/lib/clang/${pkgs.llvmPackages_latest.libclang.version}/include -I${pkgs.glib.dev}/include/glib-2.0 -I${pkgs.glib.out}/lib/glib-2.0/include/"
            export RUSTFLAGS="-C link-arg=-fuse-ld=lld"
            echo "Bevy development environment loaded!"
          '';
        };
      });
}
