From 962a28500c2906194dacbdacdd2aad34e705ddeb Mon Sep 17 00:00:00 2001 From: Samir Talwar Date: Mon, 11 Mar 2024 11:20:22 +0100 Subject: [PATCH] Set up a Nix build. 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 | 48 +++++++++++++++++++ .gitignore | 4 ++ Cargo.toml | 5 ++ flake.lock | 59 +++++++++++++++++++++--- flake.nix | 87 ++++++++++++++++++++++++++--------- justfile | 2 + rust-connector-sdk/Cargo.toml | 5 +- 7 files changed, 180 insertions(+), 30 deletions(-) diff --git a/.github/workflows/nix.yaml b/.github/workflows/nix.yaml index 3fa535ccb..90c4f426a 100644 --- a/.github/workflows/nix.yaml +++ b/.github/workflows/nix.yaml @@ -3,6 +3,54 @@ name: test Nix support 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@v4 + + - name: Run the Magic Nix Cache 🔌 + uses: DeterminateSystems/magic-nix-cache-action@v2 + + - name: Build the package + run: nix build + + nix-flake-check: + name: nix check + runs-on: ubuntu-latest + steps: + - name: Checkout 🛎️ + uses: actions/checkout@v3 + + - name: Install Nix ❄ + uses: DeterminateSystems/nix-installer-action@v4 + + - name: Run the Magic Nix Cache 🔌 + uses: DeterminateSystems/magic-nix-cache-action@v2 + + - name: Check the flake for errors + run: nix flake check + + nix-fmt: + name: nix check + runs-on: ubuntu-latest + steps: + - name: Checkout 🛎️ + uses: actions/checkout@v3 + + - name: Install Nix ❄ + uses: DeterminateSystems/nix-installer-action@v4 + + - name: Run the Magic Nix Cache 🔌 + uses: DeterminateSystems/magic-nix-cache-action@v2 + + - name: Check the formatting + run: nix fmt -- --check . + evaluate-nix-shell: name: Evaluate the Nix shell runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index f3d2a7fbe..e15b2a27e 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 adeac446d..97127f97d 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 e5826ca7e..a6adc9fe0 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 c9ba9e74c..b8e7378e4 100644 --- a/flake.nix +++ b/flake.nix @@ -7,49 +7,94 @@ # 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; }; + 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 = { + pname = "ndc-sdk"; + + src = craneLib.cleanCargoSource (craneLib.path ./.); + + strictDeps = true; + + # build-time inputs + nativeBuildInputs = [ + pkgs.openssl.dev # required to build Rust crates that can conduct TLS connections + pkgs.pkg-config # required to find OpenSSL + ]; + + # runtime inputs + buildInputs = pkgs.lib.optionals pkgs.hostPlatform.isDarwin [ + # macOS-specific dependencies + pkgs.libiconv + pkgs.darwin.apple_sdk.frameworks.CoreFoundation + pkgs.darwin.apple_sdk.frameworks.Security + pkgs.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 = pkgs.mkShell { + inputsFrom = [ self.packages.${system}.default ]; + nativeBuildInputs = [ - pkgs.cargo + rustToolchain 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 ]; - - buildInputs = pkgs.lib.optionals pkgs.stdenv.isDarwin [ - pkgs.darwin.apple_sdk.frameworks.Security - pkgs.darwin.apple_sdk.frameworks.SystemConfiguration - pkgs.libiconv - ] - - ++ pkgs.lib.optionals pkgs.stdenv.isLinux [ - pkgs.pkg-config - pkgs.openssl - ]; }; formatter = pkgs.nixpkgs-fmt; diff --git a/justfile b/justfile index ed40c3c5f..b6fb3cf98 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 0579ddca3..8f41a84f2 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"