aboutsummaryrefslogtreecommitdiffstats
path: root/modules/scripts/lib.nix
diff options
context:
space:
mode:
Diffstat (limited to 'modules/scripts/lib.nix')
-rw-r--r--modules/scripts/lib.nix59
1 files changed, 42 insertions, 17 deletions
diff --git a/modules/scripts/lib.nix b/modules/scripts/lib.nix
index 70affba..77c0a20 100644
--- a/modules/scripts/lib.nix
+++ b/modules/scripts/lib.nix
@@ -1,9 +1,7 @@
{ lib, pkgs }:
let
- shell = pkgs.runtimeShell;
-
mkScript =
- name: path: description: env: deps:
+ name: path: description: env: deps: completion:
let
descLines = lib.filter (s: s != "") (lib.splitString "\n" description);
descComment =
@@ -11,17 +9,35 @@ let
envVars = lib.concatStringsSep "\n" (
lib.mapAttrsToList (n: v: "export ${n}=${lib.escapeShellArg (toString v)}") env
);
+ base = pkgs.writeShellApplication {
+ inherit name;
+ runtimeInputs = deps;
+ text = ''
+ #!/usr/bin/env bash
+ ${descComment}${envVars}
+ ${builtins.readFile path}
+ '';
+ };
+ completionDrv =
+ pkgs.runCommand "${name}-completion"
+ {
+ nativeBuildInputs = [ pkgs.installShellFiles ];
+ }
+ ''
+ installShellCompletion --zsh --name _${name} ${pkgs.writeText "_${name}" completion}
+ '';
in
- pkgs.writeShellApplication {
- inherit name;
- runtimeInputs = deps;
- text = ''
- #!${shell}
- ${descComment}${envVars}
- ${builtins.readFile path}
- '';
- };
-
+ if completion == null then
+ base
+ else
+ pkgs.symlinkJoin {
+ name = name;
+ paths = [
+ base
+ completionDrv
+ ];
+ meta = base.meta or { };
+ };
mkScriptModule =
{
name,
@@ -32,9 +48,10 @@ let
extraLinks ? [ ],
config,
description ? "",
+ completion ? null,
}:
let
- scriptDrv = mkScript name path description env deps;
+ scriptDrv = mkScript name path description env deps completion;
in
{
options.scripts.${name} = {
@@ -48,6 +65,13 @@ let
readOnly = true;
description = "The derivation for the ${name} script.";
};
+ completion = {
+ enable = lib.mkOption {
+ type = lib.types.bool;
+ default = completion != null;
+ description = "Whether to install a zsh completion for the ${name} script.";
+ };
+ };
desktop = {
enable = lib.mkOption {
type = lib.types.bool;
@@ -66,7 +90,6 @@ let
};
};
};
-
moduleConfig = {
scripts.${name} = lib.mkIf config.scripts.${name}.enable (
{
@@ -74,7 +97,6 @@ let
}
// lib.optionalAttrs (desktop != null) { inherit desktop; }
);
-
home.file = lib.mkIf config.scripts.${name}.enable (
builtins.listToAttrs (
map (linkPath: {
@@ -85,7 +107,6 @@ let
}) extraLinks
)
);
-
assertions = [
{
assertion =
@@ -94,6 +115,10 @@ let
|| config.terminal.kitty.enable;
message = "scripts.${name}.desktop.enable requires terminal.kitty.enable because desktop entries launch scripts in kitty.";
}
+ {
+ assertion = !config.scripts.${name}.completion.enable || completion != null;
+ message = "scripts.${name}.completion.enable is true but no completion text was provided to mkScriptModule.";
+ }
];
};
};