Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the name of the current toolbox container #744

Closed
pierreprinetti opened this issue Mar 31, 2021 · 5 comments
Closed

Expose the name of the current toolbox container #744

pierreprinetti opened this issue Mar 31, 2021 · 5 comments
Labels
1. Feature request A request for a new feature

Comments

@pierreprinetti
Copy link

Is your feature request related to a problem? Please describe.
I want to display the name of the toolbox I'm currently in in my prompt, or in my tmux bar, without relying on the container engine underlying Toolbox.

Describe the solution you'd like
While there is an open issue specifically targeting the stock Bash $PS1, I'd fancy a backend-agnostic toolbox command that just outputs the current toolbox name. The reason for this is: I use a customised command prompt and I use ZSH.

Here's a proposal:

$ toolbox show
fedora-toolbox-33

Basically, toolbox show outputs the value of /run/.containerenv's name.

The command exits with 1 if the current shell is not running in a toolbox.

Describe alternatives you've considered

  • toolbox list --current
  • toolbox show <toolbox> outputs some interesting info on the container in a machine-readable format, and defaults to the current toolbox (errors if not in a toolbox, and <toolbox> is not passed)
  • I can continue parsing /run/.containerenv.

Additional context
Depends on containers/podman#6192

@pierreprinetti pierreprinetti added the 1. Feature request A request for a new feature label Mar 31, 2021
@erindru
Copy link

erindru commented Apr 19, 2021

In case anyone else stumbles across this looking for a copy+paste solution before this becomes a proper feature in Toolbox, here is what I added to my .bashrc to parse the container name out of /run/.containerenv:

if [ -f "/run/.containerenv" ]; then
	TOOLBOX_NAME=[$(cat /run/.containerenv | grep 'name=' | sed -e 's/^name="\(.*\)"$/\1/')]
else
	TOOLBOX_NAME=""
fi

It can then be used within the $PS1 to produce something like:

Screenshot from 2021-04-20 08-56-39

@ma3yta
Copy link

ma3yta commented Sep 16, 2021

My starship config can be used with any shell to get container name.

format = """${custom.toolbox}$directory$git_branch$git_metrics$git_status$dotnet$character"""

[custom.toolbox]
command = """ grep -Po 'name="\\K[^"]+' /run/.containerenv """
files = [ "/run/.containerenv" ]
when = "test -f /run/.containerenv"
style = "bg:bright-blue black"
format = "[  $output ]($style)"

Screenshot from 2021-09-16 15-42-14
starship.txt

@pierreprinetti
Copy link
Author

I ended up putting this in ~/.local/bin/get_toolbox:

#!/usr/bin/env bash

set -Eeuo pipefail

declare -r containerenvpath='/run/.containerenv'

if [ -r $containerenvpath ]; then
        source $containerenvpath
        printf '%s' "$name"
else
        exit 1
fi

@debarshiray
Copy link
Member

Oops! I have no idea how I managed to miss this issue for so long.

I can continue parsing /run/.containerenv.

Yes, please do continue to parse /run/.containerenv.

In containers/podman#6192 I had requested for the metadata that shows up in /run/.containerenv specifically for such use-cases, and I consider the contents of that file as part of our stable interface. So, please do file issues against Podman if that interface breaks, and CC me, and we will work out a fix.

The format of /run/.containerenv matches that of os-release(5) and is similar to Flatpak's /.flatpak-info. So it's as good an interface as any.

You can also get the same metadata through podman inspect ..., but in a different format.

I don't want to add yet another way to access that metadata to Toolbx, because it will be a duplicated interface that we will have to maintain, write tests that get executed on every CI run, etc.. Instead let's just rely on the existing mechanisms.

That said, if the existing mechanisms don't work for some use-case, then please do file a new issue with details.

@debarshiray
Copy link
Member

Closing.

Thanks for playing with Toolbx and sharing those interesting snippets. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1. Feature request A request for a new feature
Projects
None yet
Development

No branches or pull requests

4 participants