1 # This module defines a system-wide environment that will be
2 # initialised by pam_env (that is, not only in shells).
3 { config, lib, options, pkgs, ... }:
6 cfg = config.environment;
14 environment.sessionVariables = lib.mkOption {
17 A set of environment variables used in the global environment.
18 These variables will be set by PAM early in the login process.
20 The value of each session variable can be either a string or a
21 list of strings. The latter is concatenated, interspersed with
24 Note, due to limitations in the PAM format values may not
25 contain the `"` character.
27 Also, these variables are merged into
28 [](#opt-environment.variables) and it is
29 therefore not possible to use PAM style variables such as
32 inherit (options.environment.variables) type apply;
35 environment.profileRelativeSessionVariables = lib.mkOption {
36 type = lib.types.attrsOf (lib.types.listOf lib.types.str);
37 example = { PATH = [ "/bin" ]; MANPATH = [ "/man" "/share/man" ]; };
39 Attribute set of environment variable used in the global
40 environment. These variables will be set by PAM early in the
43 Variable substitution is available as described in
44 {manpage}`pam_env.conf(5)`.
46 Each attribute maps to a list of relative paths. Each relative
47 path is appended to the each profile of
48 {option}`environment.profiles` to form the content of
49 the corresponding environment variable.
51 Also, these variables are merged into
52 [](#opt-environment.profileRelativeEnvVars) and it is
53 therefore not possible to use PAM style variables such as
61 environment.etc."pam/environment".text = let
63 lib.flip lib.mapAttrs cfg.profileRelativeSessionVariables (envVar: suffixes:
64 lib.flip lib.concatMap cfg.profiles (profile:
65 map (suffix: "${profile}${suffix}") suffixes
69 # We're trying to use the same syntax for PAM variables and env variables.
70 # That means we need to map the env variables that people might use to their
71 # equivalent PAM variable.
72 replaceEnvVars = lib.replaceStrings ["$HOME" "$USER"] ["@{HOME}" "@{PAM_USER}"];
75 ''${n} DEFAULT="${lib.concatStringsSep ":" (map replaceEnvVars (lib.toList v))}"'';
78 lib.concatStringsSep "\n"
79 (lib.mapAttrsToList pamVariable
80 (lib.zipAttrsWith (n: lib.concatLists)
82 # Make sure security wrappers are prioritized without polluting
83 # shell environments with an extra entry. Sessions which depend on
84 # pam for its environment will otherwise have eg. broken sudo. In
85 # particular Gnome Shell sometimes fails to source a proper
86 # environment from a shell.
87 { PATH = [ config.security.wrapperDir ]; }
89 (lib.mapAttrs (n: lib.toList) cfg.sessionVariables)