#!/usr/bin/env bash
# install-gnome.sh — portable Rosé Pine profile for GNOME/Ubuntu desktops.
#
# The first successful run captures a private, one-time dconf snapshot before
# changing anything. Re-running is idempotent and never replaces that pristine
# rollback point. Use --restore (or restore-gnome.sh) to load it again.
set -Eeuo pipefail
THEME_SRC=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
STATE_ROOT=${XDG_STATE_HOME:-"$HOME/.local/state"}/pentest-env
CACHE_ROOT=${XDG_CACHE_HOME:-"$HOME/.cache"}/pentest-env
ROLLBACK_DCONF=$STATE_ROOT/gnome-before-rose-pine.dconf
TERMINAL_PREFS=$HOME/.config/ubuntu-xdg-terminals.list
TERMINAL_BACKUP=$STATE_ROOT/ubuntu-xdg-terminals.list.before-rose-pine
TERMINAL_ABSENT=$STATE_ROOT/ubuntu-xdg-terminals.list.was-absent
GTK_URL=${PE_ROSE_PINE_GTK_URL:-https://github.com/rose-pine/gtk/releases/download/v2.2.0/gtk3.tar.gz}
ICON_URL=${PE_ROSE_PINE_ICON_URL:-https://github.com/rose-pine/gtk/releases/download/v2.2.0/rose-pine-icons.tar.gz}
CURSOR_URL=${PE_ROSE_PINE_CURSOR_URL:-https://github.com/rose-pine/cursor/releases/download/v1.1.0/BreezeX-RosePine-Linux.tar.xz}
NO_DOWNLOAD=0
RESTORE=0
WALLPAPER_SOURCE=$THEME_SRC/wallpapers/rose-pine-htb.png
TMP_WORK=
say() { printf '\033[38;2;156;207;216m[gnome-theme]\033[0m %s\n' "$*"; }
warn() { printf '\033[38;2;246;193;119m[gnome-theme] %s\033[0m\n' "$*" >&2; }
die() { warn "$*"; exit 1; }
usage() {
cat <<'EOF'
Usage: install-gnome.sh [--no-download] [--wallpaper PATH]
install-gnome.sh --restore
--no-download Apply bundled/local assets without fetching GTK themes.
--wallpaper PATH Use PATH instead of the bundled Rosé Pine wallpaper.
--restore Restore the one-time GNOME and terminal preference snapshot.
-h, --help Show this help.
EOF
}
while (( $# )); do
case $1 in
--no-download) NO_DOWNLOAD=1 ;;
--restore) RESTORE=1 ;;
--wallpaper)
(( $# >= 2 )) || die "--wallpaper needs a path"
WALLPAPER_SOURCE=$2
shift
;;
-h|--help) usage; exit 0 ;;
*) die "unknown option: $1" ;;
esac
shift
done
cleanup() {
if [[ -n $TMP_WORK ]]; then
case $TMP_WORK in
"$CACHE_ROOT"/gnome-theme.*)
[[ -d $TMP_WORK ]] && rm -rf -- "$TMP_WORK"
;;
esac
fi
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' HUP TERM
ensure_tmp() {
[[ -n $TMP_WORK ]] && return 0
mkdir -p -- "$CACHE_ROOT"
chmod 700 "$CACHE_ROOT" 2>/dev/null || true
TMP_WORK=$(mktemp -d "$CACHE_ROOT/gnome-theme.XXXXXXXX")
chmod 700 "$TMP_WORK"
}
# The profile is deliberately user-scoped. Running it through sudo would save
# and modify root's dconf database instead of the active desktop user's one.
(( EUID != 0 )) || die "run this as the logged-in desktop user, not with sudo"
[[ $(uname -s) == Linux ]] || { warn "not a Linux host — skipping GNOME theming"; exit 0; }
command -v gsettings >/dev/null 2>&1 || { warn "gsettings is unavailable — skipping GNOME theming"; exit 0; }
command -v dconf >/dev/null 2>&1 || die "dconf is required for safe snapshot and restore support"
FIXED_SCHEMAS=$(gsettings list-schemas 2>/dev/null || true)
RELOCATABLE_SCHEMAS=$(gsettings list-relocatable-schemas 2>/dev/null || true)
schema_exists() {
local base=${1%%:*}
grep -Fqx -- "$base" <<<"$FIXED_SCHEMAS" \
|| grep -Fqx -- "$base" <<<"$RELOCATABLE_SCHEMAS"
}
if ! schema_exists org.gnome.desktop.interface; then
warn "GNOME schemas are unavailable — skipping desktop theming"
exit 0
fi
desktop_hint=${XDG_CURRENT_DESKTOP:-${DESKTOP_SESSION:-}}
desktop_hint=${desktop_hint,,}
if [[ $RESTORE -eq 0 && $desktop_hint != *gnome* && $desktop_hint != *ubuntu* ]]; then
if [[ -n $desktop_hint ]] || ! command -v pgrep >/dev/null 2>&1 \
|| ! pgrep -x gnome-shell >/dev/null 2>&1; then
warn "the active desktop does not appear to be GNOME — skipping desktop theming"
exit 0
fi
fi
key_exists() {
local schema=$1 key=$2
schema_exists "$schema" || return 1
gsettings list-keys "$schema" 2>/dev/null | grep -Fqx -- "$key"
}
gset() {
local schema=$1 key=$2 value=$3 writable
if ! key_exists "$schema" "$key"; then
return 0
fi
writable=$(gsettings writable "$schema" "$key" 2>/dev/null || true)
[[ $writable == true ]] || return 0
if ! gsettings set "$schema" "$key" "$value" 2>/dev/null; then
warn "could not set $schema $key"
fi
}
restore_snapshot() {
[[ -f $ROLLBACK_DCONF ]] || die "no rollback snapshot at $ROLLBACK_DCONF"
ensure_tmp
if ! dconf dump /org/gnome/ > "$TMP_WORK/gnome-before-restore.dconf"; then
die "could not capture a recovery snapshot; restore was not attempted"
fi
# dconf dump omits keys that were at their defaults. Resetting first is
# therefore required for an empty/partial snapshot to be a true rollback.
# If loading the pristine snapshot fails, put the pre-restore state back.
if ! dconf reset -f /org/gnome/ \
|| ! dconf load /org/gnome/ < "$ROLLBACK_DCONF"; then
warn "rollback load failed; attempting to recover the pre-restore settings"
dconf reset -f /org/gnome/ 2>/dev/null || true
dconf load /org/gnome/ < "$TMP_WORK/gnome-before-restore.dconf" 2>/dev/null || true
die "could not restore the GNOME dconf snapshot"
fi
if [[ -f $TERMINAL_BACKUP ]]; then
mkdir -p -- "$(dirname "$TERMINAL_PREFS")"
install -m 600 "$TERMINAL_BACKUP" "$TERMINAL_PREFS"
elif [[ -f $TERMINAL_ABSENT ]]; then
rm -f -- "$TERMINAL_PREFS"
else
warn "terminal preference snapshot is missing; GNOME settings were still restored"
fi
say "restored the original GNOME settings from $ROLLBACK_DCONF"
say "downloaded theme files and the wallpaper copy were retained"
}
if (( RESTORE )); then
restore_snapshot
exit 0
fi
capture_snapshot() {
if [[ -f $ROLLBACK_DCONF ]]; then
say "keeping existing one-time rollback snapshot: $ROLLBACK_DCONF"
return 0
fi
umask 077
mkdir -p -- "$STATE_ROOT"
chmod 700 "$STATE_ROOT"
ensure_tmp
if ! dconf dump /org/gnome/ > "$TMP_WORK/gnome.dconf"; then
die "could not capture GNOME settings; nothing was changed"
fi
# Store the terminal state first and the dconf file last. The dconf file is
# the commit marker, so an interrupted capture can safely be retried.
if [[ -f $TERMINAL_PREFS ]]; then
install -m 600 "$TERMINAL_PREFS" "$TERMINAL_BACKUP"
rm -f -- "$TERMINAL_ABSENT"
else
install -m 600 /dev/null "$TERMINAL_ABSENT"
rm -f -- "$TERMINAL_BACKUP"
fi
install -m 600 "$TMP_WORK/gnome.dconf" "$ROLLBACK_DCONF"
say "saved one-time rollback snapshot: $ROLLBACK_DCONF"
}
fetch() {
local url=$1 output=$2
if command -v curl >/dev/null 2>&1; then
curl -fL --retry 2 --connect-timeout 10 --max-time 240 \
--output "$output" "$url"
elif command -v wget >/dev/null 2>&1; then
wget -q --timeout=20 --tries=2 -O "$output" "$url"
else
return 127
fi
}
find_asset_root() {
local stage=$1 kind=$2 marker root
case $kind in
gtk)
while IFS= read -r marker; do
root=${marker%/gtk-3.0/gtk.css}
printf '%s\n' "$root"
return 0
done < <(find "$stage" -maxdepth 6 -type f -path '*/gtk-3.0/gtk.css' -print)
;;
icon|cursor)
while IFS= read -r marker; do
root=${marker%/index.theme}
printf '%s\n' "$root"
return 0
done < <(find "$stage" -maxdepth 4 -type f -name index.theme -print)
;;
esac
return 1
}
install_release_asset() {
local label=$1 url=$2 kind=$3 parent=$4 name=$5
local destination=$parent/$name archive stage source prepared
if [[ -d $destination ]]; then
say "$label already present — skipping download"
return 0
fi
ensure_tmp
archive=$TMP_WORK/$name.archive
stage=$TMP_WORK/$name.extract
prepared=$TMP_WORK/$name.ready
mkdir -p -- "$stage" "$prepared"
say "fetching pinned $label release"
if ! fetch "$url" "$archive" >/dev/null 2>&1; then
warn "could not download $label; the local fallback will be used"
return 0
fi
case $url in
*.tar.gz|*.tgz) tar -xzf "$archive" -C "$stage" 2>/dev/null || true ;;
*.tar.xz|*.txz) tar -xJf "$archive" -C "$stage" 2>/dev/null || true ;;
*) warn "unsupported archive for $label"; return 0 ;;
esac
source=$(find_asset_root "$stage" "$kind" 2>/dev/null || true)
if [[ -z $source || ! -d $source ]]; then
warn "the $label archive did not contain the expected layout"
return 0
fi
if ! cp -a -- "$source/." "$prepared/"; then
warn "could not stage $label; the local fallback will be used"
return 0
fi
mkdir -p -- "$parent"
if [[ -e $destination ]]; then
say "$label appeared during installation — keeping the existing copy"
return 0
fi
if mv -- "$prepared" "$destination"; then
say "installed $label as $destination"
else
warn "could not install $label; the local fallback will be used"
fi
}
# Keep standalone theme runs as ownership-safe as the top-level installer. The
# snapshot distinguishes a project-managed asset from a file edited after the
# last run; the one-time backup preserves an exact pre-existing path.
install_managed_asset() {
local source=$1 destination=$2 mode=$3
local backup=$destination.before-pentest-env
local snapshot=$destination.pentest-env-managed
if [[ -f $snapshot ]]; then
if [[ -e $destination || -L $destination ]] \
&& { [[ -L $destination || ! -f $destination ]] \
|| ! cmp -s -- "$destination" "$snapshot"; }; then
warn "keeping user-modified desktop asset: $destination"
return 0
fi
elif [[ -e $destination || -L $destination ]]; then
if [[ -e $backup || -L $backup ]]; then
if [[ -L $destination || ! -f $destination ]] \
|| ! cmp -s -- "$destination" "$source"; then
warn "keeping user-modified desktop asset: $destination (original remains at $backup)"
return 0
fi
else
mv -- "$destination" "$backup"
fi
fi
install -m "$mode" "$source" "$destination"
install -m 600 "$source" "$snapshot"
}
capture_snapshot
if (( ! NO_DOWNLOAD )); then
install_release_asset "Rosé Pine GTK" "$GTK_URL" gtk "$HOME/.themes" rose-pine-gtk
install_release_asset "Rosé Pine icons" "$ICON_URL" icon "$HOME/.icons" rose-pine-icons
install_release_asset "Rosé Pine cursor" "$CURSOR_URL" cursor \
"$HOME/.local/share/icons" BreezeX-RosePine-Linux
else
say "theme downloads disabled; using installed themes or system fallbacks"
fi
# Install only project-owned desktop assets. In particular, this does not write
# gtk-4.0 CSS, which avoids fighting libadwaita/GNOME version changes.
APPLICATIONS_DIR=$HOME/.local/share/applications
ICON_DIR=$HOME/.local/share/icons/hicolor/scalable/apps
mkdir -p -- "$APPLICATIONS_DIR" "$ICON_DIR" "$(dirname "$TERMINAL_PREFS")"
install_managed_asset "$THEME_SRC/gnome/htb-workspace.desktop" \
"$APPLICATIONS_DIR/htb-workspace.desktop" 644
install_managed_asset "$THEME_SRC/gnome/htb-workspace.svg" \
"$ICON_DIR/htb-workspace.svg" 644
install -m 600 "$THEME_SRC/gnome/ubuntu-xdg-terminals.list" "$TERMINAL_PREFS"
WALLPAPER_DEST=$HOME/Pictures/Wallpapers/rose-pine-htb.png
if [[ -f $WALLPAPER_SOURCE ]]; then
mkdir -p -- "$(dirname "$WALLPAPER_DEST")"
if [[ $WALLPAPER_SOURCE != "$WALLPAPER_DEST" ]]; then
install -m 644 "$WALLPAPER_SOURCE" "$WALLPAPER_DEST"
fi
else
warn "wallpaper not found: $WALLPAPER_SOURCE"
fi
choose_theme() {
local parent=$1 fallback candidate
shift
fallback=$1
shift
for candidate in "$@"; do
if [[ -d $parent/$candidate || -d /usr/share/themes/$candidate || -d /usr/share/icons/$candidate ]]; then
printf '%s\n' "$candidate"
return 0
fi
done
printf '%s\n' "$fallback"
}
if [[ -d $HOME/.themes/rose-pine-gtk ]]; then
gtk_theme=rose-pine-gtk
else
gtk_theme=$(choose_theme /usr/share/themes Adwaita-dark Yaru-purple-dark Yaru-dark Adwaita-dark Adwaita)
fi
if [[ -d $HOME/.icons/rose-pine-icons ]]; then
icon_theme=rose-pine-icons
else
icon_theme=$(choose_theme /usr/share/icons Yaru Papirus-Dark Yaru-purple-dark Yaru Adwaita)
fi
if [[ -d $HOME/.local/share/icons/BreezeX-RosePine-Linux ]]; then
cursor_theme=BreezeX-RosePine-Linux
else
cursor_theme=$(choose_theme /usr/share/icons Adwaita Yaru Adwaita)
fi
# GNOME / GTK ---------------------------------------------------------------
gset org.gnome.desktop.interface color-scheme prefer-dark
gset org.gnome.desktop.interface accent-color purple
gset org.gnome.desktop.interface gtk-theme "$gtk_theme"
gset org.gnome.desktop.interface icon-theme "$icon_theme"
gset org.gnome.desktop.interface cursor-theme "$cursor_theme"
gset org.gnome.desktop.interface monospace-font-name 'MesloLGS NF 11'
gset org.gnome.desktop.interface clock-show-date true
gset org.gnome.desktop.interface clock-show-weekday true
gset org.gnome.desktop.interface show-battery-percentage true
gset org.gnome.desktop.interface enable-hot-corners false
gset org.gnome.desktop.wm.preferences button-layout ':minimize,maximize,close'
gset org.gnome.desktop.wm.preferences workspace-names "['Web', 'Lab', 'Notes', 'Comms']"
gset org.gnome.mutter center-new-windows true
gset org.gnome.mutter edge-tiling true
# Ubuntu Dock (silently skipped on distributions without dash-to-dock).
dock=org.gnome.shell.extensions.dash-to-dock
gset "$dock" dock-position BOTTOM
gset "$dock" extend-height false
gset "$dock" dock-fixed false
gset "$dock" intellihide true
gset "$dock" autohide true
gset "$dock" dash-max-icon-size 42
gset "$dock" always-center-icons true
gset "$dock" custom-background-color true
gset "$dock" background-color '#191724'
gset "$dock" transparency-mode FIXED
gset "$dock" background-opacity 0.82
gset "$dock" show-mounts false
gset "$dock" show-trash false
gset "$dock" running-indicator-style DOTS
gset "$dock" custom-theme-customize-running-dots true
gset "$dock" custom-theme-running-dots-color '#c4a7e7'
gset "$dock" custom-theme-running-dots-border-color '#c4a7e7'
# Tiling Assistant. Names vary between releases, so every key is probed.
tiling=org.gnome.shell.extensions.tiling-assistant
gset "$tiling" window-gap 8
gset "$tiling" single-screen-gap 8
gset "$tiling" maximize-with-gap true
gset "$tiling" focus-hint-color 'rgb(196,167,231)'
if [[ -f $WALLPAPER_DEST ]]; then
wallpaper_path=$(readlink -f -- "$WALLPAPER_DEST" 2>/dev/null || printf '%s' "$WALLPAPER_DEST")
wallpaper_uri=file://$wallpaper_path
gset org.gnome.desktop.background picture-uri "$wallpaper_uri"
gset org.gnome.desktop.background picture-uri-dark "$wallpaper_uri"
gset org.gnome.desktop.background picture-options zoom
gset org.gnome.desktop.screensaver picture-uri "$wallpaper_uri"
fi
# Keep Ptyxis visually consistent for GNOME actions that launch it directly.
gset org.gnome.desktop.default-applications.terminal exec alacritty
gset org.gnome.desktop.default-applications.terminal exec-arg '-e'
gset org.gnome.Ptyxis use-system-font false
gset org.gnome.Ptyxis font-name 'MesloLGS NF 11'
gset org.gnome.Ptyxis interface-style dark
if key_exists org.gnome.Ptyxis default-profile-uuid; then
ptyxis_uuid=$(gsettings get org.gnome.Ptyxis default-profile-uuid 2>/dev/null | tr -d "'" || true)
if [[ $ptyxis_uuid =~ ^[[:alnum:]-]+$ ]]; then
ptyxis_profile="org.gnome.Ptyxis.Profile:/org/gnome/Ptyxis/Profiles/$ptyxis_uuid/"
gset "$ptyxis_profile" palette 'Rosé Pine'
gset "$ptyxis_profile" opacity 0.96
fi
fi
desktop_file_exists() {
local desktop_id=$1 directory
for directory in \
"$HOME/.local/share/applications" \
/usr/local/share/applications \
/usr/share/applications \
/var/lib/snapd/desktop/applications; do
[[ -f $directory/$desktop_id ]] && return 0
done
return 1
}
first_desktop_file() {
local desktop_id
for desktop_id in "$@"; do
if desktop_file_exists "$desktop_id"; then
printf '%s\n' "$desktop_id"
return 0
fi
done
return 1
}
favorites=()
add_favorite() {
local item=$1 existing
for existing in "${favorites[@]:-}"; do
[[ $existing == "$item" ]] && return 0
done
favorites+=("$item")
}
favorite=$(first_desktop_file firefox_firefox.desktop firefox.desktop org.mozilla.firefox.desktop \
google-chrome.desktop chromium_chromium.desktop chromium.desktop || true)
[[ -n $favorite ]] && add_favorite "$favorite"
favorite=$(first_desktop_file Alacritty.desktop org.gnome.Ptyxis.desktop || true)
[[ -n $favorite ]] && add_favorite "$favorite"
favorite=$(first_desktop_file org.gnome.Nautilus.desktop thunar.desktop nemo.desktop || true)
[[ -n $favorite ]] && add_favorite "$favorite"
add_favorite htb-workspace.desktop
favorite=$(first_desktop_file org.wireshark.Wireshark.desktop wireshark.desktop || true)
[[ -n $favorite ]] && add_favorite "$favorite"
if (( ${#favorites[@]} )); then
favorite_literal='['
for favorite in "${favorites[@]}"; do
[[ $favorite_literal == '[' ]] || favorite_literal+=', '
favorite_literal+="'$favorite'"
done
favorite_literal+=']'
gset org.gnome.shell favorite-apps "$favorite_literal"
fi
# Merge our two shortcuts into any custom shortcuts already configured.
media=org.gnome.settings-daemon.plugins.media-keys
terminal_path=/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/rose-pine-terminal/
htb_path=/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/htb-workspace/
if key_exists "$media" custom-keybindings; then
custom_bindings=$(gsettings get "$media" custom-keybindings 2>/dev/null || printf '[]')
[[ $custom_bindings == '@as []' ]] && custom_bindings='[]'
for binding_path in "$terminal_path" "$htb_path"; do
if [[ $custom_bindings != *"'$binding_path'"* ]]; then
if [[ $custom_bindings == '[]' ]]; then
custom_bindings="['$binding_path']"
else
custom_bindings=${custom_bindings%]}
custom_bindings+=", '$binding_path']"
fi
fi
done
gset "$media" custom-keybindings "$custom_bindings"
fi
terminal_schema="org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:$terminal_path"
gset "$terminal_schema" name 'Terminal'
gset "$terminal_schema" command alacritty
gset "$terminal_schema" binding '<Super>Return'
htb_schema="org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:$htb_path"
gset "$htb_schema" name 'HTB Workspace'
gset "$htb_schema" command 'alacritty --class HTBWorkspace -e htb-session'
gset "$htb_schema" binding '<Super><Shift>h'
command -v update-desktop-database >/dev/null 2>&1 \
&& update-desktop-database "$APPLICATIONS_DIR" >/dev/null 2>&1 || true
command -v gtk-update-icon-cache >/dev/null 2>&1 \
&& gtk-update-icon-cache -q -f "$HOME/.local/share/icons/hicolor" >/dev/null 2>&1 || true
command -v notify-send >/dev/null 2>&1 \
&& notify-send 'Rosé Pine' 'GNOME desktop profile applied' >/dev/null 2>&1 || true
say "GNOME profile applied (GTK: $gtk_theme, icons: $icon_theme, cursor: $cursor_theme)"
say "shortcuts: Super+Enter (terminal), Super+Shift+H (HTB workspace)"
say "restore at any time with: $THEME_SRC/restore-gnome.sh"