nixpkgs/nixos/modules/programs/less.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

149 lines
4 KiB
Nix
Raw Normal View History

2018-01-21 02:12:40 +07:00
{
config,
lib,
pkgs,
...
}:
let
cfg = config.programs.less;
configText =
if (cfg.configFile != null) then
(builtins.readFile cfg.configFile)
else
''
2018-01-21 02:12:40 +07:00
#command
${builtins.concatStringsSep "\n" (
lib.mapAttrsToList (command: action: "${command} ${action}") cfg.commands
2018-01-21 02:12:40 +07:00
)}
${lib.optionalString cfg.clearDefaultCommands "#stop"}
2018-01-21 02:12:40 +07:00
#line-edit
${builtins.concatStringsSep "\n" (
lib.mapAttrsToList (command: action: "${command} ${action}") cfg.lineEditingKeys
2018-01-21 02:12:40 +07:00
)}
2018-01-21 02:12:40 +07:00
#env
${builtins.concatStringsSep "\n" (
lib.mapAttrsToList (variable: values: "${variable}=${values}") cfg.envVariables
2018-01-21 02:12:40 +07:00
)}
'';
lessKey = pkgs.writeText "lessconfig" configText;
2018-01-21 02:12:40 +07:00
in
{
options = {
programs.less = {
# note that environment.nix sets PAGER=less, and
# therefore also enables this module
enable = lib.mkEnableOption "less, a file pager";
2018-01-21 02:12:40 +07:00
package = lib.mkPackageOption pkgs "less" { };
configFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
example = lib.literalExpression ''"''${pkgs.my-configs}/lesskey"'';
description = ''
Path to lesskey configuration file.
{option}`configFile` takes precedence over {option}`commands`,
{option}`clearDefaultCommands`, {option}`lineEditingKeys`, and
{option}`envVariables`.
'';
};
commands = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
2018-01-21 02:12:40 +07:00
default = { };
example = {
2019-09-17 00:18:14 +00:00
h = "noaction 5\\e(";
l = "noaction 5\\e)";
2018-01-21 02:12:40 +07:00
};
description = "Defines new command keys.";
2018-01-21 02:12:40 +07:00
};
clearDefaultCommands = lib.mkOption {
type = lib.types.bool;
2018-01-21 02:12:40 +07:00
default = false;
description = ''
2018-01-21 02:12:40 +07:00
Clear all default commands.
You should remember to set the quit key.
Otherwise you will not be able to leave less without killing it.
'';
};
lineEditingKeys = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
2018-01-21 02:12:40 +07:00
default = { };
example = {
2019-08-13 21:52:01 +00:00
e = "abort";
2018-01-21 02:12:40 +07:00
};
description = "Defines new line-editing keys.";
2018-01-21 02:12:40 +07:00
};
envVariables = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
default = {
LESS = "-R";
};
2018-01-21 02:12:40 +07:00
example = {
LESS = "--quit-if-one-screen";
};
description = "Defines environment variables.";
2018-01-21 02:12:40 +07:00
};
lessopen = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = lib.literalExpression ''"|''${pkgs.lesspipe}/bin/lesspipe.sh %s"'';
description = ''
Before less opens a file, it first gives your input preprocessor a chance to modify the way the contents of the file are displayed.
'';
};
lessclose = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
2022-11-07 17:51:51 +01:00
When less closes a file opened in such a way, it will call another program, called the input postprocessor,
which may perform any desired clean-up action (such as deleting the replacement file created by LESSOPEN).
2018-01-21 02:12:40 +07:00
'';
};
};
};
config = lib.mkIf cfg.enable {
2018-01-21 02:12:40 +07:00
environment.systemPackages = [ cfg.package ];
2018-01-21 02:12:40 +07:00
environment.variables =
{
LESSKEYIN_SYSTEM = builtins.toString lessKey;
}
// lib.optionalAttrs (cfg.lessopen != null) {
2019-08-13 21:52:01 +00:00
LESSOPEN = cfg.lessopen;
}
// lib.optionalAttrs (cfg.lessclose != null) {
2019-08-13 21:52:01 +00:00
LESSCLOSE = cfg.lessclose;
};
2018-01-21 02:12:40 +07:00
warnings =
lib.optional
(cfg.clearDefaultCommands && (builtins.all (x: x != "quit") (builtins.attrValues cfg.commands)))
2018-01-21 02:12:40 +07:00
''
config.programs.less.clearDefaultCommands clears all default commands of less but there is no alternative binding for exiting.
Consider adding a binding for 'quit'.
'';
};
meta.maintainers = with lib.maintainers; [ johnazoidberg ];
2018-01-21 02:12:40 +07:00
}