From 87f91867b2a656fe783c928d616af22406cb9aab Mon Sep 17 00:00:00 2001 From: Peter Pathirana Date: Thu, 1 Oct 2026 00:06:26 +0000 Subject: [PATCH] fix(template): fix dotfiles script --- .../homelab-workspace/parameters.tf | 1 + .../scripts/script-dotfiles.sh | 34 ++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/templates/kubernetes/homelab-workspace/parameters.tf b/templates/kubernetes/homelab-workspace/parameters.tf index 185a955f..a8565d25 100644 --- a/templates/kubernetes/homelab-workspace/parameters.tf +++ b/templates/kubernetes/homelab-workspace/parameters.tf @@ -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" diff --git a/templates/kubernetes/homelab-workspace/scripts/script-dotfiles.sh b/templates/kubernetes/homelab-workspace/scripts/script-dotfiles.sh index 50843762..b1fee2fd 100755 --- a/templates/kubernetes/homelab-workspace/scripts/script-dotfiles.sh +++ b/templates/kubernetes/homelab-workspace/scripts/script-dotfiles.sh @@ -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 @@ -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 @@ -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"