Files
nixpkgs/pkgs/by-name/go/go-containerregistry/package.nix
Peder Bergebakken Sundt 8bffdd4ccf treewide: gate command execution for installShellCompletion behind buildPlatform.canExecute hostPlatform
This treewide conditions execution of the built applications for the purpose of generating shell completions behind `stdenv.buildPlatform.canExecute stdenv.hostPlatform`, which helps cross. This is a common issue I spot during review, let's prevent copy-paste errors by fixing it treewide.

Candidates were located with:

```shell
rg 'installShellCompletion' -l -tnix | xargs grep -F 'stdenv.buildPlatform.canExecute stdenv.hostPlatform' -L
```

Then I migrated the obvious cases which would not require a rebuild, as a means of testing.
This diff was not scripted. Should be zero rebuilds.

<details>
<summary>
Alternatives
</summary>

Alternatively I could have use this pattern:

```nix
postInstall = lib.optionalString (stdenv.hostPlatform.emulatorAvailable buildPackages) (
let
    emulator = stdenv.hostPlatform.emulator buildPackages;
in
''
    installShellCompletion --cmd foobar \
    --bash <(${emulator} $out/bin/foobar completion bash) \
    --fish <(${emulator} $out/bin/foobar completion fish) \
    --zsh <(${emulator} $out/bin/foobar completion zsh)
''
);
```

but that would cause rebuilds and will also require testing.

---

An other alternative is to use the binary from the corresponding package in `buildPackages`.
This however would also cause rebuilds and will also require testing.

</details>
2025-09-12 14:08:39 +02:00

79 lines
1.7 KiB
Nix

{
lib,
stdenv,
buildGoModule,
fetchFromGitHub,
installShellFiles,
}:
let
bins = [
"crane"
"gcrane"
];
in
buildGoModule rec {
pname = "go-containerregistry";
version = "0.20.6";
src = fetchFromGitHub {
owner = "google";
repo = "go-containerregistry";
rev = "v${version}";
sha256 = "sha256-fmn2SPmYecyKY7HMPjPKvovRS/Ez+SwDe+1maccq4Hc=";
};
vendorHash = null;
nativeBuildInputs = [ installShellFiles ];
subPackages = [
"cmd/crane"
"cmd/gcrane"
];
outputs = [ "out" ] ++ bins;
ldflags =
let
t = "github.com/google/go-containerregistry";
in
[
"-s"
"-w"
"-X ${t}/cmd/crane/cmd.Version=v${version}"
"-X ${t}/pkg/v1/remote/transport.Version=${version}"
];
postInstall =
lib.concatStringsSep "\n" (
map (bin: ''
mkdir -p ''$${bin}/bin &&
mv $out/bin/${bin} ''$${bin}/bin/ &&
ln -s ''$${bin}/bin/${bin} $out/bin/
'') bins
)
+ lib.optionalString (stdenv.buildPlatform.canExecute stdenv.hostPlatform) ''
for cmd in crane gcrane; do
installShellCompletion --cmd "$cmd" \
--bash <($GOPATH/bin/$cmd completion bash) \
--fish <($GOPATH/bin/$cmd completion fish) \
--zsh <($GOPATH/bin/$cmd completion zsh)
done
'';
# NOTE: no tests
doCheck = false;
meta = with lib; {
description = "Tools for interacting with remote images and registries including crane and gcrane";
homepage = "https://github.com/google/go-containerregistry";
license = licenses.asl20;
mainProgram = "crane";
maintainers = with maintainers; [
yurrriq
ryan4yin
];
};
}