catthehacker / docker_images

Docker images
MIT License
212 stars 75 forks source link

tools installed with pipx don't work: no $HOME/.local/bin in PATH #128

Open satwell opened 2 months ago

satwell commented 2 months ago

When I try to install Python tools with pipx, those tools won't run because they get installed into $HOME/.local/bin, which isn't in PATH. This fails in act runner, but works fine in GitHub Actions.

Example workflow:

on: push
jobs:
  env:
    runs-on: ubuntu-latest
    steps:
      - run: pipx install hatch
      - run: hatch --version

Running this under act runner, pipx complains:

| ⚠️  Note: '/root/.local/bin' is not on
|     your PATH environment variable.
|     These apps will not be globally
|     accessible until your PATH is
|     updated. Run `pipx ensurepath` to
|     automatically add it, or manually
|     modify your PATH in your shell's
|     config file (i.e. ~/.bashrc).

GitHub's image puts $HOME/.local/bin into $PATH. Here's the full path I get by running env on GitHub:

PATH=/snap/bin:/home/runner/.local/bin:/opt/pipx_bin:/home/runner/.cargo/bin:/home/runner/.config/composer/vendor/bin:/usr/local/.ghcup/bin:/home/runner/.dotnet/tools:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Note that they also have custom pipx configuration that makes it put installed executables into /opt/pipx_bin, but this isn't strictly needed, at least for basic functionality.

I can add a step that runs echo "$HOME/.local/bin" >> "$GITHUB_PATH" as a workaround, as explained in the GitHub Action Docs. This works, but it's an extra step that's not usually required on GitHub to use pipx. Since $HOME/.local/bin is an XDG recommendation and is used by more than just pipx, I suspect adding this will fix some other incompatibilities with GitHub Actions as well.

satwell commented 2 months ago

It's easy to add /root/.local/bin to PATH in /etc/environment. But it's not clear to me whether non-root users need to be supported?

Since act runner execs bash with --noprofile --norc, the only option I can see for expanding $HOME while setting PATH is to use BASH_ENV. Basically, create a file somewhere that contains export PATH="$HOME/.local/bin:$PATH" and set BASH_ENV to the location of that file. Then bash will source that script when run non-interactively by act runner.

Which is the better option here? The simple root-only solution, or the more complex but work-for-all-users solution? Happy to contribute a PR for either.