Dotfiles & Env Management — Terminal

Dotfiles & Environment Management Dotfiles adalah file konfigurasi yang namanya diawali titik (.) — tersembunyi di file manager tapi sangat penting bagi develop

Dotfiles & Environment Management

Dotfiles adalah file konfigurasi yang namanya diawali titik (.) — tersembunyi di file manager tapi sangat penting bagi developer.

File Konfigurasi Utama

~/.bashrc       # Konfigurasi Bash (interactive non-login)
~/.bash_profile # Konfigurasi Bash (login shell)
~/.zshrc        # Konfigurasi Zsh (dijalankan setiap buka terminal)
~/.profile      # Konfigurasi umum (semua shell)

~/.gitconfig    # Konfigurasi Git global
~/.ssh/config   # Konfigurasi SSH
~/.vimrc        # Konfigurasi Vim
~/.tmux.conf    # Konfigurasi Tmux

Startup File Execution Flow

Shell membaca file startup yang berbeda tergantung jenis shell yang dijalankan — ini sering menyebabkan bingung "kenapa alias saya tidak muncul?".

Tiga jenis shell:

Jenis Kapan muncul
Login shell Saat login sistem (SSH, terminal di macOS, sudo -i)
Interactive non-login Saat buka tab terminal baru di Linux, buka shell child
Non-interactive Saat jalankan script (bash script.sh)

Urutan eksekusi untuk Bash:

Login shell:
  /etc/profile → ~/.bash_profile (atau ~/.bash_login atau ~/.profile)
                  │
                  └── (banyak .bash_profile di-set untuk source ~/.bashrc)
                           │
                           └── ~/.bashrc

Interactive non-login:
  /etc/bash.bashrc → ~/.bashrc

Urutan eksekusi untuk Zsh:

Login shell:
  /etc/zshenv → ~/.zshenv → /etc/zprofile → ~/.zprofile
  → /etc/zshrc → ~/.zshrc → /etc/zlogin → ~/.zlogin

Interactive non-login:
  /etc/zshenv → ~/.zshenv → /etc/zshrc → ~/.zshrc

Perbedaan platform:

Implikasi praktis:

# Alias di ~/.bash_profile → hilang di Linux terminal biasa
# Solusi klasik: source .bashrc dari .bash_profile
# Tambahkan di ~/.bash_profile:
[ -f ~/.bashrc ] && source ~/.bashrc

Rule of thumb:

~/.zshrc atau ~/.bashrc

File ini dijalankan setiap kali terminal dibuka. Isi yang umum:

# ── Path ──────────────────────────────────────────────
export PATH="$HOME/.local/bin:$PATH"
export PATH="$HOME/bin:$PATH"

# ── Alias ─────────────────────────────────────────────
alias ll="ls -la"
alias gs="git status"
alias ..="cd .."

# ── Functions ─────────────────────────────────────────
mkcd() { mkdir -p "$1" && cd "$1"; }

# ── Environment Variables ─────────────────────────────
export EDITOR="vim"
export VISUAL="code"
export LANG="en_US.UTF-8"

# ── Prompt ────────────────────────────────────────────
export PS1="\u@\h:\w\$ "   # user@host:path$

# ── History ───────────────────────────────────────────
export HISTSIZE=10000
export HISTFILESIZE=20000
export HISTCONTROL=ignoredups  # jangan simpan duplikat

~/.gitconfig

[user]
    name = Budi Santoso
    email = [email protected]

[core]
    editor = vim
    excludesfile = ~/.gitignore_global

[alias]
    st = status
    co = checkout
    br = branch
    lg = log --oneline --graph --all
    undo = reset HEAD~1 --soft

[pull]
    rebase = false

[init]
    defaultBranch = main

Manajemen File .env

File .env menyimpan variabel lingkungan spesifik per project:

# .env — JANGAN commit ke git!
DB_HOST=localhost
DB_PORT=5432
DB_NAME=myapp
DB_USER=admin
DB_PASS=secret123

APP_ENV=development
APP_PORT=3000
APP_KEY=base64:abc123...

API_KEY=sk-abcdefgh
REDIS_URL=redis://localhost:6379

Load .env di shell:

# Manual export satu per satu
export DB_HOST=localhost

# Load seluruh file .env
export $(grep -v '^\#' .env | xargs)

# Atau gunakan direnv (tool khusus)
# Install: brew install direnv
# .envrc file (auto-load saat masuk folder)
export DB_HOST=localhost

Dotfiles Repo — Best Practice

Developer profesional menyimpan dotfiles di Git:

# Struktur umum dotfiles repo
~/dotfiles/
├── .zshrc
├── .gitconfig
├── .vimrc
├── .tmux.conf
└── install.sh    # script untuk symlink ke ~

# Buat symlink
ln -sf ~/dotfiles/.zshrc ~/.zshrc
ln -sf ~/dotfiles/.gitconfig ~/.gitconfig

Tips Keamanan

  1. Jangan commit .env — selalu tambahkan ke .gitignore
  2. Gunakan .env.example untuk template (tanpa nilai sensitif)
  3. Pisahkan per environment.env.development, .env.production
  4. Enkripsi secret — gunakan tool seperti git-secret atau Vault
  5. Rotate secrets secara berkala