Skip to content
2 changes: 1 addition & 1 deletion src/common-utils/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "common-utils",
"version": "2.1.0",
"version": "2.1.1",
"name": "Common Utilities",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/common-utils",
"description": "Installs a set of common command line utilities, Oh My Zsh!, and sets up a non-root user.",
Expand Down
76 changes: 45 additions & 31 deletions src/common-utils/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,15 @@ if [ "${USERNAME}" != "root" ] && [ "${EXISTING_NON_ROOT_USER}" != "${USERNAME}"
EXISTING_NON_ROOT_USER="${USERNAME}"
fi

# *********************************
# ** Ensure config directory **
# *********************************
user_config_dir="${user_home}/.config"
if [ ! -d "${user_config_dir}" ]; then
mkdir -p "${user_config_dir}"
chown ${USERNAME}:${group_name} "${user_config_dir}"
fi

Comment on lines +379 to +387

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if [ "${USERNAME}" = "root" ]; then
user_home="/root"
else
user_home="/home/${USERNAME}"
if [ ! -d "${user_home}" ]; then
mkdir -p "${user_home}"
chown ${USERNAME}:${group_name} "${user_home}"
fi
fi
configures user_home, hence, should this be added after that?

@naturedamends naturedamends Aug 29, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good catch. I want a clean home directory, so if im going to program this, I want it before any rc files. Not sure how much of a pain this is going to be

I came to the conclusion that you would not want any related flags in common-utils

Is the shell-setup feature going to be in features repo?
Then would it be prudent to add a flag to prevent any rc generation here

Thus you can enable the following to get equal functionality:
Common-utils rc false
Shell-setup shell zsh

I'm not familiar with any other fancy shells. Other than one I heard of that has a consistent output table format.

Would you consider adding a shell-setup to the features repo?

Sorry for bad grammar on tablet

# *********************************
# ** Shell customization section **
# *********************************
Expand Down Expand Up @@ -428,6 +437,8 @@ fi

# Optionally configure zsh and Oh My Zsh!
if [ "${INSTALL_ZSH}" = "true" ]; then
umask g-w,o-w

if [ "${ZSH_ALREADY_INSTALLED}" != "true" ]; then
if [ "${ADJUSTED_ID}" = "rhel" ]; then
global_rc_path="/etc/zshrc"
Expand All @@ -450,57 +461,60 @@ if [ "${INSTALL_ZSH}" = "true" ]; then
chsh --shell /bin/zsh ${USERNAME}
fi

# Adapted, simplified inline Oh My Zsh! install steps that adds, defaults to a codespaces theme.
# See https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/install.sh for official script.
if [ "${INSTALL_OH_MY_ZSH}" = "true" ]; then
user_rc_file="${user_home}/.zshrc"
oh_my_install_dir="${user_home}/.oh-my-zsh"
template_path="${oh_my_install_dir}/templates/zshrc.zsh-template"
if [ ! -d "${oh_my_install_dir}" ]; then
umask g-w,o-w
mkdir -p ${oh_my_install_dir}
# Adapted, simplified inline Oh My Zsh! install steps that adds, defaults to a codespaces theme.
# See https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/install.sh for official script.
omz_source_dirname=".oh-my-zsh"
user_omz_install_dir="${user_home}/${omz_source_dirname}"
omz_added_filesnames=("${omz_source_dirname}")
if [ ! -d "${user_omz_install_dir}" ]; then
mkdir -p ${user_omz_install_dir}
git clone --depth=1 \
-c core.eol=lf \
-c core.autocrlf=false \
-c fsck.zeroPaddedFilemode=ignore \
-c fetch.fsck.zeroPaddedFilemode=ignore \
-c receive.fsck.zeroPaddedFilemode=ignore \
"https://github.com/ohmyzsh/ohmyzsh" "${oh_my_install_dir}" 2>&1

"https://github.com/ohmyzsh/ohmyzsh" "${user_omz_install_dir}" 2>&1
# Shrink git while still enabling updates
cd "${oh_my_install_dir}"
git repack -a -d -f --depth=1 --window=1
GIT_WORK_TREE="${user_omz_install_dir}" GIT_DIR="${user_omz_install_dir}/.git" git repack\
-a -d -f --depth=1 --window=1
fi

# Add Dev Containers theme
mkdir -p ${oh_my_install_dir}/custom/themes
cp -f "${FEATURE_DIR}/scripts/devcontainers.zsh-theme" "${oh_my_install_dir}/custom/themes/devcontainers.zsh-theme"
ln -sf "${oh_my_install_dir}/custom/themes/devcontainers.zsh-theme" "${oh_my_install_dir}/custom/themes/codespaces.zsh-theme"
# Add dev containers theme
user_omz_theme_filepath="${user_omz_install_dir}/custom/themes"
user_devcontainer_theme_target="${user_omz_theme_filepath}/devcontainers.zsh-theme"
user_codespaces_theme_target="${user_omz_theme_filepath}/codespaces.zsh-theme"
theme_template_path="${FEATURE_DIR}/scripts/devcontainers.zsh-theme"
mkdir -p "${user_omz_theme_filepath}"
cp -f "${theme_template_path}" "${user_devcontainer_theme_target}"
cp -f "${theme_template_path}" "${user_codespaces_theme_target}"

# Add devcontainer .zshrc template
if [ "$INSTALL_OH_MY_ZSH_CONFIG" = "true" ]; then
echo -e "$(cat "${template_path}")\nDISABLE_AUTO_UPDATE=true\nDISABLE_UPDATE_PROMPT=true" > ${user_rc_file}
# Add devcontainer .zshrc template
omz_rc_filename=".zshrc"
user_rc_file="${user_home}/${omz_rc_filename}"
omz_zshrc_template_path="${user_omz_install_dir}/templates/zshrc.zsh-template"
echo -e "$(cat "${omz_zshrc_template_path}")\nDISABLE_AUTO_UPDATE=true\nDISABLE_UPDATE_PROMPT=true" > ${user_rc_file}
sed -i -e 's/ZSH_THEME=.*/ZSH_THEME="devcontainers"/g' ${user_rc_file}
omz_added_filesnames+=("${omz_rc_filename}")
fi
# TODO remove installed files from previous step

user_omz_filepaths=( "${omz_added_filesnames[@]/#/$user_home/}" )

# Copy to non-root user if one is specified
if [ "${USERNAME}" != "root" ]; then
copy_to_user_files=("${oh_my_install_dir}")
[ -f "$user_rc_file" ] && copy_to_user_files+=("$user_rc_file")
cp -rf "${copy_to_user_files[@]}" /root
chown -R ${USERNAME}:${group_name} "${oh_my_install_dir}" "${user_rc_file}"
# Copy files to alternate user if one is specified
cp -rf "${user_omz_filepaths[@]}" /root
# Set permissions for root user
chown -R root:root "${omz_added_filesnames[@]/#//root/}"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, why is this needed?

@naturedamends naturedamends Aug 29, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security. User scripts making it into root and being sourced.

Would be tempted to copy from a mktmp-d. To prevent copying any unknown files

Ie custom shell scripts
Or scripts appended to users linked template

Nvm. I will close pr.

fi

# Set permissions for current user
chown -R "${USERNAME}:${group_name}" "${user_omz_filepaths[@]}"
fi
fi

# *********************************
# ** Ensure config directory **
# *********************************
user_config_dir="${user_home}/.config"
if [ ! -d "${user_config_dir}" ]; then
mkdir -p "${user_config_dir}"
chown ${USERNAME}:${group_name} "${user_config_dir}"
fi

# ****************************
# ** Utilities and commands **
Expand Down
12 changes: 12 additions & 0 deletions test/common-utils/configure_zsh_no_template_first_step.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
check "default-zsh-with-no-zshrc" bash -c "[ ! -e ~/.zshrc ]"

# Report result
reportResults
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
function file_not_overridden() {
cat ~/.zshrc | grep 'alias fnomockalias=' | grep testingmock
cat $1 | grep 'alias fnomockalias=' | grep testingmock
}
check "default-zsh-with-no-zshrc" file_not_overridden
check "default-zsh-with-no-zshrc" file_not_overridden /home/devcontainer/.zshrc

# Report result
reportResults
16 changes: 13 additions & 3 deletions test/common-utils/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,20 @@
}
}
},
"configure_zsh_no_template": {
"configure_zsh_no_template_second_step": {
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"postCreateCommand": "echo alias fnomockalias=testingmock >> /root/.zshrc",
"remoteUser": "root",
"postCreateCommand": "echo alias fnomockalias=testingmock >> /home/devcontainer/.zshrc",
"remoteUser": "devcontainer",
"features": {
"common-utils": {
"installZsh": true,
"installOhMyZshConfig": false
}
}
},
"configure_zsh_no_template_first_step": {
"image": "debian:bullseye",
"remoteUser": "devcontainer",
"features": {
"common-utils": {
"installZsh": true,
Expand Down
3 changes: 2 additions & 1 deletion test/common-utils/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ check "git" git --version
check "zsh" zsh --version
check "ps" ps --version
check "Oh My Zsh! theme" test -e $HOME/.oh-my-zsh/custom/themes/devcontainers.zsh-theme
check "zsh theme symlink" test -e $HOME/.oh-my-zsh/custom/themes/codespaces.zsh-theme
check "zsh theme filename" test -e $HOME/.oh-my-zsh/custom/themes/codespaces.zsh-theme

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would take the meaning of -e and symlink to mean xor... not sure Samruddhi Khandale (@samruddhikhandale)

check "oh-my-zsh executes" zsh -c 'source $HOME/.zshrc && echo $0 | grep zsh'

# Report result
reportResults