提示和技巧

使用 Toolbx

检查你是否位于一个 Toolbx 容器中

如果你经常使用 Toolbx 执行各种任务并使用多个 Toolbx 容器,有时可能会搞不清楚自己是在主机上还是在 Toolbx 容器中执行命令。毕竟目前没有任何命令告诉你正在哪个 Toolbx 容器中工作。

为了缓解这种情况,你可以在 ~/.bashrc 末尾添加下面的 shell 别名:

alias istoolbx='[ -f "/run/.toolboxenv" ] && grep -oP "(?<=name=\")[^\";]+" /run/.containerenv'

当你打开一个新的 Shell 的时候,你就能使用一个新的命令 istoolbx

  • 当你在主机执行这个命令,它会以返回值 1 退出

  • 当你在 Toolbx 容器执行这个命令,它会以返回值 0 退出同时打印当前 Toolbx 的容器名称

当然你也可以在 ~/.bashrc 添加下面的内容,这样子你的 Bash 提示符中就能直接包含 "[toolbox <name>]" 了:

function is_toolbox() {
    if [ -f "/run/.toolboxenv" ]
    then
        TOOLBOX_NAME=$(cat /run/.containerenv | grep -oP "(?<=name=\")[^\";]+")
        echo "[${HOSTNAME} ${TOOLBOX_NAME}]"
    fi
}

现在你就可以将 is_toolbox 包含在 PS1 变量中而无需再手动执行别的命令来确认你是否在 toolbox 中了。

例如:` export PS1="\[\e[31m\]`is_toolbox\`\]\e[m\]\[\e[32m\]\\$ \[\e[m\]\[\e[37m\]❱\[\e[m\] " `

如果你不在 toolbox 中,它就会显示为 $ ❱

如果你在一个名为 "default" 的 toolbox 它则会显示为 [toolbox default]$ ❱

在 Host 上运行 Toolbx 里的应用程序

This can be necessary if you want to interact with tools available from the host, for example podman, nmcli or rpm-ostree without leaving the Toolbx container in between. You can use flatpak-spawn, included in the base installation for this:

$ flatpak-spawn --host podman --help

If the application you want to call requires sudo access, the -S option must be supplied to sudo like below:

$ flatpak-spawn --host sudo -S rpm-ostree status

If you find yourself using commands like these frequently to access e.g. the flatpak command from inside the Toolbx container, you can create yourself a short custom wrapper script (inside the Toolbx container). To do this, perform the following steps:

  1. Define the istoolbx alias (for convenience) by executing the command mentioned above in your terminal

  2. Make sure you are in a Toolbx container. If the following command doesn’t produce any output, you are likely still working on the host!

    [toolbx]$ istoolbx
    <Toolbx container name here>
  3. Once you have made sure you’re in a Toolbx container, execute the following command:

    [toolbx]$ echo -e '#!/bin/sh\nexec /usr/bin/flatpak-spawn --host flatpak "$@"' | sudo tee /usr/local/bin/flatpak 1>/dev/null && sudo chmod +x /usr/local/bin/flatpak

You now have a flatpak command available that allows you to interact with flatpak as if you were running the command on the host.

Working with ostree/rpm-ostree

Tracking changes to the base OS

Some directories in ostree-based operating systems are writable by the user, like /etc. You can get a quick overview of the files changed under /etc using the following command:

$ sudo ostree admin config-diff

To get a more elaborate diff, you can use something like this:

$ sudo diff -yrW200 --suppress-common-lines --color=always /usr/etc /etc 2>/dev/null
This works because ostree keeps an unmodified copy of the /etc directory under /usr/etc. All of your changes go to /etc directly.

Working with Flatpak applications

Directly accessing Flatpak applications from the CLI

The most noticable change when using Flatpak applications instead of conventional installations is that the applications cannot be directly called from the CLI any more, like so:

$ evince
bash: command not found: evince

Instead, one can call them like this:

$ flatpak run org.gnome.Evince

In addition, most Flatpak applications export their internal binaries under an installation-dependent location:

  • For Flatpak applications installed from system remotes, these can be found under /var/lib/flatpak/exports/bin/

  • For Flatpak applications installed from user remotes, these can be found under $HOME/.local/share/flatpak/exports/bin/

If you’re unsure to which installation a Flatpak application belongs, you can use this command to print it out:

$ flatpak list --app --columns=name,installation

You can then either add these directories to your $PATH:

$ org.gnome.Evince

or setup shell `alias`es as needed to make them available to the CLI like so:

$ alias evince="flatpak run org.gnome.Evince"
  # or alias evince="org.gnome.Evince"
$ evince