Skip to content

Commit 6a6086d

Browse files
committed
zsh: add options zsh.history.{appendInc,appendIncTime}
This adds the ZSH options `INC_APPEND_HISTORY` and `INC_APPEND_HISTORY_TIME`.
1 parent 011379b commit 6a6086d

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

modules/programs/zsh/default.nix

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,37 @@ in
6464
'';
6565
};
6666

67+
appendInc = mkOption {
68+
type = types.bool;
69+
default = false;
70+
description = ''
71+
This option works like `programs.zsh.history.append` except that
72+
new history lines are added to the $HISTFILE incrementally (as
73+
soon as they are entered), rather than waiting until the shell
74+
exits. The file will still be periodically re-written to trim it
75+
when the number of lines grows 20% beyond the value specified by
76+
`programs.zsh.history.save`.
77+
'';
78+
};
79+
80+
appendIncTime = mkOption {
81+
type = types.bool;
82+
default = false;
83+
description = ''
84+
This option is a variant of `programs.zsh.history.appendInc` in
85+
which, where possible, the history entry is written out to the
86+
file after the command is finished, so that the time taken by
87+
the command is recorded correctly in the history file in
88+
EXTENDED_HISTORY format. This means that the history entry will
89+
not be available immediately from other instances of the shell
90+
that are using the same history file.
91+
92+
This option is only useful if `programs.zsh.history.append` and
93+
`programs.zsh.history.share` are turned off. The three options
94+
should be considered mutually exclusive.
95+
'';
96+
};
97+
6798
size = mkOption {
6899
type = types.int;
69100
default = 10000;
@@ -162,7 +193,16 @@ in
162193
share = mkOption {
163194
type = types.bool;
164195
default = true;
165-
description = "Share command history between zsh sessions.";
196+
description = ''
197+
This option both imports new commands from the history file,
198+
and also causes your typed commands to be appended to the
199+
history file (the latter is like specifying
200+
`programs.zsh.history.append`, which should be turned off
201+
if this option is in effect). The history lines are also output
202+
with timestamps ala `programs.zsh.history.extended` (which makes
203+
it easier to find the spot where we left off reading the file
204+
after it gets re-written).
205+
'';
166206
};
167207
};
168208
}
@@ -678,6 +718,23 @@ in
678718
in
679719
mkIf cfg.enable (
680720
lib.mkMerge [
721+
{
722+
assertions = with config.programs.zsh; [
723+
{
724+
assertion = history.share -> !history.appendInc && !history.appendIncTime;
725+
message = "zsh.hist.share can't be mixed with zsh.hist.appendInc or zsh.hist.appendIncTime";
726+
}
727+
{
728+
assertion = history.appendInc -> !history.appendIncTime && !history.share;
729+
message = "zsh.hist.appendInc can't be mixed with zsh.hist.appendIncTime or zsh.hist.share";
730+
}
731+
{
732+
assertion = history.appendIncTime -> !history.appendInc && !history.share;
733+
message = "zsh.hist.appendIncTime can't be mixed with zsh.hist.appendInc or zsh.hist.share";
734+
}
735+
];
736+
}
737+
681738
(mkIf (cfg.envExtra != "") {
682739
home.file."${relToDotDir ".zshenv"}".text = cfg.envExtra;
683740
})
@@ -854,6 +911,8 @@ in
854911
855912
setopt HIST_FCNTL_LOCK
856913
${if cfg.history.append then "setopt" else "unsetopt"} APPEND_HISTORY
914+
${if cfg.history.appendInc then "setopt" else "unsetopt"} INC_APPEND_HISTORY
915+
${if cfg.history.appendIncTime then "setopt" else "unsetopt"} INC_APPEND_HISTORY_TIME
857916
${if cfg.history.expireDuplicatesFirst then "setopt" else "unsetopt"} HIST_EXPIRE_DUPS_FIRST
858917
${if cfg.history.extended then "setopt" else "unsetopt"} EXTENDED_HISTORY
859918
${if cfg.history.findNoDups then "setopt" else "unsetopt"} HIST_FIND_NO_DUPS

tests/modules/programs/zsh/aliases.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
3434
setopt HIST_FCNTL_LOCK
3535
unsetopt APPEND_HISTORY
36+
unsetopt INC_APPEND_HISTORY
37+
unsetopt INC_APPEND_HISTORY_TIME
3638
unsetopt HIST_EXPIRE_DUPS_FIRST
3739
unsetopt EXTENDED_HISTORY
3840
unsetopt HIST_FIND_NO_DUPS

tests/modules/programs/zsh/zshrc-content-priorities.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
5252
setopt HIST_FCNTL_LOCK
5353
unsetopt APPEND_HISTORY
54+
unsetopt INC_APPEND_HISTORY
55+
unsetopt INC_APPEND_HISTORY_TIME
5456
unsetopt HIST_EXPIRE_DUPS_FIRST
5557
unsetopt EXTENDED_HISTORY
5658
unsetopt HIST_FIND_NO_DUPS

0 commit comments

Comments
 (0)