Browse Source

feat: basic support build kernel rust codes with nix (#1213)

chiichen 3 weeks ago
parent
commit
dcd79b408b
4 changed files with 67 additions and 2 deletions
  1. 7 0
      .gitignore
  2. 2 2
      kernel/Cargo.lock
  3. 8 0
      tools/nix-dev-shell/envrc-flake
  4. 50 0
      tools/nix-dev-shell/flake.nix

+ 7 - 0
.gitignore

@@ -20,3 +20,10 @@ cppcheck.xml
 compile_commands.json
 compile_commands.json
 /logs/
 /logs/
 *.log
 *.log
+
+# Nix dev files
+/.envrc
+/.direnv/
+/flake.nix
+flake.lock
+/default.nix

+ 2 - 2
kernel/Cargo.lock

@@ -143,9 +143,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
 
 
 [[package]]
 [[package]]
 name = "cc"
 name = "cc"
-version = "1.2.17"
+version = "1.2.19"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1fcb57c740ae1daf453ae85f16e37396f672b039e00d9d866e07ddb24e328e3a"
+checksum = "8e3a13707ac958681c13b39b458c073d0d9bc8a22cb1b2f4c8e55eb72c13f362"
 dependencies = [
 dependencies = [
  "jobserver",
  "jobserver",
  "libc",
  "libc",

+ 8 - 0
tools/nix-dev-shell/envrc-flake

@@ -0,0 +1,8 @@
+# If you want to use this as an .envrc file to create a shell with necessary components
+# to develop rustc, use the following command in the root of the rusr checkout:
+#
+# ln -s ./tools/nix-dev-shell/envrc-flake ./.envrc && nix flake update --flake ./tools/nix-dev-shell
+
+if nix flake show path:./tools/nix-dev-shell &> /dev/null; then
+  use flake path:./tools/nix-dev-shell
+fi

+ 50 - 0
tools/nix-dev-shell/flake.nix

@@ -0,0 +1,50 @@
+{
+  description = "dragonos-nix-dev";
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
+    fenix.url = "github:nix-community/fenix";
+    fenix.inputs.nixpkgs.follows = "nixpkgs";
+    utils.url = "github:numtide/flake-utils";
+  };
+
+  outputs = { self, nixpkgs, fenix, utils, ... } @ inputs:
+    utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = nixpkgs.legacyPackages.${system};
+        rustVer = fenix.packages.${system}.fromToolchainName {
+          name = "nightly-2024-11-05";
+          sha256 = "sha256-muM2tfsMpo2gsFsofhwHutPWgiPjjuwfBUvYCrwomAY=";
+        };
+        # 组合工具链并提取二进制路径
+        rustToolChain = rustVer.withComponents [
+          "cargo"
+          "clippy"
+          "rust-src"
+          "rustfmt"
+          "rust-analyzer"
+        ];
+      in {
+        devShells.default = pkgs.mkShell {
+          # 基础工具链
+          buildInputs = with pkgs; [
+            git
+            llvm
+            libclang
+            rustToolChain
+          ];
+
+          env = {
+              LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
+            };
+
+
+          # Shell启动脚本
+          shellHook = ''
+          '';
+        };
+
+        # 兼容旧版nix-shell命令
+        defaultPackage = self.devShells.${system}.default;
+      }
+    );
+}