1{
2 lib,
3 buildGoModule,
4 stdenv,
5 fetchFromGitHub,
6 writableTmpDirAsHomeHook,
7}:
8
9buildGoModule rec {
10 pname = "pdfcpu";
11 version = "0.11.0";
12
13 src = fetchFromGitHub {
14 owner = "pdfcpu";
15 repo = "pdfcpu";
16 rev = "v${version}";
17 hash = "sha256-HTqaFl/ug/4sdchZBD4VQiXbD1L0/DVf2efZ3BV/vx4=";
18 # Apparently upstream requires that the compiled executable will know the
19 # commit hash and the date of the commit. This information is also presented
20 # in the output of `pdfcpu version` which we use as a sanity check in the
21 # installCheckPhase. This was discussed upstream in:
22 #
23 # - https://github.com/pdfcpu/pdfcpu/issues/751
24 # - https://github.com/pdfcpu/pdfcpu/pull/752
25 #
26 # The trick used here is to write that information into files in `src`'s
27 # `$out`, and then read them into the `ldflags`. We also delete the `.git`
28 # directories in `src`'s $out afterwards, imitating what's done if
29 # `leaveDotGit = false;` See also:
30 # https://github.com/NixOS/nixpkgs/issues/8567
31 leaveDotGit = true;
32 postFetch = ''
33 cd "$out"
34 git rev-parse HEAD > $out/COMMIT
35 git log -1 --pretty=%cd --date=format:'%Y-%m-%dT%H:%M:%SZ' > $out/SOURCE_DATE
36 find "$out" -name .git -print0 | xargs -0 rm -rf
37 '';
38 };
39
40 vendorHash = "sha256-5qB3zXiee4yMFpV8Ia8jICZaw+8Zpxd2Fs7DZ/DW/Jg=";
41
42 ldflags = [
43 "-s"
44 "-w"
45 "-X main.version=v${version}"
46 ];
47
48 # ldflags based on metadata from git and source
49 preBuild = ''
50 ldflags+=" -X main.commit=$(cat COMMIT)"
51 ldflags+=" -X main.date=$(cat SOURCE_DATE)"
52 '';
53
54 # No tests
55 doCheck = false;
56 doInstallCheck = true;
57 installCheckInputs = [
58 writableTmpDirAsHomeHook
59 ];
60 # NOTE: Can't use `versionCheckHook` since a writeable $HOME is required and
61 # `versionCheckHook` uses --ignore-environment
62 installCheckPhase = ''
63 echo checking the version print of pdfcpu
64 mkdir -p $HOME/"${
65 if stdenv.hostPlatform.isDarwin then "Library/Application Support" else ".config"
66 }"/pdfcpu
67 versionOutput="$($out/bin/pdfcpu version)"
68 for part in ${version} $(cat COMMIT | cut -c1-8) $(cat SOURCE_DATE); do
69 if [[ ! "$versionOutput" =~ "$part" ]]; then
70 echo version output did not contain expected part $part . Output was:
71 echo "$versionOutput"
72 exit 3
73 fi
74 done
75 '';
76
77 subPackages = [ "cmd/pdfcpu" ];
78
79 meta = with lib; {
80 description = "PDF processor written in Go";
81 homepage = "https://pdfcpu.io";
82 license = licenses.asl20;
83 maintainers = with maintainers; [ doronbehar ];
84 mainProgram = "pdfcpu";
85 };
86}