2020-04-24 23:36:52 +00:00
|
|
|
# This module defines a system-wide environment that will be
|
|
|
|
# initialised by pam_env (that is, not only in shells).
|
2022-12-28 21:21:41 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.environment;
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
environment.sessionVariables = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = {};
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
A set of environment variables used in the global environment.
|
|
|
|
These variables will be set by PAM early in the login process.
|
|
|
|
|
|
|
|
The value of each session variable can be either a string or a
|
|
|
|
list of strings. The latter is concatenated, interspersed with
|
|
|
|
colon characters.
|
|
|
|
|
|
|
|
Note, due to limitations in the PAM format values may not
|
2022-08-12 12:06:08 +00:00
|
|
|
contain the `"` character.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
Also, these variables are merged into
|
2022-08-12 12:06:08 +00:00
|
|
|
[](#opt-environment.variables) and it is
|
2020-04-24 23:36:52 +00:00
|
|
|
therefore not possible to use PAM style variables such as
|
2022-08-12 12:06:08 +00:00
|
|
|
`@{HOME}`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
2022-12-28 21:21:41 +00:00
|
|
|
inherit (options.environment.variables) type apply;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
environment.profileRelativeSessionVariables = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf (lib.types.listOf lib.types.str);
|
2020-04-24 23:36:52 +00:00
|
|
|
example = { PATH = [ "/bin" ]; MANPATH = [ "/man" "/share/man" ]; };
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Attribute set of environment variable used in the global
|
|
|
|
environment. These variables will be set by PAM early in the
|
|
|
|
login process.
|
|
|
|
|
|
|
|
Variable substitution is available as described in
|
2022-08-21 13:32:41 +00:00
|
|
|
{manpage}`pam_env.conf(5)`.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
Each attribute maps to a list of relative paths. Each relative
|
|
|
|
path is appended to the each profile of
|
2022-08-21 13:32:41 +00:00
|
|
|
{option}`environment.profiles` to form the content of
|
2020-04-24 23:36:52 +00:00
|
|
|
the corresponding environment variable.
|
|
|
|
|
|
|
|
Also, these variables are merged into
|
2022-08-21 13:32:41 +00:00
|
|
|
[](#opt-environment.profileRelativeEnvVars) and it is
|
2020-04-24 23:36:52 +00:00
|
|
|
therefore not possible to use PAM style variables such as
|
2022-08-21 13:32:41 +00:00
|
|
|
`@{HOME}`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2021-08-22 07:53:02 +00:00
|
|
|
environment.etc."pam/environment".text = let
|
|
|
|
suffixedVariables =
|
2024-09-19 14:19:46 +00:00
|
|
|
lib.flip lib.mapAttrs cfg.profileRelativeSessionVariables (envVar: suffixes:
|
|
|
|
lib.flip lib.concatMap cfg.profiles (profile:
|
2021-08-22 07:53:02 +00:00
|
|
|
map (suffix: "${profile}${suffix}") suffixes
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
# We're trying to use the same syntax for PAM variables and env variables.
|
|
|
|
# That means we need to map the env variables that people might use to their
|
|
|
|
# equivalent PAM variable.
|
2024-09-19 14:19:46 +00:00
|
|
|
replaceEnvVars = lib.replaceStrings ["$HOME" "$USER"] ["@{HOME}" "@{PAM_USER}"];
|
2021-08-22 07:53:02 +00:00
|
|
|
|
|
|
|
pamVariable = n: v:
|
2024-09-19 14:19:46 +00:00
|
|
|
''${n} DEFAULT="${lib.concatStringsSep ":" (map replaceEnvVars (lib.toList v))}"'';
|
2021-08-22 07:53:02 +00:00
|
|
|
|
|
|
|
pamVariables =
|
2024-09-19 14:19:46 +00:00
|
|
|
lib.concatStringsSep "\n"
|
|
|
|
(lib.mapAttrsToList pamVariable
|
|
|
|
(lib.zipAttrsWith (n: lib.concatLists)
|
2021-08-22 07:53:02 +00:00
|
|
|
[
|
|
|
|
# Make sure security wrappers are prioritized without polluting
|
|
|
|
# shell environments with an extra entry. Sessions which depend on
|
|
|
|
# pam for its environment will otherwise have eg. broken sudo. In
|
|
|
|
# particular Gnome Shell sometimes fails to source a proper
|
|
|
|
# environment from a shell.
|
|
|
|
{ PATH = [ config.security.wrapperDir ]; }
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mapAttrs (n: lib.toList) cfg.sessionVariables)
|
2021-08-22 07:53:02 +00:00
|
|
|
suffixedVariables
|
|
|
|
]));
|
|
|
|
in ''
|
|
|
|
${pamVariables}
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|