Rust Nix 模板项目使用教程
1. 项目目录结构及介绍
rust-nix-template/
├── .github/
│ └── workflows/
├── .vscode/
├── src/
├── .envrc
├── .gitattributes
├── .gitignore
├── Cargo.lock
├── Cargo.toml
├── LICENSE
├── README.md
├── flake.lock
├── flake.nix
├── justfile
└── rust-toolchain.toml
目录结构介绍
- .github/workflows/: 包含 GitHub Actions 的工作流配置文件。
- .vscode/: 包含 VSCode 的配置文件,用于 IDE 的设置。
- src/: 项目的源代码目录,通常包含 Rust 代码。
- .envrc: 用于 direnv 的配置文件,自动加载环境变量。
- .gitattributes: Git 属性配置文件,用于指定文件的 Git 行为。
- .gitignore: Git 忽略文件配置,指定哪些文件或目录不需要被 Git 管理。
- Cargo.lock: Cargo 的锁定文件,确保依赖版本的一致性。
- Cargo.toml: Cargo 的配置文件,定义项目的依赖和元数据。
- LICENSE: 项目的开源许可证文件。
- README.md: 项目的说明文档。
- flake.lock: Nix Flakes 的锁定文件,确保依赖版本的一致性。
- flake.nix: Nix Flakes 的配置文件,定义项目的开发环境和构建配置。
- justfile: 类似于 Makefile 的命令脚本文件,用于简化常用命令的执行。
- rust-toolchain.toml: Rust 工具链配置文件,指定使用的 Rust 版本。
2. 项目启动文件介绍
src/main.rs
src/main.rs
是 Rust 项目的入口文件,通常包含 main
函数,负责启动整个应用程序。
fn main() {
println!("Hello, world!");
}
flake.nix
flake.nix
是 Nix Flakes 的配置文件,定义了项目的开发环境和构建配置。它通常包含以下内容:
{
description = "A template Rust project with fully functional and no-frills Nix support.";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
in {
devShell = pkgs.mkShell {
buildInputs = with pkgs; [
rustc
cargo
rust-analyzer
];
};
});
}
3. 项目配置文件介绍
Cargo.toml
Cargo.toml
是 Cargo 的配置文件,定义了项目的依赖和元数据。
[package]
name = "rust-nix-template"
version = "0.1.0"
edition = "2021"
[dependencies]
flake.nix
flake.nix
是 Nix Flakes 的配置文件,定义了项目的开发环境和构建配置。
{
description = "A template Rust project with fully functional and no-frills Nix support.";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
in {
devShell = pkgs.mkShell {
buildInputs = with pkgs; [
rustc
cargo
rust-analyzer
];
};
});
}
justfile
justfile
是一个类似于 Makefile 的命令脚本文件,用于简化常用命令的执行。
default:
@echo "Available commands:"
@echo " just build - Build the project"
@echo " just run - Run the project"
@echo " just test - Run tests"
build:
cargo build
run:
cargo run
test:
cargo test
通过以上配置文件,可以方便地进行项目的构建、运行和测试。