diff options
| author | Arpit Chakladar <arpitchakladar+git@gmail.com> | 2026-07-18 14:55:21 +0530 |
|---|---|---|
| committer | Arpit Chakladar <arpitchakladar+git@gmail.com> | 2026-07-18 14:55:21 +0530 |
| commit | 03bd9b6ad5335aff65d9cb6fb1d117b5cb4b7919 (patch) | |
| tree | 04780c0d23aac335fe60320c181bd3ac6850d5d3 /modules/scripts/lib.nix | |
| parent | bde84587b2a408970086f4c708ac9a629abfa554 (diff) | |
| download | home-manager-config-03bd9b6ad5335aff65d9cb6fb1d117b5cb4b7919.tar.gz home-manager-config-03bd9b6ad5335aff65d9cb6fb1d117b5cb4b7919.zip | |
refactor(scripts): split monolithic module into per-script submodules
Extract shared `mkScript` and `mkScriptModule` helpers into `lib.nix`
Split each script into `modules/scripts/<name>/` 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 `# <text>` 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
Diffstat (limited to 'modules/scripts/lib.nix')
| -rw-r--r-- | modules/scripts/lib.nix | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/modules/scripts/lib.nix b/modules/scripts/lib.nix new file mode 100644 index 0000000..8b04b5f --- /dev/null +++ b/modules/scripts/lib.nix @@ -0,0 +1,94 @@ +{ lib, pkgs }: +let + shell = pkgs.runtimeShell; + + mkScript = + name: path: description: env: deps: + let + descLines = lib.filter (s: s != "") (lib.splitString "\n" description); + descComment = + if descLines == [ ] then "" else lib.concatMapStringsSep "\n" (line: "# ${line}") descLines + "\n"; + envVars = lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v: "${n}=\"${toString v}\"") env); + wrappedScript = pkgs.writeTextFile { + name = name; + executable = true; + destination = "/bin/${name}"; + text = '' + #!${shell} + ${descComment}${envVars} + ${builtins.readFile path} + ''; + }; + in + pkgs.runCommand name + { + nativeBuildInputs = [ pkgs.makeWrapper ]; + meta.mainProgram = name; + } + '' + mkdir -p $out/bin + cp ${wrappedScript}/bin/${name} $out/bin/${name} + chmod +x $out/bin/${name} + + wrapProgram $out/bin/${name} \ + --prefix PATH : ${lib.makeBinPath deps} + ''; + + mkScriptModule = + { + name, + path, + env ? { }, + deps ? [ ], + desktop ? null, + config, + description ? "", + }: + { + options.scripts.${name} = { + enable = lib.mkOption { + type = lib.types.bool; + default = true; + description = "Whether to enable the ${name} script."; + }; + package = lib.mkOption { + type = lib.types.nullOr lib.types.package; + readOnly = true; + description = "The derivation for the ${name} script."; + }; + desktop = { + enable = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Whether to create a .desktop entry for this script."; + }; + displayName = lib.mkOption { + type = lib.types.str; + default = name; + description = "Display name for the .desktop entry."; + }; + }; + }; + + moduleConfig = { + scripts.${name} = lib.mkIf config.scripts.${name}.enable ( + { + package = mkScript name path description env deps; + } + // lib.optionalAttrs (desktop != null) { inherit desktop; } + ); + assertions = [ + { + assertion = + !config.scripts.${name}.enable + || !config.scripts.${name}.desktop.enable + || config.terminal.kitty.enable; + message = "scripts.${name}.desktop.enable requires terminal.kitty.enable because desktop entries launch scripts in kitty."; + } + ]; + }; + }; +in +{ + inherit mkScript mkScriptModule; +} |
