| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  | # This module defines a global environment configuration and | 
					
						
							|  |  |  | # a common configuration for all shells. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-12 20:03:14 +01:00
										 |  |  | { config, lib, utils, pkgs, ... }: | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | with lib; | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cfg = config.environment; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-29 22:38:12 +02:00
										 |  |  |   exportedEnvVars = | 
					
						
							|  |  |  |     let | 
					
						
							|  |  |  |       absoluteVariables = | 
					
						
							|  |  |  |         mapAttrs (n: toList) cfg.variables; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       suffixedVariables = | 
					
						
							|  |  |  |         flip mapAttrs cfg.profileRelativeEnvVars (envVar: listSuffixes: | 
					
						
							|  |  |  |           concatMap (profile: map (suffix: "${profile}${suffix}") listSuffixes) cfg.profiles | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       allVariables = | 
					
						
							|  |  |  |         zipAttrsWith (n: concatLists) [ absoluteVariables suffixedVariables ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       exportVariables = | 
					
						
							|  |  |  |         mapAttrsToList (n: v: ''export ${n}="${concatStringsSep ":" v}"'') allVariables; | 
					
						
							|  |  |  |     in | 
					
						
							|  |  |  |       concatStringsSep "\n" exportVariables; | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  | in | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   options = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     environment.variables = mkOption { | 
					
						
							|  |  |  |       default = {}; | 
					
						
							| 
									
										
										
										
											2018-09-02 15:21:14 -04:00
										 |  |  |       example = { EDITOR = "nvim"; VISUAL = "nvim"; }; | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |       description = ''
 | 
					
						
							|  |  |  |         A set of environment variables used in the global environment. | 
					
						
							| 
									
										
										
										
											2017-09-30 15:31:26 +09:00
										 |  |  |         These variables will be set on shell initialisation (e.g. in /etc/profile). | 
					
						
							| 
									
										
										
										
											2013-09-23 23:03:21 +02:00
										 |  |  |         The value of each variable can be either a string or a list of | 
					
						
							|  |  |  |         strings.  The latter is concatenated, interspersed with colon | 
					
						
							|  |  |  |         characters. | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2016-11-04 13:33:59 +09:00
										 |  |  |       type = with types; attrsOf (either str (listOf str)); | 
					
						
							| 
									
										
										
										
											2013-09-23 23:03:21 +02:00
										 |  |  |       apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v); | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     environment.profiles = mkOption { | 
					
						
							|  |  |  |       default = []; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         A list of profiles used to setup the global environment. | 
					
						
							|  |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2015-06-15 18:18:46 +02:00
										 |  |  |       type = types.listOf types.str; | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-29 22:38:12 +02:00
										 |  |  |     environment.profileRelativeEnvVars = mkOption { | 
					
						
							|  |  |  |       type = types.attrsOf (types.listOf types.str); | 
					
						
							| 
									
										
										
										
											2017-05-09 08:46:55 +01:00
										 |  |  |       example = { PATH = [ "/bin" ]; MANPATH = [ "/man" "/share/man" ]; }; | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |       description = ''
 | 
					
						
							| 
									
										
										
										
											2015-09-24 11:42:32 +02:00
										 |  |  |         Attribute set of environment variable.  Each attribute maps to a list | 
					
						
							|  |  |  |         of relative paths.  Each relative path is appended to the each profile | 
					
						
							| 
									
										
										
										
											2014-08-29 22:38:12 +02:00
										 |  |  |         of <option>environment.profiles</option> to form the content of the | 
					
						
							|  |  |  |         corresponding environment variable. | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # !!! isn't there a better way? | 
					
						
							|  |  |  |     environment.extraInit = mkOption { | 
					
						
							|  |  |  |       default = ""; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Shell script code called during global environment initialisation | 
					
						
							|  |  |  |         after all variables and profileVariables have been set. | 
					
						
							| 
									
										
										
										
											2018-08-13 14:28:52 +01:00
										 |  |  |         This code is assumed to be shell-independent, which means you should | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |         stick to pure sh without sh word split. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |       type = types.lines; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     environment.shellInit = mkOption { | 
					
						
							|  |  |  |       default = ""; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Shell script code called during shell initialisation. | 
					
						
							| 
									
										
										
										
											2018-08-30 10:17:05 +02:00
										 |  |  |         This code is assumed to be shell-independent, which means you should | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |         stick to pure sh without sh word split. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |       type = types.lines; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     environment.loginShellInit = mkOption { | 
					
						
							|  |  |  |       default = ""; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Shell script code called during login shell initialisation. | 
					
						
							| 
									
										
										
										
											2018-08-30 10:17:05 +02:00
										 |  |  |         This code is assumed to be shell-independent, which means you should | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |         stick to pure sh without sh word split. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |       type = types.lines; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     environment.interactiveShellInit = mkOption { | 
					
						
							|  |  |  |       default = ""; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Shell script code called during interactive shell initialisation. | 
					
						
							| 
									
										
										
										
											2018-08-30 10:17:05 +02:00
										 |  |  |         This code is assumed to be shell-independent, which means you should | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |         stick to pure sh without sh word split. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |       type = types.lines; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     environment.shellAliases = mkOption { | 
					
						
							| 
									
										
										
										
											2018-10-12 22:58:35 +09:00
										 |  |  |       example = { l = null; ll = "ls -l"; }; | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |       description = ''
 | 
					
						
							|  |  |  |         An attribute set that maps aliases (the top level attribute names in | 
					
						
							|  |  |  |         this option) to command strings or directly to build outputs. The | 
					
						
							|  |  |  |         aliases are added to all users' shells. | 
					
						
							| 
									
										
										
										
											2018-10-12 22:58:35 +09:00
										 |  |  |         Aliases mapped to <code>null</code> are ignored. | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2018-10-12 22:58:35 +09:00
										 |  |  |       type = with types; attrsOf (nullOr (either str path)); | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 23:00:58 -07:00
										 |  |  |     environment.homeBinInPath = mkOption { | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Include ~/bin/ in $PATH. | 
					
						
							|  |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2019-11-02 14:34:50 +01:00
										 |  |  |       default = false; | 
					
						
							| 
									
										
										
										
											2019-09-10 23:00:58 -07:00
										 |  |  |       type = types.bool; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |     environment.binsh = mkOption { | 
					
						
							|  |  |  |       default = "${config.system.build.binsh}/bin/sh"; | 
					
						
							| 
									
										
										
										
											2016-01-17 19:34:55 +01:00
										 |  |  |       defaultText = "\${config.system.build.binsh}/bin/sh"; | 
					
						
							| 
									
										
										
										
											2014-08-27 23:41:15 +02:00
										 |  |  |       example = literalExample ''
 | 
					
						
							|  |  |  |         "''${pkgs.dash}/bin/dash" | 
					
						
							|  |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2013-09-23 23:03:21 +02:00
										 |  |  |       type = types.path; | 
					
						
							| 
									
										
										
										
											2015-09-24 11:42:32 +02:00
										 |  |  |       visible = false; | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |       description = ''
 | 
					
						
							|  |  |  |         The shell executable that is linked system-wide to | 
					
						
							|  |  |  |         <literal>/bin/sh</literal>. Please note that NixOS assumes all | 
					
						
							|  |  |  |         over the place that shell to be Bash, so override the default | 
					
						
							|  |  |  |         setting only if you know exactly what you're doing. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     environment.shells = mkOption { | 
					
						
							|  |  |  |       default = []; | 
					
						
							| 
									
										
										
										
											2016-06-12 20:03:14 +01:00
										 |  |  |       example = literalExample "[ pkgs.bashInteractive pkgs.zsh ]"; | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |       description = ''
 | 
					
						
							|  |  |  |         A list of permissible login shells for user accounts. | 
					
						
							|  |  |  |         No need to mention <literal>/bin/sh</literal> | 
					
						
							|  |  |  |         here, it is placed into this list implicitly. | 
					
						
							|  |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2016-06-12 20:03:14 +01:00
										 |  |  |       type = types.listOf (types.either types.shellPackage types.path); | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   config = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     system.build.binsh = pkgs.bashInteractive; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-13 18:33:58 +02:00
										 |  |  |     # Set session variables in the shell as well. This is usually | 
					
						
							|  |  |  |     # unnecessary, but it allows changes to session variables to take | 
					
						
							|  |  |  |     # effect without restarting the session (e.g. by opening a new | 
					
						
							|  |  |  |     # terminal instead of logging out of X11). | 
					
						
							|  |  |  |     environment.variables = config.environment.sessionVariables; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 16:05:10 +02:00
										 |  |  |     environment.profileRelativeEnvVars = config.environment.profileRelativeSessionVariables; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-04 17:35:05 +09:00
										 |  |  |     environment.shellAliases = mapAttrs (name: mkDefault) { | 
					
						
							|  |  |  |       ls = "ls --color=tty"; | 
					
						
							|  |  |  |       ll = "ls -l"; | 
					
						
							|  |  |  |       l  = "ls -alh"; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 21:52:01 +00:00
										 |  |  |     environment.etc.shells.text = | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |       ''
 | 
					
						
							| 
									
										
										
										
											2016-06-12 20:03:14 +01:00
										 |  |  |         ${concatStringsSep "\n" (map utils.toShellPath cfg.shells)} | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  |         /bin/sh | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 16:05:48 +00:00
										 |  |  |     # For resetting environment with `. /etc/set-environment` when needed | 
					
						
							|  |  |  |     # and discoverability (see motivation of #30418). | 
					
						
							| 
									
										
										
										
											2019-08-13 21:52:01 +00:00
										 |  |  |     environment.etc.set-environment.source = config.system.build.setEnvironment; | 
					
						
							| 
									
										
										
										
											2018-08-29 16:05:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-24 10:58:01 +02:00
										 |  |  |     system.build.setEnvironment = pkgs.writeText "set-environment" | 
					
						
							| 
									
										
										
										
											2018-08-29 16:05:53 +00:00
										 |  |  |       ''
 | 
					
						
							|  |  |  |         # DO NOT EDIT -- this file has been generated automatically. | 
					
						
							| 
									
										
										
										
											2018-08-29 16:05:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 16:05:53 +00:00
										 |  |  |         # Prevent this file from being sourced by child shells. | 
					
						
							|  |  |  |         export __NIXOS_SET_ENVIRONMENT_DONE=1 | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 16:05:53 +00:00
										 |  |  |         ${exportedEnvVars} | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 16:05:53 +00:00
										 |  |  |         ${cfg.extraInit} | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 23:00:58 -07:00
										 |  |  |         ${optionalString cfg.homeBinInPath ''
 | 
					
						
							|  |  |  |           # ~/bin if it exists overrides other bin directories. | 
					
						
							|  |  |  |           export PATH="$HOME/bin:$PATH" | 
					
						
							|  |  |  |         ''}
 | 
					
						
							| 
									
										
										
										
											2018-08-29 16:05:53 +00:00
										 |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2013-09-18 03:18:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     system.activationScripts.binsh = stringAfter [ "stdio" ] | 
					
						
							|  |  |  |       ''
 | 
					
						
							|  |  |  |         # Create the required /bin/sh symlink; otherwise lots of things | 
					
						
							|  |  |  |         # (notably the system() function) won't work. | 
					
						
							|  |  |  |         mkdir -m 0755 -p /bin | 
					
						
							|  |  |  |         ln -sfn "${cfg.binsh}" /bin/.sh.tmp | 
					
						
							|  |  |  |         mv /bin/.sh.tmp /bin/sh # atomically replace /bin/sh | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |