用 chezmoi 同步 DevContainer:打造專開發環境

16 分鐘閱讀

琳 avatar

生成一篇指南,內容是如何使用 chezmoi 來同步 devcontainer 環境。指南中要包含如此做的優點以及實際操作指令。另外提及 zsh 以 zinit 管理器安裝應用的說明,和 chezmoi 搭配使用可以在 devcontainer 達成什麼事。

Felo Search avatar
Felo Search

本指南旨在說明如何運用 chezmoi 工具來同步化 devcontainer 開發環境中的個人化配置,特別是針對 Zsh shell 環境以及如何透過 zinit 插件管理器來安裝及管理工具。

透過 chezmoi,開發者能夠確保在不同的 devcontainer 實例中擁有一致且客製化的 dotfiles(如 .zshrc, .gitconfig 等設定檔)。結合 zinit 管理 Zsh 插件,可以進一步提升終端機的效率與使用體驗,例如自動建議、語法高亮和自訂佈景主題。這種組合讓開發者在任何 devcontainer 中都能快速啟動一個熟悉且功能強大的開發環境,大幅提升工作流程的順暢度與生產力。

Dev Containers 與 Chezmoi 簡介

Dev Containers

Dev Containers(開發容器)提供了一個完整且隔離的開發環境,它將應用程式程式碼、執行時期、系統工具、函式庫和擴充功能打包在一個 Docker 容器內 1234。這種方法的主要優勢包括:

Chezmoi

chezmoi 是一個跨多種不同機器的 dotfiles(點檔案,通常指 Unix-like 系統中以 . 開頭的設定檔)管理工具 9101112。它的主要特色與優勢有:

為何在 Dev Containers 中使用 Chezmoi?

在 Dev Containers 中整合 chezmoi 可以帶來多項好處:

使用 Chezmoi 同步 Devcontainer 環境:實戰指南

以下步驟將引導您設定 chezmoi 以同步您的 Devcontainer 環境。

步驟 1:在本機設定 Chezmoi

  1. 安裝 Chezmoi: 在您的本機(例如 macOS 或 Linux),可以使用套件管理器安裝 chezmoi。以 Homebrew 為例 10

    brew install chezmoi
    

    或者,使用官方腳本安裝 10

    sh -c "$(curl -fsLS get.chezmoi.io)"
    
  2. 初始化 Chezmoi 並連結到 Git 儲存庫chezmoi 會將您的 dotfiles 存放在一個 Git 儲存庫中。執行以下指令,將 <your-github-username> 替換成您的 GitHub 用戶名 14

    chezmoi init your-github-username
    

    這會在 ~/.local/share/chezmoi 建立一個由 Git 版本控制的目錄,並將其連結到您 GitHub 上的 dotfiles 儲存庫(如果不存在,chezmoi 會提示您建立)。

  3. 將您的 Dotfiles 加入 Chezmoi: 將您想要管理的設定檔加入 chezmoi。例如,加入 .zshrc.gitconfig 14

    chezmoi add ~/.zshrc
    chezmoi add ~/.gitconfig
    

    完成後,提交並推送變更到您的 dotfiles Git 儲存庫:

    chezmoi cd  # 進入 chezmoi 的原始目錄
    git add .
    git commit -m "Add initial zshrc and gitconfig"
    git push
    

步驟 2:設定 Dev Container 以整合 Chezmoi

有兩種主要方法可以在 Dev Container 中整合 chezmoi

步驟 3:在 Dev Container 中自訂 Zsh 與 Zinit

chezmoi 管理的 dotfiles 中,最核心的通常是 shell 的設定檔,例如 .zshrc

  1. 安裝 Zsh (與 Oh My Zsh - 可選): 許多基礎的 Dev Container 映像檔可能不包含 Zsh,或者您想使用特定版本的 Zsh。ghcr.io/devcontainers/features/common-utils feature 可以協助安裝 Zsh 和 Oh My Zsh,並將 Zsh 設為預設 shell 3132。 在 devcontainer.jsonfeatures 區塊加入:

    "ghcr.io/devcontainers/features/common-utils": {
        "installZsh": true,
        "installOhMyZsh": true, // 可選,Zinit 可獨立運作或與 OMZ 整合
        "configureZshAsDefaultShell": true,
        "username": "vscode" // 或您在容器中使用的使用者名稱
    }
    
  2. Zinit:輕量級 Zsh 插件管理器 Zinit 是一個功能強大且執行快速的 Zsh 插件管理器,以其 Turbo 模式(非同步載入插件以加速 shell 啟動)和彈性著稱 333435

  3. 透過 chezmoi 管理的 .zshrc 設定 Zinit: 在由 chezmoi 管理的 .zshrc (模板) 中加入 Zinit 的安裝與設定腳本。這樣每次 chezmoi apply 後,Zinit 就會被正確設定。 Zinit 自動安裝腳本範例 (加入到您的 .zshrc 中) 363437

    # ~/.zshrc
    
    # Zinit 安裝 (如果 ZINIT_HOME 不存在)
    ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"
    if [[ ! -d "$ZINIT_HOME" ]]; then
      echo "Installing Zinit..."
      command mkdir -p "$(dirname "$ZINIT_HOME")" && command chmod g-rwX "$(dirname "$ZINIT_HOME")"
      command git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME" && \
        echo "Zinit installation successful." || \
        echo "Zinit installation failed."
    fi
    
    source "${ZINIT_HOME}/zinit.zsh"
    autoload -Uz _zinit
    (( ${+_comps} )) && _comps[zinit]=_zinit
    
    # --- Zinit Plugins ---
    # 語法高亮
    zinit light zdharma-continuum/fast-syntax-highlighting
    # 自動建議
    zinit light zsh-users/zsh-autosuggestions
    # 增強補全
    zinit light zsh-users/zsh-completions
    
    # 範例:安裝 Powerlevel10k 佈景主題 (一個流行的 Zsh 佈景主題)
    # zinit ice depth"1" # Git clone depth 1 for faster clone
    # zinit light romkatv/powerlevel10k
    # ((isAuthenticated && p10k_theme_variant == "lean")) && p10k reload
    
    # 範例:安裝 fzf (模糊搜尋工具)
    # zinit ice from"gh-r" as"program"
    # zinit light junegunn/fzf
    
    # 載入 Oh My Zsh 的插件 (如果需要)
    # zinit snippet OMZP::git # 載入 Oh My Zsh 的 git 插件
    

    zinit light ... 用於輕量載入插件。Zinit 也支援 ice 修飾詞來進行更細緻的設定,例如 as"command"from"gh-r" 可以從 GitHub Releases 安裝二進位工具 3435

