chroma: use -X ldflag to do variable substitution in cmd/chroma

Previously this was done rather hackily using substituteInPlace in
postFetch, however I've since found out that the way goreleaser does it
is actually quite accessible, since it just calls go with the
appropriate -X flags.

To avoid having to call git in the build to obtain the values and to not
rely on leaveDotGit, we can use the JSON populated by nix-prefetch-git
which contains the extra information we need: the commit hash and the
date.
This commit is contained in:
sternenseemann 2021-10-17 13:31:01 +02:00 committed by sterni
parent 22a15615c7
commit 2174bfe675
2 changed files with 24 additions and 19 deletions

View File

@ -1,38 +1,32 @@
{ lib, buildGoModule, fetchFromGitHub }:
let
srcInfo = builtins.fromJSON (builtins.readFile ./src.json);
in
buildGoModule rec {
pname = "chroma";
version = "0.9.4";
# To update:
# nix-prefetch-git --rev v${version} https://github.com/alecthomas/chroma.git > src.json
src = fetchFromGitHub {
owner = "alecthomas";
repo = pname;
rev = "v${version}";
sha256 = "14jp6f83ca2srcylf9w6v7cvznrm1sbpcs6lk7pimgr3jhy5j339";
# populate values otherwise taken care of by goreleaser,
# unfortunately these require us to use git. By doing
# this in postFetch we can delete .git afterwards and
# maintain better reproducibility of the src.
leaveDotGit = true;
postFetch = ''
cd "$out"
commit="$(git rev-parse HEAD)"
date=$(git show -s --format=%aI "$commit")
substituteInPlace "$out/cmd/chroma/main.go" \
--replace 'version = "?"' 'version = "${version}"' \
--replace 'commit = "?"' "commit = \"$commit\"" \
--replace 'date = "?"' "date = \"$date\""
find "$out" -name .git -print0 | xargs -0 rm -rf
'';
inherit (srcInfo) sha256;
};
vendorSha256 = "1l5ryhwifhff41r4z1d2lifpvjcc4yi1vzrzlvkx3iy9dmxqcssl";
modRoot = "./cmd/chroma";
# substitute version info as done in goreleaser builds
ldflags = [
"-X" "main.version=${version}"
"-X" "main.commit=${srcInfo.rev}"
"-X" "main.date=${srcInfo.date}"
];
meta = with lib; {
homepage = "https://github.com/alecthomas/chroma";

View File

@ -0,0 +1,11 @@
{
"url": "https://github.com/alecthomas/chroma.git",
"rev": "6520148857c2ae3106ff371e527abea815b23915",
"date": "2021-10-17T08:46:20+11:00",
"path": "/nix/store/0s8a46d1nyjl3yhsgni2jz5vdv95cka8-chroma",
"sha256": "1iy6mymdjxbl5wbll1mivv7gqdyqhl6xpfqps99z307m7y38r1ni",
"fetchLFS": false,
"fetchSubmodules": false,
"deepClone": false,
"leaveDotGit": false
}