Skip to content

Commit

Permalink
Set up a Nix build. (#121)
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 authored Mar 12, 2024
1 parent dee4037 commit ad26485
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 39 deletions.
54 changes: 51 additions & 3 deletions .github/workflows/nix.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
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.

102 changes: 75 additions & 27 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
};

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 ad26485

Please sign in to comment.