Chezmoi、Zsh 和 Zinit 的協同效益

當 Dev Container 啟動時:

  1. VS Code (或 Dev Container feature) 複製您的 dotfiles 儲存庫到容器內。
  2. install.sh(或 feature 的內部邏輯)執行,安裝 chezmoi 並執行 chezmoi apply
  3. chezmoi 將您 Git 儲存庫中的最新版 dotfiles(包含 .zshrc)套用到容器的正確位置。
  4. 當您開啟終端機,Zsh 啟動並讀取由 chezmoi 管理的 .zshrc
  5. .zshrc 中的 Zinit 腳本會執行:
    • 如果 Zinit 尚未安裝,則會自動下載並安裝 Zinit。
    • Zinit 接著會根據 .zshrc 中的 zinit light ... 或其他 Zinit 指令,非同步下載、安裝及載入您指定的 Zsh 插件和工具 3334
  6. 最終成果:您會得到一個完全客製化、功能豐富且高效的 Zsh 環境,包含您偏好的佈景主題(如 Powerlevel10k、Agnoster 38393227)、指令自動建議、語法高亮、自訂別名、函數,以及透過 Zinit 安裝的其他命令列工具。所有這些配置都是版本控制的,並且能在任何新的 Dev Container 中自動重現。

成果展示與提示

歡迎查看我的 dotfiles repo: jim60105/dotfiles

它是本文的配置成果再加上各種改造的產物 😆

  1. Simplifying Open Source Contributions with Dev Containers ↩2 ↩3 ↩4

  2. Where do you run your code? - an intro to devcontainers

  3. Developing inside a Container - Visual Studio Code

  4. Automating Developer Environments with Dev Containers

  5. Adopting Dotfiles for Codespaces and DevContainers | Vlad's Blog ↩2 ↩3

  6. Ultimate Guide to Dev Containers ↩2 ↩3 ↩4

  7. Simplifying Open Source Contributions with Dev Containers

  8. Ultimate Guide to Dev Containers - Daytona ↩2 ↩3

  9. Chezmoi - Manage your dotfiles across multiple diverse ...

  10. A Step-by-Step Guide to Managing Your Dotfiles with Chezmoi ↩2 ↩3 ↩4

  11. Managing dotfiles with chezmoi - | Daniel Stoddart

  12. Why use chezmoi? - chezmoi ↩2 ↩3 ↩4 ↩5

  13. Development Environment Setup with Chezmoi - Daniel Schmidt ↩2 ↩3

  14. Development Environment Setup with Chezmoi | DanielMSchmidt.de ↩2 ↩3 ↩4 ↩5

  15. Why use chezmoi?

  16. Developer guide - chezmoi

  17. Developer guide - chezmoi

  18. Custom features for individual users · devcontainers - GitHub

  19. Adopting Dotfiles for Codespaces and DevContainers ↩2

  20. Custom features for individual users · devcontainers - GitHub ↩2

  21. Custom features for individual users · devcontainers · Discussion #60 · GitHub ↩2

  22. chezmoi and VS Code remote containers dotfile support #760 ↩2

  23. chezmoi and VS Code remote containers dotfile support #760

  24. chezmoi and VS Code remote containers dotfile support · Issue #760 · twpayne/chezmoi · GitHub

  25. Ultimate Guide to Dev Containers - Daytona

  26. Containers and VMs - chezmoi ↩2 ↩3

  27. VSCode devcontainer with zsh, oh-my-zsh and agnoster theme | by Jamie Thomson | Medium ↩2 ↩3 ↩4 ↩5

  28. Containers and VMs - chezmoi ↩2

  29. Containers and VMs - chezmoi ↩2

  30. rio/features: A collection of devcontainer features - GitHub

  31. Add and Customize Oh My Zsh in a Linux Development ...

  32. Add and Customize Oh My Zsh in a Linux Development Container - JosephGuadagno.net ↩2 ↩3 ↩4

  33. zdharma-continuum/zinit: Flexible and fast ZSH plugin manager ↩2

  34. GitHub - zdharma-continuum/zinit: 🌻 Flexible and fast ZSH plugin manager ↩2 ↩3 ↩4

  35. 加速你的 zsh —— 最强 zsh 插件管理器 zplugin/zinit 教程 - Aloxaf's Blog ↩2

  36. A hands-on guide to setting up zsh, oh my zsh, asdf, and ...

  37. 🚀 A hands-on guide to setting up zsh, oh my zsh, asdf, and spaceship prompt with zinit for your development environment - DEV Community

  38. VSCode devcontainer with zsh, oh-my-zsh and agnoster theme

  39. VSCode devcontainer with zsh, oh-my-zsh and agnoster theme