From 192c2330d08ff2dda2ff0a7986fec9cb1e60d27b Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Sun, 21 Jan 2018 02:12:40 +0700 Subject: nixos/less configure less with module --- nixos/modules/module-list.nix | 1 + nixos/modules/programs/less.nix | 123 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 nixos/modules/programs/less.nix (limited to 'nixos/modules') diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index bb3abc256fc1..611318fc5a1e 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 000000000000..a7f2290764a5 --- /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 ]; + +} -- cgit 1.4.1