Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions templates/kubernetes/homelab-workspace/parameters.tf
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ data "coder_parameter" "dotfiles_url" {
display_name = "Dotfiles Repository"
description = "Optional HTTPS Git repository to apply with Chezmoi when the workspace starts"
icon = "/icon/git.svg"
mutable = true
type = "string"
form_type = "input"

Expand Down
34 changes: 25 additions & 9 deletions templates/kubernetes/homelab-workspace/scripts/script-dotfiles.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#!/bin/bash
set -euo pipefail

state_dir="${HOME}/.local/state/dotfiles"
mkdir -p "${state_dir}"
rm -f "${state_dir}/applied" "${state_dir}/failed"
exec > >(/usr/bin/tee "${state_dir}/run.log") 2>&1
ENV_VARS_FILE="/home/coder/.env.pod"
STATE_DIR="${HOME}/.local/state/dotfiles"
mkdir -p "${STATE_DIR}"
rm -f "${STATE_DIR}/applied" "${STATE_DIR}/failed"
exec > >(/usr/bin/tee "${STATE_DIR}/run.log") 2>&1
record_failure() {
local status="$?"
if (( status != 0 )); then
printf '%s\n' "${status}" >"${state_dir}/failed"
printf '%s\n' "${status}" >"${STATE_DIR}/failed"
fi
}
trap record_failure EXIT

if [[ -z "${DOTFILES_URL}" ]]; then
echo "no dotfiles repository configured"
else
install_chezmoi_if_needed() {
chezmoi_bin="${HOMEBREW_PREFIX}/bin/chezmoi"
if [[ ! -x "${chezmoi_bin}" ]]; then
"${HOMEBREW_PREFIX}/bin/brew" install chezmoi
fi
}

configure_chezmoi_if_needed() {
config_dir="${XDG_CONFIG_HOME:-${HOME}/.config}/chezmoi"
config_file="${config_dir}/chezmoi.toml"
if [[ ! -e "${config_file}" ]]; then
Expand All @@ -37,8 +38,15 @@ else
printf 'bwsAccessToken = ""\n'
} >"${config_file}"
fi
}

run_chezmoi() {
chezmoi_bin="${HOMEBREW_PREFIX}/bin/chezmoi"
source_dir="$(${chezmoi_bin} source-path)"

if [[ -f "${ENV_VARS_FILE}" ]]; then
set -a; source "${ENV_VARS_FILE}"; set +a
fi
if [[ -d "${source_dir}/.git" ]]; then
"${chezmoi_bin}" update --skip-secrets
else
Expand All @@ -52,6 +60,14 @@ else
"${chezmoi_bin}" init --apply --skip-secrets
fi
fi
}

if [[ -z "${DOTFILES_URL}" ]]; then
echo "no dotfiles repository configured"
else
install_chezmoi_if_needed
configure_chezmoi_if_needed
run_chezmoi
fi

touch "${state_dir}/applied"
touch "${STATE_DIR}/applied"
Loading