commit ca266531a98df80c64782c99b88431c4c5542ac2 Author: Stefan Junker Date: Mon Dec 23 11:55:36 2024 +0100 feat: init with rust lib and bin crate diff --git a/README.md b/README.md new file mode 100644 index 0000000..77d248c --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# blueprint-universe + +This repository contains an opinionated [blueprint](https://github.com/numtide/blueprint) template. + diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..b2c8dd8 --- /dev/null +++ b/flake.nix @@ -0,0 +1,5 @@ +{ + outputs = _: { + templates.default.path = ./templates/default; + }; +} diff --git a/templates/default/.envrc b/templates/default/.envrc new file mode 100644 index 0000000..9706a5e --- /dev/null +++ b/templates/default/.envrc @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# Used by https://direnv.net + +# Automatically reload when this file changes +watch_file ./nix/devshells/default.nix +watch_file ./nix/*.nix + +# Load `nix develop` +use flake .#rust + +# Extend the environment with per-user overrides +source_env_if_exists .envrc.local diff --git a/templates/default/.gitignore b/templates/default/.gitignore new file mode 100644 index 0000000..ae0badb --- /dev/null +++ b/templates/default/.gitignore @@ -0,0 +1,24 @@ +# direnv +.direnv/ +.env +.envrc.local + +# nix +result +result-* + +# rust +target/ + +# nixago: ignore-linked-files +/treefmt.toml + +# nats +rust/*/tmp +rust/*/jwt +rust/*/*/test_leaf_server.conf +rust/*/*/leaf_server.conf +rust/*/*/resolver.conf +leaf_server.conf +.local + diff --git a/templates/default/.vscode/settings.json b/templates/default/.vscode/settings.json new file mode 100644 index 0000000..f78dcd9 --- /dev/null +++ b/templates/default/.vscode/settings.json @@ -0,0 +1,18 @@ +{ + "editor.defaultFormatter": "ibecker.treefmt-vscode", + "direnv.restart.automatic": true, + "[nix]": { + "editor.defaultFormatter": "jnoortheen.nix-ide", + "editor.formatOnSave": true + }, + "nix.enableLanguageServer": true, + "nix.serverPath": "nil", + "nix.serverSettings": { + // settings for 'nil' LSP + "nil": { + "formatting": { + "command": ["treefmt-nix", "--stdin", "neverexists.nix"] + } + } + } +} diff --git a/templates/default/Cargo.lock b/templates/default/Cargo.lock new file mode 100644 index 0000000..1822a9b --- /dev/null +++ b/templates/default/Cargo.lock @@ -0,0 +1,246 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" + +[[package]] +name = "app" +version = "0.1.0" +dependencies = [ + "anyhow", + "env_logger", + "lib", + "log", +] + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "lib" +version = "0.1.0" +dependencies = [ + "anyhow", + "log", +] + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/templates/default/Cargo.toml b/templates/default/Cargo.toml new file mode 100644 index 0000000..8a28e34 --- /dev/null +++ b/templates/default/Cargo.toml @@ -0,0 +1,19 @@ +[workspace] +resolver = "2" +package.version = "0.1.0" +metadata.crane.name = "blueprint-universe" + +members = [ + "rust/*", +] + +[workspace.dependencies] +anyhow = "1.0" +env_logger = "0.11.5" +log = "0.4.17" + +[profile.dev] +opt-level = "z" + +[profile.release] +opt-level = "z" diff --git a/templates/default/README.md b/templates/default/README.md new file mode 100644 index 0000000..74ed709 --- /dev/null +++ b/templates/default/README.md @@ -0,0 +1,25 @@ +# rust workspace template + +This is an experiment to contain the code for all components in a single repository, also known as a monorepository. + +## Repository Layout + +The code is grouped by language or framework name. + +### Nix + +``` +/flake.nix +/flake.lock +/nix/ /* [blueprint](https://github.com/numtide/blueprint) set up underneath here. */ +``` + +### Rust + +``` +/Cargo.toml +/Cargo.lock +/rust/ # all rust code lives here +/rust/common/Cargo.toml +/rust/common/src/lib.rs +``` diff --git a/templates/default/flake.lock b/templates/default/flake.lock new file mode 100644 index 0000000..3450f98 --- /dev/null +++ b/templates/default/flake.lock @@ -0,0 +1,335 @@ +{ + "nodes": { + "blueprint": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "systems": "systems" + }, + "locked": { + "lastModified": 1733562445, + "narHash": "sha256-gLmqbX40Qos+EeBvmlzvntWB3NrdiDaFxhr3VAmhrf4=", + "owner": "numtide", + "repo": "blueprint", + "rev": "97ef7fba3a6eec13e12a108ce4b3473602eb424f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "blueprint", + "type": "github" + } + }, + "crane": { + "locked": { + "lastModified": 1734808813, + "narHash": "sha256-3aH/0Y6ajIlfy7j52FGZ+s4icVX0oHhqBzRdlOeztqg=", + "owner": "ipetkov", + "repo": "crane", + "rev": "72e2d02dbac80c8c86bf6bf3e785536acf8ee926", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1653893745, + "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1653893745, + "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_3": { + "locked": { + "lastModified": 1653893745, + "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_4": { + "locked": { + "lastModified": 1653893745, + "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_5": { + "locked": { + "lastModified": 1653893745, + "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixago": { + "inputs": { + "flake-utils": "flake-utils", + "nixago-exts": "nixago-exts", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1714086354, + "narHash": "sha256-yKVQMxL9p7zCWUhnGhDzRVT8sDgHoI3V595lBK0C2YA=", + "owner": "jmgilman", + "repo": "nixago", + "rev": "5133633e9fe6b144c8e00e3b212cdbd5a173b63d", + "type": "github" + }, + "original": { + "owner": "jmgilman", + "repo": "nixago", + "type": "github" + } + }, + "nixago-exts": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixago": "nixago_2", + "nixpkgs": [ + "nixago", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1676070308, + "narHash": "sha256-QaJ65oc2l8iwQIGWUJ0EKjCeSuuCM/LqR8RauxZUUkc=", + "owner": "nix-community", + "repo": "nixago-extensions", + "rev": "e5380cb0456f4ea3c86cf94e3039eb856bf07d0b", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixago-extensions", + "type": "github" + } + }, + "nixago-exts_2": { + "inputs": { + "flake-utils": "flake-utils_4", + "nixago": "nixago_3", + "nixpkgs": [ + "nixago", + "nixago-exts", + "nixago", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1655508669, + "narHash": "sha256-BDDdo5dZQMmwNH/GNacy33nPBnCpSIydWFPZs0kkj/g=", + "owner": "nix-community", + "repo": "nixago-extensions", + "rev": "3022a932ce109258482ecc6568c163e8d0b426aa", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixago-extensions", + "type": "github" + } + }, + "nixago_2": { + "inputs": { + "flake-utils": "flake-utils_3", + "nixago-exts": "nixago-exts_2", + "nixpkgs": [ + "nixago", + "nixago-exts", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1676070010, + "narHash": "sha256-iYzJIWptE1EUD8VINAg66AAMUajizg8JUYN3oBmb8no=", + "owner": "nix-community", + "repo": "nixago", + "rev": "d480ba6c0c16e2c5c0bd2122852d6a0c9ad1ed0e", + "type": "github" + }, + "original": { + "owner": "nix-community", + "ref": "rename-config-data", + "repo": "nixago", + "type": "github" + } + }, + "nixago_3": { + "inputs": { + "flake-utils": "flake-utils_5", + "nixpkgs": [ + "nixago", + "nixago-exts", + "nixago", + "nixago-exts", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1655405483, + "narHash": "sha256-Crd49aZWNrpczlRTOwWGfwBMsTUoG9vlHDKQC7cx264=", + "owner": "nix-community", + "repo": "nixago", + "rev": "e6a9566c18063db5b120e69e048d3627414e327d", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixago", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1734875076, + "narHash": "sha256-Pzyb+YNG5u3zP79zoi8HXYMs15Q5dfjDgwCdUI5B0nY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1807c2b91223227ad5599d7067a61665c52d1295", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-24.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-unstable": { + "locked": { + "lastModified": 1734649271, + "narHash": "sha256-4EVBRhOjMDuGtMaofAIqzJbg4Ql7Ai0PSeuVZTHjyKQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "d70bd19e0a38ad4790d3913bf08fcbfc9eeca507", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "blueprint": "blueprint", + "crane": "crane", + "nixago": "nixago", + "nixpkgs": "nixpkgs", + "nixpkgs-unstable": "nixpkgs-unstable", + "rust-overlay": "rust-overlay", + "treefmt-nix": "treefmt-nix" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1734834660, + "narHash": "sha256-bm8V+Cu8rWJA+vKQnc94mXTpSDgvedyoDKxTVi/uJfw=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "b070e6030118680977bc2388868c4b3963872134", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1734704479, + "narHash": "sha256-MMi74+WckoyEWBRcg/oaGRvXC9BVVxDZNRMpL+72wBI=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "65712f5af67234dad91a5a4baee986a8b62dbf8f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/templates/default/flake.nix b/templates/default/flake.nix new file mode 100644 index 0000000..6d2cfa1 --- /dev/null +++ b/templates/default/flake.nix @@ -0,0 +1,30 @@ +{ + description = "blueprint-universe"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-24.11"; + nixpkgs-unstable.url = "github:NixOS/nixpkgs?ref=nixos-unstable"; + blueprint.url = "github:numtide/blueprint"; + blueprint.inputs.nixpkgs.follows = "nixpkgs"; + treefmt-nix.url = "github:numtide/treefmt-nix"; + treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; + nixago.url = "github:jmgilman/nixago"; + nixago.inputs.nixpkgs.follows = "nixpkgs"; + + crane.url = "github:ipetkov/crane"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = + inputs: + inputs.blueprint { + inherit inputs; + prefix = "nix/"; + + # change this if unfree software is required. + nixpkgs.config.allowUnfree = false; + }; +} diff --git a/templates/default/nix/devshells/default.nix b/templates/default/nix/devshells/default.nix new file mode 100644 index 0000000..c2cd433 --- /dev/null +++ b/templates/default/nix/devshells/default.nix @@ -0,0 +1,27 @@ +{ + pkgs, + flake, + system, +}: +pkgs.mkShell { + # Add build dependencies + packages = [ + flake.formatter.${system} + pkgs.jq + ]; + + # Add environment variables + env = { }; + + # Load custom bash code + shellHook = + # TODO(blocked/upstream): remove this once https://github.com/isbecker/treefmt-vscode/issues/3 is resolved + (flake.inputs.nixago.lib.${system}.make { + data = flake.formatter.${system}.settings; + output = "treefmt.toml"; + format = "toml"; + }).shellHook + + '' + echo $(git rev-parse --show-toplevel) + ''; +} diff --git a/templates/default/nix/devshells/rust.nix b/templates/default/nix/devshells/rust.nix new file mode 100644 index 0000000..c796b48 --- /dev/null +++ b/templates/default/nix/devshells/rust.nix @@ -0,0 +1,34 @@ +{ + flake, + system, + pkgs, + ... +}: +let + craneLib = flake.lib.mkCraneLib { inherit pkgs system; }; +in +craneLib.devShell { + inputsFrom = + [ + flake.devShells.${system}.default + ] + # Inherit inputs from rust-workspace on the platforms it's available. + ++ (pkgs.lib.lists.optionals + (pkgs.lib.meta.availableOn pkgs.stdenv.hostPlatform flake.packages.${system}.rust-workspace) + ( + [ + flake.packages.${system}.rust-workspace + + ] + ++ (builtins.attrValues flake.packages.${system}.rust-workspace.passthru.tests) + ) + ); + + # Additional dev-shell environment variables can be set directly + # MY_CUSTOM_DEVELOPMENT_VAR = "something else"; + + # Extra inputs can be added here; cargo and rustc are provided by default. + packages = [ + pkgs.natscli + ]; +} diff --git a/templates/default/nix/formatter.nix b/templates/default/nix/formatter.nix new file mode 100644 index 0000000..e29bc08 --- /dev/null +++ b/templates/default/nix/formatter.nix @@ -0,0 +1,70 @@ +{ + pkgs, + inputs, + perSystem, + ... +}: +let + settingsNix = { + package = perSystem.nixpkgs.treefmt2; + + projectRootFile = ".git/config"; + + programs = { + nixfmt.enable = true; + deadnix = { + enable = true; + no-underscore = true; + }; + statix.enable = true; + + rustfmt.enable = true; + + gofmt.enable = true; + + shfmt.enable = true; + + prettier.enable = true; + } // pkgs.lib.optionalAttrs (pkgs.system != "riscv64-linux") { shellcheck.enable = true; }; + + settings = { + global.excludes = [ + "LICENSE" + # unsupported extensions + "*.{gif,png,svg,tape,mts,lock,mod,sum,toml,env,envrc,gitignore}" + ]; + + formatter = { + deadnix = { + priority = 1; + }; + + nixfmt = { + priority = 2; + }; + + statix = { + priority = 3; + }; + + prettier = { + options = [ + "--tab-width" + "2" + ]; + includes = [ "*.{css,html,js,json,jsx,md,mdx,scss,ts,yaml}" ]; + }; + }; + }; + }; + + treefmtEval = inputs.treefmt-nix.lib.evalModule pkgs settingsNix; + +in +treefmtEval.config.build.wrapper.overrideAttrs (_: { + passthru = { + inherit (treefmtEval.config) package settings; + inherit (treefmtEval) config; + inherit settingsNix; + }; +}) diff --git a/templates/default/nix/lib/default.nix b/templates/default/nix/lib/default.nix new file mode 100644 index 0000000..3c1aa40 --- /dev/null +++ b/templates/default/nix/lib/default.nix @@ -0,0 +1,13 @@ +{ inputs, flake, ... }: + +{ + mkCraneLib = + { pkgs, system }: + let + craneLib = inputs.crane.mkLib pkgs; + toolchain = (inputs.rust-overlay.lib.mkRustBin { } pkgs).fromRustupToolchainFile ( + flake + "/rust-toolchain.toml" + ); + in + craneLib.overrideToolchain toolchain; +} diff --git a/templates/default/nix/packages/rust-workspace.nix b/templates/default/nix/packages/rust-workspace.nix new file mode 100644 index 0000000..b0d5227 --- /dev/null +++ b/templates/default/nix/packages/rust-workspace.nix @@ -0,0 +1,83 @@ +/* + This exposes all crates in the workspace as a single package attribute. + It also enforces various tests. + + Losely following the tutorial at https://crane.dev/examples/quick-start-workspace.html +*/ + +{ + flake, + pkgs, + system, + ... +}: +let + craneLib = flake.lib.mkCraneLib { inherit pkgs system; }; + src = craneLib.cleanCargoSource flake; + commonArgs = { + inherit src; + strictDeps = true; + + buildInputs = pkgs.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"; + + meta.platforms = pkgs.lib.platforms.linux; + }; + + # Build *just* the cargo dependencies (of the entire workspace), + # so we can reuse all of that work (e.g. via cachix) when running in CI + # It is *highly* recommended to use something like cargo-hakari to avoid + # cache misses when building individual top-level-crates + cargoArtifacts = craneLib.buildDepsOnly commonArgs; +in +craneLib.cargoBuild ( + commonArgs + // { + inherit cargoArtifacts; + + # NB: we disable tests since we'll run them all via cargo-nextest + doCheck = false; + + passthru.tests = { + clippy = craneLib.cargoClippy ( + commonArgs + // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + } + ); + + doc = craneLib.cargoDoc ( + commonArgs + // { + inherit cargoArtifacts; + } + ); + + # Audit licenses + deny = craneLib.cargoDeny { + inherit src; + }; + + nextest = craneLib.cargoNextest ( + commonArgs + // { + inherit cargoArtifacts; + + nativeBuildInputs = [ + # native test binaries go here + ]; + partitions = 1; + partitionType = "count"; + } + ); + + }; + + } +) diff --git a/templates/default/rust-toolchain.toml b/templates/default/rust-toolchain.toml new file mode 100644 index 0000000..7dde9f8 --- /dev/null +++ b/templates/default/rust-toolchain.toml @@ -0,0 +1,5 @@ +[toolchain] +channel = "1.83.0" +components = [ "cargo","llvm-tools", "rustc", "clippy", "rust-src" ] +targets = [ ] +profile = "minimal" diff --git a/templates/default/rust/app/Cargo.toml b/templates/default/rust/app/Cargo.toml new file mode 100644 index 0000000..52af8e5 --- /dev/null +++ b/templates/default/rust/app/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "app" +edition = "2021" +version.workspace = true + +[dependencies] +lib = { path = "../lib" } + +anyhow = { workspace = true } +log = { workspace = true } +env_logger = { workspace = true } diff --git a/templates/default/rust/app/src/main.rs b/templates/default/rust/app/src/main.rs new file mode 100644 index 0000000..debf21b --- /dev/null +++ b/templates/default/rust/app/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + let (a, b) = (1, 1); + let r = lib::add(a, b); + + println!("{a} + {b} = {r}"); +} diff --git a/templates/default/rust/lib/Cargo.toml b/templates/default/rust/lib/Cargo.toml new file mode 100644 index 0000000..ded6285 --- /dev/null +++ b/templates/default/rust/lib/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "lib" +edition = "2021" +version.workspace = true + +[dependencies] +anyhow = { workspace = true } +log = { workspace = true } diff --git a/templates/default/rust/lib/src/lib.rs b/templates/default/rust/lib/src/lib.rs new file mode 100644 index 0000000..b93cf3f --- /dev/null +++ b/templates/default/rust/lib/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/templates/default/treefmt.toml b/templates/default/treefmt.toml new file mode 120000 index 0000000..e440515 --- /dev/null +++ b/templates/default/treefmt.toml @@ -0,0 +1 @@ +/nix/store/2q0p32grc9y40gilz6z9ck5nmq5ypx4n-treefmt.toml \ No newline at end of file