How do I use different environments in zsh and oh-my-zsh - zsh

I want to use different .zshrc and .zshprofile in different environments (or the same effort), how do I do that?

Related

Setting R's default behaviour in .Rprofile and .Renviron. Understanding what to do where

I am trying to understand the use of the files .Renviron and .Rprofile. If I understand correctly, the .Rprofile is sort of a startup script, sourced as R code, that sets the options and environment variables that a user may want either all the time, or for a specific project. On the other hand .Renviron is loaded before .Rproject, and defines environment variables only.
By design I understand that R will load either the user or the project level .Renviron and .Rprofile files, but it won't load both user and project level files. Essentially, R will only load the project specific .Rprofile and .Renviron files, provided they are defined. That said, some libraries and functions would be prudent to put in the user level .Rprofile, as I need it pretty much all the time (e.g. I use dplyr syntax a lot), while at the same time I'd like to load project specific libraries and functions as well.
The purpose of the .Renviron file is more elusive to me. From what I understand, its purpose is to store environment variables, such as passwords, API keys, etc.. However, I can also set environment variables in .Rprofile using Sys.setenv(). For example, I have the environment variable set in a project's .Rprofile, to use parallelization with the package below:
Sys.setenv(OMP_NUM_THREADS=parallel::detectCores())
library(OpenMx)
Since the .Renviron doesn't use code, my assumption is I could've put this line in a .Renviron file with the following syntax:
OMP_NUM_THREADS=[number of cores]
However, I find little useful information on how to set environment variables in .Renviron, and what is advisable to put here.
My questions therefore are:
How can I load both user and project level .Renviron and .Rproject files when working in a project?
What environment variables would I typically put in .Renviron? (Any list or tutorials on how to set variables would be appreciated.)
In which cases would it be recommended to add environment variables to .Renviron over using Sys.setenv() in .Rprofile, and vice versa?
However, I can also set environment variables in .Rprofile using Sys.setenv().
"Yes but" these can under standard POSIX behaviour not alter the running process for which the variables have to be set before.
I just like you tried to get by for as long as I could with only ~/.Rprofile (or even just Rprofile.site for the whole machine) but eventually added things in .Renviron for
R_LIBS_USER to "" as I prefer not to have installations below ~
R_MAX_NUM_DLLS which has to be here
plus a few tokens for services
plus a reticulate option
plus a R CMD check option
so in some cases you do in fact have to use .Renviron (or Renvirob.site).

How should I install a zsh completion function so that it works for all users, regardless of their config?

I have written a script for internal use in my job (it's not of much use to anyone outside of my work). I've got a decent zsh completion script for it now which works great. I'd like to add an --install-zsh-completions flag to my script, which sets up completions automatically.
I see that there are a variety of different ways that zsh can be set up, depending on whether you use oh-my-zsh, prezto or your own configuration. (For example, in oh-my-zsh, you can put completion scripts in ~/.oh-my-zsh/completions and they are loaded.)
Is there a standard place or way in zsh to install custom completion functions, so that they work for all users, regardless of their zsh config? (For example, should I just modify the .zshrc file and add to the fpath? )
Is there a standard place or way in zsh to install zsh custom completion scripts?
Yes, there is: Create a symlink in /usr/local/share/zsh/site-functions that points to your completion function. That's all there is to it.
This dir is by default in every user's $fpath. So, when they (or whatever framework they're using) calls compinit, it will be picked up automatically.

Streamlit: Disable the guard against running files without a .py extension?

