From ad264854dffacc0889cabf2f46f23705007cee80 Mon Sep 17 00:00:00 2001 From: Samir Talwar Date: Tue, 12 Mar 2024 14:15:25 +0100 Subject: [PATCH] Set up a Nix build. (#121) This ensures the Nix shell uses the same Rust toolchain as `rustup`, and ensures we can build the package both with and without Nix. --- .github/workflows/nix.yaml | 54 +++++++++++++++++- .gitignore | 4 ++ Cargo.toml | 5 ++ flake.lock | 59 +++++++++++++++++--- flake.nix | 102 +++++++++++++++++++++++++--------- justfile | 2 + rust-connector-sdk/Cargo.toml | 5 +- 7 files changed, 192 insertions(+), 39 deletions(-) diff --git a/.github/workflows/nix.yaml b/.github/workflows/nix.yaml index 3fa535cc..df72d0b6 100644 --- a/.github/workflows/nix.yaml +++ b/.github/workflows/nix.yaml @@ -1,8 +1,56 @@ -name: test Nix support +name: Nix on: push jobs: + nix-build: + name: nix build + runs-on: ubuntu-latest + steps: + - name: Checkout 🛎️ + uses: actions/checkout@v3 + + - name: Install Nix ❄ + uses: DeterminateSystems/nix-installer-action@v9 + + - name: Run the Magic Nix Cache 🔌 + uses: DeterminateSystems/magic-nix-cache-action@v3 + + - name: Build the package + run: nix build + + nix-flake-check: + name: nix flake check + runs-on: ubuntu-latest + steps: + - name: Checkout 🛎️ + uses: actions/checkout@v3 + + - name: Install Nix ❄ + uses: DeterminateSystems/nix-installer-action@v9 + + - name: Run the Magic Nix Cache 🔌 + uses: DeterminateSystems/magic-nix-cache-action@v3 + + - name: Check the flake for errors + run: nix flake check + + nix-fmt: + name: nix fmt + runs-on: ubuntu-latest + steps: + - name: Checkout 🛎️ + uses: actions/checkout@v3 + + - name: Install Nix ❄ + uses: DeterminateSystems/nix-installer-action@v9 + + - name: Run the Magic Nix Cache 🔌 + uses: DeterminateSystems/magic-nix-cache-action@v3 + + - name: Check the formatting + run: nix fmt -- --check . + evaluate-nix-shell: name: Evaluate the Nix shell runs-on: ubuntu-latest @@ -11,10 +59,10 @@ jobs: uses: actions/checkout@v3 - name: Install Nix ❄ - uses: DeterminateSystems/nix-installer-action@v4 + uses: DeterminateSystems/nix-installer-action@v9 - name: Run the Magic Nix Cache 🔌 - uses: DeterminateSystems/magic-nix-cache-action@v2 + uses: DeterminateSystems/magic-nix-cache-action@v3 - name: Evaluate the Nix shell run: nix develop -c "true" diff --git a/.gitignore b/.gitignore index f3d2a7fb..e15b2a27 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ # Rust /target +# Nix +/result +/result-* + # direnv /.direnv diff --git a/Cargo.toml b/Cargo.toml index adeac446..97127f97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,10 @@ [workspace] resolver = "2" + +package.version = "0.1.0" +package.edition = "2021" +package.license = "Apache-2.0" + members = [ "rust-connector-sdk", ] diff --git a/flake.lock b/flake.lock index e5826ca7..a6adc9fe 100644 --- a/flake.lock +++ b/flake.lock @@ -1,15 +1,35 @@ { "nodes": { + "crane": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1710003968, + "narHash": "sha256-g8+K+mLiNG5uch35Oy9oDQBAmGSkCcqrd0Jjme7xiG0=", + "owner": "ipetkov", + "repo": "crane", + "rev": "10484f86201bb94bd61ecc5335b1496794fedb78", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "flake-utils": { "inputs": { "systems": "systems" }, "locked": { - "lastModified": 1692799911, - "narHash": "sha256-3eihraek4qL744EvQXsK1Ha6C3CR7nnT8X2qWap4RNk=", + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "f9e7cf818399d17d347f847525c5a5a8032e4e44", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", "type": "github" }, "original": { @@ -20,11 +40,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1694430955, - "narHash": "sha256-01IHejnGTdivgQoXTh/fS740JsbOTuCtJkgTyQ+Buq8=", + "lastModified": 1710151613, + "narHash": "sha256-7FhFufw9AFhKbALjLn2r7rpvg0z0Bjd5B/FeGdju6BM=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "bce7373adb44a76912a03548c4d31bf6f368f21b", + "rev": "245f9f2bc9deaef2248caa52ac126444ef03f810", "type": "github" }, "original": { @@ -36,8 +56,33 @@ }, "root": { "inputs": { + "crane": "crane", "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1710123130, + "narHash": "sha256-EoGL/WSM1M2L099Q91mPKO/FRV2iu2ZLOEp3y5sLfiE=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "73aca260afe5d41d3ebce932c8d896399c9d5174", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" } }, "systems": { diff --git a/flake.nix b/flake.nix index c9ba9e74..19b103be 100644 --- a/flake.nix +++ b/flake.nix @@ -7,48 +7,96 @@ # the line, `use flake`. { - description = "ndc-sdk"; + description = "ndc-hub"; inputs = { - flake-utils.url = github:numtide/flake-utils; - nixpkgs.url = github:NixOS/nixpkgs/master; + flake-utils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:NixOS/nixpkgs/master"; + + crane = { + url = "github:ipetkov/crane"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs = { + nixpkgs.follows = "nixpkgs"; + flake-utils.follows = "flake-utils"; + }; + }; }; outputs = { self , flake-utils , nixpkgs + , crane + , rust-overlay }: flake-utils.lib.eachDefaultSystem (system: let - pkgs = import nixpkgs { inherit system; }; - in - { - devShells.default = pkgs.mkShell { + pkgs = import nixpkgs { + inherit system; + overlays = [ rust-overlay.overlays.default ]; + }; + rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; + + buildArgs = with pkgs; { + pname = "ndc-sdk"; + + src = craneLib.cleanCargoSource (craneLib.path ./.); + + strictDeps = true; + + # build-time inputs nativeBuildInputs = [ - pkgs.cargo - pkgs.cargo-edit - pkgs.cargo-machete - pkgs.cargo-nextest - pkgs.cargo-watch - pkgs.clippy - pkgs.rust-analyzer - pkgs.rustPlatform.rustcSrc - pkgs.rustc - pkgs.rustfmt - - pkgs.just + openssl.dev # required to build Rust crates that can conduct TLS connections + pkg-config # required to find OpenSSL ]; - buildInputs = pkgs.lib.optionals pkgs.stdenv.isDarwin [ - pkgs.darwin.apple_sdk.frameworks.Security - pkgs.darwin.apple_sdk.frameworks.SystemConfiguration - pkgs.libiconv - ] + # runtime inputs + buildInputs = [ + openssl # required for TLS connections + protobuf # required by opentelemetry-proto, a dependency of axum-tracing-opentelemetry + ] ++ lib.optionals hostPlatform.isDarwin [ + # macOS-specific dependencies + libiconv + darwin.apple_sdk.frameworks.CoreFoundation + darwin.apple_sdk.frameworks.Security + darwin.apple_sdk.frameworks.SystemConfiguration + ]; + }; + in + { + packages = { + deps = craneLib.buildDepsOnly buildArgs; + default = craneLib.buildPackage + (buildArgs // { + cargoArtifacts = self.packages.${system}.deps; + doCheck = false; + }); + }; + + apps = { + example = flake-utils.lib.mkApp { + drv = self.packages.${system}.default; + exePath = "/bin/ndc_hub_example"; + }; + }; + + devShells.default = with pkgs; mkShell { + inputsFrom = [ self.packages.${system}.default ]; + + nativeBuildInputs = [ + rustToolchain + cargo-edit + cargo-machete + cargo-nextest + cargo-watch - ++ pkgs.lib.optionals pkgs.stdenv.isLinux [ - pkgs.pkg-config - pkgs.openssl + just ]; }; diff --git a/justfile b/justfile index ed40c3c5..b6fb3cf9 100644 --- a/justfile +++ b/justfile @@ -15,9 +15,11 @@ dev: format: cargo fmt --all + ! command -v nix > /dev/null || nix fmt format-check: cargo fmt --all --check + ! command -v nix > /dev/null || nix fmt -- --check . lint: cargo clippy --all-targets --all-features diff --git a/rust-connector-sdk/Cargo.toml b/rust-connector-sdk/Cargo.toml index 0579ddca..8f41a84f 100644 --- a/rust-connector-sdk/Cargo.toml +++ b/rust-connector-sdk/Cargo.toml @@ -1,7 +1,8 @@ [package] name = "ndc-sdk" -version = "0.1.0" -edition = "2021" +version.workspace = true +edition.workspace = true +license.workspace = true [lib] name = "ndc_sdk"