From 6a45c8c099c15958031aad85966004f9c38652a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Thu, 12 Jun 2025 11:12:01 +0200 Subject: [PATCH] refactor!: rust flake template --- templates/rust/Cargo.toml | 10 ++++ templates/rust/flake.nix | 120 ++++++++++++++++++------------------- templates/rust/src/main.rs | 3 + 3 files changed, 73 insertions(+), 60 deletions(-) create mode 100644 templates/rust/Cargo.toml create mode 100644 templates/rust/src/main.rs diff --git a/templates/rust/Cargo.toml b/templates/rust/Cargo.toml new file mode 100644 index 0000000..63b8562 --- /dev/null +++ b/templates/rust/Cargo.toml @@ -0,0 +1,10 @@ +# This line needs to come before anything else in Cargo.toml +cargo-features = ["codegen-backend"] + +[package] +name = "example" +version = "0.1.0" +edition = "2024" + +[profile.dev] +codegen-backend = "cranelift" diff --git a/templates/rust/flake.nix b/templates/rust/flake.nix index 2511ce7..dfa8a72 100644 --- a/templates/rust/flake.nix +++ b/templates/rust/flake.nix @@ -1,69 +1,69 @@ { inputs = { - crane.url = "github:ipetkov/crane"; - crane.inputs.nixpkgs.follows = "nixpkgs"; - fenix.url = "github:nix-community/fenix"; - fenix.inputs.nixpkgs.follows = "nixpkgs"; - flake-utils.url = "github:numtide/flake-utils"; - nixpkgs.url = "nixpkgs/nixos-unstable"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + rust-overlay.url = "github:oxalica/rust-overlay"; }; - outputs = { - crane, - flake-utils, - nixpkgs, - ... - } @ inputs: - flake-utils.lib.eachDefaultSystem ( - system: let - inherit (pkgs) lib; - pkgs = import nixpkgs {inherit system;}; - fenix = inputs.fenix.packages.${system}; - craneLib = crane.lib.${system}.overrideToolchain toolchain.toolchain; - mkSrc = extraPaths: - with lib.fileset; let - root = ./.; - rustFiles = fromSource (craneLib.cleanCargoSource root); - fileset = union rustFiles (unions extraPaths); - in - toSource {inherit root fileset;}; + outputs = inputs: + inputs.flake-parts.lib.mkFlake {inherit inputs;} { + systems = ["x86_64-linux"]; + perSystem = { + self', + pkgs, + system, + ... + }: let + runtimeDeps = with pkgs; []; + buildDeps = with pkgs; [pkg-config rustPlatform.bindgenHook]; + devDeps = with pkgs; []; - ## Customize here ## - toolchain = fenix.complete; # or fenix.stable; - stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv; + cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); + msrv = cargoToml.package.rust-version; + + rustPackage = features: + (pkgs.makeRustPlatform { + cargo = pkgs.rust-bin.stable.latest.minimal; + rustc = pkgs.rust-bin.stable.latest.minimal; + }).buildRustPackage { + inherit (cargoToml.package) name version; + src = ./.; + cargoLock.lockFile = ./Cargo.lock; + buildFeatures = features; + buildInputs = runtimeDeps; + nativeBuildInputs = buildDeps; + # Uncomment if your cargo tests require networking or otherwise + # don't play nicely with the Nix build sandbox: + # doCheck = false; + }; + + mkDevShell = rustc: + pkgs.mkShell.override { + stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.clangStdenv; + } { + shellHook = '' + export RUST_SRC_PATH=${pkgs.rustPlatform.rustLibSrc} + ''; + buildInputs = runtimeDeps; + nativeBuildInputs = buildDeps ++ devDeps ++ [rustc]; + }; in { - packages.default = craneLib.buildPackage { - inherit stdenv; - src = mkSrc []; - strictDeps = true; - buildInputs = - [ - # Add additional build inputs here - ] - ++ lib.optionals pkgs.stdenv.isDarwin [ - # Additional darwin specific inputs can be set here - pkgs.libiconv - ]; - # Additional environment variables can be set directly - # MY_CUSTOM_VAR = "some value"; + _module.args.pkgs = import inputs.nixpkgs { + inherit system; + overlays = [(import inputs.rust-overlay)]; }; - devShells.default = - pkgs.mkShell.override {inherit stdenv;} - { - nativeBuildInputs = with pkgs; - [ - # Add additional build inputs here - ] - ++ (with toolchain; [ - cargo - clippy - rustfmt - rustc - fenix.rust-analyzer - ]); - RUST_SRC_PATH = "${toolchain.rust-src}/lib/rustlib/src/rust/library"; - }; - } - ); + packages.default = self'.packages.example; + devShells.default = self'.devShells.nightly; + + packages.example = rustPackage ""; + + devShells.nightly = mkDevShell ((pkgs.rust-bin.selectLatestNightlyWith + (toolchain: toolchain.default)).override { + extensions = ["rustc-codegen-cranelift-preview"]; + }); + devShells.stable = mkDevShell pkgs.rust-bin.stable.latest.default; + devShells.msrv = mkDevShell pkgs.rust-bin.stable.${msrv}.default; + }; + }; } diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs new file mode 100644 index 0000000..5dd30d1 --- /dev/null +++ b/templates/rust/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello World!") +}