From 9524b777b3279248247fafeff67818266bec7d8a Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 27 Oct 2026 09:12:22 -0700 Subject: [PATCH] build(nix): build and check on Linux and macOS The flake builds the workspace with crane and checks it the way `just check` does, so that CI and a laptop run the same things: `komorebi-workspace-fmt` (nightly rustfmt, since rustfmt.toml uses unstable options), `-clippy` with warnings denied, `-doc` with rustdoc warnings denied, `-toml-fmt`, `-deny` and `-nextest`. Treefmt and a pre-commit hook format Rust, Nix, TOML and the justfile. `nix develop` gives a toolchain with the nightly rustfmt, cargo-deny, nextest and udeps. Linux is where komorebi runs, so only Linux gets packages and the overlay. An Apple silicon Mac gets the dev shell, the formatter and the checks; the tests there run against the fake desktop that arrives with the backend trait. x86_64-darwin is left out because nixpkgs 26.11 dropped it, and the flake follows the tip of nixpkgs-unstable. The commit hash is a constant (`nix-check`) for the dependencies and for every check, and the real one only goes into the final build. The version crate reads it with `env!`, so putting it in the shared arguments would rebuild the dependencies and rerun every check for each commit. The README and CONTRIBUTING.md describe `nix develop` and `nix flake check`. --- .envrc | 1 + CONTRIBUTING.md | 10 ++- README.md | 14 ++- flake.lock | 157 +++++++++++++++++++++++++++++++++ flake.nix | 225 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 402 insertions(+), 5 deletions(-) create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..8392d159 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7a7d3d34..7574fec0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,10 +31,16 @@ the 0-BSD license. ## Working on the code +`nix develop` (or [direnv](https://direnv.net), through the [`.envrc`](.envrc)) +provides the Rust toolchain, a nightly `rustfmt` and the tools the checks use. +[`rustfmt.toml`](rustfmt.toml) uses an unstable option, which is why the +formatter is a nightly one. + `just check` runs everything a change has to pass before it is pushed: the formatting check, clippy with warnings denied, the tests, and rustdoc with -warnings denied. Formatting needs a nightly `rustfmt`, because -[`rustfmt.toml`](rustfmt.toml) uses an unstable option; `just fmt` applies it. +warnings denied. `just fmt` formats the Rust sources, and `nix fmt` every file +type the repository has. `nix flake check` is what CI runs; it builds the same +checks, plus `cargo deny`, as derivations. ## Commits diff --git a/README.md b/README.md index 0147a807..883344a9 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,15 @@ moving and the drawing. ## Running it locally ```sh +nix develop cargo run --bin komorebi --locked -- --version ``` -The `rust-toolchain.toml` at the root selects the Rust toolchain. +`nix develop` provides the Rust toolchain that [`rust-toolchain.toml`](rust-toolchain.toml) +selects, a nightly `rustfmt`, and the tools `just check` and the flake's checks +use. With [direnv](https://direnv.net), the [`.envrc`](.envrc) does it on +`cd`. On Linux, `nix run .#komorebi -- --version` builds and runs the daemon +without a checkout of the toolchain. ## Crates @@ -22,8 +27,11 @@ The `rust-toolchain.toml` at the root selects the Rust toolchain. ## Checks `just check` runs the formatting check, clippy with warnings denied, the tests -and rustdoc with warnings denied. Formatting needs a nightly `rustfmt`, because -[`rustfmt.toml`](rustfmt.toml) uses an unstable option. +and rustdoc with warnings denied, inside `nix develop`. `nix flake check` runs +the same checks as derivations, plus `cargo deny`, the TOML and Nix formatting, +and the tests under `cargo nextest`, on Linux and on an Apple silicon Mac. A Mac +runs the tests against the fake desktop, since KWin only exists on Linux. +`nix fmt` formats every file the checks look at. ## Documentation diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..4c760a2d --- /dev/null +++ b/flake.lock @@ -0,0 +1,157 @@ +{ + "nodes": { + "crane": { + "locked": { + "lastModified": 1792350552, + "narHash": "sha256-292lvpvxTGKryKxh3/3tcIVi85CwYGdl4oOi113Sob8=", + "owner": "ipetkov", + "repo": "crane", + "rev": "e675a36b7377bebe41d548388a2501cbb16d380d", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1767039857, + "narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=", + "owner": "NixOS", + "repo": "flake-compat", + "rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1790898470, + "narHash": "sha256-zTwuwezD0w3hpga6a6P8emidzAZFyWqlNeRBlpWOOl8=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "024633cd702b10285db5cb19b40ad48d2399ba60", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "git-hooks-nix": { + "inputs": { + "flake-compat": "flake-compat", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1791909798, + "narHash": "sha256-VAhpCOmrun/KXimYfR8NM88bciKCui7CqHHhThSzgag=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "1ba1dbfe28738c9a71c266be6efa5b89e75491a9", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1792842277, + "narHash": "sha256-pGPnZGi8y9d76xNeracniXO3dsYVROEmMkMc8jD0Ihc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "d3b13ad75cc492690728f9307e443dec9ef9a20e", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1790477367, + "narHash": "sha256-oujbxmVKDhci2E4EM3KyAbRUgp5/IsCoiA/LfEF7OOk=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "f7cd230690d9fc982d06129f9189b101fddcfa3b", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "flake-parts": "flake-parts", + "git-hooks-nix": "git-hooks-nix", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay", + "treefmt-nix": "treefmt-nix" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1793086596, + "narHash": "sha256-ObKS44vw4IIVpQ69YJzUGjZ0IwnpWoUlwjJSY7gefdo=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "c7e9d512653a35ef4d5879c3df4d44d7b78d0e6d", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1791885023, + "narHash": "sha256-C3alQ2MEt0zVrUSho5NUDo3jmhbff72++bfwoqT9Omo=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "0d6d739a833ba9a9c93034862e4a7b061a062a1c", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..7a4f69af --- /dev/null +++ b/flake.nix @@ -0,0 +1,225 @@ +{ + description = "komorebi for KDE Plasma"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + crane.url = "github:ipetkov/crane"; + rust-overlay.url = "github:oxalica/rust-overlay"; + rust-overlay.inputs.nixpkgs.follows = "nixpkgs"; + treefmt-nix.url = "github:numtide/treefmt-nix"; + treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; + git-hooks-nix.url = "github:cachix/git-hooks.nix"; + git-hooks-nix.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = + inputs@{ + self, + nixpkgs, + flake-parts, + crane, + rust-overlay, + ... + }: + let + mkPkgs = + system: + import nixpkgs { + inherit system; + overlays = [ (import rust-overlay) ]; + }; + + mkKomorebiPackages = + { pkgs }: + let + toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + craneLib = (crane.mkLib pkgs).overrideToolchain toolchain; + # rustfmt.toml uses options only a nightly rustfmt knows + craneLibFmt = (crane.mkLib pkgs).overrideToolchain ( + pkgs.rust-bin.nightly.latest.minimal.override { extensions = [ "rustfmt" ]; } + ); + + src = craneLib.cleanCargoSource ./.; + + commonArgs = { + inherit src; + pname = "komorebi-workspace"; + version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version; + strictDeps = true; + # The version crate wants a commit at compile time. It is a constant here, so that the + # dependencies and every check are the same derivations for every commit; only the final + # build below knows the real one. + COMMIT_HASH = "nix-check"; + }; + + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + individualCrateArgs = commonArgs // { + inherit cargoArtifacts; + doCheck = false; + doDoc = false; + }; + + fullBuild = craneLib.buildPackage ( + individualCrateArgs + // { + COMMIT_HASH = self.rev or (pkgs.lib.removeSuffix "-dirty" self.dirtyRev); + } + ); + + extractBinary = + binaryName: + pkgs.runCommand "komorebi-${binaryName}" { meta = fullBuild.meta // { }; } '' + mkdir -p $out/bin + cp ${fullBuild}/bin/${binaryName} $out/bin/ + ''; + in + { + inherit + craneLib + craneLibFmt + src + commonArgs + individualCrateArgs + fullBuild + ; + komorebi = extractBinary "komorebi"; + }; + in + flake-parts.lib.mkFlake { inherit inputs; } { + # Linux is what komorebi runs on. An Apple silicon Mac gets the dev shell, the formatter and the + # checks, which run the same tests against the fake desktop. nixpkgs dropped x86_64-darwin in + # 26.11. + systems = [ + "aarch64-linux" + "x86_64-linux" + "aarch64-darwin" + ]; + + imports = [ + inputs.treefmt-nix.flakeModule + inputs.git-hooks-nix.flakeModule + ]; + + perSystem = + { + config, + lib, + system, + ... + }: + let + pkgs = mkPkgs system; + build = mkKomorebiPackages { inherit pkgs; }; + nightlyRustfmt = pkgs.rust-bin.nightly.latest.rustfmt; + nightlyToolchain = pkgs.rust-bin.nightly.latest.default; + rustToolchainWithNightlyRustfmt = pkgs.symlinkJoin { + name = "rust-toolchain-with-nightly-rustfmt"; + paths = [ + nightlyRustfmt + (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml) + ]; + }; + # cargo-udeps needs a nightly compiler + cargo-udeps = pkgs.writeShellScriptBin "cargo-udeps" '' + export PATH="${nightlyToolchain}/bin:$PATH" + exec ${pkgs.cargo-udeps}/bin/cargo-udeps "$@" + ''; + in + { + treefmt = { + projectRootFile = "flake.nix"; + programs = { + deadnix.enable = true; + just.enable = true; + nixfmt.enable = true; + taplo.enable = true; + rustfmt = { + enable = true; + package = nightlyRustfmt; + }; + }; + }; + + checks = { + komorebi-workspace-fmt = build.craneLibFmt.cargoFmt { inherit (build) src; }; + + komorebi-workspace-clippy = build.craneLib.cargoClippy ( + build.individualCrateArgs + // { + cargoClippyExtraArgs = "--workspace --all-targets -- -D warnings"; + } + ); + + komorebi-workspace-doc = build.craneLib.cargoDoc ( + build.individualCrateArgs + // { + cargoDocExtraArgs = "--workspace --no-deps"; + env.RUSTDOCFLAGS = "-D warnings"; + } + ); + + komorebi-workspace-toml-fmt = build.craneLib.taploFmt { + src = lib.sources.sourceFilesBySuffices build.src [ ".toml" ]; + }; + + komorebi-workspace-deny = build.craneLib.cargoDeny { inherit (build) src; }; + + komorebi-workspace-nextest = build.craneLib.cargoNextest ( + build.individualCrateArgs // { doCheck = true; } + ); + }; + + packages = lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux { + inherit (build) komorebi; + komorebi-full = build.fullBuild; + default = build.fullBuild; + }; + + apps = lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux { + komorebi = { + type = "app"; + program = "${build.komorebi}/bin/komorebi"; + meta.description = "The komorebi daemon"; + }; + default = config.apps.komorebi; + }; + + devShells.default = pkgs.mkShell { + name = "komorebi"; + + RUST_BACKTRACE = "full"; + + inputsFrom = [ build.fullBuild ]; + + packages = [ + rustToolchainWithNightlyRustfmt + cargo-udeps + + pkgs.cargo-deny + pkgs.cargo-nextest + pkgs.jq + pkgs.just + pkgs.ripgrep + ]; + }; + + pre-commit = { + check.enable = true; + settings.hooks.treefmt = { + enable = true; + package = config.treefmt.build.wrapper; + pass_filenames = false; + }; + }; + }; + + flake = { + overlays.default = final: _: { + inherit ((mkKomorebiPackages { pkgs = mkPkgs final.stdenv.hostPlatform.system; })) komorebi; + }; + overlays.komorebi = self.overlays.default; + }; + }; +}