add rust template

This commit is contained in:
Jonas Röger 2025-03-16 16:43:10 +01:00
parent b83a8f2db9
commit fd9a64cc5b
Signed by: jonas
GPG Key ID: 4000EB35E1AE0F07
7 changed files with 111 additions and 0 deletions

View File

@ -99,4 +99,10 @@
extraSpecialArgs = {inherit inputs;}; extraSpecialArgs = {inherit inputs;};
}; };
}; };
templates = {
rust = {
path = ./templates/rust;
description = "A simple rust binary template";
};
};
} }

17
templates/rust/.envrc Normal file
View File

@ -0,0 +1,17 @@
#!/usr/bin/env bash
# ^ make editor happy
#
# Use https://direnv.net/ to automatically load the dev shell.
#
if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4="
fi
watch_file nix/**
watch_file -- **/*.nix
# Adding files to git includes them in a flake
# But it is also a bit much reloading.
# watch_file .git/index .git/HEAD
use flake . --show-trace

3
templates/rust/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.direnv/
target/
result/

11
templates/rust/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "RustBinary"
description = "A rust binary"
publish = false
version = "0.1.0"
edition = "2021"
[[bin]]
name = "hello"
path = "src/main.rs"

60
templates/rust/flake.nix Normal file
View File

@ -0,0 +1,60 @@
{
description = "Rust-Nix";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
crate2nix = {
url = "github:nix-community/crate2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = inputs @ {
crate2nix,
flake-utils,
nixpkgs,
rust-overlay,
...
}:
flake-utils.lib.eachDefaultSystem (
system: let
# Overlay pkgs with rust-bin
overlays = [(import rust-overlay)];
pkgs = import nixpkgs {
inherit system overlays;
};
# Use rust-bin to generate the toolchain from rust-toolchain.toml
rust-toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
buildRustCrateForPkgs = pkgs:
pkgs.buildRustCrate.override {
rustc = rust-toolchain; # Use rustc from toolchain
cargo = rust-toolchain; # Use cargo from toolchain
};
# Cargo.nix for IFD
generatedCargoNix = crate2nix.tools.${system}.generatedCargoNix {
name = "rustnix";
src = ./.;
};
cargoNix = import generatedCargoNix {
inherit pkgs buildRustCrateForPkgs;
};
in {
packages = rec {
hello = cargoNix.rootCrate.build;
default = hello;
};
devShell = pkgs.mkShell {
buildInputs = [rust-toolchain];
};
}
);
}

View File

@ -0,0 +1,3 @@
[toolchain]
channel = "1.80.1"
components = [ "rustfmt", "rustc-dev", "rust-analyzer", "rust-src"]

View File

@ -0,0 +1,11 @@
use colored::*;
fn main() {
println!("{}", "Hello, world!".red());
println!("{}", "Hello, world!".green());
println!("{}", "Hello, world!".yellow());
println!("{}", "Hello, world!".blue());
println!("{}", "Hello, world!".purple());
println!("{}", "Hello, world!".cyan());
println!("{}", "Hello, world!".white());
}