From d98cfd62898c7ffb30543b83c29ba0afde5932ee Mon Sep 17 00:00:00 2001 From: Samuka007 Date: Mon, 24 Feb 2025 13:55:19 +0800 Subject: [PATCH] nix build available! --- .gitignore | 3 +- Cargo.lock | 22 ++++++------ app/Cargo.toml | 2 +- flake.lock | 36 ++++++++++--------- flake.nix | 98 ++++++++++++++++++++++++++++++++++---------------- 5 files changed, 100 insertions(+), 61 deletions(-) diff --git a/.gitignore b/.gitignore index a7d2ddd..b322924 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -.direnv/* \ No newline at end of file +.direnv/* +result \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 9cc9b29..d32afd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -243,17 +243,6 @@ version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" -[[package]] -name = "app" -version = "0.1.0" -dependencies = [ - "actix-web", - "serde", - "serde_json", - "service", - "tokio", -] - [[package]] name = "async-stream" version = "0.3.6" @@ -589,6 +578,17 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "faas-rs" +version = "0.1.0" +dependencies = [ + "actix-web", + "serde", + "serde_json", + "service", + "tokio", +] + [[package]] name = "fastrand" version = "2.3.0" diff --git a/app/Cargo.toml b/app/Cargo.toml index f8246a5..d9b1e81 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "app" +name = "faas-rs" version = "0.1.0" edition = "2024" diff --git a/flake.lock b/flake.lock index 046b997..0c50838 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,20 @@ { "nodes": { + "crane": { + "locked": { + "lastModified": 1739936662, + "narHash": "sha256-x4syUjNUuRblR07nDPeLDP7DpphaBVbUaSoeZkFbGSk=", + "owner": "ipetkov", + "repo": "crane", + "rev": "19de14aaeb869287647d9461cbd389187d8ecdb7", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "flake-utils": { "inputs": { "systems": "systems" @@ -34,24 +49,9 @@ "type": "github" } }, - "nixpkgs_2": { - "locked": { - "lastModified": 1736320768, - "narHash": "sha256-nIYdTAiKIGnFNugbomgBJR+Xv5F1ZQU+HfaBqJKroC0=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "4bc9c909d9ac828a039f288cf872d16d38185db8", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, "root": { "inputs": { + "crane": "crane", "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", "rust-overlay": "rust-overlay" @@ -59,7 +59,9 @@ }, "rust-overlay": { "inputs": { - "nixpkgs": "nixpkgs_2" + "nixpkgs": [ + "nixpkgs" + ] }, "locked": { "lastModified": 1740104932, diff --git a/flake.nix b/flake.nix index bf8ef27..019ad67 100644 --- a/flake.nix +++ b/flake.nix @@ -3,49 +3,85 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - rust-overlay.url = "github:oxalica/rust-overlay"; + crane.url = "github:ipetkov/crane"; flake-utils.url = "github:numtide/flake-utils"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; - outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }: + outputs = { self, nixpkgs, crane, flake-utils, rust-overlay, ... }: flake-utils.lib.eachDefaultSystem (system: let + # reference: https://crane.dev/examples/quick-start-workspace.html overlays = [ (import rust-overlay) ]; pkgs = import nixpkgs { inherit system overlays; }; - rustChannel = pkgs.rust-bin.nightly.latest; + + inherit (pkgs) lib; + + rustToolchainFor = p: p.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override { + extensions = [ "rust-src" ]; + }); + + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchainFor; + src = craneLib.cleanCargoSource ./.; + + commonArgs = { + inherit src; + strictDeps = true; + # Add additional build inputs here + buildInputs = with pkgs; [ + pkg-config + ]; + + nativeBuildInputs = with pkgs; [ + openssl + protobuf + ]; + }; + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + individualCrateArgs = commonArgs // { + inherit cargoArtifacts; + inherit (craneLib.crateNameFromCargoToml { inherit src; }) version; + doCheck = false; + }; + + fileSetForCrate = crate: lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + ./Cargo.toml + ./Cargo.lock + (craneLib.fileset.commonCargoSources ./app) + (craneLib.fileset.commonCargoSources ./service) + (craneLib.fileset.commonCargoSources crate) + ]; + }; + + faas-rs-crate = craneLib.buildPackage ( individualCrateArgs // { + pname = "faas-rs"; + cargoExtraArgs = "--bin faas-rs"; + src = fileSetForCrate ./app; + }); in with pkgs; { - devShells.default = mkShell { - buildInputs = [ - openssl - pkg-config - protobuf - containerd - runc - (rustChannel.default.override { - extensions = [ "rust-src" ]; - }) + checks = { inherit faas-rs-crate; }; + + packages.default = faas-rs-crate; + + devShells.default = craneLib.devShell { + checks = self.checks.${system}; + + inputsFrom = [ faas-rs-crate ]; + + packages = [ + pkgs.cargo-hakari + pkgs.containerd + pkgs.runc ]; }; - defaultPackage = stdenv.mkDerivation { - name = "faas-rs-${rustChannel._version}"; - src = ./.; # 当前项目目录作为源码 - buildInputs = [ - openssl - pkg-config - protobuf - rustChannel.rustc # Rust 编译器 - rustChannel.cargo # Cargo 工具 - ]; - buildPhase = '' - cargo build --release - ''; - installPhase = '' - mkdir -p $out/bin - cp target/release/* $out/bin - ''; - }; } ); }