@@ -0,0 +1,123 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+
7+ cfg = config.programs.less;
8+
9+ configFile = ''
10+ #command
11+ ${concatStringsSep "\n"
12+ (mapAttrsToList (command: action: "${command} ${action}") cfg.commands)
13+ }
14+ ${if cfg.clearDefaultCommands then "#stop" else ""}
15+
16+ #line-edit
17+ ${concatStringsSep "\n"
18+ (mapAttrsToList (command: action: "${command} ${action}") cfg.lineEditingKeys)
19+ }
20+
21+ #env
22+ ${concatStringsSep "\n"
23+ (mapAttrsToList (variable: values: "${variable}=${values}") cfg.envVariables)
24+ }
25+ '';
26+
27+ lessKey = pkgs.runCommand "lesskey"
28+ { src = pkgs.writeText "lessconfig" configFile; }
29+ "${pkgs.less}/bin/lesskey -o $out $src";
30+
31+ lessPipe = pkgs.writeScriptBin "lesspipe.sh" ''
32+ #! /bin/sh
33+ case "$1" in
34+ *.gz)
35+ ${pkgs.gzip}/bin/gunzip --stdout "$1" 2>/dev/null
36+ ;;
37+ *.xz)
38+ ${pkgs.xz}/bin/unxz --stdout "$1" 2>/dev/null
39+ ;;
40+ *.bz2)
41+ ${pkgs.bzip2}/bin/bunzip2 --stdout "$1" 2>/dev/null
42+ ;;
43+ *) exit 1
44+ ;;
45+ esac
46+ exit $?
47+ '';
48+
49+in
50+
51+{
52+ options = {
53+
54+ programs.less = {
55+
56+ enable = mkEnableOption "less";
57+
58+ commands = mkOption {
59+ type = types.attrsOf types.str;
60+ default = {};
61+ example = {
62+ "h" = "noaction 5\e(";
63+ "l" = "noaction 5\e)";
64+ };
65+ description = "Defines new command keys.";
66+ };
67+
68+ clearDefaultCommands = mkOption {
69+ type = types.bool;
70+ default = false;
71+ description = ''
72+ Clear all default commands.
73+ You should remember to set the quit key.
74+ Otherwise you will not be able to leave less without killing it.
75+ '';
76+ };
77+
78+ lineEditingKeys = mkOption {
79+ type = types.attrsOf types.str;
80+ default = {};
81+ example = {
82+ "\e" = "abort";
83+ };
84+ description = "Defines new line-editing keys.";
85+ };
86+
87+ envVariables = mkOption {
88+ type = types.attrsOf types.str;
89+ default = {};
90+ example = {
91+ LESS = "--quit-if-one-screen";
92+ };
93+ description = "Defines environment variables.";
94+ };
95+
96+ autoExtract = mkOption {
97+ type = types.bool;
98+ default = true;
99+ description = ''
100+ When enabled less automatically extracts .gz .xz .bz2 files before reading them.
101+ '';
102+ };
103+ };
104+ };
105+
106+ config = mkIf cfg.enable {
107+
108+ environment.systemPackages = [ pkgs.less ];
109+
110+ environment.variables."LESSKEY_SYSTEM" = toString lessKey;
111+ environment.variables."LESSOPEN" = "|${lessPipe}/bin/lesspipe.sh %s";
112+
113+ warnings = optional (
114+ cfg.clearDefaultCommands && (all (x: x != "quit") (attrValues cfg.commands))
115+ ) ''
116+ config.programs.less.clearDefaultCommands clears all default commands of less but there is no alternative binding for exiting.
117+ Consider adding a binding for 'quit'.
118+ '';
119+ };
120+
121+ meta.maintainers = with maintainers; [ johnazoidberg ];
122+
123+}