refactor!: rust flake template

This commit is contained in:
Moritz Böhme 2025-06-12 11:12:01 +02:00
parent 63e029286d
commit 6a45c8c099
No known key found for this signature in database
GPG key ID: 970C6E89EB0547A9
3 changed files with 73 additions and 60 deletions

10
templates/rust/Cargo.toml Normal file
View file

@ -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"

View file

@ -1,69 +1,69 @@
{ {
inputs = { inputs = {
crane.url = "github:ipetkov/crane"; nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
crane.inputs.nixpkgs.follows = "nixpkgs"; flake-parts.url = "github:hercules-ci/flake-parts";
fenix.url = "github:nix-community/fenix"; rust-overlay.url = "github:oxalica/rust-overlay";
fenix.inputs.nixpkgs.follows = "nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "nixpkgs/nixos-unstable";
}; };
outputs = { outputs = inputs:
crane, inputs.flake-parts.lib.mkFlake {inherit inputs;} {
flake-utils, systems = ["x86_64-linux"];
nixpkgs, perSystem = {
... self',
} @ inputs: pkgs,
flake-utils.lib.eachDefaultSystem ( system,
system: let ...
inherit (pkgs) lib; }: let
pkgs = import nixpkgs {inherit system;}; runtimeDeps = with pkgs; [];
fenix = inputs.fenix.packages.${system}; buildDeps = with pkgs; [pkg-config rustPlatform.bindgenHook];
craneLib = crane.lib.${system}.overrideToolchain toolchain.toolchain; devDeps = with pkgs; [];
mkSrc = extraPaths:
with lib.fileset; let
root = ./.;
rustFiles = fromSource (craneLib.cleanCargoSource root);
fileset = union rustFiles (unions extraPaths);
in
toSource {inherit root fileset;};
## Customize here ## cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
toolchain = fenix.complete; # or fenix.stable; msrv = cargoToml.package.rust-version;
stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv;
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 { in {
packages.default = craneLib.buildPackage { _module.args.pkgs = import inputs.nixpkgs {
inherit stdenv; inherit system;
src = mkSrc []; overlays = [(import inputs.rust-overlay)];
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";
}; };
devShells.default = packages.default = self'.packages.example;
pkgs.mkShell.override {inherit stdenv;} devShells.default = self'.devShells.nightly;
{
nativeBuildInputs = with pkgs; packages.example = rustPackage "";
[
# Add additional build inputs here devShells.nightly = mkDevShell ((pkgs.rust-bin.selectLatestNightlyWith
] (toolchain: toolchain.default)).override {
++ (with toolchain; [ extensions = ["rustc-codegen-cranelift-preview"];
cargo });
clippy devShells.stable = mkDevShell pkgs.rust-bin.stable.latest.default;
rustfmt devShells.msrv = mkDevShell pkgs.rust-bin.stable.${msrv}.default;
rustc };
fenix.rust-analyzer };
]);
RUST_SRC_PATH = "${toolchain.rust-src}/lib/rustlib/src/rust/library";
};
}
);
} }

View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello World!")
}