aboutsummaryrefslogtreecommitdiffstats
path: root/modules/scripts/default.nix
diff options
context:
space:
mode:
authorArpit Chakladar <arpitchakladar@proton.me>2026-04-23 00:07:57 +0530
committerArpit Chakladar <arpitchakladar@proton.me>2026-04-23 00:07:57 +0530
commit5ba4319dd246e018abdfd7df8c491bdef907ca1c (patch)
tree68717b976e91153a652d117a34b1a1820ad31e3d /modules/scripts/default.nix
parentd2e4e49f6264639c497949f89d81182775eef598 (diff)
downloadhome-manager-config-5ba4319dd246e018abdfd7df8c491bdef907ca1c.tar.gz
home-manager-config-5ba4319dd246e018abdfd7df8c491bdef907ca1c.zip
restructured how scripts are created
Diffstat (limited to 'modules/scripts/default.nix')
-rw-r--r--modules/scripts/default.nix278
1 files changed, 207 insertions, 71 deletions
diff --git a/modules/scripts/default.nix b/modules/scripts/default.nix
index 47c6084..9c7bba8 100644
--- a/modules/scripts/default.nix
+++ b/modules/scripts/default.nix
@@ -9,101 +9,237 @@ let
if config.tools.zsh.enable then (lib.getExe config.programs.zsh.package) else "/usr/bin/env sh";
mkScript =
- name: path: env:
+ name: path: env: deps:
let
envVars = lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v: "${n}=\"${toString v}\"") env);
+ wrappedScript = pkgs.writeTextFile {
+ name = name;
+ executable = true;
+ destination = "/bin/${name}";
+ text = ''
+ #!${shell}
+ ${envVars}
+ ${builtins.readFile path}
+ '';
+ };
in
- pkgs.writeTextFile {
+ pkgs.symlinkJoin {
name = name;
- executable = true;
- destination = "/bin/${name}";
- text = ''
- #!${shell}
- ${envVars}
- ${builtins.readFile path}
+ paths = [ wrappedScript ] ++ deps;
+ buildInputs = [ pkgs.makeWrapper ];
+ postBuild = ''
+ wrapProgram $out/bin/${name} \
+ --prefix PATH : ${lib.makeBinPath deps}
'';
+ meta.mainProgram = name;
};
- scripts = {
- deep-clean = mkScript "deep-clean" ./deep-clean.sh { };
-
- file-preview = lib.mkIf config.tools.fzf.enable (
- mkScript "file-preview" ./file-preview.sh {
- }
- );
-
- file-preview-clean = lib.mkIf config.tools.fzf.enable (
- mkScript "file-preview-clean" ./file-preview-clean.sh {
- }
- );
-
- fzf-launcher = lib.mkIf config.tools.fzf.enable (
- mkScript "fzf-launcher" ./fzf-launcher.sh {
+ # Script definitions: { path, env, deps, condition }
+ # condition: attrset of { option (string path), value (expected value) }
+ scriptDefs = {
+ deep-clean = {
+ path = ./deep-clean.sh;
+ env = { };
+ deps = [ ];
+ conditions = [ ];
+ };
+ file-preview = {
+ path = ./file-preview.sh;
+ env = { };
+ deps = [
+ pkgs.bat
+ pkgs.file
+ pkgs.ffmpeg-full
+ pkgs.ouch
+ pkgs.poppler-utils
+ ];
+ conditions = [
+ {
+ option = "tools.fzf.enable";
+ value = true;
+ }
+ ];
+ };
+ file-preview-clean = {
+ path = ./file-preview-clean.sh;
+ env = { };
+ deps = [ pkgs.fzf ];
+ conditions = [
+ {
+ option = "tools.fzf.enable";
+ value = true;
+ }
+ ];
+ };
+ fzf-launcher = {
+ path = ./fzf-launcher.sh;
+ env = {
FZF = lib.getExe pkgs.fzf;
SETSID = lib.getExe' pkgs.util-linux "setsid";
- }
- );
-
- i3-keybindings = lib.mkIf config.desktop.enable (
- mkScript "i3-keybindings" ./i3-keybindings.sh {
-
- }
- );
-
- screen-recording = lib.mkIf (config.tools.ffmpeg.enable && config.tools.slop.enable) (
- mkScript "screen-recording" ./screen-recording.sh {
+ };
+ deps = [
+ pkgs.fzf
+ pkgs.util-linux
+ ];
+ conditions = [
+ {
+ option = "tools.fzf.enable";
+ value = true;
+ }
+ ];
+ };
+ i3-keybindings = {
+ path = ./i3-keybindings.sh;
+ env = { };
+ deps = [ ];
+ conditions = [
+ {
+ option = "desktop.enable";
+ value = true;
+ }
+ ];
+ };
+ screen-recording = {
+ path = ./screen-recording.sh;
+ env = {
FFMPEG = lib.getExe pkgs.ffmpeg-full;
SLOP = lib.getExe pkgs.slop;
- }
- );
-
- system-monitor =
- 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;
- }
- );
-
- vpn-connect = lib.mkIf (config.tools.openvpn.enable && config.tools.fzf.enable) (
- mkScript "vpn-connect" ./vpn-connect.sh {
+ };
+ deps = [
+ pkgs.ffmpeg-full
+ pkgs.slop
+ ];
+ conditions = [
+ {
+ option = "tools.ffmpeg.enable";
+ value = true;
+ }
+ {
+ option = "tools.slop.enable";
+ value = true;
+ }
+ ];
+ };
+ system-monitor = {
+ path = ./system-monitor.sh;
+ env = {
+ BTM = lib.getExe pkgs.bottom;
+ NVTOP = lib.getExe pkgs.nvtopPackages.full;
+ TMUX = lib.getExe pkgs.tmux;
+ KITTY = lib.getExe pkgs.kitty;
+ };
+ deps = [
+ pkgs.bottom
+ pkgs.nvtopPackages.full
+ pkgs.tmux
+ pkgs.kitty
+ ];
+ conditions = [
+ {
+ option = "tools.bottom.enable";
+ value = true;
+ }
+ {
+ option = "tools.nvtop.enable";
+ value = true;
+ }
+ {
+ option = "tools.tmux.enable";
+ value = true;
+ }
+ {
+ option = "tools.kitty.enable";
+ value = true;
+ }
+ ];
+ };
+ vpn-connect = {
+ path = ./vpn-connect.sh;
+ env = {
FZF = lib.getExe pkgs.fzf;
OPENVPN = lib.getExe pkgs.openvpn;
SYSTEMD_RESOLVED = "${pkgs.openvpn}/libexec/update-systemd-resolved";
- }
- );
-
- vpn-disconnect = lib.mkIf (config.tools.openvpn.enable && config.tools.fzf.enable) (
- mkScript "vpn-disconnect" ./vpn-disconnect.sh { }
- );
+ };
+ deps = [
+ pkgs.fzf
+ pkgs.openvpn
+ ];
+ conditions = [
+ {
+ option = "tools.openvpn.enable";
+ value = true;
+ }
+ {
+ option = "tools.fzf.enable";
+ value = true;
+ }
+ ];
+ };
+ vpn-disconnect = {
+ path = ./vpn-disconnect.sh;
+ env = { };
+ deps = [ ];
+ conditions = [
+ {
+ option = "tools.openvpn.enable";
+ value = true;
+ }
+ {
+ option = "tools.fzf.enable";
+ value = true;
+ }
+ ];
+ };
};
+
+ # Check whether all conditions for a script are satisfied
+ conditionsMet =
+ name: def:
+ lib.all (
+ cond: lib.attrByPath (lib.splitString "." cond.option) false config == cond.value
+ ) def.conditions;
+
in
{
- options.scripts = lib.mapAttrs (
- name: _:
- lib.mkOption {
+ options.scripts = lib.mapAttrs (name: def: {
+ enable = lib.mkOption {
+ type = lib.types.bool;
+ # Auto-enable if all required tool options are satisfied
+ default = conditionsMet name def;
+ description = ''
+ Whether to enable the ${name} script.
+ Defaults to true when all required options are set.
+ '';
+ };
+ package = lib.mkOption {
type = lib.types.nullOr lib.types.package;
- default = null;
- description = "Derivation for the ${name} script, or null if disabled.";
- }
- ) scripts;
+ readOnly = true;
+ description = "The derivation for the ${name} script. Set automatically when enabled.";
+ };
+ }) scriptDefs;
config = {
- scripts = scripts;
+ # Wire up .package for each enabled script, with assertions for unmet conditions
+ scripts = lib.mapAttrs (name: def: {
+ package = lib.mkIf config.scripts.${name}.enable (mkScript name def.path def.env def.deps);
+ }) scriptDefs;
+
+ assertions = lib.concatLists (
+ lib.mapAttrsToList (
+ name: def:
+ map (cond: {
+ assertion =
+ !config.scripts.${name}.enable
+ || lib.attrByPath (lib.splitString "." cond.option) false config == cond.value;
+ message = "scripts.${name} is enabled but requires `${cond.option} = ${builtins.toJSON cond.value}`.";
+ }) def.conditions
+ ) scriptDefs
+ );
home.packages = [
pkgs.file
pkgs.bat
]
- ++ (lib.filter (x: x != null) (lib.mapAttrsToList (_: v: lib.mkIf true v) scripts));
+ ++ lib.filter (x: x != null) (lib.mapAttrsToList (_: s: s.package) config.scripts);
};
}