nix/lib/modules/scripts.nix
2023-05-07 09:19:14 +01:00

73 lines
1.7 KiB
Nix

{ user }: { lib, pkgs, config, ... }:
with lib;
let
scripts = config.programs.scripts;
hotkeyType = types.submodule ({...}: {
options = {
key = mkOption {
description = "the key";
};
modifier = mkOption {
description = "modifiers for the key";
default = "SUPER";
};
args = mkOption {
description = "args for the script when use this key";
default = "";
};
};
});
scriptType = types.submodule ({...}: {
options = {
name = mkOption {
description = "name of the executable";
};
text = mkOption {
description = "the text of the script";
};
hotkeys = mkOption {
type = types.listOf hotkeyType;
description = "keys that run this script";
default = [];
};
install = mkOption {
type = types.bool;
default = true;
};
};
});
scriptToPackage = script:
pkgs.writeShellScriptBin script.name script.text;
installScripts = scripts:
(map scriptToPackage (filter ( s: s.install ) scripts));
bindScript = script: concatStrings (
map ( hotkey: ''
bind = ${hotkey.modifier}, ${hotkey.key}, exec, ${scriptToPackage script}/bin/${script.name} ${hotkey.args}
'' ) script.hotkeys);
bindScripts = scripts:
"# === USER SCRIPTS MODULE ===\n"
+ concatStringsSep "\n" (map bindScript scripts);
in
{
options.programs = {
scripts = mkOption {
type = types.listOf scriptType;
default = [];
};
};
config = {
home-manager.users.${user}.imports = [{
home.packages = installScripts scripts;
wayland.windowManager.hyprland.extraConfig = bindScripts scripts;
}];
};
}