Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/hooks/versionCheckHook.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The variables that this phase control are:
- `dontVersionCheck`: Disable adding this hook to the [`preInstallCheckHooks`](#ssec-installCheck-phase). Useful if you do want to load the bash functions of the hook, but run them differently.
- `versionCheckProgram`: The full path to the program that should print the `${version}` string. Defaults to using the first non-empty value `$binary` out of `${NIX_MAIN_PROGRAM}` and `${pname}`, in that order, to build roughly `${placeholder "out"}/bin/$binary`. `${NIX_MAIN_PROGRAM}`'s value comes from `meta.mainProgram`, and does not normally need to be set explicitly. When setting `versionCheckProgram`, using `$out` directly won't work, as environment variables from this variable are not expanded by the hook. Hence using `placeholder "out"` is unavoidable.
- `versionCheckProgramArg`: The argument that needs to be passed to `versionCheckProgram`. If undefined the hook tries first `--help` and then `--version`. Examples: `version`, `-V`, `-v`.
- `versionCheckScript`: A bash script to execute instead of `versionCheckProgram versionCheckProgramArg`.
- `versionCheckKeepEnvironment`: A list of environment variables to keep and pass to the command. Only those variables should be added to this list that are actually required for the version command to work. If it is not feasible to explicitly list all these environment variables you can set this parameter to the special value `"*"` to disable the `--ignore-environment` flag and thus keep all environment variables.
- `preVersionCheck`: A hook to run before the check is done.
- `postVersionCheck`: A hook to run after the check is done.
Expand Down
11 changes: 4 additions & 7 deletions pkgs/by-name/ci/cilium-cli/package.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
lib,
buildGoModule,
cilium-cli,
fetchFromGitHub,
installShellFiles,
testers,
versionCheckHook,
}:

