@@ -8,6 +8,11 @@ let
88
99 cfg = config.environment;
1010
11+ pamProfiles =
12+ map
13+ (replaceStrings ["$HOME" "$USER"] ["@{HOME}" "@{PAM_USER}"])
14+ cfg.profiles;
15+
1116in
1217
1318{
@@ -18,25 +23,76 @@ in
1823 default = {};
1924 description = ''
2025 A set of environment variables used in the global environment.
21− These variables will be set by PAM.
22− The value of each variable can be either a string or a list of
23− strings. The latter is concatenated, interspersed with colon
24− characters.
26+ These variables will be set by PAM early in the login process.
27+
28+ The value of each session variable can be either a string or a
29+ list of strings. The latter is concatenated, interspersed with
30+ colon characters.
31+
32+ Note, due to limitations in the PAM format values may not
33+ contain the <literal>"</literal> character.
34+
35+ Also, these variables are merged into
36+ <xref linkend="opt-environment.variables"/> and it is
37+ therefore not possible to use PAM style variables such as
38+ <code>@{HOME}</code>.
2539 '';
2640 type = with types; attrsOf (either str (listOf str));
2741 apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v);
2842 };
2943
44+ environment.profileRelativeSessionVariables = mkOption {
45+ type = types.attrsOf (types.listOf types.str);
46+ example = { PATH = [ "/bin" ]; MANPATH = [ "/man" "/share/man" ]; };
47+ description = ''
48+ Attribute set of environment variable used in the global
49+ environment. These variables will be set by PAM early in the
50+ login process.
51+
52+ Variable substitution is available as described in
53+ <citerefentry>
54+ <refentrytitle>pam_env.conf</refentrytitle>
55+ <manvolnum>5</manvolnum>
56+ </citerefentry>.
57+
58+ Each attribute maps to a list of relative paths. Each relative
59+ path is appended to the each profile of
60+ <option>environment.profiles</option> to form the content of
61+ the corresponding environment variable.
62+
63+ Also, these variables are merged into
64+ <xref linkend="opt-environment.profileRelativeEnvVars"/> and it is
65+ therefore not possible to use PAM style variables such as
66+ <code>@{HOME}</code>.
67+ '';
68+ };
69+
3070 };
3171
3272 config = {
3373
34− system.build.pamEnvironment = pkgs.writeText "pam-environment"
35− ''
36− ${concatStringsSep "\n" (
37− (mapAttrsToList (n: v: ''${n}="${concatStringsSep ":" v}"'')
38− (zipAttrsWith (const concatLists) ([ (mapAttrs (n: v: [ v ]) cfg.sessionVariables) ]))))}
39− '';
74+ system.build.pamEnvironment =
75+ let
76+ suffixedVariables =
77+ flip mapAttrs cfg.profileRelativeSessionVariables (envVar: suffixes:
78+ flip concatMap pamProfiles (profile:
79+ map (suffix: "${profile}${suffix}") suffixes
80+ )
81+ );
82+
83+ pamVariable = n: v:
84+ ''${n} DEFAULT="${concatStringsSep ":" (toList v)}"'';
85+
86+ pamVariables =
87+ concatStringsSep "\n"
88+ (mapAttrsToList pamVariable
89+ (zipAttrsWith (n: concatLists)
90+ [
91+ (mapAttrs (n: toList) cfg.sessionVariables)
92+ suffixedVariables
93+ ]));
94+ in
95+ pkgs.writeText "pam-environment" "${pamVariables}\n";
4096
4197 };
4298