Add clap CLI, logging, and update Nix flake

Introduce a clap‑based command line interface and file logging using
simplelog. Add the corresponding dependencies (clap, log, simplelog) and
a release profile with LTO and stripping. Refactor the Nix flake:
update default.nix metadata, improve devShell, add update script and
metadata, and adjust flake.nix outputs accordingly. Update Cargo.lock
to reflect new crates.
This commit is contained in:
2026-06-30 12:27:15 +02:00
parent f7aa0b44a7
commit 6e4c509ad3
9 changed files with 482 additions and 76 deletions
Generated
+301
View File
@@ -2,6 +2,307 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "anstream"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
[[package]]
name = "anstyle-parse"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "clap"
version = "4.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "deranged"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "libc"
version = "0.2.186"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
[[package]]
name = "log"
version = "0.4.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"
[[package]]
name = "num-conv"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441"
[[package]]
name = "num_threads"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9"
dependencies = [
"libc",
]
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "powerfmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "proc-macro2"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rust-flake-template"
version = "0.1.0"
dependencies = [
"clap",
"log",
"simplelog",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "simplelog"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16257adbfaef1ee58b1363bdc0664c9b8e1e30aed86049635fb5f147d065a9c0"
dependencies = [
"log",
"termcolor",
"time",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]]
name = "time"
version = "0.3.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e48db7b415311b615f910b3dcaa4557bcd4bf1982379c95c223fd8c2a20e210"
dependencies = [
"deranged",
"libc",
"num-conv",
"num_threads",
"powerfmt",
"serde_core",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109"
[[package]]
name = "time-macros"
version = "0.2.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c431b87111666e491a90baa837f914fb45cd5dc3c268591b0220ff5057f2085f"
dependencies = [
"num-conv",
"time-core",
]
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "winapi-util"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
+10 -1
View File
@@ -1,6 +1,15 @@
[package]
name = "rust-flake-template" # change the default name to your project name
name = "rust-flake-template"
version = "0.1.0"
edition = "2024"
[profile.release]
codegen-units = 1
lto = true
opt-level = 3
strip = true
[dependencies]
clap = { version = "4.6.1", features = ["derive"] }
log = "0.4.33"
simplelog = "0.12.2"
+45 -10
View File
@@ -1,14 +1,49 @@
{
lib,
rustPlatform,
packageName,
# fetchFromGitea,
pkg-config,
# add nixpkgs if your dependencies requires system libraries
}:
rustPlatform.buildRustPackage {
name = "${packageName}";
src = ./.;
# buildInputs = [ ]; <-- add nixpkgs if your dependencies requires system libraries
nativeBuildInputs = [ pkg-config ];
cargoHash = lib.fakeHash; # <-- Put your real hash here after failed nix build
}
versionCheckHook,
nix-update-script,
}: let
pname = "rust-flake-template";
version = "0.1.0";
in
rustPlatform.buildRustPackage {
inherit pname version;
src = ./.;
# src = fetchFromGitea {
# domain = "gitea.garandplg.com";
# owner = "GarandPLG";
# repo = "${pname}";
# tag = "v${version}";
# hash = lib.fakeHash;
# };
cargoHash = lib.fakeHash;
nativeBuildInputs = [pkg-config];
# buildInputs = [];
nativeInstallCheckInputs = [versionCheckHook];
doInstallCheck = true;
passthru.updateScript = nix-update-script {};
# postInstall = ''
# cp -r statics $out/bin
# '';
meta = {
description = "Basic rust flake for development and production";
homepage = "https://gitea.garandplg.com/GarandPLG/${pname}";
license = lib.licenses.mit;
platforms = ["x86_64-linux"];
maintainers = with lib.maintainers; [
GarandPLG
];
mainProgram = pname;
};
}
Generated
+12 -12
View File
@@ -8,11 +8,11 @@
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1762929886,
"narHash": "sha256-TQZ3Ugb1FoHpTSc8KLrzN4njIZU4FemAMHyS4M3mt6s=",
"lastModified": 1782732035,
"narHash": "sha256-oREqdReAdtbMW5WFcVFDg7NM4b6rmJQglZ+sxC+CwDs=",
"owner": "nix-community",
"repo": "fenix",
"rev": "6998514dce2c365142a0a119a95ef95d89b84086",
"rev": "1748a8592edbdcbe8a2317ddc237b47a27932578",
"type": "github"
},
"original": {
@@ -31,11 +31,11 @@
]
},
"locked": {
"lastModified": 1752689277,
"narHash": "sha256-uldUBFkZe/E7qbvxa3mH1ItrWZyT6w1dBKJQF/3ZSsc=",
"lastModified": 1782220280,
"narHash": "sha256-thLTFbp9D5Qknmh8q/v4FRpLGphUSijT3E86cbLYTXo=",
"owner": "nix-community",
"repo": "naersk",
"rev": "0e72363d0938b0208d6c646d10649164c43f4d64",
"rev": "9aa07bb0256d300219b30622d2454e85f7f3667e",
"type": "github"
},
"original": {
@@ -46,11 +46,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1762844143,
"narHash": "sha256-SlybxLZ1/e4T2lb1czEtWVzDCVSTvk9WLwGhmxFmBxI=",
"lastModified": 1782723713,
"narHash": "sha256-oPXCU/SSUokcGaJREHibG1CBX3+s/W7orDWQOZDsEeQ=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "9da7f1cf7f8a6e2a7cb3001b048546c92a8258b4",
"rev": "b5aa0fbd538984f6e3d201be0005b4463d8b09f8",
"type": "github"
},
"original": {
@@ -70,11 +70,11 @@
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1762860488,
"narHash": "sha256-rMfWMCOo/pPefM2We0iMBLi2kLBAnYoB9thi4qS7uk4=",
"lastModified": 1782691063,
"narHash": "sha256-qtAe8z4KKLIe/oKu4tc2bmaB+wEG/jjPrebR34WY0zg=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "2efc80078029894eec0699f62ec8d5c1a56af763",
"rev": "972c4e7bee140acd27e26b3e04673bfd05302f89",
"type": "github"
},
"original": {
+56 -51
View File
@@ -18,62 +18,67 @@
};
};
outputs =
{
self,
nixpkgs,
naersk,
fenix,
}:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
outputs = {
self,
nixpkgs,
naersk,
fenix,
}: let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
rustToolchain = fenix.packages.${system}.stable.toolchain;
naerskLib = pkgs.callPackage naersk {
cargo = rustToolchain;
rustc = rustToolchain;
};
rustToolchain = fenix.packages.${system}.stable.toolchain;
naerskLib = pkgs.callPackage naersk {
cargo = rustToolchain;
rustc = rustToolchain;
};
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
packageName = cargoToml.package.name;
packageVersion = cargoToml.package.version;
# you can list your other dependencies
# [dependency_name]Version = cargoToml.dependencies.[dependency_name];
# ...
in
{
packages.${system} = {
default = pkgs.callPackage ./default.nix {
packageName = packageName;
};
cargoToml = fromTOML (builtins.readFile ./Cargo.toml);
packageVersion = cargoToml.package.version;
in {
packages.${system} = {
default = pkgs.callPackage ./default.nix {};
develop = naerskLib.buildPackage {
name = "${packageName}";
src = ./.;
# buildInputs = with pkgs; []; <-- add nixpkgs if your dependencies requires system libraries
nativeBuildInputs = with pkgs; [ pkg-config ];
};
};
develop = naerskLib.buildPackage {
name = "rust-flake-template";
devShells.${system}.default = pkgs.mkShell {
env.RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
buildInputs = [
rustToolchain
# pkgs. ... <-- add nixpkgs if your dependencies requires system libraries
];
nativeBuildInputs = with pkgs; [ pkg-config ];
src = ./.;
shellHook = ''
echo "${packageName} v${packageVersion}"
echo "[you can list your other dependencies here]"
echo ""
echo "Commands:"
echo " nix build - Build production version"
echo " nix run - Run production version"
echo " nix build .#develop - Build development version"
echo " nix run .#develop - Run development version"
'';
# buildInputs = with pkgs; [];
nativeBuildInputs = with pkgs; [pkg-config];
# postInstall = ''
# cp -r statics $out/bin
# '';
};
};
devShells.${system}.default = pkgs.mkShell {
env = {
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
# LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath (with pkgs; []);
};
buildInputs = [
rustToolchain
# pkgs.
];
nativeBuildInputs = with pkgs; [pkg-config];
shellHook = ''
echo "rust-flake-template v${packageVersion}"
echo ""
echo "Commands:"
echo " nix update flake - Update flakes versions"
echo " nix build - Build production version"
echo " nix run - Run production version"
echo " nix build .#develop - Build development version"
echo " nix run .#develop - Run development version"
echo ""
cargo run -- -h
'';
};
};
}
+38
View File
@@ -0,0 +1,38 @@
use crate::logs::init_logger;
use clap::{Parser, builder::StyledStr};
#[derive(Parser, Debug, Clone)]
#[command(
version,
before_help = before_help(),
)]
pub struct Cli {
#[arg(
long,
short = 'l',
help = "Enable logging to file (default: disabled)",
default_value_t = false
)]
pub log: bool,
}
impl Cli {
pub fn get_args() -> Self {
let args: Cli = Cli::parse();
if args.log {
init_logger();
}
args
}
}
fn before_help() -> StyledStr {
format!(
"{}\t\t{}",
"Basic rust flake for development and production",
format!("v{}", env!("CARGO_PKG_VERSION"))
)
.into()
}
+2
View File
@@ -0,0 +1,2 @@
pub mod cli;
pub mod logs;
+11
View File
@@ -0,0 +1,11 @@
use simplelog::{Config, LevelFilter, WriteLogger};
use std::fs::File;
pub fn init_logger() {
WriteLogger::init(
LevelFilter::Info,
Config::default(),
File::create("rust-flake-template.log").expect("Failed to create log file"),
)
.expect("Failed to initialize logger");
}
+7 -2
View File
@@ -1,3 +1,8 @@
fn main() {
println!("Hello, world!");
use rust_flake_template::cli::Cli;
use std::io::Result;
fn main() -> Result<()> {
let _args: Cli = Cli::get_args();
Ok(())
}