Skip to content

Commit ffac228

Browse files
committed
refactor(ci): use nix for code generation
feat(nix): init nix flake for code generation Signed-off-by: Francesco Noacco <francesco.noacco@secomind.com>
1 parent 45d2dd4 commit ffac228

File tree

9 files changed

+268
-11
lines changed

9 files changed

+268
-11
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ concurrency:
1818
jobs:
1919
reuse:
2020
uses: ./.github/workflows/reuse-lint.yaml
21+
nix-check:
22+
needs: [ reuse ]
23+
uses: ./.github/workflows/nix-check.yaml
2124
rust-codegen:
2225
needs: [ reuse ]
2326
uses: ./.github/workflows/rust-codegen.yaml

.github/workflows/nix-check.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2023 SECO Mind Srl
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
name: nix-check
5+
6+
permissions:
7+
contents: read
8+
9+
on:
10+
workflow_call:
11+
12+
jobs:
13+
flake:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: DeterminateSystems/nix-installer-action@main
18+
with:
19+
github-token: ${{ secrets.GITHUB_TOKEN }}
20+
- uses: DeterminateSystems/magic-nix-cache-action@main
21+
with:
22+
# Don't send the diagnostics automatically
23+
diagnostic-endpoint: ""
24+
- run: nix flake check

.github/workflows/rust-codegen.yaml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,23 @@ on:
1111

1212
env:
1313
CARGO_TERM_COLOR: always
14-
PB_REL: https://github.com/protocolbuffers/protobuf/releases
15-
PB_VERSION: 24.3
1614

1715
jobs:
1816
rust-build:
1917
runs-on: ubuntu-latest
2018
steps:
2119
- uses: actions/checkout@v4
22-
- name: Install protoc
23-
run: |
24-
curl -LO "$PB_REL/download/v$PB_VERSION/protoc-$PB_VERSION-linux-x86_64.zip"
25-
unzip "protoc-$PB_VERSION-linux-x86_64.zip" -d "$HOME/.local"
26-
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
27-
- name: Install rust toolchain
28-
uses: dtolnay/rust-toolchain@stable
20+
- uses: DeterminateSystems/nix-installer-action@main
21+
with:
22+
github-token: ${{ secrets.GITHUB_TOKEN }}
23+
- uses: DeterminateSystems/magic-nix-cache-action@main
24+
with:
25+
# Don't send the diagnostics automatically
26+
diagnostic-endpoint: ""
2927
- name: Generate Rust code
30-
run: cargo r --manifest-path rust/Cargo.toml -p rust-codegen -- -w proto/ -o out/
28+
run: nix build -L .#rust
3129
- name: Upload generated code
3230
uses: actions/upload-artifact@v3
3331
with:
3432
name: rust-proto
35-
path: out/
33+
path: result/

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
.pre-commit-config.yaml
55
/out
66
.idea/
7+
8+
# nix
9+
/result

.nix/codegen/rust.nix

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2023 SECO Mind Srl
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
{ naereskBuilder, src, lib, protobuf, makeWrapper }:
5+
let
6+
unwrapped =
7+
naereskBuilder {
8+
inherit src;
9+
name = "rust-codegen-unwrapped";
10+
pname = "rust-codegen";
11+
cargoBuildOptions = (old: old ++ [ "--package=rust-codegen" ]);
12+
};
13+
in
14+
unwrapped.overrideAttrs {
15+
passthru = { inherit unwrapped; };
16+
name = "rust-codegen";
17+
buildInputs = [ makeWrapper ];
18+
postInstall = ''
19+
wrapProgram $out/bin/rust-codegen --set PATH ${lib.makeBinPath [protobuf]}
20+
'';
21+
}

.nix/rust.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2023 SECO Mind Srl
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
{ rust-codegen, stdenvNoCC, protoSrc, protobuf }:
5+
stdenvNoCC.mkDerivation {
6+
name = "rust-edgehog-device-forwarder-proto";
7+
nativeBuildInputs = [ protobuf ];
8+
src = protoSrc;
9+
buildPhase = ''
10+
mkdir -p $out
11+
${rust-codegen}/bin/rust-codegen -w . -o $out/
12+
'';
13+
}

flake.lock

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

flake.lock.license

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Copyright 2023 SECO Mind Srl
2+
SPDX-License-Identifier: Apache-2.0

flake.nix

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2023 SECO Mind Srl
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
{
5+
inputs = {
6+
nixpkgs.url = "nixpkgs/nixpkgs-unstable";
7+
flake-utils.url = "github:numtide/flake-utils";
8+
9+
# rust
10+
naeresk.url = "github:nix-community/naersk";
11+
rust-overlay.url = "github:oxalica/rust-overlay";
12+
};
13+
14+
outputs = { self, nixpkgs, flake-utils, naeresk, rust-overlay }:
15+
flake-utils.lib.eachDefaultSystem (system:
16+
let
17+
pkgs = import nixpkgs { inherit system; overlays = [ rust-overlay.overlays.default ]; };
18+
protobuf = pkgs.protobuf;
19+
rustToolchain = pkgs.rust-bin.stable.latest.default;
20+
naeresk' = pkgs.callPackage naeresk { cargo = rustToolchain; rustc = rustToolchain; };
21+
22+
protoSrc = ./proto;
23+
in
24+
{
25+
packages = {
26+
rustCodeGen = pkgs.callPackage ./.nix/codegen/rust.nix { naereskBuilder = naeresk'.buildPackage; src = ./rust; };
27+
rust = pkgs.callPackage .nix/rust.nix { inherit protoSrc; rust-codegen = self.packages.${system}.rustCodeGen.unwrapped; };
28+
};
29+
formatter = pkgs.nixpkgs-fmt;
30+
});
31+
}

0 commit comments

Comments
 (0)