From db6534a1eb6f4f2ba9881fcd430a3ba6e5c3621b Mon Sep 17 00:00:00 2001 From: Brian Nelson Date: Wed, 25 Feb 2026 12:28:00 -0500 Subject: [PATCH] modules --- modules/config.nix | 6 +++++ modules/deep-dive/default.nix | 18 +++++++++++++ modules/deep-dive/eval.nix | 10 +++++++ modules/deep-dive/geocode.sh | 50 +++++++++++++++++++++++++++++++++++ modules/deep-dive/map.sh | 47 ++++++++++++++++++++++++++++++++ modules/default.nix | 10 +++++++ modules/options.nix | 7 +++++ 7 files changed, 148 insertions(+) create mode 100644 modules/config.nix create mode 100644 modules/deep-dive/default.nix create mode 100644 modules/deep-dive/eval.nix create mode 100644 modules/deep-dive/geocode.sh create mode 100644 modules/deep-dive/map.sh create mode 100644 modules/default.nix create mode 100644 modules/options.nix diff --git a/modules/config.nix b/modules/config.nix new file mode 100644 index 0000000..f4e8f87 --- /dev/null +++ b/modules/config.nix @@ -0,0 +1,6 @@ +{ ... }: +{ + config = { + name = "Boaty McBoatface"; + }; +} \ No newline at end of file diff --git a/modules/deep-dive/default.nix b/modules/deep-dive/default.nix new file mode 100644 index 0000000..6e57160 --- /dev/null +++ b/modules/deep-dive/default.nix @@ -0,0 +1,18 @@ +{ pkgs, lib, ... }: { + + options = { + scripts.output = lib.mkOption { + type = lib.types.package; + }; + }; + + config = { + scripts.output = pkgs.writeShellApplication { + name = "map"; + runtimeInputs = with pkgs; [ curl feh ]; + text = '' + ${./map.sh} size=640x640 scale=2 | feh - + ''; + }; + }; +} \ No newline at end of file diff --git a/modules/deep-dive/eval.nix b/modules/deep-dive/eval.nix new file mode 100644 index 0000000..3f67e69 --- /dev/null +++ b/modules/deep-dive/eval.nix @@ -0,0 +1,10 @@ +let + nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-25.11"; + pkgs = import nixpkgs { config = {}; overlays = []; }; +in +pkgs.lib.evalModules { + modules = [ + ({ config, ... }: { config._module.args = { inherit pkgs; }; }) + ./default.nix + ]; +} \ No newline at end of file diff --git a/modules/deep-dive/geocode.sh b/modules/deep-dive/geocode.sh new file mode 100644 index 0000000..d3ffc72 --- /dev/null +++ b/modules/deep-dive/geocode.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +set -euo pipefail + +rational_regex='-?[[:digit:]]+(\.[[:digit:]]+)?' +result_regex="$rational_regex,$rational_regex" + +keyFile=${XDG_DATA_HOME:-~/.local/share}/google-api/key + +if [[ ! -f "$keyFile" ]]; then + mkdir -p "$(basename "$keyFile")" + echo "No Google API key found in $keyFile" >&2 + echo "For getting one, see https://developers.google.com/maps/documentation/geocoding/overview#before-you-begin" >&2 + exit 1 +fi + +key=$(cat "$keyFile") + +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' exit + +output=$tmp/output + +curlArgs=( + https://maps.googleapis.com/maps/api/geocode/json + --silent --show-error --get --output "$output" --write-out '%{http_code}' + --data-urlencode address="$1" +) + +#echo curl ''${curlArgs[@]@Q} >&2 + +curlArgs+=(--data-urlencode key="$key") + +if status=$(curl "${curlArgs[@]}"); then + if [[ "$status" == 200 ]]; then + result=$(jq -r '.results[0].geometry.location as $loc | "\($loc | .lat),\($loc | .lng)"' "$output") + if ! [[ $result =~ $result_regex ]]; then + echo "Got a bad result of: '$result'" >&2 + exit 1 + else + echo "$result" + fi + else + echo "API returned non-200 HTTP status code $status, output is" >&2 + cat "$output" >&2 + exit 1 + fi +else + echo "curl exited with code $?" >&2 + exit 1 +fi diff --git a/modules/deep-dive/map.sh b/modules/deep-dive/map.sh new file mode 100644 index 0000000..d6698c3 --- /dev/null +++ b/modules/deep-dive/map.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +set -euo pipefail + +keyFile=${XDG_DATA_HOME:-~/.local/share}/google-api/key + +if [[ ! -f "$keyFile" ]]; then + mkdir -p "$(basename "$keyFile")" + echo "No Google API key found in $keyFile" >&2 + echo "For getting one, see https://developers.google.com/maps/documentation/maps-static/start#before-you-begin" >&2 + exit 1 +fi + +key=$(cat "$keyFile") +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' exit + +output=$tmp/output + +curlArgs=( + https://maps.googleapis.com/maps/api/staticmap + --silent --show-error --get --output "$output" --write-out %{http_code} +) + +for arg in "$@"; do + curlArgs+=(--data-urlencode "$arg") +done + +echo curl ''${curlArgs[@]@Q} >&2 + +curlArgs+=(--data-urlencode key="$key") + +if status=$(curl "${curlArgs[@]}"); then + if [[ "$status" != 200 ]]; then + echo "API returned non-200 HTTP status code $status, output is" >&2 + cat "$output" >&2 + exit 1 + fi +else + echo "curl exited with code $?" >&2 + exit 1 +fi + +if [[ -t 1 ]]; then + echo "Successful, but won't output image to tty, pipe to a file or icat instead" >&2 +else + cat "$output" +fi diff --git a/modules/default.nix b/modules/default.nix new file mode 100644 index 0000000..55c9f2b --- /dev/null +++ b/modules/default.nix @@ -0,0 +1,10 @@ +let + pkgs = import { }; + result = pkgs.lib.evalModules { + modules = [ + ./options.nix + ./config.nix + ]; + }; +in +result.config \ No newline at end of file diff --git a/modules/options.nix b/modules/options.nix new file mode 100644 index 0000000..977ce23 --- /dev/null +++ b/modules/options.nix @@ -0,0 +1,7 @@ +{ lib, ... }: + +{ + options = { + name = lib.mkOption { type = lib.types.str; }; + }; +} \ No newline at end of file