Skip to content

Conversation

@lrandersson
Copy link
Contributor

@lrandersson lrandersson commented Jan 21, 2026

Description

This PR adds an environment variable INSTALLER_PATH for installers that have post_install: ... defined.

Checklist - did you ...

  • Add a file to the news directory (using the template) for the next release's release notes?
  • Add / update necessary tests?
  • Add / update outdated documentation?

@lrandersson lrandersson requested a review from a team as a code owner January 21, 2026 19:10
@conda-bot conda-bot added the cla-signed [bot] added once the contributor has signed the CLA label Jan 21, 2026
@github-project-automation github-project-automation bot moved this to 🆕 New in 🔎 Review Jan 21, 2026
@lrandersson lrandersson changed the title [WIP] Add environment variable with installer file path Add environment variable with installer file path Jan 21, 2026
@@ -0,0 +1,19 @@
### Enhancements

* EXE: An environment variable `INSTALLER_PATH` is now defined for `post_install` scripts and set to the path of installer executable while the installer is running.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* EXE: An environment variable `INSTALLER_PATH` is now defined for `post_install` scripts and set to the path of installer executable while the installer is running.
* EXE: An environment variable `INSTALLER_PATH` is now defined for `post_install` scripts and set to the path of the installer executable while the installer is running.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@marcoesters marcoesters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth adding that variable to the SH and PKG installers, too, while we're at it.

Push $R2

{%- if post_install_exists %}
System::Call 'Kernel32::SetEnvironmentVariable(t, t) i("INSTALLER_PATH", "$EXEPATH")'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add this to where the other variables are defined in the Install section, and not make this a condition for post-install. It could also be used in pre-install scripts, maybe.

PLUGINSDIR may also be a good variable to add here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved it and added INSTALLER_PLUGINSDIR, see f8da863.
I also considered if we should have it as:

...
SetEnvironmentVariable INSTALLER_PLUGINSDIR...
{%- if pre_install_exists or post_install_exists %}
SetEnvironmentVariable INSTALLER_PATH...
{%- endif %}

Thoughts? It doesn't hurt having INSTALLER_PATH always defined, but I also wonder if it is unnecessary, is it always safe?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it be unsafe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't think of any reason, but I am bringing it up just for consideration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see an attack vector right now. The environment variable is not available outside the installation process. So, they can only be used through in scripts. Apart from the pre-install or post-install scripts of the installer, those are the pre-link or pre-unlink scripts of the packages.

However, even if someone were to place a malicious package into an installer, this doesn't give the attacker any more privileges that they'd already have. So, the limitation won't reduce the overall exposure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, good assessment!

Comment on lines 86 to 98
case "$0" in
*/*) SCRIPT_PATH="$0" ;;
*) SCRIPT_PATH="$(command -v -- "$0" 2>/dev/null)" ;;
esac

[ -z "$SCRIPT_PATH" ] && {
echo "ERROR: Cannot determine installer path"
exit 1
}
# Resolve the directory to an absolute path
SCRIPT_DIR=$(cd -- "$(dirname -- "$SCRIPT_PATH")" && pwd)
INSTALLER_PATH="$SCRIPT_DIR/$(basename -- "$SCRIPT_PATH")"
export INSTALLER_PATH
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For visibility, note that this is based on a proposed AI solution and is said to be robust for both linux/mac with a #!/bin/sh shebang. I simplified it a bit because I removed an assumption to follow symlinks, I don't think that is necessary in this case?

It also is a bit extra because it is trying to determine if the script was invoked using ., source or if the script was on $PATH.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script location is already a local variable, so you can just reuse that:

THIS_FILE=$(basename "$0")

Copy link
Contributor Author

@lrandersson lrandersson Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I noticed the entire path was already defined, I removed the earlier implementation and simply reuse the existing one: 8cf48d3


# PACKAGE_PATH is poorly documented, and might not exist across all OS versions
if [ -n "${PACKAGE_PATH:-}" ]; then
export INSTALLER_PATH="$PACKAGE_PATH" # The path to the .pkg installer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add a default value? What we are trying to achieve here doesn't seem to be well-supported by Apple for .pkg installers, however, I found this and it worked for me when I tested locally. However, it's a poorly documented variable and might not always work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, it's a poorly documented variable

That is, unfortunately, not unusual. This code may fail though with set -u. Here is how I solved it for another variable:

# The value for COMMAND_LINE_INSTALL is not documented,
# but it is unset for interactive installations. To be
# safe, set the variable for non-interactive installation
# in a roundabout way.
export INSTALLER_UNATTENDED="${COMMAND_LINE_INSTALL:-0}"
if [[ "${INSTALLER_UNATTENDED}" != "0" ]]; then
INSTALLER_UNATTENDED="1"
fi

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think a default variable makes sense here. If the location of the installer cannot be detected because the variable is not defined, then it shouldn't be set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the 0 is necessary, I used the :- syntax as well (if [ -n "${PACKAGE_PATH:-}" ]; then) but this situation evaluates to an empty string if unset.

Comment on lines 3 to 4
An environment variable `INSTALLER_PATH` is now defined for `post_install` scripts and set to the path of the installer executable while the installer is running.
* EXE: An environment variable `INSTALLER_PLUGINSDIR` is now also defined, it serves the same purpose as the NSIS variable `PLUGINSDIR`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
An environment variable `INSTALLER_PATH` is now defined for `post_install` scripts and set to the path of the installer executable while the installer is running.
* EXE: An environment variable `INSTALLER_PLUGINSDIR` is now also defined, it serves the same purpose as the NSIS variable `PLUGINSDIR`.
* An environment variable `INSTALLER_PATH` is now defined for pre-install and post-install scripts, and set to the path of the installer executable while the installer is running. (#1151)
* EXE: An environment variable `INSTALLER_PLUGINSDIR` is now also defined, it serves the same purpose as the NSIS variable `$PLUGINSDIR`. (#1151)

Please always add the PR number (and issue number, not applicable here) to the new items.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I keep forgetting that. Thanks for the reminder!

@github-project-automation github-project-automation bot moved this from 🆕 New to ✅ Approved in 🔎 Review Jan 29, 2026
@lrandersson lrandersson merged commit 8336f18 into conda:main Jan 29, 2026
18 of 20 checks passed
@github-project-automation github-project-automation bot moved this from ✅ Approved to 🏁 Done in 🔎 Review Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed [bot] added once the contributor has signed the CLA

Projects

Status: 🏁 Done

Development

Successfully merging this pull request may close these issues.

3 participants