about summary refs log tree commit diff
path: root/pkgs/tools/text/chroma
diff options
context:
space:
mode:
authorsternenseemann <sternenseemann@systemli.org>2021-10-17 13:31:01 +0200
committersterni <sternenseemann@systemli.org>2021-11-03 10:40:23 +0100
commit2174bfe6759d3054802ac3143dd9d9cfa732c2aa (patch)
treec83f3ba69a13800824a5d06db5f201184aa71266 /pkgs/tools/text/chroma
parent22a15615c797fca51e7468059d4ebbaa6e72af82 (diff)
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.
Diffstat (limited to 'pkgs/tools/text/chroma')
-rw-r--r--pkgs/tools/text/chroma/default.nix32
-rw-r--r--pkgs/tools/text/chroma/src.json11
2 files changed, 24 insertions, 19 deletions
diff --git a/pkgs/tools/text/chroma/default.nix b/pkgs/tools/text/chroma/default.nix
index 206a9e35e4d16..f99c090eb975d 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 0000000000000..9bfc8d9c2c73e
--- /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
+}