From d6b6751ebdbff5465373ba0bc4f88fd4cbc7f57a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20R=C3=B6ger?= Date: Sat, 28 Mar 2026 16:20:41 +0100 Subject: [PATCH] move templates --- flake.nix | 1 + templates/_cmake-c/.gitignore | 1 + templates/_cmake-c/CMakeLists.txt | 10 +++++ templates/_cmake-c/flake.nix | 56 ++++++++++++++++++++++++ templates/_cmake-c/nix/derivation.nix | 17 ++++++++ templates/_cmake-c/src/main.c | 6 +++ templates/_rust/.envrc | 17 ++++++++ templates/_rust/.gitignore | 3 ++ templates/_rust/Cargo.toml | 11 +++++ templates/_rust/flake.nix | 61 +++++++++++++++++++++++++++ templates/_rust/rust-toolchain.toml | 3 ++ templates/_rust/src/main.rs | 3 ++ templates/cmake-c.nix | 6 +++ templates/mergeTemplateOptions.nix | 21 +++++++++ templates/rust.nix | 13 ++++++ 15 files changed, 229 insertions(+) create mode 100644 templates/_cmake-c/.gitignore create mode 100644 templates/_cmake-c/CMakeLists.txt create mode 100644 templates/_cmake-c/flake.nix create mode 100644 templates/_cmake-c/nix/derivation.nix create mode 100644 templates/_cmake-c/src/main.c create mode 100644 templates/_rust/.envrc create mode 100644 templates/_rust/.gitignore create mode 100644 templates/_rust/Cargo.toml create mode 100644 templates/_rust/flake.nix create mode 100644 templates/_rust/rust-toolchain.toml create mode 100644 templates/_rust/src/main.rs create mode 100644 templates/cmake-c.nix create mode 100644 templates/mergeTemplateOptions.nix create mode 100644 templates/rust.nix diff --git a/flake.nix b/flake.nix index 910b131..8d16a80 100644 --- a/flake.nix +++ b/flake.nix @@ -48,6 +48,7 @@ (./home + "/jonas@comfy-station") (./home + "/jonas@monolith") (import-tree ./modules) + (import-tree ./templates) ]; }; } diff --git a/templates/_cmake-c/.gitignore b/templates/_cmake-c/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/templates/_cmake-c/.gitignore @@ -0,0 +1 @@ +build diff --git a/templates/_cmake-c/CMakeLists.txt b/templates/_cmake-c/CMakeLists.txt new file mode 100644 index 0000000..1f0e64f --- /dev/null +++ b/templates/_cmake-c/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.15) + +project(Hello + DESCRIPTION "Hello World" + LANGUAGES C +) + +add_executable(hello src/main.c) + +install(TARGETS hello) diff --git a/templates/_cmake-c/flake.nix b/templates/_cmake-c/flake.nix new file mode 100644 index 0000000..1a56dc3 --- /dev/null +++ b/templates/_cmake-c/flake.nix @@ -0,0 +1,56 @@ +{ + description = "Cmake C Flake"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + }; + + outputs = inputs @ { + self, + flake-parts, + ... + }: + flake-parts.lib.mkFlake {inherit inputs;} ( + top: { + imports = []; + + flake = { + overlays.default = final: prev: { + my-derivation = final.callPackage ./nix/derivation.nix {}; + }; + }; + + systems = [ + "x86_64-linux" + ]; + + perSystem = { + self', + pkgs, + system, + ... + }: { + _module.args.pkgs = import inputs.nixpkgs { + inherit system; + overlays = [self.overlays.default]; + }; + + packages.default = pkgs.my-derivation; + + devShells.default = pkgs.mkShell { + packages = [ + pkgs.cmake-language-server + pkgs.cmake-format + pkgs.clang-tools + pkgs.gdb + ]; + inputsFrom = [self'.packages.default]; + shellHook = '' + export CMAKE_EXPORT_COMPILE_COMMANDS=ON + ''; + }; + }; + } + ); +} diff --git a/templates/_cmake-c/nix/derivation.nix b/templates/_cmake-c/nix/derivation.nix new file mode 100644 index 0000000..c1d08b8 --- /dev/null +++ b/templates/_cmake-c/nix/derivation.nix @@ -0,0 +1,17 @@ +{ + cmake, + stdenv, + ... +}: +stdenv.mkDerivation (finalAttrs: { + pname = "my-derivation"; + version = "0.1.0"; + src = ../.; + nativeBuildInputs = [cmake]; + buildInputs = []; + + meta = { + description = "Hello World Binary"; + mainProgram = "hello"; + }; +}) diff --git a/templates/_cmake-c/src/main.c b/templates/_cmake-c/src/main.c new file mode 100644 index 0000000..39ccd04 --- /dev/null +++ b/templates/_cmake-c/src/main.c @@ -0,0 +1,6 @@ +#include + +int main() { + puts("Hello, Flake!"); + return 0; +} diff --git a/templates/_rust/.envrc b/templates/_rust/.envrc new file mode 100644 index 0000000..412cbef --- /dev/null +++ b/templates/_rust/.envrc @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# ^ make editor happy + +# +# Use https://direnv.net/ to automatically load the dev shell. +# + +if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4=" +fi + +watch_file nix/** +watch_file -- **/*.nix +# Adding files to git includes them in a flake +# But it is also a bit much reloading. +# watch_file .git/index .git/HEAD +use flake . --show-trace diff --git a/templates/_rust/.gitignore b/templates/_rust/.gitignore new file mode 100644 index 0000000..80c4fce --- /dev/null +++ b/templates/_rust/.gitignore @@ -0,0 +1,3 @@ +.direnv/ +target/ +result/ diff --git a/templates/_rust/Cargo.toml b/templates/_rust/Cargo.toml new file mode 100644 index 0000000..9322e5e --- /dev/null +++ b/templates/_rust/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "hello" +description = "A test rust binary" +publish = false +version = "0.1.0" + +edition = "2021" + +[[bin]] +name = "hello" +path = "src/main.rs" diff --git a/templates/_rust/flake.nix b/templates/_rust/flake.nix new file mode 100644 index 0000000..2d1b81a --- /dev/null +++ b/templates/_rust/flake.nix @@ -0,0 +1,61 @@ +{ + description = "Rust-Hello"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + flake-utils.url = "github:numtide/flake-utils"; + crate2nix = { + url = "github:nix-community/crate2nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = inputs @ { + crate2nix, + flake-utils, + nixpkgs, + rust-overlay, + ... + }: + flake-utils.lib.eachDefaultSystem ( + system: let + # Overlay pkgs with rust-bin + overlays = [(import rust-overlay)]; + pkgs = import nixpkgs { + inherit system overlays; + }; + + # Use rust-bin to generate the toolchain from rust-toolchain.toml + rust-toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + + buildRustCrateForPkgs = _: + pkgs.buildRustCrate.override { + rustc = rust-toolchain; # Use rustc from toolchain + cargo = rust-toolchain; # Use cargo from toolchain + }; + + # Cargo.nix for IFD + generatedCargoNix = crate2nix.tools.${system}.generatedCargoNix { + name = "rustnix"; + src = ./.; + }; + + cargoNix = import generatedCargoNix { + inherit pkgs buildRustCrateForPkgs; + }; + in { + packages = rec { + hello = cargoNix.rootCrate.build; + default = hello; + }; + + devShell = pkgs.mkShell { + buildInputs = [rust-toolchain]; + }; + } + ); +} diff --git a/templates/_rust/rust-toolchain.toml b/templates/_rust/rust-toolchain.toml new file mode 100644 index 0000000..535db1f --- /dev/null +++ b/templates/_rust/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "1.85.0" +components = [ "rustfmt", "rustc-dev", "rust-analyzer", "rust-src"] diff --git a/templates/_rust/src/main.rs b/templates/_rust/src/main.rs new file mode 100644 index 0000000..ea5b5d8 --- /dev/null +++ b/templates/_rust/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("{}", "Hello, world!"); +} diff --git a/templates/cmake-c.nix b/templates/cmake-c.nix new file mode 100644 index 0000000..2ae0eef --- /dev/null +++ b/templates/cmake-c.nix @@ -0,0 +1,6 @@ +_: { + flake.templates.cmake-c = { + path = ./_cmake-c; + description = "A simple cmake c project."; + }; +} diff --git a/templates/mergeTemplateOptions.nix b/templates/mergeTemplateOptions.nix new file mode 100644 index 0000000..3a1133e --- /dev/null +++ b/templates/mergeTemplateOptions.nix @@ -0,0 +1,21 @@ +# Flake module that declares flake.templates outputs and how to merge it +{ + lib, + flake-parts-lib, + ... +}: let + inherit (lib) mkOption types; + inherit (flake-parts-lib) mkSubmoduleOptions; +in { + options = { + flake = mkSubmoduleOptions { + templates = mkOption { + type = types.lazyAttrsOf types.unspecified; + default = {}; + description = '' + Templates that will be put in the self flake. + ''; + }; + }; + }; +} diff --git a/templates/rust.nix b/templates/rust.nix new file mode 100644 index 0000000..9bba03f --- /dev/null +++ b/templates/rust.nix @@ -0,0 +1,13 @@ +_: { + flake.templates.rust = { + path = ./_rust; + description = "A simple rust binary template"; + welcomeText = '' + # Rust template + This is a simple rust binary template. + To build the project run `cargo build`. + To run the project run `cargo run`. + Before running nix build, make sure to run `cargo generate-lockfile` first. + ''; + }; +}