From 2107737db196561b6453ac148c7afb6a1550eaf7 Mon Sep 17 00:00:00 2001
From: Ning Sun <sunng@protonmail.com>
Date: Wed, 18 Dec 2024 20:41:24 +0800
Subject: [PATCH] chore: make nix compilation environment config more robust
 (#5183)

* chore: improve nix-shell support

* fix: add pkg-config

* ci: add a github action to ensure build on clean system

* ci: optimise dependencies of task

* ci: move clean build to nightly
---
 .github/workflows/nightly-ci.yml | 11 +++++++++++
 rust-toolchain.toml              |  1 +
 shell.nix                        | 15 ++++++++++-----
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/.github/workflows/nightly-ci.yml b/.github/workflows/nightly-ci.yml
index b6ff247ffb9e..285fb61a7cb7 100644
--- a/.github/workflows/nightly-ci.yml
+++ b/.github/workflows/nightly-ci.yml
@@ -114,6 +114,17 @@ jobs:
           GT_S3_REGION: ${{ vars.AWS_CI_TEST_BUCKET_REGION }}
           UNITTEST_LOG_DIR: "__unittest_logs"
 
+  cleanbuild-linux-nix:
+    runs-on: ubuntu-latest-8-cores
+    timeout-minutes: 60
+    needs: [coverage, fmt, clippy, check]
+    steps:
+      - uses: actions/checkout@v4
+      - uses: cachix/install-nix-action@v27
+        with:
+          nix_path: nixpkgs=channel:nixos-unstable
+      - run: nix-shell --pure --run "cargo build"
+
   check-status:
     name: Check status
     needs: [sqlness-test, sqlness-windows, test-on-windows]
diff --git a/rust-toolchain.toml b/rust-toolchain.toml
index c986eedd9716..d12222a5d332 100644
--- a/rust-toolchain.toml
+++ b/rust-toolchain.toml
@@ -1,2 +1,3 @@
 [toolchain]
 channel = "nightly-2024-10-19"
+components = ["rust-analyzer"]
diff --git a/shell.nix b/shell.nix
index b255fe845c76..ce84a032764d 100644
--- a/shell.nix
+++ b/shell.nix
@@ -4,19 +4,24 @@ let
   pkgs = import nixpkgs { config = {}; overlays = []; };
 in
 
-pkgs.mkShellNoCC {
-  packages = with pkgs; [
+pkgs.mkShell rec {
+  nativeBuildInputs = with pkgs; [
+    pkg-config
     git
     clang
     gcc
-    mold
-    libgit2
     protobuf
+    mold
     (fenix.fromToolchainFile {
       dir = ./.;
     })
-    fenix.rust-analyzer
     cargo-nextest
+    taplo
+  ];
+
+  buildInputs = with pkgs; [
+    libgit2
   ];
 
+  LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath buildInputs;
 }