|
| 1 | +--- |
| 2 | +# Desktop Entry Cleanup - Hide duplicate and replaced apps from menus |
| 3 | +# |
| 4 | +# Strategy: |
| 5 | +# 1. Hide Nautilus (Files) when Nemo is installed (Nemo replaces it) |
| 6 | +# 2. Detect duplicate Flatpak apps that also have apt versions and hide the Flatpak ones |
| 7 | +# |
| 8 | +# We use user-local overrides (~/.local/share/applications/) with NoDisplay=true |
| 9 | +# This preserves the original .desktop files and survives package updates |
| 10 | + |
| 11 | +# ============================================================================ |
| 12 | +# HIDE NAUTILUS (replaced by Nemo) |
| 13 | +# ============================================================================ |
| 14 | + |
| 15 | +- name: Check if Nemo is installed |
| 16 | + ansible.builtin.stat: |
| 17 | + path: /usr/bin/nemo |
| 18 | + register: nemo_installed |
| 19 | + when: ansible_facts['distribution'] in ['Fedora', 'Ubuntu'] |
| 20 | + |
| 21 | +- name: Ensure user applications directory exists |
| 22 | + ansible.builtin.file: |
| 23 | + path: "{{ user_home }}/.local/share/applications" |
| 24 | + state: directory |
| 25 | + owner: "{{ actual_user }}" |
| 26 | + mode: '0755' |
| 27 | + when: |
| 28 | + - ansible_facts['distribution'] in ['Fedora', 'Ubuntu'] |
| 29 | + - nemo_installed.stat.exists | default(false) |
| 30 | + |
| 31 | +# Hide Nautilus from menus (user still has full Nautilus functionality if needed) |
| 32 | +- name: Hide Nautilus Files from menus (Nemo replaces it) |
| 33 | + ansible.builtin.copy: |
| 34 | + dest: "{{ user_home }}/.local/share/applications/org.gnome.Nautilus.desktop" |
| 35 | + content: | |
| 36 | + [Desktop Entry] |
| 37 | + Type=Application |
| 38 | + Name=Files |
| 39 | + NoDisplay=true |
| 40 | + Hidden=true |
| 41 | + owner: "{{ actual_user }}" |
| 42 | + mode: '0644' |
| 43 | + when: |
| 44 | + - ansible_facts['distribution'] in ['Fedora', 'Ubuntu'] |
| 45 | + - nemo_installed.stat.exists | default(false) |
| 46 | + |
| 47 | +# ============================================================================ |
| 48 | +# HIDE DUPLICATE FLATPAK APPS (when apt version exists) |
| 49 | +# ============================================================================ |
| 50 | + |
| 51 | +# Get list of Flatpak desktop files |
| 52 | +- name: Get Flatpak application desktop files |
| 53 | + ansible.builtin.find: |
| 54 | + paths: /var/lib/flatpak/exports/share/applications |
| 55 | + patterns: "*.desktop" |
| 56 | + register: flatpak_desktop_files |
| 57 | + when: ansible_facts['distribution'] in ['Fedora', 'Ubuntu'] |
| 58 | + |
| 59 | +# For each Flatpak app, check if an equivalent apt app exists |
| 60 | +# Common pattern: Flatpak uses org.foo.Bar.desktop, apt uses foo-bar.desktop |
| 61 | +- name: Check for duplicate apt versions of Flatpak apps |
| 62 | + ansible.builtin.shell: | |
| 63 | + # Get app name from Flatpak desktop file (e.g., org.onlyoffice.desktopeditors) |
| 64 | + flatpak_name="{{ item.path | basename | regex_replace('\\.desktop$', '') }}" |
| 65 | +
|
| 66 | + # Extract the last part as the likely app name (e.g., desktopeditors) |
| 67 | + app_simple=$(echo "$flatpak_name" | rev | cut -d. -f1 | rev) |
| 68 | +
|
| 69 | + # Search for matching apt .desktop file |
| 70 | + for apt_file in /usr/share/applications/*.desktop; do |
| 71 | + [ -f "$apt_file" ] || continue |
| 72 | + apt_name=$(basename "$apt_file" .desktop) |
| 73 | +
|
| 74 | + # Match if apt_name contains the app_simple name (case insensitive) |
| 75 | + if echo "$apt_name" | grep -qi "$app_simple"; then |
| 76 | + # Found duplicate - output the Flatpak desktop filename to hide |
| 77 | + echo "{{ item.path | basename }}" |
| 78 | + exit 0 |
| 79 | + fi |
| 80 | + done |
| 81 | +
|
| 82 | + # No duplicate found |
| 83 | + exit 0 |
| 84 | + register: duplicate_check |
| 85 | + loop: "{{ flatpak_desktop_files.files | default([]) }}" |
| 86 | + changed_when: false |
| 87 | + when: |
| 88 | + - ansible_facts['distribution'] in ['Fedora', 'Ubuntu'] |
| 89 | + - flatpak_desktop_files.files | default([]) | length > 0 |
| 90 | + |
| 91 | +# Build list of Flatpak desktop files to hide |
| 92 | +- name: Build list of duplicate Flatpak apps to hide |
| 93 | + ansible.builtin.set_fact: |
| 94 | + flatpak_duplicates: "{{ duplicate_check.results | default([]) | map(attribute='stdout') | select('match', '.+\\.desktop$') | list }}" |
| 95 | + when: ansible_facts['distribution'] in ['Fedora', 'Ubuntu'] |
| 96 | + |
| 97 | +# Hide each duplicate Flatpak app |
| 98 | +- name: Hide duplicate Flatpak apps (apt version exists) |
| 99 | + ansible.builtin.copy: |
| 100 | + dest: "{{ user_home }}/.local/share/applications/{{ item }}" |
| 101 | + content: | |
| 102 | + [Desktop Entry] |
| 103 | + Type=Application |
| 104 | + Name={{ item | regex_replace('\\.desktop$', '') }} |
| 105 | + NoDisplay=true |
| 106 | + Hidden=true |
| 107 | + owner: "{{ actual_user }}" |
| 108 | + mode: '0644' |
| 109 | + loop: "{{ flatpak_duplicates | default([]) }}" |
| 110 | + when: |
| 111 | + - ansible_facts['distribution'] in ['Fedora', 'Ubuntu'] |
| 112 | + - flatpak_duplicates | default([]) | length > 0 |
| 113 | + |
| 114 | +- name: Display hidden duplicate apps |
| 115 | + ansible.builtin.debug: |
| 116 | + msg: | |
| 117 | + Desktop cleanup complete: |
| 118 | + - Nautilus hidden: {{ nemo_installed.stat.exists | default(false) }} |
| 119 | + - Flatpak duplicates hidden: {{ flatpak_duplicates | default([]) | join(', ') | default('none') }} |
| 120 | + when: ansible_facts['distribution'] in ['Fedora', 'Ubuntu'] |
0 commit comments