From fd9a64cc5bd7b62af2c0a455de6c9bfcffdfdbd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20R=C3=B6ger?= Date: Sun, 16 Mar 2025 16:43:10 +0100 Subject: [PATCH] add rust template --- flake.nix | 6 +++ templates/rust/.envrc | 17 +++++++++ templates/rust/.gitignore | 3 ++ templates/rust/Cargo.toml | 11 ++++++ templates/rust/flake.nix | 60 ++++++++++++++++++++++++++++++ templates/rust/rust-toolchain.toml | 3 ++ templates/rust/src/main.rs | 11 ++++++ 7 files changed, 111 insertions(+) create mode 100644 templates/rust/.envrc create mode 100644 templates/rust/.gitignore create mode 100644 templates/rust/Cargo.toml create mode 100644 templates/rust/flake.nix create mode 100644 templates/rust/rust-toolchain.toml create mode 100644 templates/rust/src/main.rs diff --git a/flake.nix b/flake.nix index 8415401..109c877 100644 --- a/flake.nix +++ b/flake.nix @@ -99,4 +99,10 @@ extraSpecialArgs = {inherit inputs;}; }; }; + templates = { + rust = { + path = ./templates/rust; + description = "A simple rust binary template"; + }; + }; } diff --git a/templates/rust/.envrc b/templates/rust/.envrc new file mode 100644 index 0000000..412cbef --- /dev/null +++ b/templates/rust/.envrc @@ -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 diff --git a/templates/rust/.gitignore b/templates/rust/.gitignore new file mode 100644 index 0000000..80c4fce --- /dev/null +++ b/templates/rust/.gitignore @@ -0,0 +1,3 @@ +.direnv/ +target/ +result/ diff --git a/templates/rust/Cargo.toml b/templates/rust/Cargo.toml new file mode 100644 index 0000000..aad94b0 --- /dev/null +++ b/templates/rust/Cargo.toml @@ -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" diff --git a/templates/rust/flake.nix b/templates/rust/flake.nix new file mode 100644 index 0000000..79a1bee --- /dev/null +++ b/templates/rust/flake.nix @@ -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]; + }; + } + ); +} diff --git a/templates/rust/rust-toolchain.toml b/templates/rust/rust-toolchain.toml new file mode 100644 index 0000000..58e81e8 --- /dev/null +++ b/templates/rust/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "1.80.1" +components = [ "rustfmt", "rustc-dev", "rust-analyzer", "rust-src"] diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs new file mode 100644 index 0000000..ed43722 --- /dev/null +++ b/templates/rust/src/main.rs @@ -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()); +}