├── .editorconfig ├── .envrc ├── .github └── workflows │ ├── pr.yml │ └── update.yml ├── .gitignore ├── LICENSE ├── README.md ├── flake.lock ├── flake.nix ├── flakelight-rust.nix └── templates ├── default.nix └── rust-bin ├── .editorconfig ├── .envrc ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md ├── flake.nix └── src └── main.rs /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | max_line_length = 80 11 | 12 | [{*.nix,flake.lock,*.yml}] 13 | indent_size = 2 14 | 15 | [*.md] 16 | trim_trailing_whitespace = false 17 | indent_size = 2 18 | 19 | [COMMIT_EDITMSG] 20 | max_line_length = 72 21 | -------------------------------------------------------------------------------- /.envrc: -------------------------------------------------------------------------------- 1 | use flake 2 | -------------------------------------------------------------------------------- /.github/workflows/pr.yml: -------------------------------------------------------------------------------- 1 | name: PR checks 2 | 3 | on: 4 | pull_request: 5 | 6 | jobs: 7 | run-checks: 8 | name: Run checks 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v4 12 | - uses: cachix/install-nix-action@master 13 | - run: nix flake check --all-systems 14 | run-checks-template: 15 | name: Run tests 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v4 19 | - uses: cachix/install-nix-action@master 20 | - run: | 21 | nix flake check ./templates/rust-bin --all-systems \ 22 | --override-input flakelight-rust . 23 | -------------------------------------------------------------------------------- /.github/workflows/update.yml: -------------------------------------------------------------------------------- 1 | name: Update flake inputs 2 | 3 | on: 4 | schedule: 5 | - cron: "0 12 ? * TUE" 6 | workflow_dispatch: 7 | 8 | permissions: 9 | contents: write 10 | 11 | jobs: 12 | update-inputs: 13 | name: Update inputs 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: cachix/install-nix-action@master 18 | - name: Configure git 19 | run: | 20 | git config user.name "github-actions[bot]" 21 | git config user.email "41898282+github-actions[bot]@users.noreply.github.com" 22 | - run: nix flake update --commit-lock-file 23 | - run: nix flake check --all-systems 24 | - run: | 25 | nix flake check ./templates/rust-bin --all-systems \ 26 | --override-input flakelight-rust . 27 | - run: git push 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.direnv 2 | result 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (C) 2023 Archit Gupta 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the “Software”), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | 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, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # flakelight-rust 2 | 3 | Rust module for [flakelight][1]. 4 | 5 | Initializes a Rust project flake from its `Cargo.toml`. 6 | 7 | [1]: https://github.com/nix-community/flakelight 8 | 9 | ## Configured options 10 | 11 | Sets `package` to the crate at the flake source. `description` and `licence` is 12 | set from `Cargo.toml`. 13 | 14 | Adds `rust-analyzer`, `cargo`, `clippy`, `rustc`, and `rustfmt` to the default 15 | devShell and sets `RUST_SRC_PATH`. 16 | 17 | Adds checks for crate tests and clippy warnings. 18 | 19 | Configures `rs` files to be formatted with `rustfmt`. 20 | 21 | ## Options 22 | 23 | `fileset` configures the fileset the package is built with. To use all files, 24 | set it to `./.`; 25 | 26 | ## Getting started 27 | 28 | To create a new project in an empty directory, run the following: 29 | 30 | ``` 31 | nix flake init -t github:accelbread/flakelight-rust 32 | ``` 33 | 34 | Existing projects can use one of the example `flake.nix` files below. 35 | 36 | ## Example flake 37 | 38 | You can call this flake directly: 39 | 40 | ```nix 41 | { 42 | inputs.flakelight-rust.url = "github:accelbread/flakelight-rust"; 43 | outputs = { flakelight-rust, ... }: flakelight-rust ./. { }; 44 | } 45 | ``` 46 | 47 | Alternatively, add this module to your Flakelight config: 48 | 49 | ```nix 50 | { 51 | inputs = { 52 | flakelight.url = "github:nix-community/flakelight"; 53 | flakelight-rust.url = "github:accelbread/flakelight-rust"; 54 | }; 55 | outputs = { flakelight, flakelight-rust, ... }: flakelight ./. { 56 | imports = [ flakelight-rust.flakelightModules.default ]; 57 | }; 58 | } 59 | ``` 60 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "crane": { 4 | "locked": { 5 | "lastModified": 1748047550, 6 | "narHash": "sha256-t0qLLqb4C1rdtiY8IFRH5KIapTY/n3Lqt57AmxEv9mk=", 7 | "owner": "ipetkov", 8 | "repo": "crane", 9 | "rev": "b718a78696060df6280196a6f992d04c87a16aef", 10 | "type": "github" 11 | }, 12 | "original": { 13 | "owner": "ipetkov", 14 | "repo": "crane", 15 | "type": "github" 16 | } 17 | }, 18 | "flakelight": { 19 | "inputs": { 20 | "nixpkgs": "nixpkgs" 21 | }, 22 | "locked": { 23 | "lastModified": 1748263617, 24 | "narHash": "sha256-O1xypYFWzYlfiyO3fUZuzRhYac5DGptP+ZhocY9L5tQ=", 25 | "owner": "nix-community", 26 | "repo": "flakelight", 27 | "rev": "65c783ba2b85910df5a053cb1451e9eb13794c12", 28 | "type": "github" 29 | }, 30 | "original": { 31 | "owner": "nix-community", 32 | "repo": "flakelight", 33 | "type": "github" 34 | } 35 | }, 36 | "nixpkgs": { 37 | "locked": { 38 | "lastModified": 1748026106, 39 | "narHash": "sha256-6m1Y3/4pVw1RWTsrkAK2VMYSzG4MMIj7sqUy7o8th1o=", 40 | "owner": "NixOS", 41 | "repo": "nixpkgs", 42 | "rev": "063f43f2dbdef86376cc29ad646c45c46e93234c", 43 | "type": "github" 44 | }, 45 | "original": { 46 | "owner": "NixOS", 47 | "ref": "nixos-unstable", 48 | "repo": "nixpkgs", 49 | "type": "github" 50 | } 51 | }, 52 | "root": { 53 | "inputs": { 54 | "crane": "crane", 55 | "flakelight": "flakelight" 56 | } 57 | } 58 | }, 59 | "root": "root", 60 | "version": 7 61 | } 62 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | # flakelight-rust -- Rust module for flakelight 2 | # Copyright (C) 2023 Archit Gupta 3 | # SPDX-License-Identifier: MIT 4 | 5 | { 6 | description = "Rust module for flakelite"; 7 | inputs = { 8 | flakelight.url = "github:nix-community/flakelight"; 9 | crane = { 10 | url = "github:ipetkov/crane"; 11 | inputs.nixpkgs.follows = "flakelight/nixpkgs"; 12 | }; 13 | }; 14 | outputs = { flakelight, crane, ... }: flakelight ./. { 15 | imports = [ flakelight.flakelightModules.flakelightModule ]; 16 | flakelightModule = { lib, ... }: { 17 | imports = [ ./flakelight-rust.nix ]; 18 | inputs.crane = lib.mkDefault crane; 19 | }; 20 | }; 21 | } 22 | -------------------------------------------------------------------------------- /flakelight-rust.nix: -------------------------------------------------------------------------------- 1 | # flakelight-rust -- Rust module for flakelight 2 | # Copyright (C) 2023 Archit Gupta 3 | # SPDX-License-Identifier: MIT 4 | 5 | { lib, src, config, flakelight, ... }: 6 | let 7 | inherit (builtins) elem readFile pathExists; 8 | inherit (lib) mkDefault mkIf mkMerge mkOption warnIf; 9 | inherit (lib.fileset) fileFilter toSource; 10 | inherit (flakelight.types) fileset; 11 | 12 | cargoToml = fromTOML (readFile (src + /Cargo.toml)); 13 | tomlPackage = cargoToml.package or cargoToml.workspace.package; 14 | in 15 | warnIf (! builtins ? readFileType) "Unsupported Nix version in use." 16 | { 17 | options.fileset = mkOption { 18 | type = fileset; 19 | default = fileFilter 20 | (file: file.hasExt "rs" || elem file.name [ "Cargo.toml" "Cargo.lock" ]) 21 | src; 22 | }; 23 | 24 | config = mkMerge [ 25 | (mkIf (pathExists (src + /Cargo.toml)) { 26 | withOverlays = final: { inputs, ... }: rec { 27 | craneLib = inputs.crane.mkLib final; 28 | cargoArtifacts = craneLib.buildDepsOnly 29 | { inherit src; strictDeps = true; }; 30 | }; 31 | 32 | description = mkIf (tomlPackage ? description) tomlPackage.description; 33 | 34 | # license will need to be set if Cargo license is a complex expression 35 | license = mkIf (tomlPackage ? license) (mkDefault tomlPackage.license); 36 | 37 | pname = tomlPackage.name; 38 | 39 | package = { craneLib, cargoArtifacts, defaultMeta }: 40 | craneLib.buildPackage { 41 | src = toSource { root = src; inherit (config) fileset; }; 42 | inherit cargoArtifacts; 43 | doCheck = false; 44 | strictDeps = true; 45 | meta = defaultMeta; 46 | }; 47 | 48 | checks = { craneLib, cargoArtifacts, ... }: { 49 | test = craneLib.cargoTest { inherit src cargoArtifacts; }; 50 | clippy = craneLib.cargoClippy { 51 | inherit src cargoArtifacts; 52 | strictDeps = true; 53 | cargoClippyExtraArgs = "--all-targets -- --deny warnings"; 54 | }; 55 | }; 56 | }) 57 | 58 | { 59 | devShell = { 60 | packages = pkgs: with pkgs; [ rust-analyzer cargo clippy rustc rustfmt ]; 61 | 62 | env = { rustPlatform, ... }: { 63 | RUST_SRC_PATH = "${rustPlatform.rustLibSrc}"; 64 | }; 65 | }; 66 | 67 | formatters = pkgs: { 68 | "*.rs" = "${pkgs.rustfmt}/bin/rustfmt"; 69 | }; 70 | } 71 | ]; 72 | } 73 | -------------------------------------------------------------------------------- /templates/default.nix: -------------------------------------------------------------------------------- 1 | # flakelight-rust -- Rust module for flakelight 2 | # Copyright (C) 2023 Archit Gupta 3 | # SPDX-License-Identifier: MIT 4 | 5 | rec { 6 | default = rust-bin; 7 | rust-bin = { 8 | path = ./rust-bin; 9 | description = "Template Rust application."; 10 | welcomeText = '' 11 | # Flakelight Rust template 12 | Update the placeholders in `Cargo.toml`! 13 | ''; 14 | }; 15 | } 16 | -------------------------------------------------------------------------------- /templates/rust-bin/.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | max_line_length = 80 11 | 12 | [{*.nix,flake.lock}] 13 | indent_size = 2 14 | 15 | [*.md] 16 | trim_trailing_whitespace = false 17 | indent_size = 2 18 | 19 | [COMMIT_EDITMSG] 20 | max_line_length = 72 21 | 22 | [*.rs] 23 | max_line_length = 100 24 | -------------------------------------------------------------------------------- /templates/rust-bin/.envrc: -------------------------------------------------------------------------------- 1 | use flake 2 | -------------------------------------------------------------------------------- /templates/rust-bin/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | .direnv/ 3 | result 4 | -------------------------------------------------------------------------------- /templates/rust-bin/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 = "hello-world" 7 | version = "0.0.1" 8 | -------------------------------------------------------------------------------- /templates/rust-bin/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "hello-world" 3 | version = "0.0.1" 4 | authors = ["@NAME@ <@EMAIL@>"] 5 | edition = "2021" 6 | description = "Template Rust application." 7 | repository = "https://@GIT_HOST@/hello-world" 8 | license = "AGPL-3.0-or-later" 9 | keywords = ["template"] 10 | categories = ["command-line-utilities"] 11 | 12 | [dependencies] 13 | -------------------------------------------------------------------------------- /templates/rust-bin/README.md: -------------------------------------------------------------------------------- 1 | # hello-world 2 | 3 | Template Rust application. 4 | 5 | Update the placeholders in `Cargo.toml`! 6 | -------------------------------------------------------------------------------- /templates/rust-bin/flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | inputs.flakelight-rust.url = "github:accelbread/flakelight-rust"; 3 | outputs = { flakelight-rust, ... }: 4 | flakelight-rust ./. { }; 5 | } 6 | -------------------------------------------------------------------------------- /templates/rust-bin/src/main.rs: -------------------------------------------------------------------------------- 1 | //! Template Rust Application 2 | 3 | #![warn(missing_docs, clippy::pedantic, clippy::cargo)] 4 | 5 | fn main() { 6 | println!("Hello world!"); 7 | } 8 | --------------------------------------------------------------------------------