Skip to content

Commit

Permalink
Set up a Nix build.
Browse files Browse the repository at this point in the history
This ensures the Nix shell uses the same Rust toolchain as `rustup`, and
ensures we can build the package both with and without Nix.
  • Loading branch information
SamirTalwar committed Mar 12, 2024
1 parent dee4037 commit 962a285
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 30 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/nix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Rust
/target

# Nix
/result
/result-*

# direnv
/.direnv

Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
[workspace]
resolver = "2"

package.version = "0.1.0"
package.edition = "2021"
package.license = "Apache-2.0"

members = [
"rust-connector-sdk",
]
Expand Down
59 changes: 52 additions & 7 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 66 additions & 21 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions rust-connector-sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 962a285

Please sign in to comment.