refactor: simplify Alacritty theme synchronization with Nix configuration and remove obsolete scripts

This commit is contained in:
DannyDannyDanny 2025-09-12 13:13:31 +02:00
parent bd9cd434d4
commit d7302fd9a6
8 changed files with 165 additions and 401 deletions

View file

@ -1,64 +1,53 @@
# Alacritty Theme Synchronization
This directory contains the theme synchronization system for Alacritty that automatically switches between Catppuccin light and dark themes based on your macOS system theme.
Simple theme synchronization for Alacritty that automatically switches between Catppuccin light and dark themes based on your macOS system theme.
## Files
**This solution uses Nix conditional configuration - no complex scripts or wrappers needed!**
- `catppuccin-light.yml` - Catppuccin Latte (light) theme colors
- `catppuccin-dark.yml` - Catppuccin Mocha (dark) theme colors
- `README.md` - This documentation
## How It Works
## Scripts
The theme synchronization scripts are located in `/scripts/`:
- `detect-system-theme.sh` - Detects current macOS system theme (light/dark)
- `sync-alacritty-theme.sh` - Syncs Alacritty config with current system theme
- `monitor-theme-changes.sh` - Continuously monitors for theme changes
- `setup-alacritty-theme-sync.sh` - Setup script for initial configuration
1. The system detects your current macOS theme using `defaults read -g AppleInterfaceStyle`
2. The theme is written to `/Users/danny/.local/share/nvim_color_scheme`
3. Your NixOS configuration reads this file and conditionally applies:
- **Light theme** → Catppuccin Latte
- **Dark theme** → Catppuccin Mocha
4. Alacritty gets the correct theme colors through Nix configuration
## Setup
1. Run the setup script:
1. **Run the setup script:**
```bash
./scripts/setup-alacritty-theme-sync.sh
./scripts/setup-simple-theme-sync.sh
```
2. Choose your preferred method for automatic theme switching:
2. **Apply the theme to Alacritty:**
```bash
home-manager switch
```
### Option 1: Manual Sync
Run the sync script whenever you want to update the theme:
That's it! Your Alacritty will now use the correct theme based on your system theme.
## Usage
### Manual Theme Sync
When you change your system theme, run:
```bash
./scripts/sync-alacritty-theme.sh
./scripts/sync-alacritty-theme.sh && home-manager switch
```
### Option 2: Background Monitoring
Run the monitor script in the background:
```bash
./scripts/monitor-theme-changes.sh &
```
### Option 3: LaunchAgent (Recommended)
Install as a system service that runs automatically:
### Automatic Theme Switching (Optional)
For automatic switching, you can set up a LaunchAgent:
```bash
cp assets/launchd/com.user.alacritty-theme-sync.plist ~/Library/LaunchAgents/
launchctl load ~/Library/LaunchAgents/com.user.alacritty-theme-sync.plist
```
### Option 4: Shell Integration
Add to your Fish shell configuration:
```bash
echo 'source /Users/danny/dotfiles/scripts/sync-alacritty-theme.sh' >> ~/.config/fish/config.fish
```
## Files
## How It Works
1. The system detects your current macOS theme using `defaults read -g AppleInterfaceStyle`
2. Based on the theme, it applies the appropriate Catppuccin color scheme:
- **Light theme** → Catppuccin Latte
- **Dark theme** → Catppuccin Mocha
3. The Alacritty configuration is updated with the new colors
4. Running Alacritty instances are restarted to apply the new theme
- `scripts/detect-system-theme.sh` - Detects current macOS system theme
- `scripts/sync-alacritty-theme.sh` - Updates the theme file that Nix reads
- `scripts/setup-simple-theme-sync.sh` - One-time setup script
- `nixos/home/danny/home.nix` - Contains the conditional Alacritty configuration
## Theme Colors
@ -72,38 +61,24 @@ echo 'source /Users/danny/dotfiles/scripts/sync-alacritty-theme.sh' >> ~/.config
- Foreground: `#cdd6f4` (text)
- Accent colors optimized for dark backgrounds
## Troubleshooting
### Theme not updating
- Check if Alacritty config file exists and is writable
- Verify the theme detection script works: `./scripts/detect-system-theme.sh`
- Check logs in `/tmp/alacritty-theme-sync.log`
### LaunchAgent not working
- Check if the plist file is in the correct location
- Verify permissions: `ls -la ~/Library/LaunchAgents/`
- Check launchctl status: `launchctl list | grep alacritty`
### Manual theme override
If you want to manually set a theme regardless of system setting:
```bash
# Force light theme
ALACRITTY_THEME=light ./scripts/sync-alacritty-theme.sh
# Force dark theme
ALACRITTY_THEME=dark ./scripts/sync-alacritty-theme.sh
```
## Integration with NixOS
The NixOS configuration in `nixos/home/danny/home.nix` provides the base Alacritty configuration. The theme sync scripts work on top of this configuration, dynamically updating the colors section while preserving all other settings.
The solution uses Nix's conditional configuration in `home.nix`:
## Customization
```nix
colors = let
systemThemeFile = "/Users/danny/.local/share/nvim_color_scheme";
isLightTheme = builtins.pathExists systemThemeFile &&
builtins.readFile systemThemeFile == "light\n";
To customize the themes:
lightColors = { /* Catppuccin Latte colors */ };
darkColors = { /* Catppuccin Mocha colors */ };
in if isLightTheme then lightColors else darkColors;
```
1. Edit the color values in `catppuccin-light.yml` or `catppuccin-dark.yml`
2. Run the sync script to apply changes
3. The changes will persist until the next theme switch
For more advanced customization, you can modify the sync script to use different theme files or add additional theme variants.
This approach:
- ✅ Works with Spotlight/Applications folder launches
- ✅ No shell aliases or wrapper scripts needed
- ✅ Integrates cleanly with NixOS configuration
- ✅ Minimal complexity - just 3 simple scripts
- ✅ Uses the same theme file as your Neovim configuration