I have a problem. Universally, my experience working in Unix systems has been that, by the time you are ready to place an executable "thing" in a bin folder for global access, you have decided to #! the file with the requisite interpreter:
#!/bin/awk
#!/bin/bash
#!/bin/perl
#!/bin/python3.8
#!/bin/whatever
And, although it is fine to have clutter at the local scope, when one places an executable in the bin folder, it should have:
A POSIX CLI interface
No discernible language tags or what have you
This is because it is now intended to be used for difficult work that requires forgetting about the details of this or that language: one now needs to think in terms of the functions as if the composable units are part of a consistent language, rather than a dozen different languages from a dozen different expert contributors.
This is the "genius" of the Unix/Linux/Posix architecture.
Anyways, when structuring my python projects, the end game is copying python executables to a global source on the path -- whether that "global" source is a pretend global source in my home directory (i.e., ~/.mytools/bin or the actual global path, /usr/bin or something like that -- and generally I want my python executables to have the same "game feel" as C executables, perl executables, BASH/ZSH/etc. executables.
In that vein, I knock off the extensions from my scripts and executables when they go in the bin. There is no need to know, from my usage perspective, what anything is made of when I go to use it.
However, streamlit requires me to re-append the .py to the file in the global path in order to run with streamlit run. This is a case of the library reaching up out of its useful value and holding me hostage, from my perspective, unless I violate best practices when extending the bin folder with python executables.
This means I have to create special logic to handle just streamlit, and that is really a kerfluffle. I have to either: change the way I handle all executables, or hardcode just the executable that will be run with streamlit. That means that, all of a sudden, I have an arbitrary name in my meta-control code for my project.
That is bad. Why? because I have to remember that I did it, and remember to change it if I change the executable name. I also have to remember to add to it if I add another streamlit executable.
Alternatively, I can copy all my exes made with python into the root bin folders with their .py extensions, which is not what I wanted to do.
How does one bypass this issue in streamlit?
If bin/sometool needs to be invoked with Streamlit via streamlit run bin/sometool, it seems like you're already exposing "meta-control code" to users of your bin script, right?
Instead, would this solve your problem?
bin/sometool:
#!/bin/bash
DIR=$(dirname "$0")
streamlit run "$DIR"/the_actual_script.py
(Where the_actual_script.py sits inside bin, but has chmod -x so that it's not directly executable.)

How can I permanently set an environment variable using Autotools?

I'm adapting an existing program to use Autotools for its build, but the resulting process depends on an environment variable. Is there a way to permanently set this environment variable during the build or installation process?
The program is intended to be used by Unix users and I could try to concatenate an export command directly to the .bashrc file and warn the user in case it fails because most of them will actually just use Ubuntu to run it (it's a relatively simple program that targets students), but I'd like to know if there's a more portable way to do this.
That's what I wouldn't like to do:
export VAR=/my/totally/not/hardcoded/path >> $HOME/.bashrc
Sorry to come to this late, but all of the answers to date are shockingly ... incomplete.
Building and installing software are both core use cases for the Autotools, and the installation part can absolutely involve adding or modifying files that affect user environments. If the software is installed by a user with sufficient privilege, then such effects can absolutely be applied to all system users, though the details may vary a bit from system to system (and the Autotools can help with that, too!).
For example, on RedHat-family Linuxes such as RedHat Enterprise, Fedora, Oracle Linux, and various others, you can drop an appropriately named file in /etc/profile.d, and the commands in it will automatically be read and executed by every login shell. Setting environment variables for all users is one of the common uses of this feature. I'm uncertain about Debian-family Linuxes such as Ubuntu, but it is always possible to modify file /etc/profile instead to have the same effect, and you absolutely can write an Automake install hook to do that.
Or for an altogether different approach, you can always provide a wrapper script around your program that sets the needed environment variables (supposing that the point is other than to add a directory to the PATH so as to find the program in the first place). In that case, you can even install the main program in a location that is not ordinarily in the path, so that users don't accidentally run it directly. This mechanism has the advantage that the environment variables are scoped to a run of the program, not a whole login session, but the disadvantage that users cannot override them.
I guess, no.
Autotools are about building your program, not about environment setup for the program to run. That's what users/admins are supposed to do. (Well I can imagine doing this, but I really don't want to try to figure it out, because the idea itself seems broken to me)
If your program REALLY needs some environment variable during run-time, then you should patch your sources for your application to test if the variable exists, and set one to default desired value, if it doesn't. Another idea is to enforce usage of an obligatory command line switch to pass the value in.
It's not clear what this has to do with autotools (or any other build system). No build system, by itself, can arrange for an env var to be present when the program it builds is run at a later tiem.
One solution is for your program to have a hardcoded default value for the var which is used if the environment var isn't present when the program starts running. Another frequently used solution is to name your binary something like myprog.bin and install a shell script named myprog which sets up the environment before doing exec myprog.bin.
I'm adapting an existing program to use Autotools for its build, but the resulting process depends on an environment variable. Is there a way to permanently set this environment variable during the build or installation process?
You've not been very concrete about what the program is (e.g. is the program a daemon? A user program?) or the nature of the environment variable dependency (e.g. is it another program? A mount point? A URL? A DB connection string?). Being more specific might give a better answer for you.
Anyway, autotools is not likely to offer any feature to help: It's a build system. Depending on the nature of your environment variable dependency, you're likely going to need package management (if you package it) or system administration level setup.
Since you think your primary user base is on Ubuntu this help page might give you some ideas.

Create own unix commands

Is it possible to create our own unix commands?
For example: we have ls -ltr,cd,mkdir etc which perform certain actions. I want to create a similar command which would save username-password into a table in database. I'm kinda new to unix. Any suggestions?
Yes, it is easy to create your own commands that do jobs that you find useful. You can implement them in a variety of languages, from shell to Perl to C and on and on.
The only significance to the standard commands are that they are installed (usually) in /bin or /usr/bin rather than anywhere else, and they do jobs that are defined by a standard (often POSIX). Often, people place locally created commands in /usr/local/bin; others will create themselves a directory $HOME/bin and put their personal commands there. You simply need to ensure that these directories are on your PATH.
In my $HOME/bin directory (depending on which machine I'm looking at), I have from 46 commands (on this machine) up to about 500 on my main work machines. The commands do different jobs; the names are mnemonic to me (and generally not to other people). Some commands are polished and ready for production use anywhere (and these have manual pages, almost by definition of being production-ready). Others are quick hacks assembled for a quick-and-dirty job. Some of the quick hacks are removed; some get polished; some get stashed away in case I need to do something similar in the future. Only the trivial don't go under version control.
On this machine (which I only use casually and not really for development work), I have 9 shell scripts, 4 Perl scripts, and the rest are executables (Git and Go, mainly). On my main machines, I have many more shell and Perl scripts and proportionately fewer C programs. I have few Python scripts since I learned Perl first and I'm not as fluent in Python. I've been writing and collecting these scripts for a long time; the oldest versions of the oldest programs date back to about 1987.

Resources