Skip to content

Commit

Permalink
Fix installation of template pre-requisites (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs authored Apr 28, 2024
1 parent 4262c2a commit 9b8abec
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 46 deletions.
5 changes: 5 additions & 0 deletions home/.chezmoi.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ status:
exclude:
- always

hooks:
read-source-state:
pre:
command: {{ .chezmoi.workingTree }}/home/.chezmoihooks/ensure-pre-requisites.sh

{{/* Here we "export" the variables, so we can access them outside this file */ -}}
data:
is_wsl: {{ $wsl }}
Expand Down
32 changes: 32 additions & 0 deletions home/.chezmoihooks/ensure-pre-requisites.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# This script must exit as fast as possible when pre-requisites are already
# met, so we only import the scripts-library when we really need it.

set -eu

wanted_packages=(
git # used to find the latest revisions of github repositories
curl # used to find the latest version of github repositories
zsh
)

missing_packages=()

for package in "${wanted_packages[@]}"; do
if ! command -v "${package}" >/dev/null; then
missing_packages+=("${package}")
fi
done

if [[ ${#missing_packages[@]} -eq 0 ]]; then
exit 0
fi

# shellcheck source=../.chezmoitemplates/scripts-library
source "${CHEZMOI_SOURCE_DIR?}/.chezmoitemplates/scripts-library"

log_task "Installing missing packages with APT: ${missing_packages[*]}"

c sudo apt update
c sudo apt install --yes --no-install-recommends "${missing_packages[@]}"
36 changes: 0 additions & 36 deletions home/.chezmoiscripts/run_before_30-install-prerequisites.sh.tmpl

This file was deleted.

6 changes: 3 additions & 3 deletions home/.chezmoitemplates/scripts-library
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function sudo() {

# shellcheck disable=SC2312
if [[ "$(id -u)" -eq 0 ]]; then
if [[ "${exec}" == "true" ]]; then
if [[ "${exec}" == true ]]; then
exec "$@"
else
"$@"
Expand All @@ -93,7 +93,7 @@ function sudo() {
log_manual_action "Root privileges are required, please enter your password below"
command sudo --validate
fi
if [[ "${exec}" == "true" ]]; then
if [[ "${exec}" == true ]]; then
exec sudo "$@"
else
command sudo "$@"
Expand All @@ -108,7 +108,7 @@ function is_apt_package_installed() {
}

function not_during_test() {
if [[ "${DOTFILES_TEST:-}" == "true" ]]; then
if [[ "${DOTFILES_TEST:-}" == true ]]; then
log_info "Skipping '${*}' because we are in test mode"
else
"${@}"
Expand Down
16 changes: 14 additions & 2 deletions home/dot_config/rootmoi/private_chezmoi.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
{{- $chezmoiData := deepCopy . -}}
{{- $chezmoiData = unset $chezmoiData "chezmoi" -}}

sourceDir: "{{ joinPath .chezmoi.workingTree "root" }}"
{{- $sourceDir := joinPath .chezmoi.workingTree "root" -}}
sourceDir: "{{ $sourceDir }}"

destDir: "/"

verbose: true

# https://github.com/twpayne/chezmoi/issues/3257
pager: ""

diff:
exclude:
- scripts
status:
exclude:
- always

hooks:
read-source-state:
pre:
command: {{ $sourceDir }}/.chezmoihooks/ensure-pre-requisites.sh

data:
non_root_user: "{{ .chezmoi.username }}"
{{ $chezmoiData | toYaml | indent 2 }}
{{- $chezmoiData | toYaml | nindent 2 }}
9 changes: 8 additions & 1 deletion home/dot_local/bin/executable_rootmoi.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ fi
# shellcheck disable=SC2016
mkdir -p '{{ $configDir }}' '{{ $cacheDir }}'

sudo exec --preserve-env=PATH -- \
# We should not use sudo's --preserve-env option because the sudo function will
# automatically bypass sudo if the user is root already.
env_args=(env "PATH=${PATH}")
if [[ "${DOTFILES_TEST:-}" == true ]]; then
env_args+=("DOTFILES_TEST=true")
fi

sudo exec "${env_args[@]}" \
"${executable}" "$@" \
--config='{{ $configFile }}' \
--persistent-state='{{ $persistentStateFile }}' \
Expand Down
30 changes: 30 additions & 0 deletions root/.chezmoihooks/ensure-pre-requisites.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# This script must exit as fast as possible when pre-requisites are already
# met, so we only import the scripts-library when we really need it.

set -eu

wanted_packages=(
gpg # used to decrypt the gpg keys of the apt repositories
)

missing_packages=()

for package in "${wanted_packages[@]}"; do
if ! command -v "${package}" >/dev/null; then
missing_packages+=("${package}")
fi
done

if [[ ${#missing_packages[@]} -eq 0 ]]; then
exit 0
fi

# shellcheck source=../.chezmoitemplates/scripts-library
source "${CHEZMOI_SOURCE_DIR?}/.chezmoitemplates/scripts-library"

log_task "Installing missing packages with APT: ${missing_packages[*]}"

c apt update
c apt install --yes --no-install-recommends "${missing_packages[@]}"
10 changes: 9 additions & 1 deletion root/.chezmoiscripts/run_after_10-install-apt-packages.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,13 @@ if [[ ${#missing_packages[@]} -gt 0 ]]; then
log_task "Installing missing packages with APT: ${missing_packages[*]}"

c apt update --yes
c apt install --yes --install-recommends "${missing_packages[@]}"

if [[ "${DOTFILES_TEST:-}" == true ]]; then
log_manual_action "Not installing recommended packages to speed up test mode"
recommends_arg="--no-install-recommends"
else
recommends_arg="--install-recommends"
fi

c apt install --yes "${recommends_arg}" "${missing_packages[@]}"
fi

This file was deleted.

6 changes: 4 additions & 2 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ if [[ "${debug}" == "on" ]]; then
fi
export DOTFILES_TEST=true
echo 'Defaults env_keep += "DOTFILES_TEST"' | sudo tee /etc/sudoers.d/env_keep
~/.dotfiles/install.sh
Expand Down Expand Up @@ -179,6 +178,9 @@ for variant in "${variants[@]}"; do
cat <<'EOF'
export IS_WSL=true
# Exercises install-pre-requisites.sh
sudo apt remove --yes zsh curl git gpg
cat <<'EOM' | sudo tee /usr/local/bin/wslpath
#!/bin/bash
Expand All @@ -204,7 +206,7 @@ EOF
# shellcheck disable=SC2312
cat <<'EOF'
sudo apt update --yes
sudo apt install -y --no-install-recommends gnome-shell
sudo apt install --yes --no-install-recommends gnome-shell
EOF
)"
;;
Expand Down

0 comments on commit 9b8abec

Please sign in to comment.