From 1899ebbab76c81737a1a4d1d2cecafa76fd4be70 Mon Sep 17 00:00:00 2001 From: "dingfeng.wong" Date: Thu, 21 Aug 2025 14:33:14 +0800 Subject: [PATCH] add --- .vscode/openscaidentity | 1 + flake.nix | 94 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 .vscode/openscaidentity create mode 100644 flake.nix diff --git a/.vscode/openscaidentity b/.vscode/openscaidentity new file mode 100644 index 0000000..7d8ec72 --- /dev/null +++ b/.vscode/openscaidentity @@ -0,0 +1 @@ +zyafc6tj-07ud-383t-ygfa-nuzba4e3mgkt \ No newline at end of file diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..051fac7 --- /dev/null +++ b/flake.nix @@ -0,0 +1,94 @@ +{ + description = "A Rust project template using Nix flakes (rust-overlay, naersk, flake-utils)"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + rust-overlay.url = "github:oxalica/rust-overlay"; + naersk.url = "github:nix-community/naersk"; + }; + + outputs = inputs@{ self, nixpkgs, flake-utils, rust-overlay, naersk, ... }: + flake-utils.lib.eachSystem flake-utils.lib.allSystems (system: + let + projectName = "rust-template"; + projectVersion = "0.1.0"; + rustVersion = "1.78.0"; + + pkgs = import nixpkgs { + inherit system; + overlays = [ (import rust-overlay) ]; + }; + + rustToolchain = pkgs.rust-bin.fromRustVersion { + rustVersion = rustVersion; + extensions = [ "rust-src" "rustfmt" "clippy" ]; + }; + + rustLibSrc = "${rustToolchain}/lib/rustlib/src/rust"; + + commonBuildInputs = with pkgs; [ + glib + ]; + + commonNativeBuildInputs = with pkgs; [ + pkg-config + ]; + + devPackages = with pkgs; [ + # Rust-specific tools + rust-analyzer + sccache + cargo-watch + + # General developer experience + just + nil + alejandra + direnv + nix-direnv + ] ++ commonNativeBuildInputs ++ commonBuildInputs; + + myRustProject = naersk.lib."${system}".buildPackage { + inherit (self) src; + rustToolchain = rustToolchain; + pname = projectName; + version = projectVersion; + nativeBuildInputs = commonNativeBuildInputs; + buildInputs = commonBuildInputs; + cargoLock = { + lockFile = ./Cargo.lock; + }; + }; + + in + { + packages.default = myRustProject; + + packages.rustToolchain = rustToolchain; + + devShells.default = pkgs.mkShell { + name = "${projectName}-dev-shell"; + + inputsFrom = [ rustToolchain ]; + + RUST_SRC_PATH = rustLibSrc; + RUSTC_WRAPPER = "${pkgs.sccache}/bin/sccache"; + SCCACHE_DIR = "$HOME/.cache/sccache"; + + packages = devPackages; + + shellHook = '' + echo "Welcome to the Rust development shell!" + echo "Current Rust version: $(rustc --version)" + echo "Current Cargo version: $(cargo --version)" + ''; + }; + + apps.default = flake-utils.lib.mkApp { + drv = myRustProject; + }; + + checks.build = myRustProject; + }); +} \ No newline at end of file