View file

@ -1,30 +1,28 @@
# Catppuccin Mocha (Dark) theme for Alacritty
colors:
# Catppuccin Mocha palette
primary:
background: '0x1e1e2e' # base
foreground: '0xcdd6f4' # text
[colors.primary]
background = "0x1e1e2e" # base
foreground = "0xcdd6f4" # text
cursor:
text: '0x1e1e2e' # base
cursor: '0xf5e0dc' # rosewater
[colors.cursor]
text = "0x1e1e2e" # base
cursor = "0xf5e0dc" # rosewater
normal:
black: '0x45475a' # surface1
red: '0xf38ba8' # red
green: '0xa6e3a1' # green
yellow: '0xf9e2af' # yellow
blue: '0x89b4fa' # blue
magenta: '0xf5c2e7' # pink
cyan: '0x94e2d5' # teal
white: '0xbac2de' # subtext1
[colors.normal]
black = "0x45475a" # surface1
red = "0xf38ba8" # red
green = "0xa6e3a1" # green
yellow = "0xf9e2af" # yellow
blue = "0x89b4fa" # blue
magenta = "0xf5c2e7" # pink
cyan = "0x94e2d5" # teal
white = "0xbac2de" # subtext1
bright:
black: '0x585b70' # surface2
red: '0xf38ba8' # red
green: '0xa6e3a1' # green
yellow: '0xf9e2af' # yellow
blue: '0x89b4fa' # blue
magenta: '0xf5c2e7' # pink
cyan: '0x94e2d5' # teal
white: '0xa6adc8' # subtext0
[colors.bright]
black = "0x585b70" # surface2
red = "0xf38ba8" # red
green = "0xa6e3a1" # green
yellow = "0xf9e2af" # yellow
blue = "0x89b4fa" # blue
magenta = "0xf5c2e7" # pink
cyan = "0x94e2d5" # teal
white = "0xa6adc8" # subtext0

View file

@ -1,30 +1,28 @@
# Catppuccin Latte (Light) theme for Alacritty
colors:
# Catppuccin Latte palette
primary:
background: '0xeff1f5' # base
foreground: '0x4c4f69' # text
[colors.primary]
background = "0xeff1f5" # base
foreground = "0x4c4f69" # text
cursor:
text: '0xeff1f5' # base
cursor: '0xdc8a78' # rosewater
[colors.cursor]
text = "0xeff1f5" # base
cursor = "0xdc8a78" # rosewater
normal:
black: '0x5c5f77' # surface1
red: '0xd20f39' # red
green: '0x40a02b' # green
yellow: '0xdf8e1d' # yellow
blue: '0x1e40af' # blue
magenta: '0xea76cb' # pink
cyan: '0x179299' # teal
white: '0xacb0be' # subtext1
[colors.normal]
black = "0x5c5f77" # surface1
red = "0xd20f39" # red
green = "0x40a02b" # green
yellow = "0xdf8e1d" # yellow
blue = "0x1e40af" # blue
magenta = "0xea76cb" # pink
cyan = "0x179299" # teal
white = "0xacb0be" # subtext1
bright:
black: '0x6c6f85' # surface2
red: '0xd20f39' # red
green: '0x40a02b' # green
yellow: '0xdf8e1d' # yellow
blue: '0x1e40af' # blue
magenta: '0xea76cb' # pink
cyan: '0x179299' # teal
white: '0xbcc0cc' # subtext0
[colors.bright]
black = "0x6c6f85" # surface2
red = "0xd20f39" # red
green = "0x40a02b" # green
yellow = "0xdf8e1d" # yellow
blue = "0x1e40af" # blue
magenta = "0xea76cb" # pink
cyan = "0x179299" # teal
white = "0xbcc0cc" # subtext0

