flake/config/xonsh/aliases.py

101 lines
3.7 KiB
Python
Raw Normal View History

2024-11-27 13:12:29 -05:00
from os.path import dirname
from pathlib import Path
import subprocess
2024-11-27 13:12:29 -05:00
import colors
2024-11-27 13:12:29 -05:00
def run(cmd: list[str], cwd: Path = Path.cwd(), **kwargs) -> subprocess.CompletedProcess:
c = colors.Colors
print(f"{c.GREEN}Running command: {c.RED}'{' '.join(cmd)}'{c.END}")
if cwd != Path.cwd():
print(f"{c.GREEN} 󱞩 in directory: {c.YELLOW}'{cwd}'{c.END}")
return subprocess.run(cmd, cwd=cwd, check=True, **kwargs)
@aliases.register
@aliases.return_command
def _sudo(args):
return ["sudo", "--", *aliases.eval_alias(args)]
@aliases.register
def _vm(args):
if not args:
args = ["nixpkgs"]
vm_name = args.pop(0)
build_vm_args = args
if vm_name == "nixpkgs":
build_vm_args.extend(["-I", "nixpkgs=/bulk/home/cswimr/Projects/nixpkgs"])
vm_path = Path(f"/etc/nixos/hosts/virtual-machines/{vm_name}")
if vm_path.exists():
2024-11-27 13:12:29 -05:00
c = colors.Colors
print(f"{c.BLUE}Building virtual machine {c.YELLOW}{vm_name}{c.END}")
run(["nixos-rebuild", "build-vm", "-I", "nixos-config=./default.nix", *build_vm_args, "--no-flake"], cwd=vm_path)
2024-11-27 13:12:29 -05:00
print(f"{c.BLUE}Running virtual vachine {c.YELLOW}{vm_name}{c.END}")
run(["./result/bin/run-nixos-vm"], cwd=vm_path)
print(f"{c.BLUE}Virtual machine {c.YELLOW}{vm_name} {c.BLUE}has {c.RED}stopped.{c.END}")
else:
raise FileNotFoundError(f"Virtual machine {vm_name} does not exist.")
@aliases.register
@aliases.return_command
def _edit(args):
if not args:
args = ["."]
if "$SSH_CONNECTION":
return ["$EDITOR", *args]
return ["$VISUAL", *args]
alias_dictionary = {
"ff": "fastfetch",
"neofetch": "fastfetch",
"nf": "fastfetch",
"lg": "lazygit",
"lad": "lazydocker",
"clip": "wl-copy",
"paste": "wl-paste",
"cat": "bat",
"git": "hub",
"l": "eza -lhg --time-style=long-iso --icons=auto",
"la": "eza -lAh --time-style=long-iso --icons=auto",
"ll": "eza -lhg --time-style=long-iso --icons=auto",
"ls": "eza --time-style=long-iso --icons=auto",
"lsa": "eza -lah --time-style=long-iso --icons=auto",
"tree": "eza --tree --git-ignore --time-style=long-iso --icons=auto",
"create-devenv": "nix flake init --template github:cachix/devenv and direnv allow",
"dev": "nix develop --no-pure-eval",
"nixpkgs-update": "nix run --option extra-substituters 'https://nix-community.cachix.org/' --option extra-trusted-public-keys 'nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=' github:ryantm/nixpkgs-update --",
"nixrc": "edit /etc/nixos",
"upd": '''sudo nixos-generate-config --dir /etc/nixos/hosts and \
sudo rm /etc/nixos/hosts/configuration.nix and \
sudo mv /etc/nixos/hosts/hardware-configuration.nix /etc/nixos/hosts/@($HOSTNAME).nix \
and git -C /etc/nixos --git-dir=/etc/nixos/.git add /etc/nixos/hosts/@($HOSTNAME).nix and \
rm -rf ~/.config/Code/User/settings.json.bak and \
sudo nixos-rebuild switch --flake /etc/nixos --impure @($args)''', #TODO: Remove --impure once the Starship module is merged - see ../../nixos/shell.nix for more information
"taildrop": "tailscale file",
"forgejo-runner": "act_runner",
"runactions": "act_runner exec --default-actions-url=https://www.coastalcommits.com --gitea-instance=https://www.coastalcommits.com",
"e": "edit",
"c": "clear",
"s": "sudo",
}
# Create aliases for scripts with the .py extension stripped
script_path = Path("/etc/nixos/scripts")
if script_path.exists():
for script in script_path.glob("*.py"):
if script.name == "__init__.py":
continue
script_name = script.name.strip(".py")
aliases.update({script_name: [str(script)]})
aliases.update(alias_dictionary)