at 21.11-pre 6.9 kB view raw
1# This module defines a global environment configuration and 2# a common configuration for all shells. 3 4{ config, lib, utils, pkgs, ... }: 5 6with lib; 7 8let 9 10 cfg = config.environment; 11 12 exportedEnvVars = 13 let 14 absoluteVariables = 15 mapAttrs (n: toList) cfg.variables; 16 17 suffixedVariables = 18 flip mapAttrs cfg.profileRelativeEnvVars (envVar: listSuffixes: 19 concatMap (profile: map (suffix: "${profile}${suffix}") listSuffixes) cfg.profiles 20 ); 21 22 allVariables = 23 zipAttrsWith (n: concatLists) [ absoluteVariables suffixedVariables ]; 24 25 exportVariables = 26 mapAttrsToList (n: v: ''export ${n}="${concatStringsSep ":" v}"'') allVariables; 27 in 28 concatStringsSep "\n" exportVariables; 29in 30 31{ 32 33 options = { 34 35 environment.variables = mkOption { 36 default = {}; 37 example = { EDITOR = "nvim"; VISUAL = "nvim"; }; 38 description = '' 39 A set of environment variables used in the global environment. 40 These variables will be set on shell initialisation (e.g. in /etc/profile). 41 The value of each variable can be either a string or a list of 42 strings. The latter is concatenated, interspersed with colon 43 characters. 44 ''; 45 type = with types; attrsOf (either str (listOf str)); 46 apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v); 47 }; 48 49 environment.profiles = mkOption { 50 default = []; 51 description = '' 52 A list of profiles used to setup the global environment. 53 ''; 54 type = types.listOf types.str; 55 }; 56 57 environment.profileRelativeEnvVars = mkOption { 58 type = types.attrsOf (types.listOf types.str); 59 example = { PATH = [ "/bin" ]; MANPATH = [ "/man" "/share/man" ]; }; 60 description = '' 61 Attribute set of environment variable. Each attribute maps to a list 62 of relative paths. Each relative path is appended to the each profile 63 of <option>environment.profiles</option> to form the content of the 64 corresponding environment variable. 65 ''; 66 }; 67 68 # !!! isn't there a better way? 69 environment.extraInit = mkOption { 70 default = ""; 71 description = '' 72 Shell script code called during global environment initialisation 73 after all variables and profileVariables have been set. 74 This code is assumed to be shell-independent, which means you should 75 stick to pure sh without sh word split. 76 ''; 77 type = types.lines; 78 }; 79 80 environment.shellInit = mkOption { 81 default = ""; 82 description = '' 83 Shell script code called during shell initialisation. 84 This code is assumed to be shell-independent, which means you should 85 stick to pure sh without sh word split. 86 ''; 87 type = types.lines; 88 }; 89 90 environment.loginShellInit = mkOption { 91 default = ""; 92 description = '' 93 Shell script code called during login shell initialisation. 94 This code is assumed to be shell-independent, which means you should 95 stick to pure sh without sh word split. 96 ''; 97 type = types.lines; 98 }; 99 100 environment.interactiveShellInit = mkOption { 101 default = ""; 102 description = '' 103 Shell script code called during interactive shell initialisation. 104 This code is assumed to be shell-independent, which means you should 105 stick to pure sh without sh word split. 106 ''; 107 type = types.lines; 108 }; 109 110 environment.shellAliases = mkOption { 111 example = { l = null; ll = "ls -l"; }; 112 description = '' 113 An attribute set that maps aliases (the top level attribute names in 114 this option) to command strings or directly to build outputs. The 115 aliases are added to all users' shells. 116 Aliases mapped to <code>null</code> are ignored. 117 ''; 118 type = with types; attrsOf (nullOr (either str path)); 119 }; 120 121 environment.homeBinInPath = mkOption { 122 description = '' 123 Include ~/bin/ in $PATH. 124 ''; 125 default = false; 126 type = types.bool; 127 }; 128 129 environment.binsh = mkOption { 130 default = "${config.system.build.binsh}/bin/sh"; 131 defaultText = "\${config.system.build.binsh}/bin/sh"; 132 example = literalExample '' 133 "''${pkgs.dash}/bin/dash" 134 ''; 135 type = types.path; 136 visible = false; 137 description = '' 138 The shell executable that is linked system-wide to 139 <literal>/bin/sh</literal>. Please note that NixOS assumes all 140 over the place that shell to be Bash, so override the default 141 setting only if you know exactly what you're doing. 142 ''; 143 }; 144 145 environment.shells = mkOption { 146 default = []; 147 example = literalExample "[ pkgs.bashInteractive pkgs.zsh ]"; 148 description = '' 149 A list of permissible login shells for user accounts. 150 No need to mention <literal>/bin/sh</literal> 151 here, it is placed into this list implicitly. 152 ''; 153 type = types.listOf (types.either types.shellPackage types.path); 154 }; 155 156 }; 157 158 config = { 159 160 system.build.binsh = pkgs.bashInteractive; 161 162 # Set session variables in the shell as well. This is usually 163 # unnecessary, but it allows changes to session variables to take 164 # effect without restarting the session (e.g. by opening a new 165 # terminal instead of logging out of X11). 166 environment.variables = config.environment.sessionVariables; 167 168 environment.profileRelativeEnvVars = config.environment.profileRelativeSessionVariables; 169 170 environment.shellAliases = mapAttrs (name: mkDefault) { 171 ls = "ls --color=tty"; 172 ll = "ls -l"; 173 l = "ls -alh"; 174 }; 175 176 environment.etc.shells.text = 177 '' 178 ${concatStringsSep "\n" (map utils.toShellPath cfg.shells)} 179 /bin/sh 180 ''; 181 182 # For resetting environment with `. /etc/set-environment` when needed 183 # and discoverability (see motivation of #30418). 184 environment.etc.set-environment.source = config.system.build.setEnvironment; 185 186 system.build.setEnvironment = pkgs.writeText "set-environment" 187 '' 188 # DO NOT EDIT -- this file has been generated automatically. 189 190 # Prevent this file from being sourced by child shells. 191 export __NIXOS_SET_ENVIRONMENT_DONE=1 192 193 ${exportedEnvVars} 194 195 ${cfg.extraInit} 196 197 ${optionalString cfg.homeBinInPath '' 198 # ~/bin if it exists overrides other bin directories. 199 export PATH="$HOME/bin:$PATH" 200 ''} 201 ''; 202 203 system.activationScripts.binsh = stringAfter [ "stdio" ] 204 '' 205 # Create the required /bin/sh symlink; otherwise lots of things 206 # (notably the system() function) won't work. 207 mkdir -m 0755 -p /bin 208 ln -sfn "${cfg.binsh}" /bin/.sh.tmp 209 mv /bin/.sh.tmp /bin/sh # atomically replace /bin/sh 210 ''; 211 212 }; 213 214}