nix-config/modules/scripts/clone.nix
Oliver Davies fcfdc8a580
All checks were successful
/ check (push) Successful in 50s
Refactor
2025-09-03 08:30:15 +01:00

53 lines
1.2 KiB
Nix

{ withSystem, ... }:
{
perSystem =
psArgs@{ pkgs, ... }:
{
packages.clone = pkgs.writeShellApplication {
name = "clone";
runtimeInputs = with pkgs; [
git
psArgs.config.packages.tmux-sessionizer
];
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" ]] && tmux-sessionizer "$repo_path" && exit 0
mkdir -pv "$repo_path"
git clone "$repo_url" "$repo_path"
tmux-sessionizer "$repo_path"
else
exit 1
fi
'';
};
};
flake.modules.homeManager.base =
{ pkgs, ... }:
{
home.packages = [
(withSystem pkgs.system (psArgs: psArgs.config.packages.clone))
];
};
}