buildGoModule rec {
Expand Down Expand Up @@ -41,11 +40,9 @@ buildGoModule rec {
--zsh <($out/bin/cilium completion zsh)
'';

passthru.tests.version = testers.testVersion {
package = cilium-cli;
command = "cilium version --client";
version = "${version}";
};
nativeInstallCheckInputs = [ versionCheckHook ];
versionCheckScript = "${placeholder "out"}/bin/cilium version --client";
doInstallCheck = true;

meta = {
description = "CLI to install, manage & troubleshoot Kubernetes clusters running Cilium";
Expand Down
23 changes: 10 additions & 13 deletions pkgs/by-name/sr/src-cli/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
lib,
buildGoModule,
fetchFromGitHub,
stdenv,
xorg,
testers,
src-cli,
versionCheckHook,
writableTmpDirAsHomeHook,
}:

buildGoModule rec {
Expand All @@ -21,22 +19,21 @@ buildGoModule rec {

vendorHash = "sha256-bpfDnVqJoJi9WhlA6TDWAhBRkbbQn1BHfnLJ8BTmhGM=";

subPackages = [
"cmd/src"
];
subPackages = [ "cmd/src" ];

ldflags = [
"-s"
"-w"
"-X=github.com/sourcegraph/src-cli/internal/version.BuildTag=${version}"
];

passthru.tests = {
version = testers.testVersion {
package = src-cli;
command = "src version -client-only";
};
};
nativeInstallCheckInputs = [
versionCheckHook
writableTmpDirAsHomeHook
];
versionCheckScript = "${placeholder "out"}/bin/src version -client-only";
versionCheckKeepEnvironment = [ "HOME" ];
doInstallCheck = true;

meta = with lib; {
description = "Sourcegraph CLI";
Expand Down
76 changes: 42 additions & 34 deletions pkgs/by-name/ve/versionCheckHook/hook.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
_handleCmdOutput(){
local command=("$1" "$2")
local versionOutput

local envArgs=()
if [[ "$3" != "*" ]]; then
if [[ "$1" != "*" ]]; then
envArgs+=("--ignore-environment")
for var in $3; do
for var in $1; do
envArgs+=("$var=${!var}")
done
fi

shift 1
local command=("$@")

versionOutput="$(env \
--chdir=/ \
--argv0="$(basename "${command[0]}")" \
Expand Down Expand Up @@ -37,39 +39,45 @@ versionCheckHook(){
# Don't keep any environment variables by default
: "${versionCheckKeepEnvironment:=}"

local cmdProgram cmdArg echoPrefix
if [[ ! -z "${versionCheckProgram-}" ]]; then
cmdProgram="$versionCheckProgram"
elif [[ ! -z "${NIX_MAIN_PROGRAM-}" ]]; then
cmdProgram="${!outputBin}/bin/${NIX_MAIN_PROGRAM}"
elif [[ ! -z "${pname-}" ]]; then
echo "versionCheckHook: Package \`${pname}\` does not have the \`meta.mainProgram\` attribute." \
"We'll assume that the main program has the same name for now, but this behavior is deprecated," \
"because it leads to surprising errors when the assumption does not hold." \
"If the package has a main program, please set \`meta.mainProgram\` in its definition to make this warning go away." \
"Should the binary that outputs the intended version differ from \`meta.mainProgram\`, consider setting \`versionCheckProgram\` instead." >&2
cmdProgram="${!outputBin}/bin/${pname}"
else
echo "versionCheckHook: \$NIX_MAIN_PROGRAM, \$versionCheckProgram and \$pname are all empty, so" \
"we don't know how to run the versionCheckPhase." \
"To fix this, set one of \`meta.mainProgram\` or \`versionCheckProgram\`." >&2
exit 2
fi
local echoPrefix

if [[ ! -x "$cmdProgram" ]]; then
echo "versionCheckHook: $cmdProgram was not found, or is not an executable" >&2
exit 2
fi
if [[ -z "${versionCheckProgramArg}" ]]; then
for cmdArg in "--help" "--version"; do
echoPrefix="$(_handleCmdOutput "$cmdProgram" "$cmdArg" "$versionCheckKeepEnvironment")"
if [[ "$echoPrefix" == "Successfully managed to" ]]; then
break
fi
done
if [[ -n "${versionCheckScript-}" ]]; then
echoPrefix="$(_handleCmdOutput "$versionCheckKeepEnvironment" "@bash@" "-c" "$versionCheckScript")"
else
cmdArg="$versionCheckProgramArg"
echoPrefix="$(_handleCmdOutput "$cmdProgram" "$cmdArg" "$versionCheckKeepEnvironment")"
local cmdProgram cmdArg
if [[ ! -z "${versionCheckProgram-}" ]]; then
cmdProgram="$versionCheckProgram"
elif [[ ! -z "${NIX_MAIN_PROGRAM-}" ]]; then
cmdProgram="${!outputBin}/bin/${NIX_MAIN_PROGRAM}"
elif [[ ! -z "${pname-}" ]]; then
echo "versionCheckHook: Package \`${pname}\` does not have the \`meta.mainProgram\` attribute." \
"We'll assume that the main program has the same name for now, but this behavior is deprecated," \
"because it leads to surprising errors when the assumption does not hold." \
"If the package has a main program, please set \`meta.mainProgram\` in its definition to make this warning go away." \
"Should the binary that outputs the intended version differ from \`meta.mainProgram\`, consider setting \`versionCheckProgram\` instead." >&2
cmdProgram="${!outputBin}/bin/${pname}"
else
echo "versionCheckHook: \$NIX_MAIN_PROGRAM, \$versionCheckProgram and \$pname are all empty, so" \
"we don't know how to run the versionCheckPhase." \
"To fix this, set one of \`meta.mainProgram\` or \`versionCheckProgram\`." >&2
exit 2
fi

if [[ ! -x "$cmdProgram" ]]; then
echo "versionCheckHook: $cmdProgram was not found, or is not an executable" >&2
exit 2
fi
if [[ -z "${versionCheckProgramArg}" ]]; then
for cmdArg in "--help" "--version"; do
echoPrefix="$(_handleCmdOutput "$versionCheckKeepEnvironment" "$cmdProgram" "$cmdArg")"
if [[ "$echoPrefix" == "Successfully managed to" ]]; then
break
fi
done
else
cmdArg="$versionCheckProgramArg"
echoPrefix="$(_handleCmdOutput "$versionCheckKeepEnvironment" "$cmdProgram" "$cmdArg")"
fi
fi
if [[ "$echoPrefix" == "Did not" ]]; then
exit 2
Expand Down
2 changes: 2 additions & 0 deletions pkgs/by-name/ve/versionCheckHook/package.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
lib,
makeSetupHook,
bash,
}:

makeSetupHook {
name = "version-check-hook";
substitutions = {
storeDir = builtins.storeDir;
bash = lib.getExe bash;
};
meta = {
description = "Lookup for $version in the output of --help and --version";
Expand Down
Loading