Files

102 lines
2.9 KiB
Nix

{
lib,
buildGoModule,
stdenv,
fetchFromGitHub,
writableTmpDirAsHomeHook,
installShellFiles,
}:
buildGoModule (finalAttrs: {
pname = "pdfcpu";
version = "0.13.0";
src = fetchFromGitHub {
owner = "pdfcpu";
repo = "pdfcpu";
tag = "v${finalAttrs.version}";
hash = "sha256-o+gg/XdsPotmuk+H62Bzu4zG9Zu2HABlr4S/YhbtCiI=";
# Apparently upstream requires that the compiled executable will know the
# commit hash and the date of the commit. This information is also presented
# in the output of `pdfcpu version` which we use as a sanity check in the
# installCheckPhase. This was discussed upstream in:
#
# - https://github.com/pdfcpu/pdfcpu/issues/751
# - https://github.com/pdfcpu/pdfcpu/pull/752
#
# The trick used here is to write that information into files in `src`'s
# `$out`, and then read them into the `ldflags`. We also delete the `.git`
# directories in `src`'s $out afterwards, imitating what's done if
# `leaveDotGit = false;` See also:
# https://github.com/NixOS/nixpkgs/issues/8567
leaveDotGit = true;
postFetch = ''
cd "$out"
git rev-parse HEAD > $out/COMMIT
git log -1 --pretty=%cd --date=format:'%Y-%m-%d %H:%M:%S UTC' > $out/SOURCE_DATE
find "$out" -name .git -print0 | xargs -0 rm -rf
'';
};
vendorHash = "sha256-yieD29GFQQrYVbYNwFHDQX9l0KOKu0usng1OPoaVBZ8=";
ldflags = [
"-s"
"-w"
"-X main.version=v${finalAttrs.version}"
];
# ldflags based on metadata from git and source
preBuild = ''
ldflags+=(
"-X 'main.commit=$(cat COMMIT)'"
"-X 'main.date=$(cat SOURCE_DATE)'"
)
'';
nativeBuildInputs = [
installShellFiles
];
postInstall = ''
installShellCompletion --cmd pdfcpu \
--zsh <($out/bin/pdfcpu completion zsh) \
--fish <($out/bin/pdfcpu completion fish) \
--bash <($out/bin/pdfcpu completion bash)
'';
# No tests
doCheck = false;
doInstallCheck = true;
installCheckInputs = [
writableTmpDirAsHomeHook
];
# NOTE: Can't use `versionCheckHook` since a writeable $HOME is required and
# `versionCheckHook` uses --ignore-environment
installCheckPhase = ''
echo checking the version print of pdfcpu
mkdir -p $HOME/"${
if stdenv.hostPlatform.isDarwin then "Library/Application Support" else ".config"
}"/pdfcpu
versionOutput="$($out/bin/pdfcpu version)"
for part in ${finalAttrs.version} "$(cut -c1-8 COMMIT)" "$(cat SOURCE_DATE)"; do
if [[ ! "$versionOutput" =~ "$part" ]]; then
echo version output did not contain expected part \"$part\" . Output was:
echo "$versionOutput"
exit 3
fi
done
'';
subPackages = [ "cmd/pdfcpu" ];
meta = {
description = "PDF processor written in Go";
changelog = "https://pdfcpu.io/changelog/";
homepage = "https://pdfcpu.io";
license = lib.licenses.asl20;
maintainers = with lib.maintainers; [ doronbehar ];
mainProgram = "pdfcpu";
};
})