From 192c2330d08ff2dda2ff0a7986fec9cb1e60d27b Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Sun, 21 Jan 2018 02:12:40 +0700 Subject: [PATCH 1/2] nixos/less configure less with module --- lib/maintainers.nix | 1 + nixos/modules/module-list.nix | 1 + nixos/modules/programs/less.nix | 123 ++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 nixos/modules/programs/less.nix diff --git a/lib/maintainers.nix b/lib/maintainers.nix index 65718e31897..e1fdc3a6e5a 100644 --- a/lib/maintainers.nix +++ b/lib/maintainers.nix @@ -325,6 +325,7 @@ joelmo = "Joel Moberg "; joelteon = "Joel Taylor "; johbo = "Johannes Bornhold "; + johnazoidberg = "Daniel Schäfer "; johnmh = "John M. Harris, Jr. "; johnramsden = "John Ramsden "; joko = "Ioannis Koutras "; diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index bb3abc256fc..611318fc5a1 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -84,6 +84,7 @@ ./programs/info.nix ./programs/java.nix ./programs/kbdlight.nix + ./programs/less.nix ./programs/light.nix ./programs/man.nix ./programs/mosh.nix diff --git a/nixos/modules/programs/less.nix b/nixos/modules/programs/less.nix new file mode 100644 index 00000000000..a7f2290764a --- /dev/null +++ b/nixos/modules/programs/less.nix @@ -0,0 +1,123 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.programs.less; + + configFile = '' + #command + ${concatStringsSep "\n" + (mapAttrsToList (command: action: "${command} ${action}") cfg.commands) + } + ${if cfg.clearDefaultCommands then "#stop" else ""} + + #line-edit + ${concatStringsSep "\n" + (mapAttrsToList (command: action: "${command} ${action}") cfg.lineEditingKeys) + } + + #env + ${concatStringsSep "\n" + (mapAttrsToList (variable: values: "${variable}=${values}") cfg.envVariables) + } + ''; + + lessKey = pkgs.runCommand "lesskey" + { src = pkgs.writeText "lessconfig" configFile; } + "${pkgs.less}/bin/lesskey -o $out $src"; + + lessPipe = pkgs.writeScriptBin "lesspipe.sh" '' + #! /bin/sh + case "$1" in + *.gz) + ${pkgs.gzip}/bin/gunzip --stdout "$1" 2>/dev/null + ;; + *.xz) + ${pkgs.xz}/bin/unxz --stdout "$1" 2>/dev/null + ;; + *.bz2) + ${pkgs.bzip2}/bin/bunzip2 --stdout "$1" 2>/dev/null + ;; + *) exit 1 + ;; + esac + exit $? + ''; + +in + +{ + options = { + + programs.less = { + + enable = mkEnableOption "less"; + + commands = mkOption { + type = types.attrsOf types.str; + default = {}; + example = { + "h" = "noaction 5\e("; + "l" = "noaction 5\e)"; + }; + description = "Defines new command keys."; + }; + + clearDefaultCommands = mkOption { + type = types.bool; + default = false; + description = '' + 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 = mkOption { + type = types.attrsOf types.str; + default = {}; + example = { + "\e" = "abort"; + }; + description = "Defines new line-editing keys."; + }; + + envVariables = mkOption { + type = types.attrsOf types.str; + default = {}; + example = { + LESS = "--quit-if-one-screen"; + }; + description = "Defines environment variables."; + }; + + autoExtract = mkOption { + type = types.bool; + default = true; + description = '' + When enabled less automatically extracts .gz .xz .bz2 files before reading them. + ''; + }; + }; + }; + + config = mkIf cfg.enable { + + environment.systemPackages = [ pkgs.less ]; + + environment.variables."LESSKEY_SYSTEM" = toString lessKey; + environment.variables."LESSOPEN" = "|${lessPipe}/bin/lesspipe.sh %s"; + + warnings = optional ( + cfg.clearDefaultCommands && (all (x: x != "quit") (attrValues cfg.commands)) + ) '' + 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 maintainers; [ johnazoidberg ]; + +} From 288898d6f1dc532e1dfb8785a591a2d8ad55320b Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Mon, 29 Jan 2018 22:08:32 +0700 Subject: [PATCH 2/2] nixos/less: use lesspipe package for preprocessing Rather than a custom script the less config now uses the lesspipe package config by default. --- nixos/modules/programs/less.nix | 43 +++++++++++++++------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/nixos/modules/programs/less.nix b/nixos/modules/programs/less.nix index a7f2290764a..c0283c9e686 100644 --- a/nixos/modules/programs/less.nix +++ b/nixos/modules/programs/less.nix @@ -28,24 +28,6 @@ let { src = pkgs.writeText "lessconfig" configFile; } "${pkgs.less}/bin/lesskey -o $out $src"; - lessPipe = pkgs.writeScriptBin "lesspipe.sh" '' - #! /bin/sh - case "$1" in - *.gz) - ${pkgs.gzip}/bin/gunzip --stdout "$1" 2>/dev/null - ;; - *.xz) - ${pkgs.xz}/bin/unxz --stdout "$1" 2>/dev/null - ;; - *.bz2) - ${pkgs.bzip2}/bin/bunzip2 --stdout "$1" 2>/dev/null - ;; - *) exit 1 - ;; - esac - exit $? - ''; - in { @@ -93,11 +75,19 @@ in description = "Defines environment variables."; }; - autoExtract = mkOption { - type = types.bool; - default = true; + lessopen = mkOption { + type = types.nullOr types.str; + default = "|${pkgs.lesspipe}/bin/lesspipe.sh %s"; description = '' - When enabled less automatically extracts .gz .xz .bz2 files before reading them. + 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 = mkOption { + type = types.nullOr types.str; + default = null; + description = '' + 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). ''; }; }; @@ -107,8 +97,13 @@ in environment.systemPackages = [ pkgs.less ]; - environment.variables."LESSKEY_SYSTEM" = toString lessKey; - environment.variables."LESSOPEN" = "|${lessPipe}/bin/lesspipe.sh %s"; + environment.variables = { + "LESSKEY_SYSTEM" = toString lessKey; + } // optionalAttrs (cfg.lessopen != null) { + "LESSOPEN" = cfg.lessopen; + } // optionalAttrs (cfg.lessclose != null) { + "LESSCLOSE" = cfg.lessclose; + }; warnings = optional ( cfg.clearDefaultCommands && (all (x: x != "quit") (attrValues cfg.commands))