diff --git a/install_zsh.sh b/install_zsh.sh new file mode 100644 index 0000000..e191ae9 --- /dev/null +++ b/install_zsh.sh @@ -0,0 +1,323 @@ +#!/bin/bash +set -euo pipefail + +# Script d'installation idempotent de Zsh avec Powerlevel10k sur Debian 12 +# Usage: sudo ./install_zsh.sh [username] + +# Couleurs pour les messages +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' + +# Fonction pour afficher des messages +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +log_step() { + echo -e "${BLUE}[STEP]${NC} $1" +} + +# Vérifier les privilèges root +if [[ $EUID -ne 0 ]]; then + log_error "Ce script doit être exécuté avec les privilèges root (sudo)" + exit 1 +fi + +# Déterminer l'utilisateur cible +if [ $# -eq 1 ]; then + TARGET_USER="$1" +else + TARGET_USER="${SUDO_USER:-$USER}" +fi + +# Vérifier que l'utilisateur existe +if ! id "$TARGET_USER" &>/dev/null; then + log_error "L'utilisateur '$TARGET_USER' n'existe pas" + exit 1 +fi + +TARGET_HOME=$(eval echo ~"$TARGET_USER") +log_info "Installation de Zsh avec Powerlevel10k pour: $TARGET_USER" +log_info "Répertoire home: $TARGET_HOME" + +# Étape 1: Mise à jour et installation des paquets +log_step "1/6 - Mise à jour et installation des paquets nécessaires" +apt-get update -qq + +PACKAGES="zsh git curl wget fontconfig" +for pkg in $PACKAGES; do + if ! dpkg -l | grep -q "^ii $pkg "; then + log_info "Installation de $pkg..." + apt-get install -y "$pkg" + else + log_info "$pkg est déjà installé" + fi +done + +# Étape 2: Installation des polices Nerd Fonts (MesloLGS) +log_step "2/6 - Installation des polices MesloLGS NF pour Powerlevel10k" +FONT_DIR="$TARGET_HOME/.local/share/fonts" +if [ ! -f "$FONT_DIR/MesloLGS NF Regular.ttf" ]; then + log_info "Téléchargement des polices MesloLGS NF..." + sudo -u "$TARGET_USER" mkdir -p "$FONT_DIR" + + cd "$FONT_DIR" + sudo -u "$TARGET_USER" wget -q https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf + sudo -u "$TARGET_USER" wget -q https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf + sudo -u "$TARGET_USER" wget -q https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf + sudo -u "$TARGET_USER" wget -q https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold%20Italic.ttf + + fc-cache -fv "$FONT_DIR" &>/dev/null + log_info "Polices MesloLGS NF installées" +else + log_info "Polices MesloLGS NF déjà installées" +fi + +# Étape 3: Installation de Oh My Zsh +log_step "3/6 - Installation de Oh My Zsh" +OMZ_DIR="$TARGET_HOME/.oh-my-zsh" +if [ ! -d "$OMZ_DIR" ]; then + log_info "Installation de Oh My Zsh..." + sudo -u "$TARGET_USER" sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended + log_info "Oh My Zsh installé" +else + log_info "Oh My Zsh est déjà installé" +fi + +# Étape 4: Installation de Powerlevel10k +log_step "4/6 - Installation du thème Powerlevel10k" +P10K_DIR="${ZSH_CUSTOM:-$TARGET_HOME/.oh-my-zsh/custom}/themes/powerlevel10k" +if [ ! -d "$P10K_DIR" ]; then + log_info "Clonage de Powerlevel10k..." + sudo -u "$TARGET_USER" git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "$P10K_DIR" + log_info "Powerlevel10k installé" +else + log_info "Powerlevel10k est déjà installé" + log_info "Mise à jour de Powerlevel10k..." + sudo -u "$TARGET_USER" git -C "$P10K_DIR" pull +fi + +# Étape 5: Installation de plugins recommandés +log_step "5/6 - Installation des plugins Zsh" +ZSH_CUSTOM="${ZSH_CUSTOM:-$TARGET_HOME/.oh-my-zsh/custom}" + +# Installation des paquets nécessaires pour certains plugins +log_info "Installation des dépendances pour les plugins..." +PLUGIN_DEPS="fzf ripgrep fd-find bat exa" +for dep in $PLUGIN_DEPS; do + if ! dpkg -l | grep -q "^ii $dep "; then + apt-get install -y "$dep" 2>/dev/null || log_warn "$dep non disponible, ignoré" + fi +done + +# zsh-autosuggestions (autocomplétion basée sur l'historique) +AUTOSUGG_DIR="$ZSH_CUSTOM/plugins/zsh-autosuggestions" +if [ ! -d "$AUTOSUGG_DIR" ]; then + log_info "Installation de zsh-autosuggestions..." + sudo -u "$TARGET_USER" git clone https://github.com/zsh-users/zsh-autosuggestions "$AUTOSUGG_DIR" +else + log_info "zsh-autosuggestions déjà installé" + sudo -u "$TARGET_USER" git -C "$AUTOSUGG_DIR" pull -q +fi + +# zsh-syntax-highlighting (coloration syntaxique) +SYNTAX_DIR="$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" +if [ ! -d "$SYNTAX_DIR" ]; then + log_info "Installation de zsh-syntax-highlighting..." + sudo -u "$TARGET_USER" git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "$SYNTAX_DIR" +else + log_info "zsh-syntax-highlighting déjà installé" + sudo -u "$TARGET_USER" git -C "$SYNTAX_DIR" pull -q +fi + +# zsh-completions (completions supplémentaires) +COMPLETIONS_DIR="$ZSH_CUSTOM/plugins/zsh-completions" +if [ ! -d "$COMPLETIONS_DIR" ]; then + log_info "Installation de zsh-completions..." + sudo -u "$TARGET_USER" git clone https://github.com/zsh-users/zsh-completions "$COMPLETIONS_DIR" +else + log_info "zsh-completions déjà installé" + sudo -u "$TARGET_USER" git -C "$COMPLETIONS_DIR" pull -q +fi + +# zsh-history-substring-search (recherche dans l'historique) +HISTORY_DIR="$ZSH_CUSTOM/plugins/zsh-history-substring-search" +if [ ! -d "$HISTORY_DIR" ]; then + log_info "Installation de zsh-history-substring-search..." + sudo -u "$TARGET_USER" git clone https://github.com/zsh-users/zsh-history-substring-search "$HISTORY_DIR" +else + log_info "zsh-history-substring-search déjà installé" + sudo -u "$TARGET_USER" git -C "$HISTORY_DIR" pull -q +fi + +# fzf-tab (meilleure complétion avec fzf) +FZFTAB_DIR="$ZSH_CUSTOM/plugins/fzf-tab" +if [ ! -d "$FZFTAB_DIR" ]; then + log_info "Installation de fzf-tab..." + sudo -u "$TARGET_USER" git clone https://github.com/Aloxaf/fzf-tab "$FZFTAB_DIR" +else + log_info "fzf-tab déjà installé" + sudo -u "$TARGET_USER" git -C "$FZFTAB_DIR" pull -q +fi + +# Étape 6: Configuration de .zshrc +log_step "6/6 - Configuration de .zshrc" +ZSHRC="$TARGET_HOME/.zshrc" + +# Backup du .zshrc existant +if [ -f "$ZSHRC" ]; then + if ! grep -q "powerlevel10k" "$ZSHRC"; then + log_info "Sauvegarde de .zshrc existant..." + sudo -u "$TARGET_USER" cp "$ZSHRC" "$ZSHRC.backup.$(date +%Y%m%d_%H%M%S)" + fi +fi + +# Créer ou mettre à jour .zshrc +cat > "$ZSHRC" << 'EOF' +# Enable Powerlevel10k instant prompt +if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then + source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" +fi + +# Path to oh-my-zsh installation +export ZSH="$HOME/.oh-my-zsh" + +# Set theme +ZSH_THEME="powerlevel10k/powerlevel10k" + +# Plugins - ordre important! fzf-tab doit être avant zsh-autosuggestions +plugins=( + git # Aliases et fonctions Git + docker # Autocomplétion Docker + docker-compose # Autocomplétion Docker Compose + sudo # ESC x2 pour préfixer avec sudo + history # Aliases pour l'historique + colored-man-pages # Pages man en couleur + command-not-found # Suggestions si commande non trouvée + zsh-completions # Completions supplémentaires + fzf-tab # Meilleure completion avec fzf + zsh-autosuggestions # Suggestions basées sur l'historique + zsh-syntax-highlighting # Coloration syntaxique + zsh-history-substring-search # Recherche dans l'historique +) + +source $ZSH/oh-my-zsh.sh + +# User configuration +export LANG=fr_FR.UTF-8 +export EDITOR='nano' + +# Configuration de l'historique +HISTSIZE=10000 +SAVEHIST=10000 +setopt HIST_IGNORE_ALL_DUPS +setopt HIST_FIND_NO_DUPS +setopt SHARE_HISTORY + +# Configuration zsh-autosuggestions +ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8' +ZSH_AUTOSUGGEST_STRATEGY=(history completion) + +# Configuration fzf si disponible +if command -v fzf &> /dev/null; then + export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border' + export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git' + export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND" +fi + +# Bind keys pour history-substring-search +bindkey '^[[A' history-substring-search-up +bindkey '^[[B' history-substring-search-down +bindkey -M vicmd 'k' history-substring-search-up +bindkey -M vicmd 'j' history-substring-search-down + +# Aliases +alias ll='ls -lah' +alias la='ls -A' +alias l='ls -CF' +alias zshconfig="nano ~/.zshrc" +alias ohmyzsh="nano ~/.oh-my-zsh" +alias update-zsh="omz update && cd ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions && git pull && cd ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting && git pull && cd" + +# Aliases Git utiles +alias gs='git status' +alias ga='git add' +alias gc='git commit' +alias gp='git push' +alias gl='git log --oneline --graph --decorate' + +# Aliases Docker +alias dps='docker ps' +alias dpa='docker ps -a' +alias di='docker images' +alias dex='docker exec -it' + +# Utiliser exa si disponible (ls amélioré) +if command -v exa &> /dev/null; then + alias ls='exa --icons' + alias ll='exa -lah --icons' + alias tree='exa --tree --icons' +fi + +# Utiliser bat si disponible (cat amélioré) +if command -v bat &> /dev/null; then + alias cat='bat --style=plain' + alias ccat='/bin/cat' +fi + +# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh. +[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh +EOF + +chown "$TARGET_USER:$TARGET_USER" "$ZSHRC" +log_info "Fichier .zshrc configuré" + +# Changer le shell par défaut +log_step "Configuration du shell par défaut" +CURRENT_SHELL=$(grep "^$TARGET_USER:" /etc/passwd | cut -d: -f7) +ZSH_PATH=$(command -v zsh) + +if [ "$CURRENT_SHELL" != "$ZSH_PATH" ]; then + log_info "Changement du shell par défaut vers Zsh..." + chsh -s "$ZSH_PATH" "$TARGET_USER" + log_info "Shell par défaut changé pour $TARGET_USER" +else + log_info "Zsh est déjà le shell par défaut" +fi + +# Messages finaux +echo "" +log_info "✓ Installation terminée avec succès!" +echo "" +echo -e "${BLUE}Prochaines étapes:${NC}" +echo "1. Déconnectez-vous et reconnectez-vous (ou exécutez: su - $TARGET_USER)" +echo "2. Au premier lancement, Powerlevel10k lancera un assistant de configuration" +echo "3. Suivez les instructions pour personnaliser votre prompt" +echo "4. Pour reconfigurer plus tard, exécutez: p10k configure" +echo "" +echo -e "${GREEN}Plugins installés:${NC}" +echo " • zsh-autosuggestions - Suggestions basées sur l'historique (flèche →)" +echo " • zsh-syntax-highlighting - Coloration des commandes" +echo " • zsh-completions - Completions supplémentaires" +echo " • zsh-history-substring-search - Recherche avec ↑/↓" +echo " • fzf-tab - Complétion interactive avec fzf (Tab)" +echo " • git, docker, sudo - Plugins Oh My Zsh intégrés" +echo "" +echo -e "${GREEN}Commandes utiles:${NC}" +echo " • Ctrl+R : Recherche dans l'historique avec fzf" +echo " • ESC ESC : Ajouter sudo à la commande actuelle" +echo " • update-zsh : Mettre à jour Oh My Zsh et tous les plugins" +echo "" +echo -e "${YELLOW}Note:${NC} Si vous utilisez SSH, configurez votre terminal pour utiliser la police 'MesloLGS NF'" +echo "" \ No newline at end of file