From 2174bfe6759d3054802ac3143dd9d9cfa732c2aa Mon Sep 17 00:00:00 2001 From: sternenseemann Date: Sun, 17 Oct 2021 13:31:01 +0200 Subject: [PATCH] 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. --- pkgs/tools/text/chroma/default.nix | 32 ++++++++++++------------------ pkgs/tools/text/chroma/src.json | 11 ++++++++++ 2 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 pkgs/tools/text/chroma/src.json diff --git a/pkgs/tools/text/chroma/default.nix b/pkgs/tools/text/chroma/default.nix index 206a9e35e4d1..f99c090eb975 100644 --- a/pkgs/tools/text/chroma/default.nix +++ b/pkgs/tools/text/chroma/default.nix @@ -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"; diff --git a/pkgs/tools/text/chroma/src.json b/pkgs/tools/text/chroma/src.json new file mode 100644 index 000000000000..9bfc8d9c2c73 --- /dev/null +++ b/pkgs/tools/text/chroma/src.json @@ -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 +}