boxxy puts bad Linux applications in a box with only their files.
boxxy
boxxy(区分大小写)是一款用于隔离行为不当的 Linux 应用程序的工具,它能强制这些应用程序将其文件和目录放置在正确的位置,无需使用符号链接!
boxxy 是 amyware discord server 的一部分。
如果您喜欢我的作品,欢迎通过 Patreon 支持我:
仅限 Linux 系统!boxxy 利用 Linux 命名空间实现其功能。
例如,以 AWS CLI 为例。它希望将其配置和凭据存储在 ~/.aws/ 目录下,但通过 boxxy,您可以将其重定向到 ~/.config/aws/ 目录:
# ~/.config/boxxy/boxxy.yaml
rules:
- name: "Store AWS CLI config in ~/.config/aws"
target: "~/.aws"
rewrite: "~/.config/aws"
boxxy 让您能够控制应用程序读取和写入文件的位置——无论是为了 XDG 合规性、项目特定配置,还是您需要的任何其他路径重定向。虽然随着时间的推移,一些应用程序(如 tmux 3.1+)已添加了原生 XDG 支持,但许多其他应用程序尚未支持,而且 boxxy 还支持更高级的使用场景,例如应用程序原生永远不会支持的依赖于上下文的配置。
维护状态
由于健康原因,我目前暂停维护开源项目。仍会接受 PR,也会查看问题,但无法保证何时会处理。
开发动机
我最近需要使用 AWS CLI。它希望将数据保存在 ~/.aws 中,但我不希望它就这样随意弄乱我的 $HOME。boxxy 让我可以强制它将数据存放在合适且规范的位置。
功能特性
- 为任何程序提供隔离环境,强制其将文件/目录存放在您指定的位置
- 依赖于上下文的隔离,即根据您的配置,不同目录应用不同规则
- 开销极小
- 规则重写之外的文件系统默认为只读,即只有您在规则中指定的文件/目录是可写的
0.5.0版本:boxxy 可以扫描您的主目录,自动为您推荐规则!
0.6.0版本:boxxy 可以使用项目本地的boxxy.yaml文件,并且可以为您加载.env文件!
0.6.1版本:boxxy 规则可以注入环境变量:
0.7.2版本:boxxy 可以使用--daemon标志将被隔离的进程 fork 到后台运行。0.8.0版本:boxxy 可以通过命令行--rule传递规则,并使用--no-config禁用加载配置文件。0.8.2版本:说明如何正确运行 AppImages:
潜在缺点
- 新项目,版本号为 0.x.y,存在相关风险提示
- 无法在容器内使用 sudo(详见 #6)
- 主要针对我的使用场景进行了测试
示例用法
git:(mistress) | ▶ cat ~/.config/boxxy/boxxy.yaml
rules:
- name: "Store AWS CLI config in ~/.config/aws"
target: "~/.aws"
rewrite: "~/.config/aws"
git:(mistress) | ▶ boxxy aws configure
INFO boxxy > loaded 1 rules
INFO boxxy::enclosure > applying rule 'Store AWS CLI config in ~/.config/aws'
INFO boxxy::enclosure > redirect: ~/.aws -> ~/.config/aws
INFO boxxy::enclosure > boxed "aws" ♥
AWS Access Key ID [****************d]: a
AWS Secret Access Key [****************c]: b
Default region name [b]: c
Default output format [a]: d
git:(mistress) | ▶ ls ~/.aws
git:(mistress) | ▶ ls ~/.config/aws
config credentials
git:(mistress) | ▶ cat ~/.config/aws/config
[default]
region = c
output = d
git:(mistress) | ▶
建议用法
alias aws="boxxy aws"(其他工具可重复此操作)- 使用上下文在磁盘上分开保存项目配置
- 点文件!
- 停止使用符号链接!!!
- 编写代码时不再有开发配置文件
要求
boxxy 需要 newuidmap 才能运行,并非所有发行版都默认包含此工具。安装方法:
Alpine:
$ apk add shadow-uidmap
Debian / Ubuntu:
$ apt install uidmap
红帽企业版 Linux / Fedora:
$ yum install shadow-utils
配置
boxxy 配置文件位于 ~/.config/boxxy/boxxy.yaml。如果该文件不存在,系统将为您创建一个空文件。
rules:
# The name of the rule. User-friendly name for your reference
- name: "redirect aws-cli from ~/.aws to ~/.config/aws"
# The target of the rule, ie the file/directory that will be shadowed by the
# rewrite.
target: "~/.aws"
# The rewrite of the rule, ie the file/directory that will be used instead of
# the target.
rewrite: "~/.config/aws"
- name: "use different k8s configs when in ~/Projects/my-cool-startup"
target: "~/.kube/config"
rewrite: "~/Projects/my-cool-startup/.kube/config"
# The context for the rule. Any paths listed in the context are paths where
# this rule will apply. If no context is specified, the rule applies
# globally.
context:
- "~/Projects/my-cool-startup"
# The mode of this rule, either `directory` or `file`. `directory` is the
# default. Must be specified for the correct behaviour when the target is a
# file. Required because the target file/directory may not exist yet.
mode: "file"
# The list of commands that this rule applies to. If no commands are
# specified, the rule applies to all programs run with boxxy.
only:
- "kubectl"
语法
rules:
- name: "any valid string" # required
target: "path" # required
rewrite: "path" # required
context: # optional
- "path"
- "path"
mode: "directory | file" # optional
only: # optional
- "binary name"
- "binary name"
env: # optional
KEY: "value"
开发
- 设置 pre-commit:
pre-commit install - 确保能够构建:
cargo build - 开始开发!
- 使用你选择的命令进行测试,例如:
cargo run -- ls -lah ~/.config
工作原理
- 在 /tmp 中创建临时目录
- 设置新的用户/挂载命名空间
- 将
/绑定挂载到临时目录 - 绑定挂载规则以读写模式挂载,以便目标程序能够使用它们
- 将
/重新挂载为只读 - 运行程序!
鸣谢
fixtures/helloworld-appimage-x86_84.AppImage:https://github.com/ClonedRepos/hello-world-appimage