From c0d67ed1085aab2c15e657620745ad37005982e9 Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Sat, 3 May 2025 23:23:02 +0800 Subject: [PATCH] add templates as flake output --- flake.nix | 2 + flake/templates.nix | 16 ++++++ templates/default.nix | 6 +++ templates/rust/.envrc | 3 ++ templates/rust/.gitignore | 2 + templates/rust/Cargo.lock | 7 +++ templates/rust/Cargo.toml | 6 +++ templates/rust/flake.lock | 82 ++++++++++++++++++++++++++++++ templates/rust/flake.nix | 38 ++++++++++++++ templates/rust/rust-toolchain.toml | 3 ++ templates/rust/src/main.rs | 3 ++ 11 files changed, 168 insertions(+) create mode 100644 flake/templates.nix create mode 100644 templates/default.nix create mode 100644 templates/rust/.envrc create mode 100644 templates/rust/.gitignore create mode 100644 templates/rust/Cargo.lock create mode 100644 templates/rust/Cargo.toml create mode 100644 templates/rust/flake.lock 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 2392f6c..bd51d13 100644 --- a/flake.nix +++ b/flake.nix @@ -176,6 +176,7 @@ nixos = importApply ./flake/nixos.nix {inherit rootPath outputs;}; home = importApply ./flake/home.nix {inherit rootPath outputs;}; deploy = importApply ./flake/deploy.nix {inherit outputs;}; + templates = importApply ./flake/templates.nix {inherit rootPath;}; in { systems = flake-utils.lib.defaultSystems; imports = [ @@ -183,6 +184,7 @@ nixos home deploy + templates ]; perSystem = { pkgs, diff --git a/flake/templates.nix b/flake/templates.nix new file mode 100644 index 0000000..7f71168 --- /dev/null +++ b/flake/templates.nix @@ -0,0 +1,16 @@ +{rootPath, ...}: { + flake-parts-lib, + lib, + ... +}: { + options = { + flake = flake-parts-lib.mkSubmoduleOptions { + templates = lib.mkOption { + type = lib.types.lazyAttrsOf lib.types.raw; + }; + }; + }; + config = { + flake.templates = import (rootPath + "/templates"); + }; +} diff --git a/templates/default.nix b/templates/default.nix new file mode 100644 index 0000000..7f50671 --- /dev/null +++ b/templates/default.nix @@ -0,0 +1,6 @@ +{ + rust = { + path = ./rust; + description = "Rust template."; + }; +} diff --git a/templates/rust/.envrc b/templates/rust/.envrc new file mode 100644 index 0000000..46942de --- /dev/null +++ b/templates/rust/.envrc @@ -0,0 +1,3 @@ +watch_file *.nix flake.lock + +use flake diff --git a/templates/rust/.gitignore b/templates/rust/.gitignore new file mode 100644 index 0000000..bd32e74 --- /dev/null +++ b/templates/rust/.gitignore @@ -0,0 +1,2 @@ +.direnv +target diff --git a/templates/rust/Cargo.lock b/templates/rust/Cargo.lock new file mode 100644 index 0000000..b9d42e1 --- /dev/null +++ b/templates/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "rust" +version = "0.1.0" diff --git a/templates/rust/Cargo.toml b/templates/rust/Cargo.toml new file mode 100644 index 0000000..1c61e9e --- /dev/null +++ b/templates/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/templates/rust/flake.lock b/templates/rust/flake.lock new file mode 100644 index 0000000..91e00c3 --- /dev/null +++ b/templates/rust/flake.lock @@ -0,0 +1,82 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1746300365, + "narHash": "sha256-thYTdWqCRipwPRxWiTiH1vusLuAy0okjOyzRx4hLWh4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "f21e4546e3ede7ae34d12a84602a22246b31f7e0", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1746326315, + "narHash": "sha256-IDqSls/r6yBfdOBRSMQ/noTUoigmsKnTQ7TqpsBtN4Y=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "dd280c436961ec5adccf0135efe5b66a23d84497", + "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" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/templates/rust/flake.nix b/templates/rust/flake.nix new file mode 100644 index 0000000..2a8f295 --- /dev/null +++ b/templates/rust/flake.nix @@ -0,0 +1,38 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-utils = { + url = "github:numtide/flake-utils"; + }; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs = { + nixpkgs.follows = "nixpkgs"; + }; + }; + }; + outputs = { + flake-utils, + nixpkgs, + rust-overlay, + ... + }: + flake-utils.lib.eachDefaultSystem (system: let + pkgs = import nixpkgs { + inherit system; + overlays = [(import rust-overlay)]; + }; + rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + rustPlatform = pkgs.makeRustPlatform { + cargo = rustToolchain; + rustc = rustToolchain; + }; + in { + devShells.default = pkgs.mkShell { + packages = with pkgs; [ + rust-analyzer + rustToolchain + ]; + }; + }); +} diff --git a/templates/rust/rust-toolchain.toml b/templates/rust/rust-toolchain.toml new file mode 100644 index 0000000..02cb8fc --- /dev/null +++ b/templates/rust/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "stable" +profile = "default" diff --git a/templates/rust/src/main.rs b/templates/rust/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/templates/rust/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}