From 665525fb9ef662a7053f9d7d158874f217fb3f0d Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 3 Sep 2025 01:16:55 +0100 Subject: [PATCH] Add `clone` script --- modules/scripts/clone.nix | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 modules/scripts/clone.nix diff --git a/modules/scripts/clone.nix b/modules/scripts/clone.nix new file mode 100644 index 00000000..90457570 --- /dev/null +++ b/modules/scripts/clone.nix @@ -0,0 +1,42 @@ +{ + flake.modules.homeManager.base = + { pkgs, ... }: + { + home.packages = [ + (pkgs.writeShellApplication { + name = "clone"; + + runtimeInputs = with pkgs; [ git ]; + + text = '' + repo_url="$1" + repo_url="''${repo_url%.git}" + + if [[ "$repo_url" =~ ^(git@|https://|ssh://forgejo@)?([^:/]+)[:/](.*)/(.*)$ ]]; then + domain="''${BASH_REMATCH[2]}" + + if [[ "$domain" == "ssh.oliverdavies.uk" ]]; then + domain="code.oliverdavies.uk" + fi + + user="''${BASH_REMATCH[3]}" + name="''${BASH_REMATCH[4]}" + + user_path="$XDG_REPOS_DIR/$domain/$user" + repo_path="$user_path/$name" + + [[ -d "$repo_path" ]] && t "$repo_path" && exit 0 + + mkdir -pv "$repo_path" + + git clone "$repo_url" "$repo_path" + + t "$repo_path" + else + exit 1 + fi + ''; + }) + ]; + }; +}