Skip to content

Commit 88c15eb

Browse files
committed
feat: Add Nix flake and CI
1 parent e258d07 commit 88c15eb

File tree

8 files changed

+320
-4
lines changed

8 files changed

+320
-4
lines changed

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

.github/workflows/nix.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Nix CI
2+
3+
# If using Garnix, edit to run after Garnix checks using `on: check_suite`
4+
# https://garnix.io/docs/gh-actions
5+
on:
6+
push:
7+
branches: [main, nix-flake]
8+
pull_request:
9+
workflow_dispatch:
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
# Runs Rust tests via Nix
17+
nix-test:
18+
name: Nix Tests
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v5
22+
- uses: cachix/install-nix-action@v31
23+
with:
24+
nix_path: nixpkgs=channel:nixos-unstable
25+
github_access_token: ${{ secrets.GITHUB_TOKEN }}
26+
- run: nix flake check --print-build-logs
27+
- run: nix build --print-build-logs --rebuild --accept-flake-config
28+
29+
# Tests Nix devShell support on Ubuntu
30+
nix-devshell:
31+
name: Nix devShell Tests
32+
runs-on: ubuntu-latest
33+
steps:
34+
- uses: actions/checkout@v5
35+
- uses: cachix/install-nix-action@v31
36+
with:
37+
nix_path: nixpkgs=channel:nixos-unstable
38+
github_access_token: ${{ secrets.GITHUB_TOKEN }}
39+
# Builds and runs tests using Lake as a Nix package
40+
- run: nix develop --accept-flake-config --command bash -c "cargo build && cargo nextest run"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ target/
88

99
# MSVC Windows builds of rustc generate these, which store debugging information
1010
*.pdb
11+
12+
.direnv/
13+
result

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ version = "0.1.0"
44
edition = "2024"
55
authors = ["Argument Engineering <[email protected]>"]
66
license = "MIT OR Apache-2.0"
7-
# EDIT AS NEEDED
87
rust-version = "1.88"
98

109
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Base template for a Rust library with CI, config files, and branch protection
88
```
99
# Replace all occurrences with the desired library name
1010
$ rg -i template --glob '!deny.toml'
11-
# Update Rust toolchain if desired
12-
$ rg "EDIT AS NEEDED"
11+
# Update Rust toolchain if desired in `rust-toolchain.toml` and `Cargo.toml`
1312
# Then rewrite this `README`
1413
```
14+
- Optional: Enable the Warpbuild and Garnix GitHub apps for CI and Nix caching
1515
- Create a branch protection rule for `main` and enable the following as needed:
1616
- Require a pull request before merging
1717
- Require 1 approval

flake.lock

Lines changed: 150 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{
2+
description = "Rust Nix flake";
3+
4+
# Must first enable Garnix GitHub app for the repo
5+
#nixConfig = {
6+
# extra-substituters = [
7+
# "https://cache.garnix.io"
8+
# ];
9+
# extra-trusted-public-keys = [
10+
# "cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g="
11+
# ];
12+
#};
13+
14+
inputs = {
15+
# System packages
16+
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
17+
18+
# Helper: flake-parts for easier outputs
19+
flake-parts.url = "github:hercules-ci/flake-parts";
20+
21+
# Rust-related inputs
22+
fenix = {
23+
url = "github:nix-community/fenix";
24+
# Follow top-level nixpkgs so we stay in sync
25+
inputs.nixpkgs.follows = "nixpkgs";
26+
};
27+
28+
crane.url = "github:ipetkov/crane";
29+
flake-utils.url = "github:numtide/flake-utils";
30+
};
31+
32+
outputs = inputs @ { nixpkgs, flake-parts, fenix, crane, ... }:
33+
flake-parts.lib.mkFlake { inherit inputs; } {
34+
# Systems we want to build for
35+
systems = [
36+
"aarch64-darwin"
37+
"aarch64-linux"
38+
"x86_64-darwin"
39+
"x86_64-linux"
40+
];
41+
42+
perSystem = { system, pkgs, ... }:
43+
let
44+
# Pins the Rust toolchain
45+
rustToolchain = fenix.packages.${system}.fromToolchainFile {
46+
file = ./rust-toolchain.toml;
47+
# Update this hash when `rust-toolchain.toml` changes
48+
# Just copy the expected hash from the `nix build` error message
49+
sha256 = "sha256-Qxt8XAuaUR2OMdKbN4u8dBJOhSHxS+uS06Wl9+flVEk=";
50+
};
51+
# Rust package
52+
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
53+
commonArgs = {
54+
src = craneLib.cleanCargoSource ./.;
55+
strictDeps = true;
56+
57+
buildInputs = [
58+
# Add additional build inputs here
59+
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
60+
# Additional darwin specific inputs can be set here
61+
pkgs.libiconv
62+
];
63+
};
64+
craneLibLLvmTools = craneLib.overrideToolchain rustToolchain;
65+
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
66+
67+
template = craneLib.buildPackage (commonArgs // {
68+
inherit cargoArtifacts;
69+
});
70+
71+
# Run tests with cargo-nextest
72+
# Consider setting `doCheck = false` on `my-crate` if you do not want
73+
# the tests to run twice
74+
template-nextest = craneLib.cargoNextest (
75+
commonArgs
76+
// {
77+
inherit cargoArtifacts;
78+
partitions = 1;
79+
partitionType = "count";
80+
cargoNextestPartitionsExtraArgs = "--no-tests=pass";
81+
}
82+
);
83+
84+
# Workspace example for `client` and `server` subcrates
85+
# serverPkg = craneLib.buildPackage (commonArgs // {
86+
# inherit cargoArtifacts;
87+
# pname = "server";
88+
# cargoExtraArgs = "-p server";
89+
# });
90+
# clientPkg = craneLib.buildPackage (commonArgs // {
91+
# inherit cargoArtifacts;
92+
# pname = "client";
93+
# cargoExtraArgs = "-p client";
94+
# });
95+
in
96+
{
97+
checks = {
98+
inherit template template-nextest;
99+
};
100+
101+
packages = {
102+
default = template-nextest;
103+
104+
# Workspace example
105+
# server = serverPkg;
106+
# client = clientPkg;
107+
};
108+
109+
# Provide a dev shell with `cargo` and the pinned toolchain
110+
devShells.default = pkgs.mkShell {
111+
packages = with pkgs; [
112+
pkg-config
113+
openssl
114+
ocl-icd
115+
gcc
116+
clang
117+
rustToolchain
118+
rust-analyzer
119+
cargo-nextest
120+
];
121+
};
122+
};
123+
};
124+
}

rust-toolchain.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[toolchain]
22
# The default profile includes rustc, rust-std, cargo, rust-docs, rustfmt and clippy.
33
profile = "default"
4-
# EDIT AS NEEDED
54
channel = "1.88"

0 commit comments

Comments
 (0)