{
  description = "Chainfire - Distributed Key-Value Store with Raft and Gossip";

  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" ];
        };

        nativeBuildInputs = with pkgs; [
          rustToolchain
          pkg-config
          protobuf
          cmake
        ];

        buildInputs = with pkgs; [
          # For RocksDB bindgen
          llvmPackages.libclang
          llvmPackages.clang

          # RocksDB build dependencies (let cargo build rocksdb from source)
          snappy
          lz4
          zstd
          zlib
          bzip2

          # OpenSSL for potential TLS support
          openssl
        ];

        # Environment variables for build
        shellHook = ''
          export LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib"
          export PROTOC="${pkgs.protobuf}/bin/protoc"
        '';

      in
      {
        devShells.default = pkgs.mkShell {
          inherit nativeBuildInputs buildInputs shellHook;

          LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
          PROTOC = "${pkgs.protobuf}/bin/protoc";
        };

        packages.default = pkgs.rustPlatform.buildRustPackage {
          pname = "chainfire";
          version = "0.1.0";
          src = ./.;

          cargoLock = {
            lockFile = ./Cargo.lock;
          };

          inherit nativeBuildInputs buildInputs;

          LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
          PROTOC = "${pkgs.protobuf}/bin/protoc";

          # Skip tests during nix build (run separately)
          doCheck = false;
        };
      }
    );
}
