diff --git a/lib/spack/spack/config.py b/lib/spack/spack/config.py index c15bb5206ac..b23b7e4ae92 100644 --- a/lib/spack/spack/config.py +++ b/lib/spack/spack/config.py @@ -53,6 +53,7 @@ import spack.schema.definitions import spack.schema.develop import spack.schema.env +import spack.schema.env_vars import spack.schema.mirrors import spack.schema.modules import spack.schema.packages @@ -70,6 +71,7 @@ "compilers": spack.schema.compilers.schema, "concretizer": spack.schema.concretizer.schema, "definitions": spack.schema.definitions.schema, + "env_vars": spack.schema.env_vars.schema, "view": spack.schema.view.schema, "develop": spack.schema.develop.schema, "mirrors": spack.schema.mirrors.schema, diff --git a/lib/spack/spack/environment/shell.py b/lib/spack/spack/environment/shell.py index cb2b541e9ed..534ced56ea4 100644 --- a/lib/spack/spack/environment/shell.py +++ b/lib/spack/spack/environment/shell.py @@ -10,6 +10,7 @@ import spack.environment as ev import spack.repo +import spack.schema.environment import spack.store from spack.util.environment import EnvironmentModifications @@ -156,6 +157,11 @@ def activate( # MANPATH, PYTHONPATH, etc. All variables that end in PATH (case-sensitive) # become PATH variables. # + + env_vars_yaml = env.manifest.configuration.get("env_vars", None) + if env_vars_yaml: + env_mods.extend(spack.schema.environment.parse(env_vars_yaml)) + try: if view and env.has_view(view): with spack.store.STORE.db.read_transaction(): @@ -189,6 +195,10 @@ def deactivate() -> EnvironmentModifications: if active is None: return env_mods + env_vars_yaml = active.manifest.configuration.get("env_vars", None) + if env_vars_yaml: + env_mods.extend(spack.schema.environment.parse(env_vars_yaml).reversed()) + active_view = os.getenv(ev.spack_env_view_var) if active_view and active.has_view(active_view): diff --git a/lib/spack/spack/schema/env_vars.py b/lib/spack/spack/schema/env_vars.py new file mode 100644 index 00000000000..009961cf5a0 --- /dev/null +++ b/lib/spack/spack/schema/env_vars.py @@ -0,0 +1,22 @@ +# Copyright Spack Project Developers. See COPYRIGHT file for details. +# +# SPDX-License-Identifier: (Apache-2.0 OR MIT) +"""Schema for env_vars.yaml configuration file. + +.. literalinclude:: _spack_root/lib/spack/spack/schema/env_vars.py + :lines: 15- +""" +from typing import Any, Dict + +import spack.schema.environment + +properties: Dict[str, Any] = {"env_vars": spack.schema.environment.definition} + +#: Full schema with metadata +schema = { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Spack env_vars configuration file schema", + "type": "object", + "additionalProperties": False, + "properties": properties, +} diff --git a/lib/spack/spack/schema/merged.py b/lib/spack/spack/schema/merged.py index ec921152500..9d1c88465b2 100644 --- a/lib/spack/spack/schema/merged.py +++ b/lib/spack/spack/schema/merged.py @@ -20,6 +20,7 @@ import spack.schema.container import spack.schema.definitions import spack.schema.develop +import spack.schema.env_vars import spack.schema.mirrors import spack.schema.modules import spack.schema.packages @@ -38,6 +39,7 @@ spack.schema.ci.properties, spack.schema.definitions.properties, spack.schema.develop.properties, + spack.schema.env_vars.properties, spack.schema.mirrors.properties, spack.schema.modules.properties, spack.schema.packages.properties, diff --git a/lib/spack/spack/test/cmd/env.py b/lib/spack/spack/test/cmd/env.py index 0cbe9ddecd8..f8408b736cb 100644 --- a/lib/spack/spack/test/cmd/env.py +++ b/lib/spack/spack/test/cmd/env.py @@ -3023,6 +3023,35 @@ def test_stack_view_activate_from_default( assert "FOOBAR=mpileaks" in shell +def test_envvar_set_in_activate(tmpdir, mock_fetch, mock_packages, mock_archive, install_mockery): + filename = str(tmpdir.join("spack.yaml")) + with open(filename, "w", encoding="utf-8") as f: + f.write( + """\ +spack: + specs: + - cmake%gcc + env_vars: + set: + ENVAR_SET_IN_ENV_LOAD: "True" +""" + ) + with tmpdir.as_cwd(): + env("create", "test", "./spack.yaml") + with ev.read("test"): + install() + + test_env = ev.read("test") + output = env("activate", "--sh", "test") + + assert "ENVAR_SET_IN_ENV_LOAD=True" in output + + with test_env: + with spack.util.environment.set_env(ENVAR_SET_IN_ENV_LOAD="True"): + output = env("deactivate", "--sh") + assert "unset ENVAR_SET_IN_ENV_LOAD" in output + + def test_stack_view_no_activate_without_default( installed_environment, template_combinatorial_env, tmp_path ): diff --git a/share/spack/spack-completion.fish b/share/spack/spack-completion.fish index b5ed5d541ee..41ae4c91c3d 100644 --- a/share/spack/spack-completion.fish +++ b/share/spack/spack-completion.fish @@ -1201,19 +1201,19 @@ complete -c spack -n '__fish_spack_using_command config' -l scope -r -d 'configu # spack config get set -g __fish_spack_optspecs_spack_config_get h/help -complete -c spack -n '__fish_spack_using_command_pos 0 config get' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop mirrors modules packages repos upstreams view' +complete -c spack -n '__fish_spack_using_command_pos 0 config get' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop env_vars mirrors modules packages repos upstreams view' complete -c spack -n '__fish_spack_using_command config get' -s h -l help -f -a help complete -c spack -n '__fish_spack_using_command config get' -s h -l help -d 'show this help message and exit' # spack config blame set -g __fish_spack_optspecs_spack_config_blame h/help -complete -c spack -n '__fish_spack_using_command_pos 0 config blame' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop mirrors modules packages repos upstreams view' +complete -c spack -n '__fish_spack_using_command_pos 0 config blame' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop env_vars mirrors modules packages repos upstreams view' complete -c spack -n '__fish_spack_using_command config blame' -s h -l help -f -a help complete -c spack -n '__fish_spack_using_command config blame' -s h -l help -d 'show this help message and exit' # spack config edit set -g __fish_spack_optspecs_spack_config_edit h/help print-file -complete -c spack -n '__fish_spack_using_command_pos 0 config edit' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop mirrors modules packages repos upstreams view' +complete -c spack -n '__fish_spack_using_command_pos 0 config edit' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop env_vars mirrors modules packages repos upstreams view' complete -c spack -n '__fish_spack_using_command config edit' -s h -l help -f -a help complete -c spack -n '__fish_spack_using_command config edit' -s h -l help -d 'show this help message and exit' complete -c spack -n '__fish_spack_using_command config edit' -l print-file -f -a print_file