Skip to content

Commit 75fd629

Browse files
committed
nix: try a workflow
1 parent 43e361c commit 75fd629

3 files changed

Lines changed: 339 additions & 0 deletions

File tree

.github/workflows/nix.yaml

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
name: Build with Nix
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
build:
13+
name: Build with Nix (Ubuntu)
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
with:
19+
path: smp-server-fixtures
20+
persist-credentials: false
21+
22+
# Install Nix with flakes enabled
23+
- name: Install Nix
24+
uses: DeterminateSystems/nix-installer-action@main
25+
with:
26+
extra-conf: |
27+
experimental-features = nix-command flakes
28+
29+
# Cache the Nix store for fast dependency restoration
30+
- name: Cache Nix store
31+
uses: DeterminateSystems/magic-nix-cache-action@main
32+
33+
# Install system GCC for portable native_sim builds
34+
- name: Install system GCC multilib
35+
run: |
36+
sudo apt-get update
37+
sudo apt-get install -y gcc gcc-multilib g++-multilib
38+
39+
# Cache the Zephyr SDK
40+
- name: Cache Zephyr SDK
41+
uses: actions/cache@v4
42+
with:
43+
path: ~/zephyr-sdk-0.17.4
44+
key: zephyr-sdk-0.17.4-${{ runner.os }}
45+
46+
# Cache the workspace (zephyr, modules, bootloader)
47+
# Exclude the current repo to avoid caching build artifacts
48+
- name: Cache Zephyr workspace
49+
uses: actions/cache@v4
50+
with:
51+
path: |
52+
zephyr
53+
modules
54+
bootloader
55+
.west
56+
key: zephyr-workspace-${{ runner.os }}-${{ hashFiles('smp-server-fixtures/west.yml') }}
57+
restore-keys: |
58+
zephyr-workspace-${{ runner.os }}-
59+
60+
# Cache Python virtual environment
61+
# Invalidates when west.yml changes (affects Python dependencies via west packages)
62+
- name: Cache Python venv
63+
uses: actions/cache@v4
64+
with:
65+
path: .venv
66+
key: python-venv-${{ runner.os }}-${{ hashFiles('smp-server-fixtures/west.yml') }}
67+
restore-keys: |
68+
python-venv-${{ runner.os }}-
69+
70+
# Cache ccache for faster C/C++ compilation
71+
- name: Cache ccache
72+
uses: actions/cache@v4
73+
with:
74+
path: .ccache
75+
key: ccache-${{ runner.os }}-${{ github.sha }}
76+
restore-keys: |
77+
ccache-${{ runner.os }}-
78+
79+
# Initialize west workspace if not cached
80+
# Note: west update will be run later by nix develop shell hook
81+
# This step just sets up the workspace structure
82+
- name: Initialize west workspace
83+
working-directory: smp-server-fixtures
84+
run: |
85+
if [ ! -d "../.west" ]; then
86+
echo "Initializing west workspace..."
87+
pip3 install west
88+
cd ..
89+
west init -l smp-server-fixtures
90+
else
91+
echo "West workspace already initialized (from cache)"
92+
fi
93+
94+
# Enter Nix development environment and build
95+
- name: Build with Nix environment
96+
working-directory: smp-server-fixtures
97+
run: |
98+
nix develop --command bash -c '
99+
set -e
100+
101+
# Show environment info
102+
echo "=== Environment Info ==="
103+
echo "GCC: $(which gcc) - $(gcc --version | head -1)"
104+
echo "Python: $(which python) - $(python --version)"
105+
echo "West: $(which west) - $(west --version)"
106+
echo "Ccache: $(ccache -s || echo "empty")"
107+
echo ""
108+
109+
# Build native_sim serial fixture
110+
echo "=== Building native_sim serial fixture ==="
111+
west build -b native_sim apps/smp-server -T smp_server.fixture.serial.native_sim
112+
113+
# Run Twister integration tests
114+
echo "=== Running Twister Integration Tests ==="
115+
west twister -T apps -v --inline-logs --integration --short-build-path -O /tmp/twister-out
116+
117+
# Show ccache stats
118+
echo ""
119+
echo "=== Ccache Statistics ==="
120+
ccache -s
121+
'
122+
123+
# Upload native_sim serial fixture
124+
- name: Upload native_sim serial fixture
125+
uses: actions/upload-artifact@v4
126+
with:
127+
name: smp-server-fixture-serial-${{ github.sha }}-native_sim-nix
128+
path: smp-server-fixtures/build/smp-server/zephyr/zephyr.exe
129+
130+
# Upload twister artifacts
131+
- name: Upload twister artifacts
132+
uses: actions/upload-artifact@v4
133+
with:
134+
name: smp-server-fixtures-nix-${{ github.sha }}-ubuntu
135+
path: |
136+
/tmp/twister-out/**/*.hex
137+
/tmp/twister-out/**/*.bin
138+
/tmp/twister-out/**/*.exe
139+
/tmp/twister-out/**/*.elf
140+
/tmp/twister-out/**/*.map
141+
/tmp/twister-out/**/*.stat
142+
/tmp/twister-out/**/zephyr.dts
143+
/tmp/twister-out/**/build.log
144+
/tmp/twister-out/twister.log

