twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
13.36k stars 493 forks source link

modify script executed from weird path (even when modify script is not templated) #2934

Closed VorpalBlade closed 9 months ago

VorpalBlade commented 1 year ago

What exactly are you trying to do?

I was trying to be smart and inspect the path to the modify script (in order to detect an associated data file located relative to it). My modify script is actually a config file being executed by a rust program:

#!/path/to/rust/program

some directives here;
ignore blah;
transform quux;

The idea was that the rust program would inspect the path to this modify_ "script" to locate additional data files. However, when chezmoi actually executes this script the path is something like: /tmp/3306499162.Trolltech.conf.

This would make sense if the modify script was templated (modify_blah.tmpl) as chezmoi would need to replace things in the file. But that is not the case here, it is NOT a template. How do I get the original path to this file? Do I simply have to accept that I can't, and make it a .tmpl and insert something like: source "{{ .chezmoi.sourceDir }}/{{ .chezmoi.sourceFile}}"?

Either way, I can't find where this behaviour is documented.

What have you tried so far?

Scratching my head at this (it is late local time...).

Where else have you checked for solutions?

Output of any commands you've tried with --verbose flag

Irrelevant without the whole setup of my rust program. But if you really want to try it I can setup a test case.

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE warning version v2.33.1, built at 2023-04-08T07:27:16Z ok latest-version v2.33.1 ok os-arch linux/amd64 (Arch Linux) ok uname Linux theseus 6.2.11-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 13 Apr 2023 16:59:24 +0000 x86_64 GNU/Linux ok go-version go1.20.3 (gc) ok executable /usr/bin/chezmoi ok config-file ~/.config/chezmoi/chezmoi.yaml, last modified 2023-04-05T12:45:46+02:00 warning source-dir ~/.local/share/chezmoi is a git working tree (dirty) warning suspicious-entries ~/.local/share/chezmoi/.chezmoi_modify_manager.add_hook warning working-tree ~/.local/share/chezmoi is a git working tree (dirty) ok dest-dir ~ is a directory ok umask 022 ok cd-command found /bin/zsh ok cd-args /bin/zsh info diff-command not set ok edit-command found /usr/bin/code ok edit-args code -w ok git-command found /usr/bin/git, version 2.40.0 ok merge-command found /usr/bin/bcompare ok shell-command found /bin/zsh ok shell-args /bin/zsh info age-command age not found in $PATH ok gpg-command found /usr/bin/gpg, version 2.2.41 info pinentry-command not set info 1password-command op not found in $PATH info bitwarden-command bw not found in $PATH info dashlane-command dcli not found in $PATH info gopass-command gopass not found in $PATH ok keepassxc-command found /usr/bin/keepassxc-cli, version 2.7.4 info keepassxc-db not set info keeper-command keeper not found in $PATH info lastpass-command lpass not found in $PATH info pass-command pass not found in $PATH info passhole-command ph not found in $PATH info rbw-command rbw not found in $PATH info vault-command vault not found in $PATH info secret-command not set ```

Additional context

Add any other context about the problem here.

VorpalBlade commented 1 year ago

From the manual:

chezmoi sets a number of CHEZMOI* environment variables when running scripts, corresponding to commonly-used template data variables.

That could have been one way (if CHEZMOI_SOURCE_DIR and CHEZMOI_SOURCEFILE had been set), except none of these appear to be set for `modify` scripts.

twpayne commented 1 year ago

chezmoi copies modify_ scripts to a file in a temporary directory, sets the executable bit on the file, and then executes them. This is needed even if the modify_ script is not a template as the executable bit is not set in the source directory.

How do I get the original path to this file? Do I simply have to accept that I can't, and make it a .tmpl and insert something like: source "{{ .chezmoi.sourceDir }}/{{ .chezmoi.sourceFile}}"?

Right now, you have to accept that you can't. Note that the .chezmoi.sourceFile template variable does not exist.

That could have been one way (if CHEZMOI_SOURCE_DIR and CHEZMOI_SOURCEFILE had been set), except none of these appear to be set for `modify` scripts.

That was an oversight. Fixed in #2935.

VorpalBlade commented 1 year ago

On Wed, 19 Apr 2023, 00:57 Tom Payne, @.***> wrote:

chezmoi copies modify scripts to a file in a temporary directory, sets the executable bit on the file, and then executes them. This is needed even if the modify script is not a template as the executable bit is not set in the source directory.

How do I get the original path to this file? Do I simply have to accept that I can't, and make it a .tmpl and insert something like: source "{{ .chezmoi.sourceDir }}/{{ .chezmoi.sourceFile}}"?

Right now, you have to accept that you can't. Note that the .chezmoi.sourceFile template variable does not exist.

Yes, it does. And I have used it successfully in modify scripts. It was apparently added in https://github.com/twpayne/chezmoi/issues/1210

That could have been one way (if CHEZMOI_SOURCE_DIR and CHEZMOI_SOURCE_FILE

had been set), except none of these appear to be set for modify_ scripts.

That was an oversight. Fixed in #2935 https://github.com/twpayne/chezmoi/pull/2935.

Good, would be even better if the source file name was also exported this way. :)

— Reply to this email directly, view it on GitHub https://github.com/twpayne/chezmoi/issues/2934#issuecomment-1513887222, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZJM2SENACGU7YB74CXDRTXB4L6TANCNFSM6AAAAAAXDG7JOE . You are receiving this because you authored the thread.Message ID: @.***>