From c37a55d570c165364a40a43863037f4696e87e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sat, 20 Jan 2024 13:08:31 +0100 Subject: [PATCH] feat: add rust template --- flake.nix | 5 ++++ templates/rust/.cargo/config.toml | 2 ++ templates/rust/.envrc | 1 + templates/rust/.gitignore | 24 +++++++++++++++ templates/rust/flake.nix | 50 +++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 templates/rust/.cargo/config.toml create mode 100644 templates/rust/.envrc create mode 100644 templates/rust/.gitignore create mode 100644 templates/rust/flake.nix diff --git a/flake.nix b/flake.nix index a68262c..ef50c78 100644 --- a/flake.nix +++ b/flake.nix @@ -204,8 +204,13 @@ templates = { python = { + description = "Simple poetry shell.nix"; path = ./templates/python; }; + rust = { + description = "Crane + Fenix flake with mold for faster local builds."; + path = ./templates/rust; + }; }; }; }; diff --git a/templates/rust/.cargo/config.toml b/templates/rust/.cargo/config.toml new file mode 100644 index 0000000..4b929fe --- /dev/null +++ b/templates/rust/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.x86_64-unknown-linux-gnu] +linker = "clang" diff --git a/templates/rust/.envrc b/templates/rust/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/templates/rust/.envrc @@ -0,0 +1 @@ +use flake diff --git a/templates/rust/.gitignore b/templates/rust/.gitignore new file mode 100644 index 0000000..d38c33a --- /dev/null +++ b/templates/rust/.gitignore @@ -0,0 +1,24 @@ +# Created by https://www.toptal.com/developers/gitignore/api/rust,direnv +# Edit at https://www.toptal.com/developers/gitignore?templates=rust,direnv + +### direnv ### +.direnv +.envrc + +### Rust ### +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +# Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + +# End of https://www.toptal.com/developers/gitignore/api/rust,direnv diff --git a/templates/rust/flake.nix b/templates/rust/flake.nix new file mode 100644 index 0000000..3c67202 --- /dev/null +++ b/templates/rust/flake.nix @@ -0,0 +1,50 @@ +{ + inputs = { + crane = { + url = "github:ipetkov/crane"; + inputs = { + flake-utils.follows = "flake-utils"; + nixpkgs.follows = "nixpkgs"; + }; + }; + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + flake-utils.url = "github:numtide/flake-utils"; + nixpkgs.url = "nixpkgs/nixos-unstable"; + }; + + outputs = { self, crane, fenix, flake-utils, nixpkgs }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = (import nixpkgs) { + inherit system; + }; + toolchain = fenix.packages.${system}.stable; + craneLib = crane.lib.${system}.overrideToolchain toolchain.toolchain; + in + { + packages.default = + let + inherit (pkgs.lib) fileset; + in + craneLib.buildPackage { + src = fileset.toSource { + root = ./.; + fileset = fileset.intersection + (fileset.difference ./. + (fileset.unions [ ./.cargo ./flake.nix ./flake.lock ./.envrc ])) + (fileset.gitTracked ./.); + }; + }; + + devShell = pkgs.mkShell.override + { + stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.clangStdenv; + } + { + nativeBuildInputs = with toolchain; [ rustc cargo rust-analyzer clippy ]; + }; + }); +}