nix-config/modules/scripts/clone.nix

54 lines
1.2 KiB
Nix
Raw Normal View History

2025-09-03 08:30:15 +01:00
{ withSystem, ... }:
2025-09-03 01:16:55 +01:00
{
2025-09-03 08:30:15 +01:00
perSystem =
psArgs@{ pkgs, ... }:
2025-09-03 01:16:55 +01:00
{
2025-09-03 08:30:15 +01:00
packages.clone = pkgs.writeShellApplication {
name = "clone";
2025-09-03 01:16:55 +01:00
2025-09-03 08:30:15 +01:00
runtimeInputs = with pkgs; [
git
psArgs.config.packages.tmux-sessionizer
];
2025-09-03 01:16:55 +01:00
2025-09-03 08:30:15 +01:00
text = ''
repo_url="$1"
repo_url="''${repo_url%.git}"
2025-09-03 01:16:55 +01:00
2025-09-03 08:30:15 +01:00
if [[ "$repo_url" =~ ^(git@|https://|ssh://forgejo@)?([^:/]+)[:/](.*)/(.*)$ ]]; then
domain="''${BASH_REMATCH[2]}"
2025-09-03 01:16:55 +01:00
2025-09-03 08:30:15 +01:00
if [[ "$domain" == "ssh.oliverdavies.uk" ]]; then
domain="code.oliverdavies.uk"
fi
2025-09-03 01:16:55 +01:00
2025-09-03 08:30:15 +01:00
user="''${BASH_REMATCH[3]}"
name="''${BASH_REMATCH[4]}"
2025-09-03 01:16:55 +01:00
2025-09-03 08:30:15 +01:00
user_path="$XDG_REPOS_DIR/$domain/$user"
repo_path="$user_path/$name"
2025-09-03 01:16:55 +01:00
2025-09-03 08:30:15 +01:00
[[ -d "$repo_path" ]] && tmux-sessionizer "$repo_path" && exit 0
2025-09-03 01:16:55 +01:00
2025-09-03 08:30:15 +01:00
mkdir -pv "$repo_path"
2025-09-03 01:16:55 +01:00
2025-09-03 08:30:15 +01:00
git clone "$repo_url" "$repo_path"
2025-09-03 01:16:55 +01:00
2025-09-03 08:30:15 +01:00
tmux-sessionizer "$repo_path"
else
exit 1
fi
'';
};
};
flake.modules.homeManager.base =
{ pkgs, ... }:
{
home.packages = [
(withSystem pkgs.system (psArgs: psArgs.config.packages.clone))
2025-09-03 01:16:55 +01:00
];
};
}