Skip to content

Commit

Permalink
feat(chezmoi): Allow specifying branch for chezmoi init (#357)
Browse files Browse the repository at this point in the history
* feat: allow specifying branch for `chezmoi init`

* fix: don't assume the default branch is `main`

* fix: get rid of `main` reference in the README
  • Loading branch information
yacoob authored Nov 8, 2024
1 parent be9f17c commit e328da9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
18 changes: 9 additions & 9 deletions modules/chezmoi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
The `chezmoi` module takes care of installing, initializing and updating your dotfiles.
Each feature can be enabled or disabled individually.

Installation of the `chezmoi` binary happens at build time and is done by downloading the `amd64` binary from the latest release to `/usr/bin/chezmoi`.
Installation of the `chezmoi` binary happens at build time and is done by downloading the `amd64` binary from the latest release to `/usr/bin/chezmoi`.
This can be disabled by setting `install` to false. (defaults: true)

Choose how `chezmoi` handles conflicting files with `file-conflict-policy`.
Choose how `chezmoi` handles conflicting files with `file-conflict-policy`.
The following values are valid:
`"skip"` Will not take any action if the file has changed from what is in your dotfiles repository.
This executes `chezmoi update --no-tty --keep-going` under the hood.
`"skip"` Will not take any action if the file has changed from what is in your dotfiles repository.
This executes `chezmoi update --no-tty --keep-going` under the hood.
`"replace"` Will overwrite the file if it has changed from what is in your dotfiles repository.
This executes `chezmoi update --no-tty --force` under the hood.

See `chezmoi`s documentation for [`--no-tty`](https://www.chezmoi.io/reference/command-line-flags/global/#-no-tty), [`--keep-going`](https://www.chezmoi.io/reference/command-line-flags/global/#-k-keep-going) and [`--force`](https://www.chezmoi.io/reference/command-line-flags/global/#-force) for details.

A systemd user service is installed that will initialize a `chezmoi` repository on chezmoi's default path (`~/.local/share/chezmoi`) for any user when it logs in, or at boot if it has lingering enabled.
The service will only run if `~/.local/share/chezmoi` does not exist.
Set `repository` to the URL of your dotfiles repository. (eg. `repository: https://example.org/user/dotfiles`)
Set `repository` to the URL of your dotfiles repository. (eg. `repository: https://example.org/user/dotfiles`). You can also set `branch` if you want to use a branch different than the default.
:::note
The value of `repository` will be passed directly to `chezmoi init --apply ${repository}`.
The value of `repository` and `branch` will be passed directly to `chezmoi init --apply ${repository} --branch ${branch}`.
See the [`chezmoi init` documentation](https://www.chezmoi.io/reference/commands/init/) for detailed syntax.
:::
:::
Set `disable-init` to `true` if you do not want to install the init service.

:::caution
Expand All @@ -46,10 +46,10 @@ sudo systemctl enable --user chesmoi-init.service chezmoi-update.timer
To turn on lingering for a given user, run the following command with sudo:

:::note
By default, any systemd units in a user's namespace will run after the user logs in, and will close after the user closes their last session.
By default, any systemd units in a user's namespace will run after the user logs in, and will close after the user closes their last session.
When you enable lingering for a user, that user's units will run at boot and will continue running even if the user has no active sessions.

If your dotfiles only contain things used by humans, such as cosmetic settings and aliases, you shouldn't need this.
If your dotfiles only contain things used by humans, such as cosmetic settings and aliases, you shouldn't need this.
If you understand the above implications, and decide you need this feature, you can enable it with the following command, after installation:
:::

Expand Down
13 changes: 9 additions & 4 deletions modules/chezmoi/chezmoi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ fi

# The repository with your chezmoi dotfiles. (default: null)
DOTFILE_REPOSITORY=$(echo "$1" | yq -I=0 ".repository") # (string)
# The chezmoi repository branch to use.
DOTFILE_BRANCH=$(echo "$1" | yq -I=0 ".branch")
if [[ -n ${DOTFILE_BRANCH} ]]; then
INIT_BRANCH_FLAG="--branch ${DOTFILE_BRANCH}"
else
INIT_BRANCH_FLAG=""
fi

# If true, chezmoi services will be enabled for all logged in users, and users with lingering enabled. (default: true)
# If false, chezmoi services will not be enabled for any users, but can be enabled manually, after installation.
Expand Down Expand Up @@ -85,7 +92,6 @@ if [[ (-z $DOTFILE_REPOSITORY || $DOTFILE_REPOSITORY == "null") && $DISABLE_INIT
exit 1
fi


echo "Checking if /usr/bin/chezmoi exists"
if [ -e /usr/bin/chezmoi ]; then
echo "chezmoi binary already exists, no need to redownload it"
Expand All @@ -104,18 +110,17 @@ else
fi
fi


if [[ $DISABLE_INIT == false ]]; then
# Write the service to initialize Chezmoi, and insert the repo url in the file
echo "Writing init service to user unit directory"
cat >>/usr/lib/systemd/user/chezmoi-init.service <<EOF
[Unit]
Description=Initializes Chezmoi if directory is missing
# This service will not execute for a user with an existing chezmoi directory
ConditionPathExists=!%h/.local/share/chezmoi/.git/
[Service]
ExecStart=/usr/bin/chezmoi init --apply ${DOTFILE_REPOSITORY}
ExecStart=/usr/bin/chezmoi init --apply ${DOTFILE_REPOSITORY} ${INIT_BRANCH_FLAG}
Type=oneshot
[Install]
Expand Down
7 changes: 5 additions & 2 deletions modules/chezmoi/chezmoi.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ model ChezmoiModule {
/** Git repository to initialize. */
repository: string;

/** Git branch of the chezmoi repository. */
branch?: string = "";

/** Whether to enable the modules services globally for all users, if false users need to enable services manually. */
"all-users"?: boolean = true;

Expand All @@ -19,7 +22,7 @@ model ChezmoiModule {

/** Dotfile updates will wait this long after a boot before running. */
"wait-after-boot"?: string = "5m";

/** Disable the service that initializes `repository` on users that are logged in or have linger enabled UI. */
"disable-init"?: boolean = false;

Expand All @@ -28,4 +31,4 @@ model ChezmoiModule {

/** What to do when file different that exists on your repo is has been changed or exists locally. Accepts "skip" or "replace". */
"file-conflict-policy"?: "skip" | "replace" = "skip";
}
}

0 comments on commit e328da9

Please sign in to comment.