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 = {
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;
};
};
}

View file

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