View file

@ -131,8 +131,7 @@
};
};
# Alacritty terminal configuration (managed by Home Manager)
# Note: Colors are managed by the theme sync script for dynamic switching
# Alacritty terminal configuration with conditional theme switching
programs.alacritty = {
enable = true;
settings = {
@ -154,9 +153,32 @@
program = "${pkgs.fish}/bin/fish";
};
};
# Default colors (Catppuccin Mocha - will be overridden by theme sync)
colors = {
# Conditional colors based on system theme
colors = let
# Read system theme from file (created by theme detection script)
systemThemeFile = "/Users/danny/.local/share/nvim_color_scheme";
# Default to dark theme if file doesn't exist
isLightTheme = builtins.pathExists systemThemeFile &&
builtins.readFile systemThemeFile == "light\n";
# Catppuccin Latte (Light) colors
lightColors = {
primary = { background = "0xeff1f5"; foreground = "0x4c4f69"; };
cursor = { text = "0xeff1f5"; cursor = "0xdc8a78"; };
normal = {
black = "0x5c5f77"; red = "0xd20f39"; green = "0x40a02b"; yellow = "0xdf8e1d";
blue = "0x1e40af"; magenta = "0xea76cb"; cyan = "0x179299"; white = "0xacb0be";
};
bright = {
black = "0x6c6f85"; red = "0xd20f39"; green = "0x40a02b"; yellow = "0xdf8e1d";
blue = "0x1e40af"; magenta = "0xea76cb"; cyan = "0x179299"; white = "0xbcc0cc";
};
};
# Catppuccin Mocha (Dark) colors
darkColors = {
primary = { background = "0x1e1e2e"; foreground = "0xcdd6f4"; };
cursor = { text = "0x1e1e2e"; cursor = "0xf5e0dc"; };
normal = {
black = "0x45475a"; red = "0xf38ba8"; green = "0xa6e3a1"; yellow = "0xf9e2af";
blue = "0x89b4fa"; magenta = "0xf5c2e7"; cyan = "0x94e2d5"; white = "0xbac2de";
@ -166,9 +188,11 @@
blue = "0x89b4fa"; magenta = "0xf5c2e7"; cyan = "0x94e2d5"; white = "0xa6adc8";
};
};
in if isLightTheme then lightColors else darkColors;
};
};
# TODO: Put user-installed binaries here if you want HM to own them (optional)
# home.packages = with pkgs; [
# ];

View file

@ -1,55 +0,0 @@
#!/bin/bash
# Monitor system theme changes and sync Alacritty theme
# This script runs continuously and only updates Alacritty when the theme changes
set -e
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SYNC_SCRIPT="$SCRIPT_DIR/sync-alacritty-theme.sh"
# State file to track the last known theme
STATE_FILE="/tmp/alacritty-theme-state"
# Function to get current theme
get_current_theme() {
"$SCRIPT_DIR/detect-system-theme.sh"
}
# Function to get last known theme
get_last_theme() {
if [ -f "$STATE_FILE" ]; then
cat "$STATE_FILE"
else
echo ""
fi
}
# Function to save current theme
save_theme() {
echo "$1" > "$STATE_FILE"
}
# Initial sync
echo "Starting Alacritty theme monitor..."
CURRENT_THEME=$(get_current_theme)
echo "Current theme: $CURRENT_THEME"
# Run initial sync
"$SYNC_SCRIPT"
save_theme "$CURRENT_THEME"
# Monitor for changes
while true; do
sleep 5 # Check every 5 seconds
NEW_THEME=$(get_current_theme)
LAST_THEME=$(get_last_theme)
if [ "$NEW_THEME" != "$LAST_THEME" ]; then
echo "Theme changed from '$LAST_THEME' to '$NEW_THEME'"
"$SYNC_SCRIPT"
save_theme "$NEW_THEME"
fi
done

View file

