1{
2 lib,
3 stdenv,
4 buildGoModule,
5 fetchFromGitHub,
6 installShellFiles,
7 versionCheckHook,
8 writeScript,
9}:
10
11let
12 commitHash = "d5bc935e4801a02fdbd953f8f0ae7989eaef50cf"; # matches tag release
13 shortCommitHash = builtins.substring 0 7 commitHash;
14in
15buildGoModule (finalAttrs: {
16 pname = "copywrite";
17 version = "0.22.0";
18
19 src = fetchFromGitHub {
20 owner = "hashicorp";
21 repo = "copywrite";
22 tag = "v${finalAttrs.version}";
23 hash = "sha256-gPVlHgFlLxoAj4pkg3OxD4CGQaLdAL312/Zn/pJ+7fg=";
24 };
25
26 vendorHash = "sha256-Qxp6BwN/Y6Xb1BwFGT/T8WYsXGPgN27mzoTE0i6cS1Q=";
27
28 ldflags = [
29 "-s"
30 "-w"
31 "-X github.com/hashicorp/copywrite/cmd.version=${finalAttrs.version}"
32 "-X github.com/hashicorp/copywrite/cmd.commit=${shortCommitHash}"
33 ];
34
35 env.CGO_ENABLED = 0;
36
37 nativeBuildInputs = [ installShellFiles ];
38 postInstall = lib.optionalString (stdenv.buildPlatform.canExecute stdenv.hostPlatform) ''
39 $out/bin/copywrite completion bash > copywrite.bash
40 $out/bin/copywrite completion zsh > copywrite.zsh
41 $out/bin/copywrite completion fish > copywrite.fish
42 installShellCompletion copywrite.{bash,zsh,fish}
43 '';
44
45 nativeInstallCheckInputs = [ versionCheckHook ];
46 doInstallCheck = true;
47
48 passthru.updateScript = writeScript "update-copywrite" ''
49 #!/usr/bin/env nix-shell
50 #!nix-shell -i bash -p curl jq nix-update
51
52 set -eu -o pipefail
53
54 gh_metadata="$(curl -sS https://api.github.com/repos/hashicorp/copywrite/tags)"
55 version="$(echo "$gh_metadata" | jq -r '.[] | .name' | sort --version-sort | tail -1)"
56 commit_hash="$(echo "$gh_metadata" | jq -r --arg ver "$version" '.[] | select(.name == $ver).commit.sha')"
57
58 filename="$(nix-instantiate --eval -E "with import ./. {}; (builtins.unsafeGetAttrPos \"version\" copywrite).file" | tr -d '"')"
59 sed -i "s/commitHash = \"[^\"]*\"/commitHash = \"$commit_hash\"/" $filename
60
61 nix-update copywrite
62 '';
63
64 meta = {
65 description = "Automate copyright headers and license files at scale";
66 mainProgram = "copywrite";
67 homepage = "https://github.com/hashicorp/copywrite";
68 changelog = "https://github.com/hashicorp/copywrite/releases/tag/v${finalAttrs.version}";
69 license = lib.licenses.mpl20;
70 maintainers = with lib.maintainers; [ dvcorreia ];
71 };
72})