Skip to content

Commit c40747a

Browse files
bcummingRMeli
andauthored
add a guide for creating custom uenv environments (#206)
In response to https://jira.cscs.ch/browse/SD-66316 https://docs.tds.cscs.ch/206/software/uenv/#custom-environments --------- Co-authored-by: Rocco Meli <[email protected]>
1 parent 3ddd89e commit c40747a

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

docs/guides/terminal.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,49 @@ export PATH=$xdgbase/bin:$PATH
7676
!!! note "XDG what?"
7777
The [XDG base directory specification](https://specifications.freedesktop.org/basedir-spec/latest/) is used by most applications to determine where to look for configurations, and where to store data and temporary files.
7878

79+
[](){#ref-guides-terminal-bashrc}
80+
## Modifying bashrc
81+
82+
The `~/.bashrc` in your home directory is executed __every time__ you log in, and there is no way to log in without executing it.
83+
84+
It is strongly recommended that customization in `~/.bashrc` should be kept to the bare minimum:
85+
86+
1. It sets a fixed set of environment options every time you log in, and all downstream scripts and Slurm batch jobs might assume that these commands have run, so that later modifications to `~/.bashrc` can break workflows in ways that are difficult to debug.
87+
* If a script or batch job requires environment modifications, implement them there.
88+
* In other words, move the definition of environment used by a workflow to the workflow definition.
89+
1. It makes it difficult for CSCS to provide support, because it is difficult for support staff to reproduce your environment, and it can take a lot of back and forth before we determine that the root cause of an issue is a command in `~/.bashrc`.
90+
91+
92+
!!! warning "Do not call `module` in bashrc"
93+
Calls to `module use` and `module load` in `~/.bashrc` is possible, however avoid it for the reasons above.
94+
If there are module commands in your `~/.bashrc`, remember to provide a full copy of `~/.bashrc` with support tickets.
95+
96+
!!! danger "Do not call `uenv` in bashrc"
97+
The `uenv` command is designed for creating isolated environments, and calling it in `~/.bashrc` will not work as expected.
98+
See the [uenv docs][ref-uenv-customenv] for more information about how to create bespoke uenv environments that can be started with a single command.
99+
100+
??? note "Help, I broke bashrc!"
101+
It is possible to add commands to bashrc that will stop you from being able to log in.
102+
The author of these docs has done it more than once, after ignoring their own advice.
103+
104+
For example, if the command `exit` is added to `~/.bashrc` you will be logged out every time you log in.
105+
106+
The first thing to try is to execute a command that will back up `~/.bashrc`, and remove `~/.bashrc`:
107+
```bash
108+
ssh eiger.cscs.ch 'bash --norc --noprofile -c "mv ~/.bashrc ~/.bashrc.back"'
109+
```
110+
If this works, you can then log in normally, and edit the backup and copy it back to `~/.bashrc`.
111+
112+
If there is a critical error, like calling `exit`, the approach above won't work.
113+
In such cases, the only solution that doesn't require root permissions is to log in and hit `<ctrl-c>` during the log in.
114+
With luck, this will cancel the login process before `~/.bashrc` is executed, and you will be able to edit and fix `~/.bashrc`.
115+
Note that you might have to try a few times to get the timing right.
116+
117+
If this does not work, create a [service desk ticket][ref-get-in-touch] with the following message:
118+
119+
!!! example "Help request"
120+
My bashrc has been modified, and I can't log in any longer to `insert-system-name`.
121+
My username is `insert-cscs-username`.
122+
Can you please make a backup copy of my bashrc, i.e. `mv ~/.bashrc ~/.bashrc.back`,
123+
so that I can log in and fix the issue.
124+

docs/software/uenv/index.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,66 @@ uenv image find @'*'%gh200
601601

602602
!!! note
603603
The wild card `*` used for "all systems" must always be escaped in single quotes: `@'*'`.
604+
605+
[](){#ref-uenv-customenv}
606+
## Custom environments
607+
608+
!!! warning "[Keep bashrc clean][ref-guides-terminal-bashrc]"
609+
610+
It is common practice to add `module` commands to `~.bashrc`, for example
611+
```bash title="~/.bashrc"
612+
# make my custom modules available
613+
module use $STORE/myenv/modules
614+
# load the modules that I always want in the environment
615+
module load ncview
616+
```
617+
618+
This will make custom modules available, and load `ncview`, every time you log in.
619+
It is not possible to do the equivalent with `uenv start`, for example:
620+
```bash title="~/.bashrc"
621+
# start the uenv that I always use
622+
uenv start prgenv-gnu/24.11:v2 --view=default
623+
# ERROR: the following lines will not be executed
624+
module use $STORE/myenv/modules
625+
module load ncview
626+
```
627+
628+
!!! question "Why can't I use `uenv start` in `~/.bashrc`?"
629+
The `module` command uses some "clever" tricks to modify the environment variables in your current shell.
630+
For example, `module load ncview` will modify the value of environment variables like `PATH`, `LD_LIBRARY_PATH`, and `PKG_CONFIG_PATH`.
631+
632+
The `uenv start` command loads a uenv, and __starts a new shell__, ready for you to enter commands.
633+
This means that lines in the `.bashrc` that follow the command are never executed.
634+
635+
Things are further complicated because if `uenv start` is executed inside `~/.bashrc`, the shell is not a tty shell.
636+
637+
It is possible to create a custom command that will start a new shell with a uenv loaded, with additional customizations to the environment (e.g. loading modules and setting environment variables).
638+
639+
The first step is to create a script that performs the the customization steps to perform once the uenv has been loaded.
640+
Here is an example for an environment called `myenv`:
641+
642+
```bash title="~/.myenvrc"
643+
# always add this line
644+
source ~/.bashrc
645+
646+
# then add customization commands here
647+
module use $STORE/myenv/modules
648+
module load ncview
649+
export DATAPATH=$STORE/2025/data
650+
```
651+
652+
Then create an alias in `~/.bashrc` for the `myenv` environment:
653+
654+
```bash title="~/.bashrc"
655+
alias myenv='uenv run prgenv-gnu/24.11:v2 --view=default -- bash --rcfile ~/.myenvrc'
656+
```
657+
658+
This alias uses `uenv run` to start a new bash shell that will apply the customizations in `~/.myenvrc` once the uenv has been loaded.
659+
Then, the environment can be started with a single command once logged in.
660+
661+
```console
662+
$ ssh eiger.cscs.ch
663+
$ myenv
664+
```
665+
666+
The benefit of this approach is that you can create multiple environments, whereas modifying `.bashrc` will lock you into using the same environment every time you log in.

0 commit comments

Comments
 (0)