deadc0de6 / dotdrop

Save your dotfiles once, deploy them everywhere
https://dotdrop.readthedocs.io
GNU General Public License v3.0
1.79k stars 105 forks source link

Managing the same dotfile location but for different profiles #26

Closed smac89 closed 6 years ago

smac89 commented 6 years ago

I find myself doing the following in config.yml

dotfiles:
  f_zshrc_host1:
    dst: ~/.zshrc
    src: zshrc_host1
  f_zshrc_host2:
    dst: ~/.zshrc
    src: zshrc_host2
profiles:
  host1:
    dotfiles:
    - f_zshrc_host1
  host2:
    dotfiles:
    - f_zshrc_host2

So in essence, I wanted to manage the same location for my .zshrc, but for different hosts and by extension different configurations.

When I did dotdrop import -p host1 ~/.zshrc, it creates a dotfile with the name zshrc. Then when I did the same for my other host (dotdrop import -p host2 ~/.zshrc), it will simply default to sharing that file for both hosts. This is not what I want.

To accomplish what I wanted, I have created separate files for both hosts and changed the config accordingly. Am I missing a command for doing this, or do I have to do this manually each time? I feel like this should be part of the basic functionality.

I have no problem doing it manually seeing as it still works when I install the files, but I was wondering if I am missing a built-in way for doing this.

Thanks

deadc0de6 commented 6 years ago

The idea of dotdrop is to have the ability to store a specific dotfile only once and deploy it with a different content on different host. To achieve this, it uses jinja2 which is a templating engine that allows to specify, during the dotfile installation with dotdrop, based on the selected profile, how (with what content) the dotfile will be installed. See the example in the README: https://github.com/deadc0de6/dotdrop#example

Therefore you need to import your zshrc only once (on host1 for example with the import command) and then edit it in dotdrop to insert specific jinja2 templating commands such that its content will be changed depending on which profile you use when you install it. These jinja2 commands are usually only if statement that will define if a specific part of the file is to be written to the final file when installing for a specific profile.

For example, looking at your dotfiles, only a single zshrc should be present and then needs to be adapted/edited such that it will generate a different content if you install it for profile host1 or profile host2. Below is an example on how the beginning of that zshrc file should look like in dotdrop (I'm taking zshrc_home is for host host1):

################################################################################
################################### ZGEN #######################################
################################################################################
source "$HOME/.zgen/zgen.zsh"

zgen load zsh-users/zsh-completions

{%@@ if profile == "host1" @@%}
# brew
export HOMEBREW_BUILD_FROM_SOURCE=1
zgen load smac89/linuxbrew
{%@@ endif @@%}

zgen load zdharma/fast-syntax-highlighting
zgen load zsh-users/zsh-autosuggestions

# nvm
export NVM_DIR="$HOME/.nvm"
{%@@ if profile == "host1" @@%}
export NVM_LAZY_LOAD=true
{%@@ endif @@%}
export NVM_AUTO_USE=true
zgen load lukechilds/zsh-nvm
...

The if statements will only be taken when installing for profile host1 and thus those parts will be present in the final file only when installing for host1 thus allowing the same dotfile (stored in dotdrop) to be generated differently on different host. Of course more complex behavior (for loops, env variables, ...) can be achieved with jinja2, for more, see the jinja2 offical doc as well as how other people are using dotdrop: https://github.com/deadc0de6/dotdrop#people-using-dotdrop.

Hope this helps

smac89 commented 6 years ago

@deadc0de6 That's pretty cool. I will make use of the templating stuff then.

Thank you!