boxxy:Linux应用文件路径重定向工具,无需符号链接即可控制文件存储位置

boxxy puts bad Linux applications in a box with only their files.

Branch11Tags40
This repository is empty

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 可以扫描您的主目录,自动为您推荐规则!boxxy 扫描图像
  • 0.6.0 版本:boxxy 可以使用项目本地的 boxxy.yaml 文件,并且可以为您加载 .env 文件!0.6.0 版本功能图像
  • 0.6.1 版本:boxxy 规则可以注入环境变量:0.6.1 版本功能图像
  • 0.7.2 版本:boxxy 可以使用 --daemon 标志将被隔离的进程 fork 到后台运行。
  • 0.8.0 版本:boxxy 可以通过命令行 --rule 传递规则,并使用 --no-config 禁用加载配置文件。
  • 0.8.2 版本:说明如何正确运行 AppImages:0.8.2 版本功能图像

潜在缺点

  • 新项目,版本号为 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"

开发

  1. 设置 pre-commit:pre-commit install
  2. 确保能够构建:cargo build
  3. 开始开发!
  4. 使用你选择的命令进行测试,例如:cargo run -- ls -lah ~/.config

工作原理

  • 在 /tmp 中创建临时目录
  • 设置新的用户/挂载命名空间
  • / 绑定挂载到临时目录
  • 绑定挂载规则以读写模式挂载,以便目标程序能够使用它们
  • / 重新挂载为只读
  • 运行程序!

鸣谢

Introduction

Boxxy 将不良的Linux应用程序置于一个仅包含其文件的环境中。【此简介由AI生成】

Customize your domain
71.75 K32Visit GitHub