git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/system-environment: introduce environment.profileRelativeSessionVariables

There is a need for having sessionVariables set relative to the Nix Profiles. Such as in #68383.

commit866cc3e7923633095dce48493303c52238e16637 authorRobert Helgesson <robert@rycee.net> dateMon 19 Aug 2019, 16:05 (7 years ago) parentf7571a0
7 files changed+73−15
nixos/modules/config/shells-environment.nix+2 nixos/modules/config/system-environment.nix+66−10 nixos/modules/programs/environment.nix+1−1 nixos/modules/security/pam.nix+1−1 nixos/modules/services/x11/display-managers/gdm.nix+1−1 nixos/modules/services/x11/display-managers/lightdm.nix+1−1 nixos/modules/services/x11/display-managers/sddm.nix+1−1
nixos/modules/config/shells-environment.nix+2
@@ -157,6 +157,8 @@ in
157157 # terminal instead of logging out of X11).
158158 environment.variables = config.environment.sessionVariables;
159159
160+ environment.profileRelativeEnvVars = config.environment.profileRelativeSessionVariables;
161+
160162 environment.shellAliases = mapAttrs (name: mkDefault) {
161163 ls = "ls --color=tty";
162164 ll = "ls -l";
nixos/modules/config/system-environment.nix+66−10
@@ -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
nixos/modules/programs/environment.nix+1−1
@@ -30,7 +30,7 @@ in
3030 ];
3131
3232 # TODO: move most of these elsewhere
33− environment.profileRelativeEnvVars =
33+ environment.profileRelativeSessionVariables =
3434 { PATH = [ "/bin" ];
3535 INFOPATH = [ "/info" "/share/info" ];
3636 KDEDIRS = [ "" ];
nixos/modules/security/pam.nix+1−1
@@ -415,7 +415,7 @@ let
415415
416416 # Session management.
417417 ${optionalString cfg.setEnvironment ''
418− session required pam_env.so envfile=${config.system.build.pamEnvironment}
418+ session required pam_env.so conffile=${config.system.build.pamEnvironment} readenv=0
419419 ''}
420420 session required pam_unix.so
421421 ${optionalString cfg.setLoginUid
nixos/modules/services/x11/display-managers/gdm.nix+1−1
@@ -262,7 +262,7 @@ in
262262 password required pam_deny.so
263263
264264 session required pam_succeed_if.so audit quiet_success user = gdm
265− session required pam_env.so envfile=${config.system.build.pamEnvironment}
265+ session required pam_env.so conffile=${config.system.build.pamEnvironment} readenv=0
266266 session optional ${pkgs.systemd}/lib/security/pam_systemd.so
267267 session optional pam_keyinit.so force revoke
268268 session optional pam_permit.so
nixos/modules/services/x11/display-managers/lightdm.nix+1−1
@@ -249,7 +249,7 @@ in
249249 password required pam_deny.so
250250
251251 session required pam_succeed_if.so audit quiet_success user = lightdm
252− session required pam_env.so envfile=${config.system.build.pamEnvironment}
252+ session required pam_env.so conffile=${config.system.build.pamEnvironment} readenv=0
253253 session optional ${pkgs.systemd}/lib/security/pam_systemd.so
254254 session optional pam_keyinit.so force revoke
255255 session optional pam_permit.so
nixos/modules/services/x11/display-managers/sddm.nix+1−1
@@ -242,7 +242,7 @@ in
242242 password required pam_deny.so
243243
244244 session required pam_succeed_if.so audit quiet_success user = sddm
245− session required pam_env.so envfile=${config.system.build.pamEnvironment}
245+ session required pam_env.so conffile=${config.system.build.pamEnvironment} readenv=0
246246 session optional ${pkgs.systemd}/lib/security/pam_systemd.so
247247 session optional pam_keyinit.so force revoke
248248 session optional pam_permit.so