flake.lock

Lines changed: 61 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: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
{
2+
description = "Zephyr RTOS development environment for smp-server-fixtures";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs =
10+
{
11+
self,
12+
nixpkgs,
13+
flake-utils,
14+
}:
15+
flake-utils.lib.eachDefaultSystem (
16+
system:
17+
let
18+
pkgs = import nixpkgs {
19+
inherit system;
20+
};
21+
22+
# These packages match the apt requirements:
23+
# git cmake ninja-build gperf ccache dfu-util device-tree-compiler wget
24+
# python3-dev python3-venv python3-tk xz-utils file make gcc gcc-multilib
25+
# g++-multilib libsdl2-dev libmagic1
26+
buildInputs =
27+
with pkgs;
28+
[
29+
git
30+
cmake
31+
ninja
32+
gperf
33+
ccache
34+
gnumake
35+
file
36+
wget
37+
xz
38+
dtc
39+
# NOTE: We intentionally do NOT include gcc/stdenv.cc from Nix
40+
# For native_sim builds, we need to use the system's GCC to produce
41+
# portable binaries that can run on standard Ubuntu systems
42+
uv
43+
pkg-config
44+
nixfmt-rfc-style
45+
]
46+
++ pkgs.lib.optionals pkgs.stdenv.isLinux [
47+
];
48+
49+
# Shell hook that sets up the environment
50+
shellHook = ''
51+
set -e # Exit on any error
52+
53+
echo "🚀 Entering Zephyr development environment"
54+
55+
# Prepend system paths to ensure system GCC is found first
56+
# This is critical for portable native_sim builds
57+
export PATH="/usr/bin:/usr/local/bin:$PATH"
58+
59+
# Check for system GCC (required for portable native_sim builds)
60+
if ! command -v gcc &> /dev/null; then
61+
echo "❌ Error: System GCC not found in PATH"
62+
echo " For portable native_sim builds, you need system GCC installed."
63+
echo " Install with: sudo apt install gcc gcc-multilib g++-multilib"
64+
exit 1
65+
fi
66+
67+
SYSTEM_GCC=$(which gcc)
68+
if [[ "$SYSTEM_GCC" == *"/nix/store"* ]]; then
69+
echo "❌ Error: GCC is still pointing to Nix store: $SYSTEM_GCC"
70+
echo " System GCC should be in /usr/bin, not Nix store"
71+
exit 1
72+
fi
73+
echo "✓ Using system GCC: $SYSTEM_GCC ($(gcc --version | head -1))"
74+
75+
# Ensure we're in a west workspace
76+
if [ ! -d "../.west" ]; then
77+
echo "❌ Error: Not in a west workspace root. Directory ../.west not found."
78+
echo " Please run 'west init' first or ensure you're in the correct directory."
79+
exit 1
80+
fi
81+
82+
# Set up ccache
83+
export CCACHE_DIR="../.ccache"
84+
export CCACHE_MAXSIZE="2G"
85+
export USE_CCACHE=1
86+
mkdir -p "$CCACHE_DIR" || { echo "❌ Failed to create ccache directory"; exit 1; }
87+
88+
# Set up Python virtual environment with uv
89+
if [ ! -d "../.venv" ]; then
90+
echo "📦 Creating Python 3.13 virtual environment with uv..."
91+
cd .. || exit 1
92+
uv venv --python 3.13 --seed || { echo "❌ Failed to create venv"; exit 1; }
93+
cd - > /dev/null || exit 1
94+
fi
95+
96+
# Activate the virtual environment
97+
if [ -f "../.venv/bin/activate" ]; then
98+
source ../.venv/bin/activate || { echo "❌ Failed to activate venv"; exit 1; }
99+
else
100+
echo "❌ Error: Virtual environment not found at ../.venv"
101+
exit 1
102+
fi
103+
104+
echo "📦 Installing west..."
105+
pip install west || { echo "❌ Failed to install west"; exit 1; }
106+
107+
# Run west update to sync dependencies
108+
echo "🔄 Running west update..."
109+
west update || { echo "❌ west update failed"; exit 1; }
110+
111+
echo "📦 Installing Python dependencies via west..."
112+
west zephyr-export || { echo "❌ west zephyr-export failed"; exit 1; }
113+
west packages pip --install || { echo "❌ west packages pip --install failed"; exit 1; }
114+
115+
echo "🔧 Installing Zephyr SDK (arm-zephyr-eabi toolchain)..."
116+
west sdk install -t arm-zephyr-eabi || { echo "❌ Zephyr SDK installation failed"; exit 1; }
117+
118+
# Set Zephyr environment variables with absolute paths
119+
export ZEPHYR_BASE="$(realpath ../zephyr)"
120+
echo "✓ ZEPHYR_BASE set to: $ZEPHYR_BASE"
121+
122+
set +e # Restore normal error handling for interactive shell
123+
'';
124+
125+
in
126+
{
127+
devShells.default = pkgs.mkShell {
128+
inherit buildInputs shellHook;
129+
130+
# Environment variables are set in shellHook to use absolute paths
131+
};
132+
}
133+
);
134+
}

0 commit comments

Comments
 (0)