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 +++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 28 deletions(-) (limited to 'modules/scripts/default.nix') 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"; + } )) ]; }; -- cgit v1.2.3