├── .envrc ├── .gitattributes ├── .github └── workflows │ ├── ci-nix.yml │ └── ci.yml ├── .gitignore ├── .vscode ├── extensions.json └── settings.json ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── flake.lock ├── flake.nix ├── justfile ├── nix └── modules │ ├── devshell.nix │ ├── pre-commit.nix │ ├── rust.nix │ └── template.nix ├── rust-toolchain.toml └── src └── main.rs /.envrc: -------------------------------------------------------------------------------- 1 | watch_file \ 2 | rust-toolchain.toml \ 3 | nix/modules/*.nix 4 | use flake 5 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | flake.lock linguist-generated=true 2 | -------------------------------------------------------------------------------- /.github/workflows/ci-nix.yml: -------------------------------------------------------------------------------- 1 | name: "CI Nix" 2 | on: 3 | # Run only when pushing to master branch, and making PRs 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | jobs: 9 | build: 10 | runs-on: ${{ matrix.os }} 11 | strategy: 12 | matrix: 13 | os: [ubuntu-latest, macos-14] 14 | steps: 15 | - uses: actions/checkout@v4 16 | - uses: DeterminateSystems/nix-installer-action@main 17 | - name: Install omnix 18 | run: nix --accept-flake-config profile install "github:juspay/omnix" 19 | - run: om ci 20 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | on: [push] 2 | 3 | name: CI 4 | 5 | jobs: 6 | check: 7 | name: Rust project 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v2 11 | - name: Install latest nightly 12 | uses: actions-rs/toolchain@v1 13 | with: 14 | toolchain: nightly 15 | override: true 16 | components: rustfmt, clippy 17 | 18 | # `cargo check` command here will use installed `nightly` 19 | # as it is set as an "override" for current directory 20 | 21 | - name: Run cargo check 22 | uses: actions-rs/cargo@v1 23 | with: 24 | command: check 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /result 3 | /result-lib 4 | .direnv 5 | 6 | /.pre-commit-config.yaml 7 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "rust-lang.rust-analyzer", 4 | "jnoortheen.nix-ide", 5 | "mkhl.direnv", 6 | "tamasfe.even-better-toml", 7 | "fill-labs.dependi" 8 | ] 9 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.formatOnSave": true, 3 | "rust-analyzer.files.excludeDirs": [ 4 | ".direnv" 5 | ], 6 | // https://twitter.com/sridca/status/1674947342607216641 7 | // "editor.inlayHints.enabled": "offUnlessPressed", 8 | } 9 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "anstream" 7 | version = "0.6.13" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" 10 | dependencies = [ 11 | "anstyle", 12 | "anstyle-parse", 13 | "anstyle-query", 14 | "anstyle-wincon", 15 | "colorchoice", 16 | "utf8parse", 17 | ] 18 | 19 | [[package]] 20 | name = "anstyle" 21 | version = "1.0.6" 22 | source = "registry+https://github.com/rust-lang/crates.io-index" 23 | checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" 24 | 25 | [[package]] 26 | name = "anstyle-parse" 27 | version = "0.2.3" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" 30 | dependencies = [ 31 | "utf8parse", 32 | ] 33 | 34 | [[package]] 35 | name = "anstyle-query" 36 | version = "1.0.2" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" 39 | dependencies = [ 40 | "windows-sys", 41 | ] 42 | 43 | [[package]] 44 | name = "anstyle-wincon" 45 | version = "3.0.2" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" 48 | dependencies = [ 49 | "anstyle", 50 | "windows-sys", 51 | ] 52 | 53 | [[package]] 54 | name = "clap" 55 | version = "4.5.4" 56 | source = "registry+https://github.com/rust-lang/crates.io-index" 57 | checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" 58 | dependencies = [ 59 | "clap_builder", 60 | "clap_derive", 61 | ] 62 | 63 | [[package]] 64 | name = "clap_builder" 65 | version = "4.5.2" 66 | source = "registry+https://github.com/rust-lang/crates.io-index" 67 | checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" 68 | dependencies = [ 69 | "anstream", 70 | "anstyle", 71 | "clap_lex", 72 | "strsim", 73 | ] 74 | 75 | [[package]] 76 | name = "clap_derive" 77 | version = "4.5.4" 78 | source = "registry+https://github.com/rust-lang/crates.io-index" 79 | checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" 80 | dependencies = [ 81 | "heck", 82 | "proc-macro2", 83 | "quote", 84 | "syn", 85 | ] 86 | 87 | [[package]] 88 | name = "clap_lex" 89 | version = "0.7.0" 90 | source = "registry+https://github.com/rust-lang/crates.io-index" 91 | checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" 92 | 93 | [[package]] 94 | name = "colorchoice" 95 | version = "1.0.0" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" 98 | 99 | [[package]] 100 | name = "heck" 101 | version = "0.5.0" 102 | source = "registry+https://github.com/rust-lang/crates.io-index" 103 | checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" 104 | 105 | [[package]] 106 | name = "proc-macro2" 107 | version = "1.0.81" 108 | source = "registry+https://github.com/rust-lang/crates.io-index" 109 | checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba" 110 | dependencies = [ 111 | "unicode-ident", 112 | ] 113 | 114 | [[package]] 115 | name = "quote" 116 | version = "1.0.36" 117 | source = "registry+https://github.com/rust-lang/crates.io-index" 118 | checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" 119 | dependencies = [ 120 | "proc-macro2", 121 | ] 122 | 123 | [[package]] 124 | name = "rust-nix-template" 125 | version = "0.1.0" 126 | dependencies = [ 127 | "clap", 128 | ] 129 | 130 | [[package]] 131 | name = "strsim" 132 | version = "0.11.1" 133 | source = "registry+https://github.com/rust-lang/crates.io-index" 134 | checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" 135 | 136 | [[package]] 137 | name = "syn" 138 | version = "2.0.60" 139 | source = "registry+https://github.com/rust-lang/crates.io-index" 140 | checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3" 141 | dependencies = [ 142 | "proc-macro2", 143 | "quote", 144 | "unicode-ident", 145 | ] 146 | 147 | [[package]] 148 | name = "unicode-ident" 149 | version = "1.0.12" 150 | source = "registry+https://github.com/rust-lang/crates.io-index" 151 | checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" 152 | 153 | [[package]] 154 | name = "utf8parse" 155 | version = "0.2.1" 156 | source = "registry+https://github.com/rust-lang/crates.io-index" 157 | checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" 158 | 159 | [[package]] 160 | name = "windows-sys" 161 | version = "0.52.0" 162 | source = "registry+https://github.com/rust-lang/crates.io-index" 163 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 164 | dependencies = [ 165 | "windows-targets", 166 | ] 167 | 168 | [[package]] 169 | name = "windows-targets" 170 | version = "0.52.5" 171 | source = "registry+https://github.com/rust-lang/crates.io-index" 172 | checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" 173 | dependencies = [ 174 | "windows_aarch64_gnullvm", 175 | "windows_aarch64_msvc", 176 | "windows_i686_gnu", 177 | "windows_i686_gnullvm", 178 | "windows_i686_msvc", 179 | "windows_x86_64_gnu", 180 | "windows_x86_64_gnullvm", 181 | "windows_x86_64_msvc", 182 | ] 183 | 184 | [[package]] 185 | name = "windows_aarch64_gnullvm" 186 | version = "0.52.5" 187 | source = "registry+https://github.com/rust-lang/crates.io-index" 188 | checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" 189 | 190 | [[package]] 191 | name = "windows_aarch64_msvc" 192 | version = "0.52.5" 193 | source = "registry+https://github.com/rust-lang/crates.io-index" 194 | checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" 195 | 196 | [[package]] 197 | name = "windows_i686_gnu" 198 | version = "0.52.5" 199 | source = "registry+https://github.com/rust-lang/crates.io-index" 200 | checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" 201 | 202 | [[package]] 203 | name = "windows_i686_gnullvm" 204 | version = "0.52.5" 205 | source = "registry+https://github.com/rust-lang/crates.io-index" 206 | checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" 207 | 208 | [[package]] 209 | name = "windows_i686_msvc" 210 | version = "0.52.5" 211 | source = "registry+https://github.com/rust-lang/crates.io-index" 212 | checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" 213 | 214 | [[package]] 215 | name = "windows_x86_64_gnu" 216 | version = "0.52.5" 217 | source = "registry+https://github.com/rust-lang/crates.io-index" 218 | checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" 219 | 220 | [[package]] 221 | name = "windows_x86_64_gnullvm" 222 | version = "0.52.5" 223 | source = "registry+https://github.com/rust-lang/crates.io-index" 224 | checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" 225 | 226 | [[package]] 227 | name = "windows_x86_64_msvc" 228 | version = "0.52.5" 229 | source = "registry+https://github.com/rust-lang/crates.io-index" 230 | checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" 231 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors = ["Sridhar Ratnakumar "] 3 | edition = "2021" 4 | # If you change the name here, you must also do it in flake.nix (and run `cargo generate-lockfile` afterwards) 5 | name = "rust-nix-template" 6 | description = "A simple Rust project using Nix" 7 | version = "0.1.0" 8 | 9 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 10 | 11 | [dependencies] 12 | clap = { version = "4.3.14", features = ["derive"] } 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Sridhar Ratnakumar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A template Rust project with fully functional and no-frills Nix support, as well as builtin VSCode configuration to get IDE experience without any manual setup (just [install direnv](https://nixos.asia/en/direnv), open in VSCode and accept the suggestions). It uses [crane](https://crane.dev/), via [rust-flake](https://github.com/juspay/rust-flake). 2 | 3 | > [!NOTE] 4 | > If you are looking for the original template based on [this blog post](https://srid.ca/rust-nix)'s use of `crate2nix`, browse from [this tag](https://github.com/srid/rust-nix-template/tree/crate2nix). The evolution of this template can be gleaned from [releases](https://github.com/srid/rust-nix-template/releases). 5 | 6 | ## Usage 7 | 8 | You can use [omnix](https://omnix.page/om/init.html)[^omnix] to initialize this template: 9 | ``` 10 | nix --accept-flake-config run github:juspay/omnix -- init github:srid/rust-nix-template -o ~/my-rust-project 11 | ``` 12 | 13 | [^omnix]: If initializing manually, make sure to: 14 | - Change `name` in Cargo.toml. 15 | - Run `cargo generate-lockfile` in the nix shelld 16 | 17 | ## Adapting this template 18 | 19 | - There are two CI workflows, and one of them uses Nix which is slower (unless you configure a cache) than the other one based on rustup. Pick one or the other depending on your trade-offs. 20 | 21 | ## Development (Flakes) 22 | 23 | This repo uses [Flakes](https://nixos.asia/en/flakes) from the get-go. 24 | 25 | ```bash 26 | # Dev shell 27 | nix develop 28 | 29 | # or run via cargo 30 | nix develop -c cargo run 31 | 32 | # build 33 | nix build 34 | ``` 35 | 36 | We also provide a [`justfile`](https://just.systems/) for Makefile'esque commands to be run inside of the devShell. 37 | 38 | ## Tips 39 | 40 | - Run `nix flake update` to update all flake inputs. 41 | - Run `nix --accept-flake-config run github:juspay/omnix ci` to build _all_ outputs. 42 | - [pre-commit] hooks will automatically be setup in Nix shell. You can also run `pre-commit run -a` manually to run the hooks (e.g.: to autoformat the project tree using `rustfmt`, `nixpkgs-fmt`, etc.). 43 | 44 | ## Discussion 45 | 46 | - [Zulip](https://nixos.zulipchat.com/#narrow/stream/413950-nix) 47 | 48 | ## See Also 49 | 50 | - [nixos.wiki: Packaging Rust projects with nix](https://nixos.wiki/wiki/Rust#Packaging_Rust_projects_with_nix) 51 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "cargo-doc-live": { 4 | "locked": { 5 | "lastModified": 1724704668, 6 | "narHash": "sha256-kJFYXlWUodg5WhJ0NuvrP0mCvOT/2AOIo8oGeYLXocs=", 7 | "owner": "srid", 8 | "repo": "cargo-doc-live", 9 | "rev": "b09d5d258d2498829e03014931fc19aed499b86f", 10 | "type": "github" 11 | }, 12 | "original": { 13 | "owner": "srid", 14 | "repo": "cargo-doc-live", 15 | "type": "github" 16 | } 17 | }, 18 | "crane": { 19 | "locked": { 20 | "lastModified": 1736566337, 21 | "narHash": "sha256-SC0eDcZPqISVt6R0UfGPyQLrI0+BppjjtQ3wcSlk0oI=", 22 | "owner": "ipetkov", 23 | "repo": "crane", 24 | "rev": "9172acc1ee6c7e1cbafc3044ff850c568c75a5a3", 25 | "type": "github" 26 | }, 27 | "original": { 28 | "owner": "ipetkov", 29 | "repo": "crane", 30 | "type": "github" 31 | } 32 | }, 33 | "flake-parts": { 34 | "inputs": { 35 | "nixpkgs-lib": [ 36 | "nixpkgs" 37 | ] 38 | }, 39 | "locked": { 40 | "lastModified": 1725234343, 41 | "narHash": "sha256-+ebgonl3NbiKD2UD0x4BszCZQ6sTfL4xioaM49o5B3Y=", 42 | "owner": "hercules-ci", 43 | "repo": "flake-parts", 44 | "rev": "567b938d64d4b4112ee253b9274472dc3a346eb6", 45 | "type": "github" 46 | }, 47 | "original": { 48 | "owner": "hercules-ci", 49 | "repo": "flake-parts", 50 | "type": "github" 51 | } 52 | }, 53 | "git-hooks": { 54 | "flake": false, 55 | "locked": { 56 | "lastModified": 1734425854, 57 | "narHash": "sha256-nzE5UbJ41aPEKf8R2ZFYtLkqPmF7EIUbNEdHMBLg0Ig=", 58 | "owner": "cachix", 59 | "repo": "git-hooks.nix", 60 | "rev": "0ddd26d0925f618c3a5d85a4fa5eb1e23a09491d", 61 | "type": "github" 62 | }, 63 | "original": { 64 | "owner": "cachix", 65 | "repo": "git-hooks.nix", 66 | "type": "github" 67 | } 68 | }, 69 | "nixpkgs": { 70 | "locked": { 71 | "lastModified": 1736693123, 72 | "narHash": "sha256-9lIfXCaBPwUA7FnfDnoH4gxxdOvXG78k6UlUw0+ZDxc=", 73 | "owner": "nixos", 74 | "repo": "nixpkgs", 75 | "rev": "2fdec2c2e68b7b7845d1ea4e0894c63143e3261b", 76 | "type": "github" 77 | }, 78 | "original": { 79 | "owner": "nixos", 80 | "ref": "nixpkgs-unstable", 81 | "repo": "nixpkgs", 82 | "type": "github" 83 | } 84 | }, 85 | "process-compose-flake": { 86 | "locked": { 87 | "lastModified": 1724606023, 88 | "narHash": "sha256-rdGeNa/lCS8E1lXzPqgl+vZUUvnbEZT11Bqkx5jfYug=", 89 | "owner": "Platonic-Systems", 90 | "repo": "process-compose-flake", 91 | "rev": "f6ce9481df9aec739e4e06b67492401a5bb4f0b1", 92 | "type": "github" 93 | }, 94 | "original": { 95 | "owner": "Platonic-Systems", 96 | "repo": "process-compose-flake", 97 | "type": "github" 98 | } 99 | }, 100 | "root": { 101 | "inputs": { 102 | "cargo-doc-live": "cargo-doc-live", 103 | "flake-parts": "flake-parts", 104 | "git-hooks": "git-hooks", 105 | "nixpkgs": "nixpkgs", 106 | "process-compose-flake": "process-compose-flake", 107 | "rust-flake": "rust-flake", 108 | "systems": "systems" 109 | } 110 | }, 111 | "rust-flake": { 112 | "inputs": { 113 | "crane": "crane", 114 | "nixpkgs": [ 115 | "nixpkgs" 116 | ], 117 | "rust-overlay": "rust-overlay" 118 | }, 119 | "locked": { 120 | "lastModified": 1736806612, 121 | "narHash": "sha256-WioA+Vk7suDK+Ek77rDlbuxV6WqwFt30JsKHrmDCSiU=", 122 | "owner": "juspay", 123 | "repo": "rust-flake", 124 | "rev": "b5f39885e2fcf137bfaf75decc077f9cca2bd984", 125 | "type": "github" 126 | }, 127 | "original": { 128 | "owner": "juspay", 129 | "repo": "rust-flake", 130 | "type": "github" 131 | } 132 | }, 133 | "rust-overlay": { 134 | "inputs": { 135 | "nixpkgs": [ 136 | "rust-flake", 137 | "nixpkgs" 138 | ] 139 | }, 140 | "locked": { 141 | "lastModified": 1736700680, 142 | "narHash": "sha256-9gmWIb8xsycWHEYpd2SiVIAZnUULX6Y+IMMZBcDUCQU=", 143 | "owner": "oxalica", 144 | "repo": "rust-overlay", 145 | "rev": "5d1865c0da63b4c949f383d982b6b43519946e8f", 146 | "type": "github" 147 | }, 148 | "original": { 149 | "owner": "oxalica", 150 | "repo": "rust-overlay", 151 | "type": "github" 152 | } 153 | }, 154 | "systems": { 155 | "locked": { 156 | "lastModified": 1681028828, 157 | "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", 158 | "owner": "nix-systems", 159 | "repo": "default", 160 | "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", 161 | "type": "github" 162 | }, 163 | "original": { 164 | "owner": "nix-systems", 165 | "repo": "default", 166 | "type": "github" 167 | } 168 | } 169 | }, 170 | "root": "root", 171 | "version": 7 172 | } 173 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | inputs = { 3 | nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; 4 | flake-parts.url = "github:hercules-ci/flake-parts"; 5 | flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; 6 | systems.url = "github:nix-systems/default"; 7 | rust-flake.url = "github:juspay/rust-flake"; 8 | rust-flake.inputs.nixpkgs.follows = "nixpkgs"; 9 | process-compose-flake.url = "github:Platonic-Systems/process-compose-flake"; 10 | cargo-doc-live.url = "github:srid/cargo-doc-live"; 11 | 12 | git-hooks.url = "github:cachix/git-hooks.nix"; 13 | git-hooks.flake = false; 14 | }; 15 | 16 | outputs = inputs: 17 | inputs.flake-parts.lib.mkFlake { inherit inputs; } { 18 | systems = import inputs.systems; 19 | 20 | # See ./nix/modules/*.nix for the modules that are imported here. 21 | imports = with builtins; 22 | map 23 | (fn: ./nix/modules/${fn}) 24 | (attrNames (readDir ./nix/modules)); 25 | }; 26 | } 27 | -------------------------------------------------------------------------------- /justfile: -------------------------------------------------------------------------------- 1 | default: 2 | @just --list 3 | 4 | # Run pre-commit hooks on all files, including autoformatting 5 | pre-commit-all: 6 | pre-commit run --all-files 7 | 8 | # Run 'cargo run' on the project 9 | run *ARGS: 10 | cargo run {{ARGS}} 11 | 12 | # Run 'bacon' to run the project (auto-recompiles) 13 | watch *ARGS: 14 | bacon --job run -- -- {{ ARGS }} 15 | -------------------------------------------------------------------------------- /nix/modules/devshell.nix: -------------------------------------------------------------------------------- 1 | { inputs, ... }: 2 | { 3 | perSystem = { config, self', pkgs, lib, ... }: { 4 | devShells.default = pkgs.mkShell { 5 | name = "rust-nix-template-shell"; 6 | inputsFrom = [ 7 | self'.devShells.rust 8 | config.pre-commit.devShell # See ./nix/modules/pre-commit.nix 9 | ]; 10 | packages = with pkgs; [ 11 | just 12 | nixd # Nix language server 13 | bacon 14 | config.process-compose.cargo-doc-live.outputs.package 15 | ]; 16 | }; 17 | }; 18 | } 19 | -------------------------------------------------------------------------------- /nix/modules/pre-commit.nix: -------------------------------------------------------------------------------- 1 | { inputs, ... }: 2 | { 3 | imports = [ 4 | (inputs.git-hooks + /flake-module.nix) 5 | ]; 6 | perSystem = { config, self', pkgs, lib, ... }: { 7 | pre-commit.settings = { 8 | hooks = { 9 | nixpkgs-fmt.enable = true; 10 | rustfmt.enable = true; 11 | }; 12 | }; 13 | }; 14 | } 15 | -------------------------------------------------------------------------------- /nix/modules/rust.nix: -------------------------------------------------------------------------------- 1 | { inputs, ... }: 2 | { 3 | imports = [ 4 | inputs.rust-flake.flakeModules.default 5 | inputs.rust-flake.flakeModules.nixpkgs 6 | inputs.process-compose-flake.flakeModule 7 | inputs.cargo-doc-live.flakeModule 8 | ]; 9 | perSystem = { config, self', pkgs, lib, ... }: { 10 | rust-project.crates."rust-nix-template".crane.args = { 11 | buildInputs = lib.optionals pkgs.stdenv.isDarwin ( 12 | with pkgs.darwin.apple_sdk.frameworks; [ 13 | IOKit 14 | ] 15 | ); 16 | }; 17 | packages.default = self'.packages.rust-nix-template; 18 | }; 19 | } 20 | -------------------------------------------------------------------------------- /nix/modules/template.nix: -------------------------------------------------------------------------------- 1 | { inputs, ... }: 2 | 3 | { 4 | flake = rec { 5 | templates.default = { 6 | description = "A batteries-included Rust project template for Nix"; 7 | path = builtins.path { path = inputs.self; }; 8 | }; 9 | 10 | # https://omnix.page/om/init.html#spec 11 | om.templates.rust-nix-template = { 12 | template = templates.default; 13 | params = [ 14 | { 15 | name = "package-name"; 16 | description = "Name of the Rust package"; 17 | placeholder = "rust-nix-template"; 18 | } 19 | { 20 | name = "author"; 21 | description = "Author name"; 22 | placeholder = "Sridhar Ratnakumar"; 23 | } 24 | { 25 | name = "author-email"; 26 | description = "Author email"; 27 | placeholder = "srid@srid.ca"; 28 | } 29 | { 30 | name = "vscode"; 31 | description = "Include the VSCode settings folder (./.vscode)"; 32 | paths = [ ".vscode" ]; 33 | value = true; 34 | } 35 | { 36 | name = "github-ci"; 37 | description = "Include GitHub Actions workflow configuration"; 38 | paths = [ ".github" ]; 39 | value = true; 40 | } 41 | { 42 | name = "nix-template"; 43 | description = "Keep the flake template in the project"; 44 | paths = [ "**/template.nix" ]; 45 | value = false; 46 | } 47 | ]; 48 | tests = { 49 | default = { 50 | params = { 51 | package-name = "qux"; 52 | author = "John"; 53 | author-email = "john@example.com"; 54 | }; 55 | asserts = { 56 | source = { 57 | "Cargo.toml" = true; 58 | "flake.nix" = true; 59 | ".github/workflows/ci.yml" = true; 60 | ".vscode" = true; 61 | "nix/modules/template.nix" = false; 62 | }; 63 | packages.default = { 64 | "bin/qux" = true; 65 | }; 66 | }; 67 | }; 68 | }; 69 | }; 70 | }; 71 | } 72 | -------------------------------------------------------------------------------- /rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "stable" 3 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use clap::Parser; 2 | 3 | #[derive(Parser, Debug)] 4 | #[clap(author = "Sridhar Ratnakumar", version, about)] 5 | /// Application configuration 6 | struct Args { 7 | /// whether to be verbose 8 | #[arg(short = 'v')] 9 | verbose: bool, 10 | 11 | /// an optional name to greet 12 | #[arg()] 13 | name: Option, 14 | } 15 | 16 | fn main() { 17 | let args = Args::parse(); 18 | if args.verbose { 19 | println!("DEBUG {args:?}"); 20 | } 21 | println!( 22 | "Hello {} (from rust-nix-template)!", 23 | args.name.unwrap_or("world".to_string()) 24 | ); 25 | } 26 | --------------------------------------------------------------------------------