diff --git a/flake.nix b/flake.nix index 5b348c2..2d21039 100644 --- a/flake.nix +++ b/flake.nix @@ -10,6 +10,9 @@ systems = [ "x86_64-linux" "aarch64-linux" ]; forSystems = f: lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system}); + # The commit the package's version shows, if the flake is from Git + rev = self.shortRev or self.dirtyShortRev or null; + # Each module defaults its `package` to this flake's build for the system it's used on withPackage = path: option: { lib, pkgs, ... }: { imports = [ path ]; @@ -19,7 +22,7 @@ in { packages = forSystems (pkgs: rec { - slop-broker = pkgs.callPackage ./nix/package.nix { }; + slop-broker = pkgs.callPackage ./nix/package.nix { inherit rev; }; default = slop-broker; # The demo's VMs (see `docs/demo.md`) demo = import ./nix/demo/agent.nix { inherit pkgs slop-broker; }; @@ -27,7 +30,7 @@ }); overlays.default = final: _prev: { - slop-broker = final.callPackage ./nix/package.nix { }; + slop-broker = final.callPackage ./nix/package.nix { inherit rev; }; }; nixosModules = { diff --git a/nix/package.nix b/nix/package.nix index 8065442..5f95d3a 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -1,10 +1,13 @@ -{ lib, rustPlatform, pkg-config, openssl, sqlite }: +# `rev` is the commit the build is from, like `2d59b59` or `2d59b59-dirty`, which the version +# shows. It's part of the build, so every commit rebuilds the package. +{ lib, rustPlatform, pkg-config, openssl, sqlite, rev ? null }: let inherit (lib.fileset) toSource unions; + inherit ((builtins.fromTOML (builtins.readFile ../Cargo.toml)).package) version; in rustPlatform.buildRustPackage { pname = "slop-broker"; - inherit ((builtins.fromTOML (builtins.readFile ../Cargo.toml)).package) version; + inherit version; src = toSource { root = ../.; @@ -16,6 +19,10 @@ rustPlatform.buildRustPackage { nativeBuildInputs = [ pkg-config ]; buildInputs = [ openssl sqlite ]; + env = lib.optionalAttrs (rev != null) { + SLOP_BROKER_VERSION = "${version}+${rev}"; + }; + postInstall = '' ln -s slop-broker $out/bin/slop-approve install -Dm644 slop-tools.md $out/share/slop-broker/slop-tools.md diff --git a/src/bin/slop-broker.rs b/src/bin/slop-broker.rs index bc06630..f7dcfe3 100644 --- a/src/bin/slop-broker.rs +++ b/src/bin/slop-broker.rs @@ -12,7 +12,7 @@ use slop_broker::server::{self, Broker}; use slop_broker::{approve, web}; #[derive(Parser)] -#[command(about = "Approval broker for coding agents")] +#[command(version = slop_broker::VERSION, about = "Approval broker for coding agents")] enum Command { /// Run the broker. Serve { diff --git a/src/bin/slop.rs b/src/bin/slop.rs index 30ee16b..32b86b5 100644 --- a/src/bin/slop.rs +++ b/src/bin/slop.rs @@ -27,6 +27,8 @@ const GUIDE: &str = include_str!("../../slop-tools.md"); #[derive(Parser)] #[command( + name = "slop", + version = slop_broker::VERSION, about = "Ask the slop broker to run privileged operations", after_help = "Requests wait for an approver for as long as the broker allows (it says how long);\n\ don't retry them in a loop.\n\ diff --git a/src/lib.rs b/src/lib.rs index 013b07a..7b57057 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,3 +15,10 @@ pub mod server; pub mod targets; pub mod types; pub mod web; + +/// The version, like `0.3.0+2d59b59`: Nix builds add the commit they're built from (with `-dirty` +/// for uncommitted changes) in `SLOP_BROKER_VERSION`. +pub const VERSION: &str = match option_env!("SLOP_BROKER_VERSION") { + Some(version) => version, + None => env!("CARGO_PKG_VERSION"), +}; diff --git a/src/server.rs b/src/server.rs index ecb08af..b05c04d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -176,8 +176,9 @@ pub async fn handle(broker: Arc Option { if request.version != PROTOCOL_VERSION { return Some(ServerMessage::Error(format!( - "protocol version {} is not supported (the broker uses {PROTOCOL_VERSION}); update slop", - request.version + "protocol version {} is not supported (slop-broker {} uses {PROTOCOL_VERSION}); update slop", + request.version, + crate::VERSION ))); } diff --git a/src/web/page.rs b/src/web/page.rs index ae511d8..f0ee27c 100644 --- a/src/web/page.rs +++ b/src/web/page.rs @@ -14,7 +14,7 @@ use crate::ops::printable; use crate::queue::{Decision, PendingInfo}; /// The broker's version, which signed-in pages show. -pub const VERSION: &str = env!("CARGO_PKG_VERSION"); +pub use crate::VERSION; /// HTML escaping, after replacing what [`printable`] replaces. #[derive(Debug, Clone, Copy)]