dotfiles를 GitHub Codespaces에서 사용하기

4 minute read

dotfiles란?

In Unix-like operating systems, any file or folder that starts with a dot character (for example, home/user.config), commonly called a dot file or dotfile, …

A convention arose of using dotfiles in the user’s home directory to store per-user configuration or informational text. Early uses of this were the well-known dotfiles .profile, .login, and .cshrc, which are configuration files for the Bourne shell and C shell and shells compatible with them, and .plan and .project, both used by the finger and name commands.

Hidden file and hidden directory - en.wikipedia.org

Unix-like 운영체제에서 .bashrc 처럼 . 으로 시작하는 파일에 설정 같은 걸 저장한다. 이런 파일들을 . 으로 시작한다고 dotfile 이라고 부른다.

user home 디렉터리에 사용자별 설정을 저장할 때도 사용한다. 그래서 GitHub에 자기 설정 파일을 사람들이 올려놓기 시작했다. 컴퓨터가 바뀌어도 자기 dotfiles 리포지토리를 받고 세팅하면 어딜가나 같은 설정을 사용할 수 있기 때문이다. 나도 ohyecloudy/dotfiles 리포지토리로 관리하고 있다. 회사와 집 컴퓨터 세팅을 편하게 싱크하고 있다.

GitHub Codespaces란?

A codespace is a development environment that’s hosted in the cloud. You can customize your project for GitHub Codespaces by committing configuration files to your repository (often known as Configuration-as-Code), which creates a repeatable codespace configuration for all users of your project.

GitHub Codespaces 개요 - GitHub Docs - docs.github.com

GitHub Codespaces는 cloud에서 호스팅하는 개발 환경이다. Azure에 호스팅하는 가상 머신에 Docker 컨테이너에 연결해서 개발하는 방법을 제공한다. 설정을 코드로 할 수 있는 장점이 있다. 즉 devcontainer.json 파일을 버전 컨트롤한다면 팀원이 같은 개발 환경에서 개발할 수 있다. Visual Studio Code 같은 툴에서 지원을 잘 해줘서 로컬에서 개발하는 것처럼 훌륭한 개발 경험을 제공한다. 안타깝게도 Emacs로 GitHub Codespaces에 붙어서 개발을 해봤는데, 파일을 여는게 너무 느리다. Visual Studio Code에서는 캐시를 적극적으로 해서 그렇게 쾌적한가?

게다가 Azure에서 호스팅하는 Docker 컨테이너에서 개발하기에 내 컴퓨터 성능이 안 좋아도 개발하는 데 전혀 지장이 없다. iPad로도 가능?

GitHub Codespaces에 개인 설정을 담은 dotfiles 설치를 편하게 할 수 있단 얘기를 들었다.

GitHub Codespaces에서 dotfiles 리포지토리를 지원한다

GitHub Codespaces 세팅 파일은 버전 컨트롤을 한다. 그래서 개인적인 세팅을 넣기가 그렇다. 누가 Emacs를 사용하려고 할까? Codespaces를 만들고 난 뒤에 세팅해도 되지만 버전 컨트롤하는 세팅 파일이 업그레이드되면 Codespaces를 리빌드해야 하는데, 이때 개인 세팅을 또 해야 한다.

GitHub은 이런 가려운 곳을 dotfiles 지원으로 풀었다. Settings > Code, planning, and automation > Codespaces > Dotfiles > Automatically install dotfiles 선택하고 dotfiles 리포지토리를 지정하면 세팅 파일을 사용해 Codespaces를 만든 다음 내 dotfiles를 설치해 준다.

Linux를 지원하게 dotfiles bootstrap 스크립트를 짰다면 dotfiles 리포지토리를 지정하고 Codespaces를 만들면 바로 적용이 된다. 하지만 난 Windows와 macOS만 지원하고 있어서 dotfiles bootstrap 스크립트를 수정해야 한다.

dotfiles에 Linux 운영체제용 bootstrap 추가

현재 ohyecloudy/dotfiles 리포지토리는 Windows와 macOS만 지원하고 있다. Linux 지원을 추가해야 한다.

우선 macOS와 Linux를 bash 스크립트에서 구분할 수 있어야 한다. <bash - How to check if running in Cygwin, Mac or Linux? - Stack Overflow> 글을 참고해 uname 프로그램을 사용해서 구분했다.

unameOut="$(uname -s)"
case "${unameOut}" in
    Linux*)     machine=Linux;;
    Darwin*)    machine=Mac;;
    *)          echo "NOT SUPPORTED:${unameOut}";exit 1
esac

딴 건 지원할 일이 생기면 추가하도록 하고 Linux와 macOS만 검사한다. Linux 혹은 Mac을 할당한 machine 변수를 체크해 각 OS만 해야 할 작업을 해줬다. 예를 들면 macOS에서만 Homebrew를 설치하고 패키지를 설치하는 스크립트를 실행한다.

자 이제 돌려볼까?

dotfiles 설치 문제 해결

한 번에 잘 될 리가 없지. dotfiles bootstrap 스크립트 파일 실행이 실패한 것 같다. ’Troubleshooting personalization options for GitHub Codespaces - GitHub Docs’ 문서를 참고해서 문제를 추적했다.

/workspaces/.codespaces/.persistedshare/dotfiles 디렉터리에 dotfiles 저장소를 clone한다. script/bootstrap 스크립트를 실행해 봤다.

[16896 ms]
Configuration file '/etc/zsh/zprofile'
 ==> File on system created by you or by a script.
 ==> File also in package provided by package maintainer.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : start a shell to examine the situation
 The default action is to keep your current version.
*** zprofile (Y/I/N/O/D/Z) [default=N] ?

Linux면 현재 내가 메인 shell로 사용 중인 Zsh를 설치하게 했다. GitHub Codespaces를 만들면 Zsh 설치 여부와 상관없이 zprofile 파일을 만들어 관련 설정을 넣어둔다. Zsh 설치 중에 이 파일을 발견하고 어떻게 할지 프롬프트를 띄웠다.

zprofile 파일을 덮어쓰지 않고 놔두는 디폴트 액션을 선택하면 된다.

echo | sudo apt install -y zsh

echo 를 사용해서 enter가 입력되게 했다.

이제 GitHub Codespaces에서 git st 를 입력하면 status가 잘 나온다. 손에 익은 git alias를 편하게 사용할 수 있다.

링크