From 50f55eebd0dd581376463b865d2e0ba363c37f41 Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Sun, 12 Jan 2025 18:09:08 +0100 Subject: [PATCH] bash: Make sure HISTFILE's directory exists --- modules/programs/bash.nix | 7 +++++-- .../bash/bash-history-control-with-file.nix | 21 +++++++++++++++++++ tests/modules/programs/bash/default.nix | 1 + 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 tests/modules/programs/bash/bash-history-control-with-file.nix diff --git a/modules/programs/bash.nix b/modules/programs/bash.nix index 3a367b9e73ac..23f6317dba4b 100644 --- a/modules/programs/bash.nix +++ b/modules/programs/bash.nix @@ -180,7 +180,7 @@ in { sessionVarsStr = config.lib.shell.exportAll cfg.sessionVariables; - historyControlStr = concatStringsSep "\n" + historyControlStr = (concatStringsSep "\n" (mapAttrsToList (n: v: "${n}=${v}") ({ HISTFILESIZE = toString cfg.historyFileSize; HISTSIZE = toString cfg.historySize; @@ -190,7 +190,10 @@ in { HISTCONTROL = concatStringsSep ":" cfg.historyControl; } // optionalAttrs (cfg.historyIgnore != [ ]) { HISTIGNORE = escapeShellArg (concatStringsSep ":" cfg.historyIgnore); - })); + }))) + (optionalString (cfg.historyFile != null) '' + + mkdir -p "$(dirname "$HISTFILE")" + ''); in mkIf cfg.enable { home.packages = [ cfg.package ]; diff --git a/tests/modules/programs/bash/bash-history-control-with-file.nix b/tests/modules/programs/bash/bash-history-control-with-file.nix new file mode 100644 index 000000000000..1d63dcc88795 --- /dev/null +++ b/tests/modules/programs/bash/bash-history-control-with-file.nix @@ -0,0 +1,21 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + config = { + programs.bash = { + enable = true; + historyControl = [ "erasedups" ]; + historyFile = "/home/hm-user/foo/bash/history"; + }; + + nmt.script = '' + assertFileExists home-files/.bashrc + + assertFileRegex \ + home-files/.bashrc \ + '^mkdir -p "\$(dirname "\$HISTFILE")"' + ''; + }; +} diff --git a/tests/modules/programs/bash/default.nix b/tests/modules/programs/bash/default.nix index 1dbbee777ebe..6b20d1fbc1c6 100644 --- a/tests/modules/programs/bash/default.nix +++ b/tests/modules/programs/bash/default.nix @@ -2,4 +2,5 @@ bash-completion = ./completion.nix; bash-logout = ./logout.nix; bash-session-variables = ./session-variables.nix; + bash-history-control-with-file = ./bash-history-control-with-file.nix; }