Skip to content

Commit

Permalink
feat(chezmoi): add dotfiles-apply-rootmoi and initialize-zsh commands
Browse files Browse the repository at this point in the history
  • Loading branch information
entelecheia committed May 25, 2023
1 parent 10d4173 commit 968192d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
# {{- if and (eq .chezmoi.os "linux") (.system.apply_rootmoi) (.system.is_sudoer) }}
# shellcheck source=/dev/null
source "${HOME}/.config/shrc/00-dotfiles-export"
# dotfiles-apply-rootmoi hash: {{ include "dot_local/bin/executable_dotfiles-apply-rootmoi.tmpl" | sha256sum }}
dotfiles-apply-rootmoi
# {{- end }}
55 changes: 55 additions & 0 deletions chezmoi/dot_local/bin/executable_dotfiles-apply-rootmoi.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
# {{ if and (eq .chezmoi.os "linux") (.system.is_sudoer) -}}
# {{ template "scripts-library" }}
log_task "Installing dotfiles for system user with rootmoi"

# shellcheck disable=all
original_args=({{ range (rest .chezmoi.args) }} {{ . | quote }}{{ end }})
args=(apply)
diff_args=(diff)

# Filter incompatible args
skip_one_more=false
remove_apply=false
for i in "${!original_args[@]}"; do
if [[ "${skip_one_more}" == true ]]; then
skip_one_more=false
continue
fi

if [[ "${original_args[i]}" == "-S" || "${original_args[i]}" == "--source" ]]; then
skip_one_more=true
continue
fi

if [[ "${original_args[i]}" == "-S="* || "${original_args[i]}" == "--source="* ]]; then
continue
fi

# We will always apply, so we don't need any --apply flags
if [[ "${original_args[i]}" == "-a" || "${original_args[i]}" == "-a="* || "${original_args[i]}" == "--apply" || "${original_args[i]}" == "--apply="* ]]; then
continue
fi

# --init never makes sense, because rootmoi's configuration is handled by chezmoi earlier
if [[ "${original_args[i]}" == "--init" ]]; then
continue
fi

# Remove any positional args, as we will always use apply
if [[ "${original_args[i]}" != "-"* ]]; then
continue
fi

args+=("${original_args[i]}")
diff_args+=("${original_args[i]}")
done

# .config/rootmoi/.rootmoiversion hash: {{ include "dot_config/rootmoi/dot_rootmoiversion" | sha256sum }}
ROOTMOI_VERSION=$(cat "${HOME}/.config/rootmoi/.rootmoiversion")
ROOTMOI='{{ joinPath .chezmoi.homeDir ".local/bin/rootmoi" }}'
log_task "Applying root dotfiles with rootmoi (version ${ROOTMOI_VERSION})"
log_c "rootmoi" "${args[@]}"
exec "${ROOTMOI}" "${args[@]}"

# {{ end -}}
29 changes: 29 additions & 0 deletions chezmoi/dot_local/bin/executable_dotfiles-initialize-zsh.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
# {{- template "scripts-library" }}
log_task "Initializing ZSH"

# {{- if and (eq .chezmoi.os "linux") }}

zsh_path="/bin/zsh"

if ! getent passwd '{{ .chezmoi.username }}' | cut -d : -f 7- | grep -q "^${zsh_path}$"; then
log_task "Configuring ZSH as default shell"

# {{ if .system.is_sudoer }}
c sudo usermod --shell "${zsh_path}" '{{ .chezmoi.username }}'
# {{ else }}
c chsh --shell "${zsh_path}"
# {{ end }}
fi

# {{- else if eq .chezmoi.os "darwin" }}

if [ "$SHELL" != "$(which zsh)" ]; then
log_task "Configuring ZSH as default shell"

# c sudo dscl . -create "/Users/{{ .chezmoi.username }}" UserShell "$(which zsh)"
c sudo sh -c "echo $(which zsh) >> /etc/shells"
c sudo chsh -s "$(which zsh)" "{{ .chezmoi.username }}"
fi

# {{- end }}

0 comments on commit 968192d

Please sign in to comment.