twpayne / chezmoi

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

Can not get script to run before attempting to execute templates #830

Closed Ionshard closed 4 years ago

Ionshard commented 4 years ago

What exactly are you trying to do?

There is discussion in #664 to allow providing a custom init script. Furthermore in #794 it's discussed that chezmoi will run the files in alphabetic order though not actually documented.

I am trying to get chezmoi to be responsible for install lastpass so that I can use it within chezmoi since one of the big draws to chezmoi is limiting the "bootstrap" problem.

I have created a run_once_00_bootstrap.sh.tmpl file in my repository and I was hoping that would run before attempting to create my SSH keys. This is not the case.

Super basic example of this here: https://gitlab.com/Kasuko/dotfiles

What have you tried so far?

I have tried calling the bootstrap file:

No matter what I do, chezmoi always seems to attempt to execute my SSH templates before running the bootstrap script.

Where else have you checked for solutions?

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

$ chezmoi apply --verbose

lpass --version: exec: "lpass": executable file not found in $PATH
chezmoi: template: /home/vling/.local/share/chezmoi/private_dot_ssh/private_id_gitlab.tmpl:1:11: executing "/home/vling/.local/share/chezmoi/private_dot_ssh/private_id_gitlab.tmpl" at <lastpass "GitLab SSH Key">: error calling lastpass: exec: "lpass": executable file not found in $PATH
$ chezmoi execute-template < ~/.local/share/chezmoi/run_once_00_bootstrap.sh.tmpl
#!/usr/bin/env bash

## Bootstrap chezmoi

## Bootstrap lpass

sudo pacman --noconfirm -S lastpass-cli

lpass login <email>

Ignore all the white space above, I am also attempting to bootstrap chezmoi away from the single line install into the OS specific package. (which is not the issue because that's also not happening before rendering the template and does work if I manually install lastpass)

Output of chezmoi doctor

$ chezmoi doctor
warning: version 1.8.3
     ok: runtime.GOOS linux, runtime.GOARCH amd64
     ok: /home/vling/.local/share/chezmoi (source directory, perm 700)
     ok: /home/vling (destination directory, perm 700)
warning: /home/vling/.config/chezmoi/chezmoi.toml (configuration file)
     ok: /bin/bash (shell)
     ok: /usr/local/bin/vi (editor)
     ok: /usr/bin/vimdiff (merge command)
     ok: /usr/bin/git (source VCS command, version 2.28.0)
     ok: /usr/bin/gpg (GnuPG, version 2.2.21)
warning: op (1Password CLI, not found)
warning: bw (Bitwarden CLI, not found)
warning: gopass (gopass CLI, not found)
warning: keepassxc-cli (KeePassXC CLI, not found)
warning: lpass (LastPass CLI, not found)
warning: pass (pass CLI, not found)
warning: vault (Vault CLI, not found)

Additional context

I understand this is not a supported feature and will probably be officially supported in v2, but I was wondering if there was something I could do in the meantime?

twpayne commented 4 years ago

Thank you very much for reporting this and for the very clear and complete report!

I think what's happening here is that chezmoi applies things in alphabetical order based on the target name. .ssh/config begins with the character ., which has byte value 46. run_once_00_bootstrap.sh begins with the character 0 (after you strip the run_once_ prefix) which has byte value 48. This means that chezmoi will apply .ssh/config before running run_once_00_bootstrap.sh.

I think the work-around is to add a run_once_ script that sorts before .ssh/config, for example (and this is a horrible hack) by renaming run_once_00_bootstrap.sh to run_once_.00_bootstrap.sh.

Please note that I haven't tested this and would very much appreciate it if you could test it and report back here. chezmoi certainly could do much better here.

Ionshard commented 4 years ago

Awesome! Thanks that was it. I was paying too much attention to the source name. I have changed my script to be run_once_+00_bootsrap.sh.tmpl to make it a little more readable that the . in the middle of the filename and it works great!