@ -1,104 +0,0 @@
#!/bin/bash
# Setup script for Alacritty theme synchronization
# This script installs the necessary components to sync Alacritty with system theme
set -e
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOTFILES_DIR="$(dirname "$SCRIPT_DIR")"
echo "Setting up Alacritty theme synchronization..."
# Check if we're on macOS
if [[ "$OSTYPE" != "darwin"* ]]; then
echo "Error: This script is designed for macOS"
exit 1
fi
# Check if Alacritty is installed
if ! command -v alacritty >/dev/null 2>&1; then
echo "Warning: Alacritty is not installed or not in PATH"
echo "Please install Alacritty first: brew install alacritty"
fi
# Create Alacritty config directory if it doesn't exist
ALACRITTY_CONFIG_DIR="$HOME/.config/alacritty"
mkdir -p "$ALACRITTY_CONFIG_DIR"
# Check if Alacritty config exists
ALACRITTY_CONFIG="$ALACRITTY_CONFIG_DIR/alacritty.yml"
if [ ! -f "$ALACRITTY_CONFIG" ]; then
echo "Creating default Alacritty configuration..."
cat > "$ALACRITTY_CONFIG" << 'EOF'
# Alacritty configuration
# This file will be automatically updated by the theme sync script
window:
padding:
x: 8
y: 8
dynamic_padding: true
decorations: buttonless
opacity: 0.95
startup_mode: Fullscreen
option_as_alt: Both
scrolling:
history: 10000
multiplier: 3
font:
size: 13.0
cursor:
style: Block
unfocused_hollow: true
terminal:
shell:
program: /bin/fish
# Colors will be automatically managed by theme sync
colors:
primary:
background: '0x1e1e2e'
foreground: '0xcdd6f4'
EOF
echo "Created default Alacritty config at $ALACRITTY_CONFIG"
fi
# Test the theme detection script
echo "Testing theme detection..."
CURRENT_THEME=$("$SCRIPT_DIR/detect-system-theme.sh")
echo "Current system theme: $CURRENT_THEME"
# Test the sync script
echo "Testing theme synchronization..."
"$SCRIPT_DIR/sync-alacritty-theme.sh"
echo ""
echo "Setup complete! Here's what was configured:"
echo ""
echo "1. Theme detection script: $SCRIPT_DIR/detect-system-theme.sh"
echo "2. Theme sync script: $SCRIPT_DIR/sync-alacritty-theme.sh"
echo "3. Theme monitor script: $SCRIPT_DIR/monitor-theme-changes.sh"
echo "4. Alacritty config: $ALACRITTY_CONFIG"
echo ""
echo "To enable automatic theme switching, you have several options:"
echo ""
echo "Option 1 - Manual sync (run when needed):"
echo " $SCRIPT_DIR/sync-alacritty-theme.sh"
echo ""
echo "Option 2 - Background monitoring (runs continuously):"
echo " $SCRIPT_DIR/monitor-theme-changes.sh &"
echo ""
echo "Option 3 - LaunchAgent (automatic startup):"
echo " cp $DOTFILES_DIR/assets/launchd/com.user.alacritty-theme-sync.plist ~/Library/LaunchAgents/"
echo " launchctl load ~/Library/LaunchAgents/com.user.alacritty-theme-sync.plist"
echo ""
echo "Option 4 - Add to shell profile (runs on terminal startup):"
echo " echo 'source $SCRIPT_DIR/sync-alacritty-theme.sh' >> ~/.config/fish/config.fish"
echo ""
echo "Current theme applied: $CURRENT_THEME"

View file

@ -0,0 +1,27 @@
#!/bin/bash
# Simple setup for Alacritty theme synchronization
# This creates the theme file and rebuilds the Nix configuration
set -e
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "Setting up simple Alacritty theme synchronization..."
# Run the theme sync script to create the initial theme file
echo "Detecting current system theme..."
"$SCRIPT_DIR/sync-alacritty-theme.sh"
echo ""
echo "Setup complete!"
echo ""
echo "To apply the theme to Alacritty, run:"
echo " home-manager switch"
echo ""
echo "To sync themes when your system theme changes:"
echo " $SCRIPT_DIR/sync-alacritty-theme.sh && home-manager switch"
echo ""
echo "For automatic theme switching, you can set up a LaunchAgent or"
echo "run the sync script manually when needed."

View file

