From 8268de92c46c2896c22847d34b1f650fb7c00773 Mon Sep 17 00:00:00 2001 From: Igor Ramazanov Date: Tue, 11 Mar 2025 17:46:02 +0000 Subject: [PATCH] feat: do not rely on `git` for versioning It causes lots of pain when building with `nix` for a local `flake.nix`. Not sure if it's a good general solution: * now it requires a manual step of updating `./version` on each release. * we're losing commit hash abbreviation, like `0.5.3-5-gbe5a291` I guess, this can be fixed by installing git-pre-commit-hook, but even then it'd be wrong, because the output of `git describe --tags --always` won't be the same before and after commit. --- flake.nix | 3 +-- nix/agent.nix | 3 +-- nix/package.nix | 6 +----- version | 1 + vite.config.ts | 5 ++--- 5 files changed, 6 insertions(+), 12 deletions(-) create mode 100644 version diff --git a/flake.nix b/flake.nix index 5da87e9..9572c20 100644 --- a/flake.nix +++ b/flake.nix @@ -28,10 +28,9 @@ rec { }; in { formatter = pkgs.alejandra; - packages = rec { + packages = { headplane = pkgs.callPackage ./nix/package.nix {}; headplane-agent = pkgs.callPackage ./nix/agent.nix {}; - default = headplane; }; devShell = pkgs.devshell.mkShell rec { name = description; diff --git a/nix/agent.nix b/nix/agent.nix index 2ac815f..6948b0a 100644 --- a/nix/agent.nix +++ b/nix/agent.nix @@ -1,8 +1,7 @@ {buildGoModule}: buildGoModule { pname = "hp_agent"; - # TODO: take the latest `git tag`, if commits do not match, append `-SNAPSHOT`. - version = "0.5.3"; + version = builtins.replaceStrings ["\n"] [""] (builtins.readFile ../version); src = ../.; vendorHash = "sha256-G0kahv3mPTL/mxU2U+0IytJaFVPXMbMBktbLMfM0BO8="; ldflags = ["-s" "-w"]; diff --git a/nix/package.nix b/nix/package.nix index 7dbb035..7e5d9a3 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -9,11 +9,7 @@ }: stdenv.mkDerivation (finalAttrs: { pname = "headplane"; - - # TODO: take the latest `git tag`, if commits do not match, append `-SNAPSHOT`. - version = "0.5.3"; - - # TODO: requires `.git` directory. + version = builtins.replaceStrings ["\n"] [""] (builtins.readFile ../version); src = ../.; nativeBuildInputs = [ diff --git a/version b/version new file mode 100644 index 0000000..f5633c8 --- /dev/null +++ b/version @@ -0,0 +1 @@ +0.5.3-SNAPSHOT diff --git a/vite.config.ts b/vite.config.ts index 1f6ca48..e59bb8a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -2,7 +2,7 @@ import { reactRouter } from '@react-router/dev/vite'; import { defineConfig } from 'vite'; import babel from 'vite-plugin-babel'; import tsconfigPaths from 'vite-tsconfig-paths'; -import { execSync } from 'node:child_process'; +import fs from 'node:fs'; import tailwindcss from 'tailwindcss'; import autoprefixer from 'autoprefixer'; @@ -11,8 +11,7 @@ if (prefix.endsWith('/')) { throw new Error('Prefix must not end with a slash'); } -// Load the version via git tags -const version = execSync('git describe --tags --always').toString().trim(); +const version = fs.readFileSync("version", "utf8"); if (!version) { throw new Error('Unable to execute git describe'); }