From e87c356c8104c7c016c41fe477d0654056847343 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Mon, 13 Apr 2026 16:38:55 +0530 Subject: injecting variables into scripts so that they use the actual path of th binaries of the packages --- modules/scripts/default.nix | 78 ++++++++++++++++++++++++------------- modules/scripts/screen-recording.sh | 6 +-- modules/scripts/system-monitor.sh | 24 ++++++------ modules/scripts/vpn-connect.sh | 11 +++--- 4 files changed, 70 insertions(+), 49 deletions(-) diff --git a/modules/scripts/default.nix b/modules/scripts/default.nix index c312800..a3aa54e 100644 --- a/modules/scripts/default.nix +++ b/modules/scripts/default.nix @@ -5,46 +5,68 @@ ... }: -# Scripts - Home directory shell scripts (deep-clean, screen-recording, system-monitor) let + # name: Script name + # path: Path to script file + # env: (Optional) Attribute set of environment variables { KEY = "VALUE"; } mkScript = - name: path: - pkgs.writeShellScriptBin name '' - #!${ - if config.tools.zsh.enable then (lib.getExe config.programs.zsh.package) else "#!/usr/bin/env sh" - } - ${builtins.readFile path} - ''; + name: path: env: + let + # Generate the shell shebang based on your config + shell = + if config.tools.zsh.enable then (lib.getExe config.programs.zsh.package) else "/usr/bin/env sh"; + + # Convert the 'env' attribute set into a string of "export KEY='VALUE'" lines + envVars = lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v: "${n}=\"${toString v}\"") env); + in + pkgs.writeTextFile { + name = name; + executable = true; + destination = "/bin/${name}"; + text = '' + #!${shell} + ${envVars} + ${builtins.readFile path} + ''; + }; in { config = { home.packages = [ - # Always included - (mkScript "deep-clean" ./deep-clean.sh) + # Always included (pass empty set {} if no env vars needed) + (mkScript "deep-clean" ./deep-clean.sh { }) - # Conditional scripts (requies some tools) + # Conditional scripts (lib.mkIf (config.tools.ffmpeg.enable && config.tools.slop.enable) ( - mkScript "screen-recording" ./screen-recording.sh + mkScript "screen-recording" ./screen-recording.sh { + FFMPEG = lib.getExe pkgs.ffmpeg-full; + SLOP = lib.getExe pkgs.slop; + } )) - (lib.mkIf ( - config.tools.bottom.enable - && config.tools.nvtop.enable - && config.tools.tmux.enable - && config.tools.kitty.enable - ) (mkScript "system-monitor" ./system-monitor.sh)) + (lib.mkIf + ( + config.tools.bottom.enable + && config.tools.nvtop.enable + && config.tools.tmux.enable + && config.tools.kitty.enable + ) + ( + mkScript "system-monitor" ./system-monitor.sh { + BTM = lib.getExe pkgs.bottom; + NVTOP = lib.getExe pkgs.nvtopPackages.full; + TMUX = lib.getExe pkgs.tmux; + KITTY = lib.getExe pkgs.kitty; + } + ) + ) (lib.mkIf (config.tools.openvpn.enable && config.tools.fzf.enable) ( - pkgs.writeShellScriptBin "vpn-connect" '' - #!${if config.tools.zsh.enable then (lib.getExe config.programs.zsh.package) else "/usr/bin/env sh"} - - # Inject Nix store paths as environment variables for the script to use - export FZF_BIN="${lib.getExe pkgs.fzf}" - export OPENVPN_BIN="${lib.getExe pkgs.openvpn}" - export RESOLVED_BIN="${pkgs.openvpn}/libexec/update-systemd-resolved" - - ${builtins.readFile ./vpn-connect.sh} - '' + mkScript "vpn-connect" ./vpn-connect.sh { + FZF = lib.getExe pkgs.fzf; + OPENVPN = lib.getExe pkgs.openvpn; + SYSTEMD_RESOLVED = "${pkgs.openvpn}/libexec/update-systemd-resolved"; + } )) ]; }; diff --git a/modules/scripts/screen-recording.sh b/modules/scripts/screen-recording.sh index c0e8525..5bd5ee9 100644 --- a/modules/scripts/screen-recording.sh +++ b/modules/scripts/screen-recording.sh @@ -34,10 +34,10 @@ if command -v xwininfo >/dev/null 2>&1; then fi if [ "$SELECT_MODE" = true ]; then - if command -v slop >/dev/null 2>&1; 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) + read -r W H X Y < <("$SLOP" -f "%w %h %x %y" --nokeyboard) if [ -n "$W" ]; then WIDTH=$W @@ -67,7 +67,7 @@ echo "File: $FILENAME" echo "------------------------------------------------" # We pass arguments explicitly here to avoid "Unrecognized option" shell errors -ffmpeg -hide_banner \ +"$FFMPEG" -hide_banner \ -f x11grab \ -video_size "${WIDTH}x${HEIGHT}" \ -framerate 30 \ diff --git a/modules/scripts/system-monitor.sh b/modules/scripts/system-monitor.sh index cadae3a..4fd59ac 100644 --- a/modules/scripts/system-monitor.sh +++ b/modules/scripts/system-monitor.sh @@ -4,26 +4,26 @@ # - Right pane: nvtop (GPU monitor) SESSION="system-monitor" -kitty @ set-font-size -- -4 +"$KITTY" @ set-font-size -- -4 cleanup() { - kitty @ set-font-size -- +4 + "$KITTY" @ set-font-size -- +4 } trap cleanup EXIT INT TERM # Kill any existing session -tmux kill-session -t "$SESSION" 2>/dev/null +"$TMUX" kill-session -t "$SESSION" 2>/dev/null -# Start new tmux session with btm wrapped to kill session on exit -tmux new-session -d -s "$SESSION" -c "$HOME" "bash -c 'btm; tmux kill-session -t $SESSION'" +# Start new "$TMUX" session with $BTM wrapped to kill session on exit +"$TMUX" new-session -d -s "$SESSION" -c "$HOME" "bash -c '\"$BTM\"; \"$TMUX\" kill-session -t $SESSION'" -# Split horizontally: right pane runs nvtop wrapped similarly -tmux split-window -h -t "$SESSION:0" -c "$HOME" "bash -c 'nvtop; tmux kill-session -t $SESSION'" +# Split horizontally: right pane runs $NVTOP wrapped similarly +"$TMUX" split-window -h -t "$SESSION:0" -c "$HOME" "bash -c '\"$NVTOP\"; \"$TMUX\" kill-session -t $SESSION'" -tmux set -g status off -tmux set -g mouse on -tmux set -g focus-events on +"$TMUX" set -g status off +"$TMUX" set -g mouse on +"$TMUX" set -g focus-events on # Select main pane and attach -tmux select-pane -t "$SESSION:0.0" -tmux attach-session -t "$SESSION" +"$TMUX" select-pane -t "$SESSION:0.0" +"$TMUX" attach-session -t "$SESSION" diff --git a/modules/scripts/vpn-connect.sh b/modules/scripts/vpn-connect.sh index 0366725..885796a 100644 --- a/modules/scripts/vpn-connect.sh +++ b/modules/scripts/vpn-connect.sh @@ -24,11 +24,10 @@ fi # Selection logic if [[ "$REFRESH" == true ]]; then - # These variables ($FZF_BIN, etc) are provided by the Nix wrapper below - CRED=$(find "$CREDS_DIR" -type f | "$FZF_BIN" --prompt="Select Credentials > ") + CRED=$(find "$CREDS_DIR" -type f | "$FZF" --prompt="Select Credentials > ") [ -z "$CRED" ] && exit 1 - SERVER=$(find "$SERVERS_DIR" -type f \( -name "*.conf" -o -name "*.ovpn" \) | "$FZF_BIN" --prompt="Select VPN Server > ") + SERVER=$(find "$SERVERS_DIR" -type f \( -name "*.conf" -o -name "*.ovpn" \) | "$FZF" --prompt="Select VPN Server > ") [ -z "$SERVER" ] && exit 1 echo "CRED=\"$CRED\"" > "$CACHE_FILE" @@ -49,10 +48,10 @@ sed -e '/up \/etc\/openvpn\/update-resolv-conf/d' \ trap 'rm -f "$TMP_CONF"' EXIT # Execute openvpn using the temporary file -sudo "$OPENVPN_BIN" \ +sudo "$OPENVPN" \ --config "$TMP_CONF" \ --auth-user-pass "$CRED" \ --script-security 2 \ - --up "$RESOLVED_BIN" \ - --down "$RESOLVED_BIN" \ + --up "$SYSTEMD_RESOLVED" \ + --down "$SYSTEMD_RESOLVED" \ --down-pre -- cgit v1.2.3