Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions modules/programs/less.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ in
options = lib.mkOption {
type =
with lib.types;
attrsOf (oneOf [
bool
int
str
]);
let
scalar = oneOf [
bool
int
str
];
in
attrsOf (either scalar (listOf scalar));
default = { };
description = "GNU-style options to be set via {env}`$LESS`.";
example = {
Expand All @@ -59,9 +62,33 @@ in
xdg.configFile."lesskey" = lib.mkIf (cfg.config != "") { text = cfg.config; };

programs.less.config = lib.mkIf (cfg.options != { }) (
let
color = lib.intersectAttrs {
color = null;
D = null;
} cfg.options;
prompt = lib.intersectAttrs {
prompt = null;
P = null;
} cfg.options;
otherOptions = lib.removeAttrs cfg.options [
"color"
"D"
"P"
"prompt"
];

toCommandLine = lib.cli.toGNUCommandLineShell { };

orderedOptions = lib.filter (x: x != { }) [
otherOptions
color # colors need to come after `--use-color`.
prompt # the prompt has to be the last option.
];
in
lib.mkBefore ''
#env
LESS = ${lib.cli.toGNUCommandLineShell { } cfg.options}
LESS = ${lib.concatMapStringsSep " " toCommandLine orderedOptions}
''
);
};
Expand Down
21 changes: 21 additions & 0 deletions tests/modules/programs/less/correct-option-order.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
programs.less = {
enable = true;
options = {
color = [
"HkK" # header: gray
"Mkb" # marks: blue
];
prompt = "s%f";
quiet = true;
};
};

nmt.script = ''
assertFileExists home-files/.config/lesskey
assertFileContent home-files/.config/lesskey ${builtins.toFile "lesskey.expected" ''
#env
LESS = --quiet --color HkK --color Mkb --prompt s%f
''}
'';
}
1 change: 1 addition & 0 deletions tests/modules/programs/less/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
less-correct-option-order = ./correct-option-order.nix;
less-custom-config = ./custom-config.nix;
less-custom-options = ./custom-options.nix;
less-custom-options-and-config = ./custom-options-and-config.nix;
Expand Down