diff --git a/v3/engine/bin/engine/main.rs b/v3/engine/bin/engine/main.rs index 10d4b0dd9b9..e5970aa38c5 100644 --- a/v3/engine/bin/engine/main.rs +++ b/v3/engine/bin/engine/main.rs @@ -1,3 +1,7 @@ +use std::fmt::Display; +use std::path::PathBuf; +use std::sync::Arc; + use axum::{ body::HttpBody, extract::State, @@ -8,23 +12,22 @@ use axum::{ Extension, Json, Router, }; use clap::Parser; - -use ::engine::authentication::{AuthConfig, AuthConfig::V1 as V1AuthConfig, AuthModeConfig}; -use engine::{schema::GDS, VERSION}; -use hasura_authn_core::Session; -use hasura_authn_jwt::auth as jwt_auth; -use hasura_authn_jwt::jwt; -use hasura_authn_webhook::webhook; -use lang_graphql as gql; -use std::path::PathBuf; -use std::{fmt::Display, sync::Arc}; use tower_http::trace::TraceLayer; use tracing_util::{ add_event_on_active_span, set_status_on_current_span, AttributeVisibility, ErrorVisibility, SpanVisibility, TraceableError, TraceableHttpResponse, }; +use engine::authentication::{AuthConfig, AuthConfig::V1 as V1AuthConfig, AuthModeConfig}; +use engine::{schema::GDS, VERSION}; +use hasura_authn_core::Session; +use hasura_authn_jwt::auth as jwt_auth; +use hasura_authn_jwt::jwt; +use hasura_authn_webhook::webhook; +use lang_graphql as gql; + #[derive(Parser)] +#[command(version = VERSION)] struct ServerOptions { #[arg(long, value_name = "METADATA_FILE", env = "METADATA_PATH")] metadata_path: PathBuf, diff --git a/v3/engine/build.rs b/v3/engine/build.rs index 9f473f87b0c..2143f44f7e5 100644 --- a/v3/engine/build.rs +++ b/v3/engine/build.rs @@ -1,12 +1,27 @@ -fn main() { - // Cargo sets the PROFILE environment variable - let profile = std::env::var("PROFILE").unwrap(); - if profile == "release" { - // For release builds (cargo build --release ...), set the version to the git commit short - let commit_short = build_data::get_git_commit_short().unwrap(); - println!("cargo:rustc-env=CARGO_V3_ENGINE_VERSION={}", commit_short); +fn main() -> Result<(), String> { + // Ensure that we rebuild if the version is specified. + println!("cargo:rerun-if-env-changed=RELEASE_VERSION"); + + // On release builds, use the Git commit as a backup if the version is not set. + // On debug builds, use "dev". + // If we fail to get the Git information, fail. + let build_profile = std::env::var("PROFILE").map_err(|err| err.to_string())?; + let release_version = if build_profile == "release" { + match option_env!("RELEASE_VERSION") { + Some(version) => version.to_string(), + None => { + let git_commit_ref = build_data::get_git_commit_short()?; + let git_dirty = build_data::get_git_dirty().unwrap_or(false); + if git_dirty { + format!("{git_commit_ref}-dirty") + } else { + git_commit_ref + } + } + } } else { - // For non-release builds, set the version to 'dev' - println!("cargo:rustc-env=CARGO_V3_ENGINE_VERSION=dev"); - } + "dev".to_string() + }; + println!("cargo:rustc-env=CARGO_V3_ENGINE_VERSION={release_version}"); + Ok(()) } diff --git a/v3/flake.nix b/v3/flake.nix index 24bc891e981..de0906f1387 100644 --- a/v3/flake.nix +++ b/v3/flake.nix @@ -33,9 +33,24 @@ in { formatter = pkgs.nixpkgs-fmt; + packages = { # a binary for whichever is the local computer - default = rust.callPackage ./nix/app.nix { }; + default = rust.callPackage ./nix/app.nix { + version = if self ? "dirtyRev" then self.dirtyShortRev else self.shortRev; + }; + }; + + apps = { + default = self.apps.${localSystem}.engine; + engine = flake-utils.lib.mkApp { + drv = self.packages.${localSystem}.default; + name = "engine"; + }; + agent = flake-utils.lib.mkApp { + drv = self.packages.${localSystem}.default; + name = "engine"; + }; }; devShells = { diff --git a/v3/nix/app.nix b/v3/nix/app.nix index a89f6156b52..0c0a257aefd 100644 --- a/v3/nix/app.nix +++ b/v3/nix/app.nix @@ -1,6 +1,7 @@ # This is a function that returns a derivation for the compiled Rust project. { craneLib , lib +, version , stdenv , openssl , libiconv @@ -14,11 +15,13 @@ let src = let - isJsonFile = path: _type: builtins.match ".*json" path != null; isGraphqlFile = path: _type: builtins.match ".*graphql" path != null; + isHtmlFile = path: _type: builtins.match ".*html" path != null; + isJsonFile = path: _type: builtins.match ".*json" path != null; isSourceFile = path: type: - isJsonFile path type - || isGraphqlFile path type + isGraphqlFile path type + || isHtmlFile path type + || isJsonFile path type || craneLib.filterCargoSources path type; in lib.cleanSourceWith { src = craneLib.path ./..; filter = isSourceFile; }; @@ -45,4 +48,5 @@ craneLib.buildPackage (buildArgs // { inherit cargoArtifacts; doCheck = false; + RELEASE_VERSION = version; })