summary refs log tree commit diff
path: root/nixos/modules/programs
diff options
context:
space:
mode:
authorDaniel Schaefer <git@danielschaefer.me>2018-01-21 02:12:40 +0700
committerDaniel Schaefer <git@danielschaefer.me>2018-01-29 18:40:22 +0700
commit192c2330d08ff2dda2ff0a7986fec9cb1e60d27b (patch)
tree26b498dba7151d73ad3a927f5b1cb8c245a003d2 /nixos/modules/programs
parent959e9336ee4607be142cf8879c98dff6560e3cd0 (diff)
downloadnixlib-192c2330d08ff2dda2ff0a7986fec9cb1e60d27b.tar
nixlib-192c2330d08ff2dda2ff0a7986fec9cb1e60d27b.tar.gz
nixlib-192c2330d08ff2dda2ff0a7986fec9cb1e60d27b.tar.bz2
nixlib-192c2330d08ff2dda2ff0a7986fec9cb1e60d27b.tar.lz
nixlib-192c2330d08ff2dda2ff0a7986fec9cb1e60d27b.tar.xz
nixlib-192c2330d08ff2dda2ff0a7986fec9cb1e60d27b.tar.zst
nixlib-192c2330d08ff2dda2ff0a7986fec9cb1e60d27b.zip
nixos/less configure less with module
Diffstat (limited to 'nixos/modules/programs')
-rw-r--r--nixos/modules/programs/less.nix123
1 files changed, 123 insertions, 0 deletions
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 ];
+
+}