How to clone git repo if it doesn't exist

Hi,

I want to sync git repos between my laptop and my desktop computer, but I only want to clone new git repos if they don’t exists on one of the computers. Further data sync is done with git pull/push as usual.

For example, if I have the following configuration

Desktop computer:

  • dev/
    ** projectA/
    ** projectB/
    ** projectC/

Laptop:

  • dev/
    ** projectA/
    ** projectB/

I would like that my next nixos-rebuild switch on the laptop makes a clone of projectC inside the dev/ directory

Of course, nixos configuration is synched with a git repo between the two computers.

I was thinking about using home manager opt-home.activation option Appendix A. Configuration Options but there’s maybe a better way to do this.

Any advice ?

Thanks.

home.activation is the only way to do it, I’d still not recommend doing that, just clone the repos necessary once.

The effort writing an activation script that only fires once and does not fail everytime its run, isn’t worth it.

1 Like

It’s worth it for me as a practical exercise to improve my nixos skills :smiley: but I agree it’s a lot of work in order to just clone a repo.

If you need an example, I do something like this in my flake here. I check if a directory doesn’t exist and clone the repo if needed.

I have two config options that I created — dotfilesRepo which is the SSH URL and dotfilesPath. I have to convert the SSH URL to HTTPS in order to clone without an SSH key, so it would probably be simpler if I just started with an HTTPS URL instead.

When using the activation script, I tried to make sure I was adhering to the “dry run” guarantee, but otherwise it’s fairly straightforward.

1 Like