dotfiles have an inseparable relationship with engineer life. I would like to introduce how to grow such dotfiles into the ideal shape.
As a first step, let's start by managing dotfiles with Git. There are various benefits such as history management and improved portability. (Also grow grass)
Create a repository called dotfiles and commit various dotfiles such as .bashrc
under the repository.
Use the ln
command to the home directory [symbolic link](https://ja.wikipedia.org/wiki/%E3%82%BD%E3%83%95%E3%83%88%E3%83" % AA% E3% 83% B3% E3% 82% AF).
** By creating a symbolic link, files in the repository can be centrally managed, making it easy to update and update. ** **
.Example of creating a symbolic link in bashrc
#Creating a link
$ cd path/to/dotfiles
$ ln -s .bashrc ~/.bashrc
#Check the link
$ ls -l ~/.bashrc
lrwxrwxrwx 1 reireias reireias 59 June 8 22:30 /home/reireias/.bashrc -> /home/reireias/dev/src/github.com/reireias/dotfiles/.bashrc
There are quite a lot of dotfiles repositories published on GitHub. https://github.com/topics/dotfiles
** By the way, be careful not to disclose authentication information such as ʻAWS_ACCESS_KEY_ID` **
Creating a symbolic link is done only once for that environment, but as the number of target dotfiles increases, it is the nature of the engineer that he wants to make the work more efficient.
It can be implemented with a simple shell script. You can implement it with something other than a shell script, but shell scripts that can be used in most environments are often used because it is easier to install in a clean environment with fewer packages.
The following script will create symbolic links for all files starting with .
directly under the dotfiles repository in your home directory.
install.sh
#!/bin/bash -e
IGNORE_PATTERN="^\.(git|travis)"
echo "Create dotfile links."
for dotfile in .??*; do
[[ $dotfile =~ $IGNORE_PATTERN ]] && continue
ln -snfv "$(pwd)/$dotfile" "$HOME/$dotfile"
done
echo "Success"
Some dotfiles may require you to install packages and libraries.
For example
--There is .tmux.conf
, so I want to install tmux
--A self-made function that uses the peco
command is defined in .bashrc
.
--A particular plugin for vim depends on a python library
And so on.
Initially, you may have a list of dependent packages in the README. After all it is an engineer to automate.
For example, in the case of shell script, simply enumerating the commands as shown below will make a good installation script.
An example of an installation script
#!/bin/bash
brew install tmux peco neovim
pip install neovim
Let's make the installation script conscious of idempotence. By ensuring that the same result is obtained no matter how many times it is executed, it is possible to execute the installation script for the existing environment even if a new process to be performed by the installation script is added.
A simple example is shown below.
#No idempotence
mv config /etc/hoge/config
echo "some setting" >> ~/.hogerc
#Idempotent
cp config /etc/hoge/config
if ! grep -q "^some setting$" ~/.hogerc; then
echo "some setting" >> ~/.hogerc
fi
The larger the installation script, the more difficult it is to ensure equivalence with a shell script, so Ansible and [Chef]( Writing an installation script using a configuration management tool such as https://www.chef.io/) is also a good choice.
However, if you start using a tool other than shell script, you need to be careful because the procedure for installing in a clean environment will increase a little.
Both cats and scoops [CI](https://ja.wikipedia.org/wiki/%E7%B6%99%E7%B6%9A%E7%9A%84%E3%82%A4%E3%83%B3 % E3% 83% 86% E3% 82% B0% E3% 83% AC% E3% 83% BC% E3% 82% B7% E3% 83% A7% E3% 83% B3) It is a world. Let's also turn CI in dotfiles.
For scripts that install the latest version without specifying the version of the package or library to install, incompatible updates may cause the installation script to fail. It's a good idea to run the installation script and check if the files are placed properly.
Here's a very simple sample code on GitHub Actions.
yaml:.github/workflows/main.yml
---
name: main
on: [push]
jobs:
main:
runs-on: ubuntu-latest # or macOS-latest
steps:
- uses: actions/checkout@v1
- name: install dotfiles
run: bash install.sh
- name: test
run: # some tests
As dotfiles grow steadily, .bashrc
(.zshrc
) and .vimrc
become quite huge.
Engineers usually consider a huge file to be evil due to conditional reflection, so I want to divide it somehow.
Fortunately, .bashrc
and .vimrc
have a function to read another file, so it is easy to divide.
For example, you can read another file from .bashrc
as follows.
.bashrc
source ${HOME}/.bash/keybind.sh
source ${HOME}/.bash/alias.sh
dotfiles also grow when seen by humans (?) Develop a README and make it a cool dotfiles repository.
Personally, if the following is described, I want to refer to it ~~ plagiarism ~~.
--Target editors, shells, tools --Terminal screenshot --Concept --Dependent packages and libraries
If you are a rare person who uses both Linux and Mac OS ~~ metamorphosis ~~, you may be happy if you prepare settings and installation scripts for both environments. CI should also be executed in both Linux and Mac OS environments by utilizing matrix build etc.
By the way, recently, I'm in trouble because there is no way to write Linux apt and Mac OS brew in Ansible without using branching by when.
So what about your dotfiles? I will post it because it seems to be said. Don't expect that much.
[^ 1]: Unix-like hidden file mechanism was a bad habit born from the developer Poka
Recommended Posts