flake/config/xonsh/aliases.py

94 lines
3.3 KiB
Python

from pathlib import Path
import subprocess
def run(cmd: list[str]) -> subprocess.CompletedProcess:
print(f"* {' '.join(cmd)}")
return subprocess.run(cmd, check=True)
@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():
print(f"Building virtual machine {vm_name}")
run(["nixos-rebuild", "build-vm", "-I", "nixos-config=./default.nix", *build_vm_args, "--no-flake"], cwd=vm_path)
print(f"Running virtual vachine {vm_name}")
run([str(vm_path / "result" / "bin" / "run-nixos-vm")], cwd=vm_path)
print(f"Virtual machine {vm_name} has stopped.")
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)