Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow two chezmoi instances to run in parallel when they run as a different user #1516

Closed
felipecrs opened this issue Oct 10, 2021 · 4 comments · Fixed by #1604
Closed

Allow two chezmoi instances to run in parallel when they run as a different user #1516

felipecrs opened this issue Oct 10, 2021 · 4 comments · Fixed by #1604
Labels
enhancement New feature or request

Comments

@felipecrs
Copy link
Contributor

felipecrs commented Oct 10, 2021

Is your feature request related to a problem? Please describe.

I'm experimenting with chezmoi to manage my root dir, as otherwise I have to basically rewrite a lot of the functionalities that chezmoi provides out-of-the-box in my run_ scripts.

My goal is to have 2 different sourceDir's, one with my normal home dir, and another with the "/" destination dir in mind. Then, as a run_always_after script in my home source, I want to run chezmoi as root to apply the changes of the root source dir.

POC: felipecrs/dotfiles#44

❯ chezmoi apply --force --verbose
diff --git a/always_after_apply_root.sh b/always_after_apply_root.sh
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..13d09e8df999a478d3f01da3263c2f35897a1dd9 100755
--- a/always_after_apply_root.sh
+++ b/always_after_apply_root.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+echo "Applying chezmoi to root..."
+
+chezmoi=$(command -v chezmoi)
+
+config="${HOME}/.config/chezmoi/root.config.yaml"
+
+if [[ -f "${config}" ]]; then
+  sudo "${chezmoi}" --config "${HOME}/.config/chezmoi/root.config.yaml" apply --verbose --force
+else
+  echo "The file '${config}' does not exist. Did you apply it?"
+  exit 1
+fi
Applying chezmoi to root...
chezmoi: timeout obtaining persistent state lock, is another instance of chezmoi running?
chezmoi: exit status 1

Describe the solution you'd like

I believe that multiple instances of chezmoi should be able to run in parallel as long as they are running by different users. In my case, the first user will be myself while the second will be root.

But I also think that on shared computers, nothing prevents multiple users to manage their dotfiles with chezmoi, and then they could hit this limitation if both runs in parallel by any chance.

Describe alternatives you've considered

Instead of adding the sudo chezmoi call to my run_always_ script, I could call it manually once the chezmoi apply for my home dir finishes. But it's a very not desirable solution for me. :)

Even so, it would not fix the possible 2 users in a shared computer issue that I stated.

Additional context

Well, I know chezmoi's goal is not to manage folders other than the home, but regardless, probably everyone's dotfiles has at least one script to install something by running as root (sudo apt install zsh) for example.

And I'm just experimenting... perhaps what I'm trying to do is not a good alternative anyway.

@felipecrs felipecrs added the enhancement New feature or request label Oct 10, 2021
@felipecrs
Copy link
Contributor Author

felipecrs commented Oct 10, 2021

Also, to make this solution even better, it would be awesome if some additional template variables existed, so I could call chezmoi like so:

sudo "{{ .chezmoi.call.binPath }}" "{{ .chezmoi.call.command }}" {{ .chezmoi.call.options }} --config $HOME/.config/chezmoi/root.chezmoi.yaml

So that if I called chezmoi apply --verbose --dry-run as my user, the root call of it would also inherit it. The same of chezmoi init --apply.

@felipecrs
Copy link
Contributor Author

@twpayne an option would be to expose the persistent file location as a CLI option, so that users can tweak it as needed too.

https://github.dev/twpayne/chezmoi/blob/56b31f931b2f4e77b3f127914bc50e4d1a5c0ddd/internal/cmd/config.go#L184

I would for example do:

sudo path/to/chezmoi --config $HOME/.config/chezmoi/root.chezmoi.yaml --persist $HOME/.config/chezmoi/root.chezmoi.boltdb

What do you think?

@felipecrs
Copy link
Contributor Author

felipecrs commented Nov 5, 2021

Another would option be to automatically infer the persistent file's name from the config file name. For example:

chezmoi.yaml -> chezmoi.boltdb

But:

root.chezmoi.yaml -> root.chezmoi.boltdb

@twpayne
Copy link
Owner

twpayne commented Nov 9, 2021

Thank you for the discussion earlier today. #1604 is an attempt to fix this using your suggestion for a --persist flag.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants