{
  description = "polymodel: AT Protocol-native model/project sharing app built with Rust/Dioxus/Jacquard";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    flake-utils.url = "github:numtide/flake-utils";
    rust-overlay.url = "github:oxalica/rust-overlay";
    # Use nixpkgs dioxus-cli; do not import the upstream Dioxus flake because it may be ahead of Cargo.toml.



  };

  outputs = {
    self,
    nixpkgs,
    flake-utils,
    rust-overlay,

    ...
  }:
    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {
        inherit system;
        overlays = [
          (import rust-overlay)



        ];
      };

      rustToolchain = pkgs.rust-bin.selectLatestNightlyWith (toolchain:
        toolchain.default.override {
          targets = ["wasm32-unknown-unknown"];
          extensions = [
            "rust-src"
            "rust-analyzer"
            "clippy"
            "rustfmt"
          ];
        });
    in {
      devShells.default = pkgs.mkShell {
        packages = with pkgs; [
          rustToolchain
          rust-analyzer
          cargo-nextest
          cargo-insta
          cargo-bloat
          # Provides the `cargo sqlx` subcommand (package name in nixpkgs is sqlx-cli).
          sqlx-cli

          dioxus-cli
          wasm-bindgen-cli
          wasm-pack
          binaryen.out
          wabt
          twiggy

          llvmPackages_18.clang-unwrapped
          llvmPackages_18.llvm
          llvmPackages_18.libclang
          llvmPackages_18.bintools-unwrapped

          sqlite
          pkg-config
          openssl
          patchelf

          nodejs
          esbuild
          chromium
          chromedriver
          playwright-driver.browsers

          just
          jq
          nixd
          alejandra
        ];

        nativeBuildInputs = with pkgs; [
          llvmPackages_18.clang-unwrapped
          llvmPackages_18.bintools-unwrapped
          rustPlatform.bindgenHook
        ];

        buildInputs = with pkgs;
          [
            sqlite
            pkg-config
            openssl
          ]
          ++ lib.optionals stdenv.isDarwin [
            libiconv
          ];

        RUST_BACKTRACE = "1";
        NIX_LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
          pkgs.stdenv.cc.cc
          pkgs.openssl
          pkgs.sqlite
        ];
        NIX_LD = pkgs.lib.fileContents "${pkgs.stdenv.cc}/nix-support/dynamic-linker";

        PLAYWRIGHT_BROWSERS_PATH = "${pkgs.playwright-driver.browsers}";
        PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "1";
        PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS = true;
        PLAYWRIGHT_HOST_PLATFORM_OVERRIDE = "ubuntu-24.04";

        shellHook = ''
          export PATH="${pkgs.dioxus-cli}/bin:${pkgs.wasm-bindgen-cli}/bin:$PATH"; export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$NIX_LD_LIBRARY_PATH"
          export CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER="wasm-bindgen-test-runner"
          export CHROME="${pkgs.chromium}/bin/chromium"
          export CC_wasm32_unknown_unknown="${pkgs.llvmPackages_18.clang-unwrapped}/bin/clang"
          export AR_wasm32_unknown_unknown="${pkgs.llvmPackages_18.bintools-unwrapped}/bin/llvm-ar"

          CLANG_MAJOR_VERSION="18"
          CLANG_RESOURCE_DIR="${pkgs.llvmPackages_18.clang-unwrapped}/lib/clang/$CLANG_MAJOR_VERSION"
          LIBCLANG_INCLUDE="${pkgs.llvmPackages_18.libclang.lib}/lib/clang/$CLANG_MAJOR_VERSION/include"

          export LIBCLANG_PATH="${pkgs.llvmPackages_18.libclang.lib}/lib"
          export BINDGEN_EXTRA_CLANG_ARGS="-isystem ${pkgs.glibc.dev}/include -isystem $LIBCLANG_INCLUDE"
          export CFLAGS_wasm32_unknown_unknown="-isystem $LIBCLANG_INCLUDE -resource-dir $CLANG_RESOURCE_DIR"
          export CPPFLAGS="-isystem $LIBCLANG_INCLUDE -resource-dir $CLANG_RESOURCE_DIR"

          echo "polymodel dev shell: Rust + Dioxus + Jacquard + SQLite + Playwright"
        '';
      };
    });
}
