2025-02-09 15:31:32 +01:00
|
|
|
{
|
|
|
|
lib,
|
2025-02-09 17:04:15 +01:00
|
|
|
pkgs,
|
|
|
|
config,
|
2025-02-09 15:31:32 +01:00
|
|
|
...
|
|
|
|
}: let
|
2025-02-09 17:04:15 +01:00
|
|
|
inherit (lib.attrsets) mapAttrsToList;
|
|
|
|
inherit (lib.lists) isList;
|
|
|
|
inherit (lib.modules) mkIf;
|
2025-02-09 15:31:32 +01:00
|
|
|
inherit (lib.options) mkOption;
|
|
|
|
inherit (lib.strings) concatStringsSep;
|
2025-02-09 17:04:15 +01:00
|
|
|
inherit (lib.types) attrsOf listOf oneOf int path str;
|
2025-02-09 15:31:32 +01:00
|
|
|
|
2025-02-09 17:04:15 +01:00
|
|
|
inherit (pkgs) writeShellScript;
|
2025-02-09 15:31:32 +01:00
|
|
|
|
2025-02-09 17:04:15 +01:00
|
|
|
cfg = config.environment;
|
2025-02-09 15:31:32 +01:00
|
|
|
|
2025-02-09 17:04:15 +01:00
|
|
|
writeEnvScript = let
|
|
|
|
toEnv = env:
|
|
|
|
if isList env
|
|
|
|
then concatStringsSep ":" (map toString env)
|
|
|
|
else toString env;
|
|
|
|
in
|
|
|
|
attrs:
|
|
|
|
writeShellScript "load-env"
|
2025-02-17 14:12:42 +01:00
|
|
|
(
|
|
|
|
''
|
|
|
|
# Only execute this file once per shell.
|
|
|
|
if [ -n "$__ETC_PROFILE_SOURCED" ]; then return; fi
|
|
|
|
__ETC_PROFILE_SOURCED=1
|
|
|
|
|
|
|
|
# Prevent this file from being sourced by interactive non-login child shells.
|
|
|
|
export __ETC_PROFILE_DONE=1
|
|
|
|
|
|
|
|
# Session variables
|
|
|
|
''
|
|
|
|
+ (concatStringsSep "\n"
|
|
|
|
(mapAttrsToList (name: value: "export ${name}=\"${toEnv value}\"") attrs))
|
|
|
|
);
|
2025-02-09 15:31:32 +01:00
|
|
|
in {
|
|
|
|
options.environment = {
|
|
|
|
sessionVariables = mkOption {
|
|
|
|
type = attrsOf (oneOf [(listOf (oneOf [int str path])) int str path]);
|
|
|
|
default = {};
|
|
|
|
example = {
|
|
|
|
EDITOR = "nvim";
|
|
|
|
VISUAL = "nvim";
|
|
|
|
};
|
|
|
|
description = ''
|
|
|
|
A set of environment variables used in the user environment.
|
2025-02-17 14:12:42 +01:00
|
|
|
These variables will be set using {file}`~/.profile`.
|
|
|
|
|
|
|
|
If a list of strings is used, they will be concatenated with colon
|
2025-02-09 15:31:32 +01:00
|
|
|
characters.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2025-02-09 17:04:15 +01:00
|
|
|
config.files.".profile".text = mkIf (cfg.sessionVariables != {}) ''
|
|
|
|
. ${writeEnvScript cfg.sessionVariables}
|
|
|
|
'';
|
2025-02-09 15:31:32 +01:00
|
|
|
}
|