Created
July 10, 2026 07:18
-
-
Save 0x77dev/73aad6adff4d5a7090f774ff12409809 to your computer and use it in GitHub Desktop.
Add a location to GNOME Weather via nominatim — NixOS-friendly nix-shell shebang rewrite of leo-joao/add-location-gnome-weather
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env nix-shell | |
| #!nix-shell -i bash -p bash curl jq bc glib | |
| # Add a location to GNOME Weather (and the GNOME Shell weather widget). | |
| # NixOS-friendly rewrite of: | |
| # https://github.com/leo-joao/add-location-gnome-weather/blob/main/addlocation.sh | |
| # | |
| # Usage: ./addlocation.sh [search terms...] | |
| set -euo pipefail | |
| if ! command -v gnome-weather >/dev/null 2>&1; then | |
| echo "GNOME Weather isn't installed" >&2 | |
| exit 1 | |
| fi | |
| # On NixOS the org.gnome.Weather gschema is not in gsettings' default search | |
| # path; it lives next to the package in the system profile. | |
| schemas_dir=$(ls -d /run/current-system/sw/share/gsettings-schemas/gnome-weather-* 2>/dev/null | head -1 || true) | |
| if [[ -n "$schemas_dir" ]]; then | |
| export XDG_DATA_DIRS="$schemas_dir:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}" | |
| fi | |
| language=$(locale | sed -n 's/^LANG=\([^_]*\).*/\1/p') | |
| if [[ $# -gt 0 ]]; then | |
| query="$*" | |
| else | |
| read -rp "Type the name of the location you want to add to GNOME Weather: " query | |
| fi | |
| request=$(curl -sG "https://nominatim.openstreetmap.org/search" \ | |
| --data-urlencode "q=$query" \ | |
| --data-urlencode "format=json" \ | |
| --data-urlencode "limit=1" \ | |
| -H "Accept-Language: ${language:-en}" \ | |
| -A "addlocation.sh (gnome-weather helper)") | |
| if [[ $(jq 'length' <<<"$request") -eq 0 ]]; then | |
| echo "No locations found, consider removing some search terms" >&2 | |
| exit 1 | |
| fi | |
| display_name=$(jq -r '.[0].display_name' <<<"$request") | |
| name=$(jq -r '.[0].name' <<<"$request") | |
| read -rp "If this is not the location you wanted, consider adding search terms | |
| Are you sure you want to add \"$display_name\"? [y/n]: " answer | |
| if [[ "$answer" != "y" ]]; then | |
| echo "Not adding location" | |
| exit 0 | |
| fi | |
| # GNOME Weather stores coordinates in radians. | |
| lat=$(bc -l <<<"$(jq -r '.[0].lat' <<<"$request") * 4 * a(1) / 180") | |
| lon=$(bc -l <<<"$(jq -r '.[0].lon' <<<"$request") * 4 * a(1) / 180") | |
| location="<(uint32 2, <('$name', '', false, [($lat, $lon)], @a(dd) [])>)>" | |
| add_to_schema() { | |
| local schema=$1 | |
| local current | |
| current=$(gsettings get "$schema" locations) | |
| if [[ "$current" == "@av []" ]]; then | |
| gsettings set "$schema" locations "[$location]" | |
| elif [[ "$current" == *"'$name'"* ]]; then | |
| echo "$schema already contains '$name', skipping" | |
| return | |
| else | |
| gsettings set "$schema" locations "${current%]}, $location]" | |
| fi | |
| echo "Added '$name' to $schema" | |
| } | |
| add_to_schema org.gnome.Weather | |
| add_to_schema org.gnome.shell.weather |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment