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/screen-recording/assertions.nix | 11 ++++ modules/scripts/screen-recording/default.nix | 29 ++++++++++ modules/scripts/screen-recording/script.sh | 77 +++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 modules/scripts/screen-recording/assertions.nix create mode 100644 modules/scripts/screen-recording/default.nix create mode 100644 modules/scripts/screen-recording/script.sh (limited to 'modules/scripts/screen-recording') diff --git a/modules/scripts/screen-recording/assertions.nix b/modules/scripts/screen-recording/assertions.nix new file mode 100644 index 0000000..ad4d1eb --- /dev/null +++ b/modules/scripts/screen-recording/assertions.nix @@ -0,0 +1,11 @@ +{ config, ... }: +[ + { + assertion = !config.scripts.screen-recording.enable || config.media.ffmpeg.enable; + message = "scripts.screen-recording is enabled but requires `media.ffmpeg.enable`."; + } + { + assertion = !config.scripts.screen-recording.enable || config.media.slop.enable; + message = "scripts.screen-recording is enabled but requires `media.slop.enable`."; + } +] diff --git a/modules/scripts/screen-recording/default.nix b/modules/scripts/screen-recording/default.nix new file mode 100644 index 0000000..f687d36 --- /dev/null +++ b/modules/scripts/screen-recording/default.nix @@ -0,0 +1,29 @@ +{ + config, + lib, + pkgs, + ... +}: +let + inherit ((import ../lib.nix { inherit lib pkgs; })) mkScriptModule; + base = mkScriptModule { + name = "screen-recording"; + path = ./script.sh; + description = "Screen recording script using FFmpeg and slop for region selection\nRecords X11 screen to MP4 with x264 encoding\nUsage: screen-recording.sh [-s|--select]\n -s, --select Launch slop to select recording region"; + deps = [ + config.media.ffmpeg.package + config.media.slop.package + ]; + desktop = { + enable = true; + displayName = "Screen Recording"; + }; + inherit config; + }; +in +{ + options = base.options; + config = base.moduleConfig // { + assertions = base.moduleConfig.assertions ++ (import ./assertions.nix { inherit config lib; }); + }; +} diff --git a/modules/scripts/screen-recording/script.sh b/modules/scripts/screen-recording/script.sh new file mode 100644 index 0000000..a3bf86a --- /dev/null +++ b/modules/scripts/screen-recording/script.sh @@ -0,0 +1,77 @@ +OUT_DIR="$HOME/Videos/Recordings" +mkdir -p "$OUT_DIR" + +# Colors +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +RED='\033[0;31m' +NC='\033[0m' + +# --- 1. ARGUMENT PARSING --- +SELECT_MODE=false +while [[ "$#" -gt 0 ]]; do + case $1 in + -s|--select) SELECT_MODE=true ;; + esac + shift +done + +# --- 2. GEOMETRY SELECTION --- +# Default to a standard 1080p fallback if detection fails +WIDTH=1920 +HEIGHT=1080 +X_OFF=0 +Y_OFF=0 + +# Try to get actual root window size if xwininfo exists +if command -v xwininfo >/dev/null 2>&1; then + WIDTH=$(xwininfo -root | grep 'Width:' | awk '{print $2}') + HEIGHT=$(xwininfo -root | grep 'Height:' | awk '{print $2}') +fi + +if [ "$SELECT_MODE" = true ]; then + if command -v slop >/dev/null 2>&1; then + echo -e "${YELLOW}Select a window or draw a box...${NC}" + # slop output: 123 456 10 20 + read -r W H X Y < <(slop -f "%w %h %x %y" --nokeyboard) + + if [ -n "$W" ]; then + WIDTH=$W + HEIGHT=$H + X_OFF=$X + Y_OFF=$Y + else + echo "Selection cancelled. Exiting." + exit 1 + fi + else + echo -e "${RED}Error: 'slop' not found.${NC} Recording full screen..." + sleep 1 + fi +fi + +# Ensure even dimensions (FFmpeg/x264 requirement) +WIDTH=$(( WIDTH + WIDTH % 2 )) +HEIGHT=$(( HEIGHT + HEIGHT % 2 )) + +# --- 3. START RECORDING --- +FILENAME="$OUT_DIR/recording-$(date +%Y%m%d-%H%M%S).mp4" + +echo -e "${GREEN}Recording started!${NC}" +echo -e "Press ${YELLOW}'q'${NC} in this terminal to stop." +echo "File: $FILENAME" +echo "------------------------------------------------" + +# We pass arguments explicitly here to avoid "Unrecognized option" shell errors +ffmpeg -hide_banner \ + -f x11grab \ + -video_size "${WIDTH}x${HEIGHT}" \ + -framerate 30 \ + -i "${DISPLAY:-:0.0}+${X_OFF},${Y_OFF}" \ + -c:v libx264 \ + -preset ultrafast \ + -crf 23 \ + -pix_fmt yuv420p \ + "$FILENAME" + +echo -e "\n${GREEN}Done!${NC} Video saved to $FILENAME" -- cgit v1.2.3