From 03bd9b6ad5335aff65d9cb6fb1d117b5cb4b7919 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Sat, 18 Jul 2026 14:55:21 +0530 Subject: refactor(scripts): split monolithic module into per-script submodules Extract shared `mkScript` and `mkScriptModule` helpers into `lib.nix` Split each script into `modules/scripts//` with its own `default.nix`, `script.sh`, and `assertions.nix` Remove `conditions` parameter from `mkScriptModule`; move condition assertions into per-script `assertions.nix` files with inline checks and error messages Simplify `mkScriptModule` to return `{ options, moduleConfig }` instead of a module function, taking `config` as a direct parameter Add `description` field to `mkScript` that inserts the description as a `# ` comment between the shebang and env vars Thread `description` through `mkScriptModule` to each `default.nix` Move top comment lines from each `script.sh` into the `description` field and remove from sources to avoid duplication Remove old flat `.sh` files from `modules/scripts/` root --- modules/scripts/neomutt-sync/assertions.nix | 7 ++++ modules/scripts/neomutt-sync/default.nix | 30 ++++++++++++++ modules/scripts/neomutt-sync/script.sh | 61 +++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 modules/scripts/neomutt-sync/assertions.nix create mode 100644 modules/scripts/neomutt-sync/default.nix create mode 100644 modules/scripts/neomutt-sync/script.sh (limited to 'modules/scripts/neomutt-sync') diff --git a/modules/scripts/neomutt-sync/assertions.nix b/modules/scripts/neomutt-sync/assertions.nix new file mode 100644 index 0000000..38f48e4 --- /dev/null +++ b/modules/scripts/neomutt-sync/assertions.nix @@ -0,0 +1,7 @@ +{ config, ... }: +[ + { + assertion = !config.scripts.neomutt-sync.enable || config.communication.neomutt.enable; + message = "scripts.neomutt-sync is enabled but requires `communication.neomutt.enable`."; + } +] diff --git a/modules/scripts/neomutt-sync/default.nix b/modules/scripts/neomutt-sync/default.nix new file mode 100644 index 0000000..816c9f7 --- /dev/null +++ b/modules/scripts/neomutt-sync/default.nix @@ -0,0 +1,30 @@ +{ + config, + lib, + pkgs, + ... +}: +let + inherit ((import ../lib.nix { inherit lib pkgs; })) mkScriptModule; + base = mkScriptModule { + name = "neomutt-sync"; + path = ./script.sh; + description = "Neomutt-sync - Interactive mail sync with dialog progress bar"; + deps = [ + pkgs.dialog + pkgs.coreutils + pkgs.gawk + pkgs.gnused + pkgs.util-linux + config.programs.mbsync.package + config.programs.notmuch.package + ]; + inherit config; + }; +in +{ + options = base.options; + config = base.moduleConfig // { + assertions = base.moduleConfig.assertions ++ (import ./assertions.nix { inherit config lib; }); + }; +} diff --git a/modules/scripts/neomutt-sync/script.sh b/modules/scripts/neomutt-sync/script.sh new file mode 100644 index 0000000..96d866d --- /dev/null +++ b/modules/scripts/neomutt-sync/script.sh @@ -0,0 +1,61 @@ +export DIALOGRC=$(mktemp) +NOTMUCH_LOG=$(mktemp) +TITLE="[SYNCING MAIL]" +BACK_TITLE= +trap 'rm -f "$DIALOGRC" "$NOTMUCH_LOG"' EXIT # Clean up everything on exit + +cat << 'EOF' > "$DIALOGRC" +use_shadow = OFF +use_colors = ON +screen_color = (WHITE,BLACK,OFF) +dialog_color = (WHITE,BLACK,OFF) +title_color = (CYAN,BLACK,ON) +gauge_color = (WHITE,BLACK,ON) +border_color = (WHITE,BLACK,OFF) +border2_color = (WHITE,BLACK,OFF) +shadow_color = (BLACK,BLACK,OFF) + +# Active buttons: Blue text and border with no background +button_active_color = (BLUE,BLACK,ON) +button_key_active_color = (BLUE,BLACK,ON) +button_label_active_color = (BLUE,BLACK,ON) + +# Inactive buttons: Yellow text and border with no background +button_inactive_color = (YELLOW,BLACK,OFF) +button_key_inactive_color = (YELLOW,BLACK,OFF) +button_label_inactive_color = (YELLOW,BLACK,OFF) +EOF + +# 2. Run the sync and indexing progress bar +( + # --- PHASE 1: mbsync --- + script -q -e -c "mbsync -a" /dev/null | awk -v RS='\r' ' + { + c_str = "" + b_str = "" + for(i=1; i<=NF; i++) { + if ($i == "C:") c_str = $(i+1) + if ($i == "B:") b_str = $(i+1) + } + if (b_str != "") { + split(b_str, arr, "/") + if (arr[2] > 0) { + percent = int((arr[1] / arr[2]) * 100) + printf "XXX\n%d\nSyncing | Channel: %s | Mailbox: %s\nXXX\n", percent, c_str, b_str + } + } + fflush() + }' + + # --- PHASE 2: notmuch --- + printf "XXX\n100\nIndexing new mail with notmuch...\nXXX\n" + notmuch new > "$NOTMUCH_LOG" 2>&1 +) | dialog --title "$TITLE" --gauge "Initializing..." 8 80 0 + +# 3. Format the captured notmuch output into a single clean line +CLEAN_OUT=$(tr '\n' ' ' < "$NOTMUCH_LOG" | sed 's/ */ /g') + +# 4. Display final results and wait for user input (Enter/Esc) to dismiss +dialog --title "$TITLE" --msgbox "Done!\n$CLEAN_OUT" 8 80 + +clear -- cgit v1.2.3