{
  description = "Lux - A functional programming language with first-class effects";

  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" ];
        };
      in
      {
        devShells.default = pkgs.mkShell {
          buildInputs = with pkgs; [
            rustToolchain
            cargo-watch
            cargo-edit
            pkg-config
            openssl
            # Benchmark tools
            hyperfine
            poop
          ];

          RUST_BACKTRACE = "1";
          RUST_SRC_PATH = "${rustToolchain}/lib/rustlib/src/rust/library";

          shellHook = ''
            # Build and add lux to PATH
            if [ ! -f target/release/lux ] || [ Cargo.toml -nt target/release/lux ] || [ -n "$(find src -name '*.rs' -newer target/release/lux 2>/dev/null | head -1)" ]; then
              echo "Building lux..."
              cargo build --release
            fi
            export PATH="$PWD/target/release:$PATH"

            printf "\n"
            printf "  \033[1;35m╦  ╦ ╦╦ ╦\033[0m\n"
            printf "  \033[1;35m║  ║ ║╔╣\033[0m\n"
            printf "  \033[1;35m╩═╝╚═╝╩ ╩\033[0m  v0.1.0\n"
            printf "\n"
            printf "  Functional language with first-class effects\n"
            printf "\n"
            printf "  \033[1mCommands:\033[0m\n"
            printf "    lux                     Start the REPL\n"
            printf "    lux \033[3m<file.lux>\033[0m          Run a file\n"
            printf "    lux compile \033[3m<file.lux>\033[0m  JIT compile and run\n"
            printf "\n"
            printf "  \033[1mDevelopment:\033[0m\n"
            printf "    cargo build     Build the compiler\n"
            printf "    cargo test      Run tests\n"
            printf "\n"
          '';
        };

        packages.default = pkgs.rustPlatform.buildRustPackage {
          pname = "lux";
          version = "0.1.0";
          src = ./.;
          cargoLock.lockFile = ./Cargo.lock;

          nativeBuildInputs = [ pkgs.pkg-config ];
          buildInputs = [ pkgs.openssl ];

          doCheck = false;
        };

        # Benchmark scripts
        apps = {
          # Run hyperfine benchmark comparison
          bench = {
            type = "app";
            program = toString (pkgs.writeShellScript "lux-bench" ''
              set -e
              echo "=== Lux Performance Benchmarks ==="
              echo ""

              # Build Lux
              echo "Building Lux..."
              cd ${self}
              ${pkgs.cargo}/bin/cargo build --release 2>/dev/null

              # Compile benchmarks
              echo "Compiling benchmark binaries..."
              ./target/release/lux compile benchmarks/fib.lux -o /tmp/fib_lux 2>/dev/null
              ${pkgs.gcc}/bin/gcc -O3 benchmarks/fib.c -o /tmp/fib_c 2>/dev/null
              ${pkgs.rustc}/bin/rustc -C opt-level=3 -C lto benchmarks/fib.rs -o /tmp/fib_rust 2>/dev/null
              ${pkgs.zig}/bin/zig build-exe benchmarks/fib.zig -O ReleaseFast -femit-bin=/tmp/fib_zig 2>/dev/null

              echo ""
              echo "Running hyperfine benchmark..."
              echo ""
              ${pkgs.hyperfine}/bin/hyperfine --warmup 3 --runs 10 \
                --export-markdown /tmp/bench_results.md \
                '/tmp/fib_lux' \
                '/tmp/fib_c' \
                '/tmp/fib_rust' \
                '/tmp/fib_zig'

              echo ""
              echo "Results saved to /tmp/bench_results.md"
            '');
          };

          # Run poop benchmark for detailed CPU metrics
          bench-poop = {
            type = "app";
            program = toString (pkgs.writeShellScript "lux-bench-poop" ''
              set -e
              echo "=== Lux Performance Benchmarks (poop) ==="
              echo ""

              # Build Lux
              echo "Building Lux..."
              cd ${self}
              ${pkgs.cargo}/bin/cargo build --release 2>/dev/null

              # Compile benchmarks
              echo "Compiling benchmark binaries..."
              ./target/release/lux compile benchmarks/fib.lux -o /tmp/fib_lux 2>/dev/null
              ${pkgs.gcc}/bin/gcc -O3 benchmarks/fib.c -o /tmp/fib_c 2>/dev/null
              ${pkgs.rustc}/bin/rustc -C opt-level=3 -C lto benchmarks/fib.rs -o /tmp/fib_rust 2>/dev/null
              ${pkgs.zig}/bin/zig build-exe benchmarks/fib.zig -O ReleaseFast -femit-bin=/tmp/fib_zig 2>/dev/null

              echo ""
              echo "Running poop benchmark (detailed CPU metrics)..."
              echo ""
              ${pkgs.poop}/bin/poop '/tmp/fib_c' '/tmp/fib_lux' '/tmp/fib_rust' '/tmp/fib_zig'
            '');
          };

          # Quick benchmark (just Lux vs C)
          bench-quick = {
            type = "app";
            program = toString (pkgs.writeShellScript "lux-bench-quick" ''
              set -e
              echo "=== Quick Lux vs C Benchmark ==="
              echo ""

              cd ${self}
              ${pkgs.cargo}/bin/cargo build --release 2>/dev/null
              ./target/release/lux compile benchmarks/fib.lux -o /tmp/fib_lux 2>/dev/null
              ${pkgs.gcc}/bin/gcc -O3 benchmarks/fib.c -o /tmp/fib_c 2>/dev/null

              ${pkgs.hyperfine}/bin/hyperfine --warmup 3 '/tmp/fib_lux' '/tmp/fib_c'
            '');
          };
        };
      }
    );
}
