From db8cf858ff1ba5a6a454fa2e6a8732578f212f3c Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 6 Nov 2025 13:44:35 -0800 Subject: [PATCH] build(nix): pull git rev info from flake inputs This commit adds a new workspace crate, version, which is consumed by komorebi and komorebic. If either of those binaries are being built by the Nix Flake, the git rev info will be pulled from inputs.self.rev instead of querying the git repo, because the git files are not passed to the derivation. The build.rs for the version crate decides if the build is happening by querying the shadow-rs build output for BRANCH, which will always be empty when a "nix build" is running. Cargo builds will use the standard git information retrieval mechanisms in shadow-rs. The information in both cases is exposed as version::LONG_VERSION, and passed to clap as the string to return when either binary is called with the --version flag. --- Cargo.lock | 9 +++++ Cargo.toml | 8 +++- deny.toml | 5 +++ flake.nix | 68 +++++++++++++-------------------- justfile | 3 +- komorebi/Cargo.toml | 4 +- komorebi/build.rs | 5 --- komorebi/src/lib.rs | 2 - komorebi/src/main.rs | 2 +- komorebi/src/process_command.rs | 2 +- komorebic/Cargo.toml | 1 + komorebic/build.rs | 4 -- komorebic/src/main.rs | 4 +- shell.nix | 3 +- version/Cargo.toml | 10 +++++ version/build.rs | 42 ++++++++++++++++++++ version/src/lib.rs | 3 ++ 17 files changed, 112 insertions(+), 63 deletions(-) delete mode 100644 komorebi/build.rs create mode 100644 version/Cargo.toml create mode 100644 version/build.rs create mode 100644 version/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index bbe798f2..fce2c79f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2967,6 +2967,7 @@ dependencies = [ "tracing-appender", "tracing-subscriber", "uuid", + "version", ] [[package]] @@ -3050,6 +3051,7 @@ dependencies = [ "serde_json_lenient", "shadow-rs", "sysinfo", + "version", ] [[package]] @@ -6479,6 +6481,13 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "version" +version = "0.1.0" +dependencies = [ + "shadow-rs", +] + [[package]] name = "version-compare" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 8202d407..c51b83b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,13 @@ [workspace] resolver = "2" -members = ["komorebi", "komorebi-bar", "komorebi-client", "komorebic"] +members = [ + "komorebi", + "komorebi-bar", + "komorebi-client", + "komorebic", + "version", +] [workspace.package] version = "0.1.0" diff --git a/deny.toml b/deny.toml index 7a56b2a8..8b01210d 100644 --- a/deny.toml +++ b/deny.toml @@ -62,6 +62,11 @@ crate = "komorebi-themes" expression = "LicenseRef-Komorebi-2.0" license-files = [] +[[licenses.clarify]] +crate = "version" +expression = "LicenseRef-Komorebi-2.0" +license-files = [] + [[licenses.clarify]] crate = "base16-egui-themes" expression = "MIT" diff --git a/flake.nix b/flake.nix index ef4f7694..994950e3 100644 --- a/flake.nix +++ b/flake.nix @@ -9,6 +9,7 @@ }; outputs = { + self, nixpkgs, crane, flake-utils, @@ -35,60 +36,50 @@ || (builtins.match ".*/docs/.*" path != null); }; - cargoArtifacts = craneLib.buildDepsOnly commonArgs; - commonArgs = { - inherit src version cargoArtifacts; + inherit src version; strictDeps = true; + COMMIT_HASH = self.rev or (lib.removeSuffix "-dirty" self.dirtyRev); + buildInputs = [ pkgs.gcc pkgs.libiconv ]; }; + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + individualCrateArgs = commonArgs // { + inherit cargoArtifacts; doCheck = false; + doDoc = false; }; - packages = { - komorebi = craneLib.buildPackage ( + packages = let + fullBuild = craneLib.buildPackage ( individualCrateArgs // { - inherit version; - pname = "komorebi"; - cargoExtraArgs = "-p komorebi"; + pname = "komorebi-workspace"; } ); - komorebic = craneLib.buildPackage ( - individualCrateArgs - // { - inherit version; - pname = "komorebic"; - cargoExtraArgs = "-p komorebic"; + extractBinary = binaryName: + pkgs.runCommand "komorebi-${binaryName}" + { + meta = fullBuild.meta // {}; } - ); - - komorebi-bar = craneLib.buildPackage ( - individualCrateArgs - // { - inherit version; - pname = "komorebi-bar"; - cargoExtraArgs = "-p komorebi-bar"; - } - ); - - komorebi-full = craneLib.buildPackage ( - individualCrateArgs - // { - inherit version; - pname = "komorebi-full"; - cargoExtraArgs = "-p komorebi -p komorebic -p komorebi-bar"; - } - ); + '' + mkdir -p $out/bin + cp ${fullBuild}/bin/${binaryName} $out/bin/ + ''; + in { + komorebi-full = fullBuild; + komorebi = extractBinary "komorebi"; + komorebic = extractBinary "komorebic"; + komorebi-bar = extractBinary "komorebi-bar"; }; in packages @@ -108,23 +99,18 @@ buildResult = komorebiBuild system; inherit (buildResult) + individualCrateArgs komorebi komorebic komorebi-bar komorebi-full pkgs craneLib - commonArgs src ; in { checks = { - komorebi-workspace-clippy = craneLib.cargoClippy ( - commonArgs - // { - cargoClippyExtraArgs = "--all-targets -- --deny warnings"; - } - ); + komorebi-workspace-clippy = craneLib.cargoClippy individualCrateArgs; komorebi-workspace-fmt = craneLib.cargoFmt { inherit src; @@ -138,7 +124,7 @@ inherit src; }; - komorebi-workspace-nextest = craneLib.cargoNextest commonArgs; + komorebi-workspace-nextest = craneLib.cargoNextest individualCrateArgs; }; packages = { diff --git a/justfile b/justfile index 97516fb8..46bc15af 100644 --- a/justfile +++ b/justfile @@ -4,6 +4,7 @@ fmt: test -z "$(rg 'eyre!' --type rust)" || (echo "eyre! macro not allowed"; false) test -z "$(rg 'dbg!' --type rust)" || (echo "dbg! macro not allowed"; false) test -z "$(rg 'println!' --type rust ./komorebi)" || (echo "println! macro not allowed"; false) + taplo fmt Cargo.toml */Cargo.toml cargo +nightly fmt cargo +stable clippy alejandra . @@ -56,4 +57,4 @@ depgen: push: git push origin master - git push komocorp master \ No newline at end of file + git push komocorp master diff --git a/komorebi/Cargo.toml b/komorebi/Cargo.toml index 2d87c3b1..ae779f00 100644 --- a/komorebi/Cargo.toml +++ b/komorebi/Cargo.toml @@ -7,6 +7,7 @@ edition = "2024" [dependencies] komorebi-themes = { workspace = true } quickxml_to_serde = { git = "https://github.com/LGUG2Z/quickxml_to_serde.git", version = "0.6.1" } +version = { path = "../version" } base64 = "0.21" block2 = "0.6" @@ -50,9 +51,6 @@ tracing = { workspace = true } tracing-appender = { workspace = true } tracing-subscriber = { workspace = true } -[build-dependencies] -shadow-rs = { workspace = true } - [dev-dependencies] uuid = { version = "1", features = ["v4"] } diff --git a/komorebi/build.rs b/komorebi/build.rs deleted file mode 100644 index 44fad00f..00000000 --- a/komorebi/build.rs +++ /dev/null @@ -1,5 +0,0 @@ -use shadow_rs::ShadowBuilder; - -fn main() { - ShadowBuilder::builder().build().unwrap(); -} diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 4a00c5d9..87fbbc4e 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -149,8 +149,6 @@ lazy_static! { static ref UNMANAGED_WINDOW_IDS: Arc>> = Arc::new(Mutex::new(vec![])); } -shadow_rs::shadow!(build); - pub static DEFAULT_WORKSPACE_PADDING: AtomicI32 = AtomicI32::new(5); pub static DEFAULT_CONTAINER_PADDING: AtomicI32 = AtomicI32::new(5); diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index 9e851504..7cccd778 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -173,7 +173,7 @@ enum LogLevel { } #[derive(Parser)] -#[clap(author, about, version = komorebi::build::CLAP_LONG_VERSION)] +#[clap(author, about, version = version::LONG_VERSION)] struct Opts { /// Path to a static configuration JSON file #[clap(short, long)] diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 4ebe8eeb..d4f13b7a 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -12,7 +12,6 @@ use crate::WORKSPACE_MATCHING_RULES; use crate::accessibility::AccessibilityApi; use crate::application::Application; use crate::border_manager; -use crate::build; use crate::cf_array_as; use crate::core::ApplicationIdentifier; use crate::core::MoveBehaviour; @@ -64,6 +63,7 @@ use std::str::FromStr; use std::sync::Arc; use std::sync::atomic::Ordering; use std::time::Duration; +use version::build; #[tracing::instrument] pub fn listen_for_commands(wm: Arc>) { diff --git a/komorebic/Cargo.toml b/komorebic/Cargo.toml index ccbec568..3817caa4 100644 --- a/komorebic/Cargo.toml +++ b/komorebic/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" [dependencies] komorebi-client = { path = "../komorebi-client", default-features = false } +version = { path = "../version" } clap = { workspace = true } clap_complete = "4" diff --git a/komorebic/build.rs b/komorebic/build.rs index 6da11b8a..b7c1f27e 100644 --- a/komorebic/build.rs +++ b/komorebic/build.rs @@ -1,5 +1,3 @@ -use shadow_rs::ShadowBuilder; - fn main() { if std::fs::metadata("applications.json").is_err() { let applications_json = reqwest::blocking::get( @@ -7,6 +5,4 @@ fn main() { ).unwrap().text().unwrap(); std::fs::write("applications.json", applications_json).unwrap(); } - - ShadowBuilder::builder().build().unwrap(); } diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 2a898158..1770d847 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -61,8 +61,6 @@ lazy_static! { .join("komorebi"); } -shadow_rs::shadow!(build); - #[derive(Copy, Clone, ValueEnum)] enum BooleanState { Enable, @@ -668,7 +666,7 @@ struct Splash { } #[derive(Parser)] -#[clap(author, about, version = build::CLAP_LONG_VERSION)] +#[clap(author, about, version = version::LONG_VERSION)] struct Opts { #[clap(subcommand)] subcmd: SubCommand, diff --git a/shell.nix b/shell.nix index 32a27942..fb685225 100644 --- a/shell.nix +++ b/shell.nix @@ -8,13 +8,14 @@ with pkgs; buildInputs = [ alejandra cargo-deny - cargo-udeps cargo-nextest + cargo-udeps gcc jq just libiconv prettier + taplo python311Packages.mkdocs-material python311Packages.mkdocs-macros diff --git a/version/Cargo.toml b/version/Cargo.toml new file mode 100644 index 00000000..ac610fad --- /dev/null +++ b/version/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "version" +edition = "2024" +version.workspace = true + +[dependencies] +shadow-rs = { workspace = true } + +[build-dependencies] +shadow-rs = { workspace = true } diff --git a/version/build.rs b/version/build.rs new file mode 100644 index 00000000..6c2f158a --- /dev/null +++ b/version/build.rs @@ -0,0 +1,42 @@ +use std::fs::File; +use std::io::Write; + +use shadow_rs::SdResult; +use shadow_rs::ShadowBuilder; + +fn main() { + let is_flake_build = ShadowBuilder::builder() + .build() + .unwrap() + .map + .get("BRANCH") + .map_or_else(|| true, |entry| entry.v.is_empty()); + + if is_flake_build { + ShadowBuilder::builder().hook(flake_hook).build().unwrap(); + } else { + ShadowBuilder::builder().hook(raw_hook).build().unwrap(); + } +} + +const RAW_VERSION_CONST: &str = r##"pub const LONG_VERSION:&str = shadow_rs::formatcp!(r#"{} +branch:{} +commit_hash:{} +build_env:{},{}"#,PKG_VERSION, BRANCH, COMMIT_HASH, RUST_VERSION, RUST_CHANNEL +); +"##; + +fn raw_hook(mut file: &File) -> SdResult<()> { + writeln!(file, "{RAW_VERSION_CONST}")?; + Ok(()) +} + +const FLAKE_VERSION_CONST: &str = r##"pub const LONG_VERSION:&str = shadow_rs::formatcp!(r#"{} +commit_hash:{} +build_env:{},{}"#,PKG_VERSION, env!("COMMIT_HASH"), RUST_VERSION, RUST_CHANNEL +);"##; + +fn flake_hook(mut file: &File) -> SdResult<()> { + writeln!(file, "{FLAKE_VERSION_CONST}")?; + Ok(()) +} diff --git a/version/src/lib.rs b/version/src/lib.rs new file mode 100644 index 00000000..b537c9ce --- /dev/null +++ b/version/src/lib.rs @@ -0,0 +1,3 @@ +shadow_rs::shadow!(build); + +pub use build::LONG_VERSION;