Add an option ‘environment.interactiveShellInit’

This is like ‘environment.shellInit’, but only for interactive shells.
This commit is contained in:
Eelco Dolstra 2013-01-30 14:05:53 +01:00
parent 0c021be95f
commit 2f97304833
2 changed files with 35 additions and 20 deletions

View file

@ -7,7 +7,9 @@ with pkgs.lib;
let let
initBashCompletion = optionalString config.environment.enableBashCompletion '' cfg = config.environment;
initBashCompletion = optionalString cfg.enableBashCompletion ''
# Check whether we're running a version of Bash that has support for # Check whether we're running a version of Bash that has support for
# programmable completion. If we do, enable all modules installed in # programmable completion. If we do, enable all modules installed in
# the system (and user profile). # the system (and user profile).
@ -26,7 +28,7 @@ let
''; '';
shellAliases = concatStringsSep "\n" ( shellAliases = concatStringsSep "\n" (
mapAttrsFlatten (k: v: "alias ${k}='${v}'") config.environment.shellAliases mapAttrsFlatten (k: v: "alias ${k}='${v}'") cfg.shellAliases
); );
options = { options = {
@ -41,24 +43,33 @@ let
PS1="\[\033]2;\h:\u:\w\007\]$PS1" PS1="\[\033]2;\h:\u:\w\007\]$PS1"
fi fi
''; '';
description = " description = ''
Script used to initialized shell prompt. Shell script code used to initialise the shell prompt.
"; '';
type = with pkgs.lib.types; string; type = with pkgs.lib.types; string;
}; };
environment.shellInit = mkOption { environment.shellInit = mkOption {
default = ""; default = "";
example = ''export PATH=/godi/bin/:$PATH''; example = ''export PATH=/godi/bin/:$PATH'';
description = " description = ''
Script used to initialized user shell environments. Shell script code called during login shell initialisation.
"; '';
type = with pkgs.lib.types; string;
};
environment.interactiveShellInit = mkOption {
default = "";
example = ''export PATH=/godi/bin/:$PATH'';
description = ''
Shell script code called during interactive shell initialisation.
'';
type = with pkgs.lib.types; string; type = with pkgs.lib.types; string;
}; };
environment.enableBashCompletion = mkOption { environment.enableBashCompletion = mkOption {
default = false; default = false;
description = "Enable bash-completion for all interactive shells."; description = "Enable Bash completion for all interactive shells.";
type = with pkgs.lib.types; bool; type = with pkgs.lib.types; bool;
}; };
@ -86,7 +97,7 @@ in
source = pkgs.substituteAll { source = pkgs.substituteAll {
src = ./profile.sh; src = ./profile.sh;
wrapperDir = config.security.wrapperDir; wrapperDir = config.security.wrapperDir;
shellInit = config.environment.shellInit; inherit (cfg) shellInit;
}; };
target = "profile"; target = "profile";
} }
@ -96,8 +107,7 @@ in
# configured properly. # configured properly.
source = pkgs.substituteAll { source = pkgs.substituteAll {
src = ./bashrc.sh; src = ./bashrc.sh;
inherit (config.environment) promptInit; inherit (cfg) interactiveShellInit;
inherit initBashCompletion shellAliases;
}; };
target = "bashrc"; target = "bashrc";
} }
@ -115,6 +125,16 @@ in
which = "type -P"; which = "type -P";
}; };
environment.interactiveShellInit =
''
# Check the window size after every command.
shopt -s checkwinsize
${cfg.promptInit}
${initBashCompletion}
${shellAliases}
'';
system.build.binsh = pkgs.bashInteractive; system.build.binsh = pkgs.bashInteractive;
system.activationScripts.binsh = stringAfter [ "stdio" ] system.activationScripts.binsh = stringAfter [ "stdio" ]
@ -122,9 +142,9 @@ in
# Create the required /bin/sh symlink; otherwise lots of things # Create the required /bin/sh symlink; otherwise lots of things
# (notably the system() function) won't work. # (notably the system() function) won't work.
mkdir -m 0755 -p /bin mkdir -m 0755 -p /bin
ln -sfn "${config.environment.binsh}" /bin/.sh.tmp ln -sfn "${cfg.binsh}" /bin/.sh.tmp
mv /bin/.sh.tmp /bin/sh # atomically replace /bin/sh mv /bin/.sh.tmp /bin/sh # atomically replace /bin/sh
''; '';
environment.pathsToLink = optional config.environment.enableBashCompletion "/etc/bash_completion.d"; environment.pathsToLink = optional cfg.enableBashCompletion "/etc/bash_completion.d";
} }

View file

@ -16,9 +16,4 @@ fi
# We are not always an interactive shell. # We are not always an interactive shell.
if [ -z "$PS1" ]; then return; fi if [ -z "$PS1" ]; then return; fi
# Check the window size after every command. @interactiveShellInit@
shopt -s checkwinsize
@promptInit@
@initBashCompletion@
@shellAliases@