Skip to content

Commit 173a2f8

Browse files
committed
build with flake.nix
1 parent 72aabd8 commit 173a2f8

2 files changed

Lines changed: 63 additions & 6 deletions

File tree

.github/workflows/build.yml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and Push Docker Images with Nix
1+
name: Build and Push Docker Images with Nix Flakes
22

33
on:
44
push:
@@ -31,15 +31,19 @@ jobs:
3131
- name: Checkout
3232
uses: actions/checkout@v4
3333

34-
- name: Set up Nix
34+
- name: Set up Nix with Flakes support
3535
uses: cachix/install-nix-action@v22
3636
with:
3737
nix_path: nixpkgs=channel:nixos-unstable
38+
extra_nix_config: |
39+
experimental-features = nix-command flakes
40+
allow-import-from-derivation = true
3841
39-
- name: Build Docker image with Nix
42+
- name: Build Docker image with Nix Flakes
4043
run: |
41-
# 使用 nix-shell 提供所需的环境,避免全局安装
42-
nix-shell -p dockerTools gnutar gzip --run "nix-build docker.nix --option sandbox false"
44+
# 使用 nix flake 构建,利用 flake.nix 中定义的环境
45+
# 启用缓存以提高构建速度
46+
nix build .#docker-image --option sandbox false
4347
4448
- name: Set up Docker Buildx
4549
uses: docker/setup-buildx-action@v3
@@ -54,6 +58,7 @@ jobs:
5458

5559
- name: Load Docker image
5660
run: |
61+
# 使用 nix build 的结果加载 Docker 镜像
5762
docker load < result
5863
5964
- name: Extract metadata
@@ -177,13 +182,17 @@ jobs:
177182
steps:
178183
- name: Generate summary
179184
run: |
180-
echo "## 🐳 Docker Image Build Summary" >> $GITHUB_STEP_SUMMARY
185+
echo "## 🐳 Docker Image Build Summary (Nix Flakes)" >> $GITHUB_STEP_SUMMARY
181186
echo "" >> $GITHUB_STEP_SUMMARY
182187
echo "**Image:** \`${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}\`" >> $GITHUB_STEP_SUMMARY
183188
echo "" >> $GITHUB_STEP_SUMMARY
184189
echo "**Tags:**" >> $GITHUB_STEP_SUMMARY
185190
echo "- \`secure-latest\`" >> $GITHUB_STEP_SUMMARY
186191
echo "" >> $GITHUB_STEP_SUMMARY
192+
echo "**Build System:**" >> $GITHUB_STEP_SUMMARY
193+
echo "- ✅ Nix Flakes for reproducible builds" >> $GITHUB_STEP_SUMMARY
194+
echo "- ✅ Declarative environment management" >> $GITHUB_STEP_SUMMARY
195+
echo "" >> $GITHUB_STEP_SUMMARY
187196
echo "**Features:**" >> $GITHUB_STEP_SUMMARY
188197
echo "- ✅ Secure Python 3.12 environment" >> $GITHUB_STEP_SUMMARY
189198
echo "- ✅ UV package manager" >> $GITHUB_STEP_SUMMARY

flake.nix

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
inputs = {
3+
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
4+
flake-utils.url = "github:numtide/flake-utils";
5+
6+
rust-overlay = {
7+
url = "github:oxalica/rust-overlay";
8+
inputs = {
9+
nixpkgs.follows = "nixpkgs";
10+
};
11+
};
12+
};
13+
14+
outputs = {
15+
self,
16+
nixpkgs,
17+
flake-utils,
18+
rust-overlay,
19+
}:
20+
flake-utils.lib.eachDefaultSystem (
21+
system: let
22+
pkgs = import nixpkgs {
23+
inherit system;
24+
overlays = [(import rust-overlay)];
25+
};
26+
in {
27+
formatter = pkgs.alejandra;
28+
devShells.default = import ./shell.nix {inherit pkgs;};
29+
30+
packages = {
31+
reaslab-proto = import ./pkgs/reaslab-proto.nix {inherit pkgs;};
32+
33+
reaslab-be = import ./pkgs/reaslab-be.nix {
34+
inherit pkgs;
35+
reaslab-proto = self.outputs.packages."${system}".reaslab-proto;
36+
};
37+
38+
reaslab-be-image = import ./pkgs/reaslab-be-image.nix {
39+
inherit pkgs;
40+
reaslab-be = self.outputs.packages."${system}".reaslab-be;
41+
};
42+
43+
# Docker image package for CI/CD
44+
docker-image = import ./docker.nix {inherit pkgs;};
45+
};
46+
}
47+
);
48+
}

0 commit comments

Comments
 (0)