From a569142769bfe32f37c9c1418d40f2c49524b34c Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Wed, 18 Feb 2026 19:32:23 -0300 Subject: [PATCH] Add nix flake with dev shell and package build Provides a nix flake using crane + rust-overlay for building ethlambda and a development shell with Rust 1.92.0, clippy, rustfmt, and system dependencies (libclang, pkg-config). Includes direnv integration via .envrc and handles vergen-git2 env var fallback for nix builds where .git is absent. --- .envrc | 1 + flake.lock | 64 ++++++++++++++++++++++++++++++++++++ flake.nix | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..fb30092 --- /dev/null +++ b/flake.lock @@ -0,0 +1,64 @@ +{ + "nodes": { + "crane": { + "locked": { + "lastModified": 1771438068, + "narHash": "sha256-nGBbXvEZVe/egCPVPFcu89RFtd8Rf6J+4RFoVCFec0A=", + "owner": "ipetkov", + "repo": "crane", + "rev": "b5090e53e9d68c523a4bb9ad42b4737ee6747597", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1771177547, + "narHash": "sha256-trTtk3WTOHz7hSw89xIIvahkgoFJYQ0G43IlqprFoMA=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "ac055f38c798b0d87695240c7b761b82fc7e5bc2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1771384185, + "narHash": "sha256-KvmjUeA7uODwzbcQoN/B8DCZIbhT/Q/uErF1BBMcYnw=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "23dd7fa91602a68bd04847ac41bc10af1e6e2fd2", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..84b0139 --- /dev/null +++ b/flake.nix @@ -0,0 +1,96 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + crane.url = "github:ipetkov/crane"; + }; + + outputs = { self, nixpkgs, rust-overlay, crane }: + let + supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + + mkPkgs = system: import nixpkgs { + inherit system; + overlays = [ rust-overlay.overlays.default ]; + }; + + rustToolchain = pkgs: pkgs.rust-bin.stable."1.92.0".default.override { + extensions = [ "clippy" "rustfmt" ]; + }; + + mkCraneLib = pkgs: (crane.mkLib pkgs).overrideToolchain (rustToolchain pkgs); + in + { + packages = forAllSystems (system: + let + pkgs = mkPkgs system; + craneLib = mkCraneLib pkgs; + + commonArgs = { + pname = "ethlambda"; + src = craneLib.cleanCargoSource ./.; + strictDeps = true; + + nativeBuildInputs = with pkgs; [ + pkg-config + ]; + + buildInputs = with pkgs; [ + llvmPackages.libclang + ] ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isDarwin (with pkgs; [ + libiconv + apple-sdk_15 + ]); + + LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; + + # vergen-git2 falls back to env vars when .git is absent + VERGEN_GIT_SHA = self.shortRev or self.dirtyShortRev or "unknown"; + VERGEN_GIT_BRANCH = "nix"; + }; + + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + ethlambda = craneLib.buildPackage (commonArgs // { + inherit cargoArtifacts; + # Only install the main binary + cargoExtraArgs = "--bin ethlambda"; + }); + in + { + default = ethlambda; + inherit ethlambda; + } + ); + + devShells = forAllSystems (system: + let + pkgs = mkPkgs system; + in + { + default = pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + (rustToolchain pkgs) + pkg-config + cargo-watch + ]; + + buildInputs = with pkgs; [ + llvmPackages.libclang + ] ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isDarwin (with pkgs; [ + libiconv + apple-sdk_15 + ]); + + LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; + }; + } + ); + }; +}