@ -1,43 +1,19 @@
#!/bin/bash
# Sync Alacritty theme with system theme
# This script detects the current system theme and updates Alacritty configuration accordingly
# This script detects the current system theme and updates the theme file that Nix reads
set -e
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOTFILES_DIR="$(dirname "$SCRIPT_DIR")"
# Paths
THEME_DETECTION_SCRIPT="$SCRIPT_DIR/detect-system-theme.sh"
LIGHT_THEME="$DOTFILES_DIR/assets/alacritty/catppuccin-light.yml"
DARK_THEME="$DOTFILES_DIR/assets/alacritty/catppuccin-dark.yml"
THEME_FILE="/Users/danny/.local/share/nvim_color_scheme"
# Alacritty config locations (try different possible locations)
ALACRITTY_CONFIG_LOCATIONS=(
"$HOME/.config/alacritty/alacritty.yml"
"$HOME/.alacritty.yml"
"$HOME/Library/Application Support/Alacritty/alacritty.yml"
)
# Find the actual Alacritty config file
ALACRITTY_CONFIG=""
for location in "${ALACRITTY_CONFIG_LOCATIONS[@]}"; do
if [ -f "$location" ]; then
ALACRITTY_CONFIG="$location"
break
fi
done
if [ -z "$ALACRITTY_CONFIG" ]; then
echo "Error: Could not find Alacritty configuration file"
echo "Tried locations:"
for location in "${ALACRITTY_CONFIG_LOCATIONS[@]}"; do
echo " - $location"
done
exit 1
fi
# Create the directory if it doesn't exist
mkdir -p "$(dirname "$THEME_FILE")"
# Detect current system theme
if [ ! -f "$THEME_DETECTION_SCRIPT" ]; then
@ -48,83 +24,8 @@ fi
CURRENT_THEME=$("$THEME_DETECTION_SCRIPT")
echo "Current system theme: $CURRENT_THEME"
# Determine which theme file to use
if [ "$CURRENT_THEME" = "light" ]; then
THEME_FILE="$LIGHT_THEME"
THEME_NAME="Catppuccin Latte (Light)"
elif [ "$CURRENT_THEME" = "dark" ]; then
THEME_FILE="$DARK_THEME"
THEME_NAME="Catppuccin Mocha (Dark)"
else
echo "Error: Unknown theme '$CURRENT_THEME'. Expected 'light' or 'dark'"
exit 1
fi
# Write the theme to the file that Nix reads
echo "$CURRENT_THEME" > "$THEME_FILE"
if [ ! -f "$THEME_FILE" ]; then
echo "Error: Theme file not found at $THEME_FILE"
exit 1
fi
echo "Applying theme: $THEME_NAME"
# Create backup of current config
BACKUP_FILE="${ALACRITTY_CONFIG}.backup.$(date +%Y%m%d_%H%M%S)"
cp "$ALACRITTY_CONFIG" "$BACKUP_FILE"
echo "Backup created: $BACKUP_FILE"
# Create a temporary file for the new config
TEMP_CONFIG=$(mktemp)
# Function to merge theme colors into config
merge_theme() {
local config_file="$1"
local theme_file="$2"
local output_file="$3"
# Use yq to merge the theme colors into the config
# If yq is not available, fall back to a simpler approach
if command -v yq >/dev/null 2>&1; then
# Use yq for proper YAML merging
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' "$config_file" "$theme_file" > "$output_file"
else
# Fallback: simple approach that replaces the colors section
# This is less robust but works without yq
awk '
BEGIN { in_colors = 0; colors_printed = 0 }
/^colors:/ {
in_colors = 1
if (!colors_printed) {
print "colors:"
while ((getline line < "'"$theme_file"'") > 0) {
if (line ~ /^colors:/) continue
print " " line
}
close("'"$theme_file"'")
colors_printed = 1
}
next
}
in_colors && /^[a-zA-Z]/ && !/^ / { in_colors = 0 }
!in_colors { print }
' "$config_file" > "$output_file"
fi
}
# Merge the theme into the config
merge_theme "$ALACRITTY_CONFIG" "$THEME_FILE" "$TEMP_CONFIG"
# Replace the original config with the new one
mv "$TEMP_CONFIG" "$ALACRITTY_CONFIG"
echo "Alacritty theme synchronized successfully!"
echo "Config file: $ALACRITTY_CONFIG"
echo "Applied theme: $THEME_NAME"
# Optionally, send a signal to running Alacritty instances to reload config
# This requires Alacritty to be running with live config reload enabled
if command -v osascript >/dev/null 2>&1; then
# Try to reload Alacritty config using AppleScript
osascript -e 'tell application "Alacritty" to quit' 2>/dev/null || true
# Restart Alacritty (you might want to adjust this based on your setup)
open -a Alacritty 2>/dev/null || true
fi
echo "Theme file updated: $THEME_FILE"
echo "Run 'home-manager switch' to apply the new theme to Alacritty"