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.

+24 -19
+13 -19
pkgs/tools/text/chroma/default.nix
··· 1 { lib, buildGoModule, fetchFromGitHub }: 2 3 buildGoModule rec { 4 pname = "chroma"; 5 version = "0.9.4"; 6 7 src = fetchFromGitHub { 8 owner = "alecthomas"; 9 repo = pname; 10 rev = "v${version}"; 11 - sha256 = "14jp6f83ca2srcylf9w6v7cvznrm1sbpcs6lk7pimgr3jhy5j339"; 12 - # populate values otherwise taken care of by goreleaser, 13 - # unfortunately these require us to use git. By doing 14 - # this in postFetch we can delete .git afterwards and 15 - # maintain better reproducibility of the src. 16 - leaveDotGit = true; 17 - postFetch = '' 18 - cd "$out" 19 - 20 - commit="$(git rev-parse HEAD)" 21 - date=$(git show -s --format=%aI "$commit") 22 - 23 - substituteInPlace "$out/cmd/chroma/main.go" \ 24 - --replace 'version = "?"' 'version = "${version}"' \ 25 - --replace 'commit = "?"' "commit = \"$commit\"" \ 26 - --replace 'date = "?"' "date = \"$date\"" 27 - 28 - find "$out" -name .git -print0 | xargs -0 rm -rf 29 - ''; 30 }; 31 32 vendorSha256 = "1l5ryhwifhff41r4z1d2lifpvjcc4yi1vzrzlvkx3iy9dmxqcssl"; 33 34 modRoot = "./cmd/chroma"; 35 36 37 meta = with lib; { 38 homepage = "https://github.com/alecthomas/chroma";
··· 1 { lib, buildGoModule, fetchFromGitHub }: 2 3 + let 4 + srcInfo = builtins.fromJSON (builtins.readFile ./src.json); 5 + in 6 + 7 buildGoModule rec { 8 pname = "chroma"; 9 version = "0.9.4"; 10 11 + # To update: 12 + # nix-prefetch-git --rev v${version} https://github.com/alecthomas/chroma.git > src.json 13 src = fetchFromGitHub { 14 owner = "alecthomas"; 15 repo = pname; 16 rev = "v${version}"; 17 + inherit (srcInfo) sha256; 18 }; 19 20 vendorSha256 = "1l5ryhwifhff41r4z1d2lifpvjcc4yi1vzrzlvkx3iy9dmxqcssl"; 21 22 modRoot = "./cmd/chroma"; 23 24 + # substitute version info as done in goreleaser builds 25 + ldflags = [ 26 + "-X" "main.version=${version}" 27 + "-X" "main.commit=${srcInfo.rev}" 28 + "-X" "main.date=${srcInfo.date}" 29 + ]; 30 31 meta = with lib; { 32 homepage = "https://github.com/alecthomas/chroma";
+11
pkgs/tools/text/chroma/src.json
···
··· 1 + { 2 + "url": "https://github.com/alecthomas/chroma.git", 3 + "rev": "6520148857c2ae3106ff371e527abea815b23915", 4 + "date": "2021-10-17T08:46:20+11:00", 5 + "path": "/nix/store/0s8a46d1nyjl3yhsgni2jz5vdv95cka8-chroma", 6 + "sha256": "1iy6mymdjxbl5wbll1mivv7gqdyqhl6xpfqps99z307m7y38r1ni", 7 + "fetchLFS": false, 8 + "fetchSubmodules": false, 9 + "deepClone": false, 10 + "leaveDotGit": false 11 + }