improve upd alias and add some other stuff to xonsh configs

This commit is contained in:
Seaswimmer 2024-11-28 14:43:41 -05:00
parent b6d72de51b
commit ed0e65c81f
Signed by: cswimr
GPG key ID: 0EC431A8DA8F8087

View file

@ -1,14 +1,26 @@
from os.path import dirname
from os import environ
from pathlib import Path
import subprocess
from socket import gethostname
import colors
def run(cmd: list[str], cwd: Path = Path.cwd(), **kwargs) -> subprocess.CompletedProcess:
def format_link(link: str, text: str) -> str:
return f"\033]8;;{link}\033\\{text}\033]8;;\033\\"
def run(cmd: list[str], cwd: Path = Path.cwd(), exit_on_error: bool = True, **kwargs) -> subprocess.CompletedProcess:
c = colors.Colors
print(f"{c.GREEN}Running command: {c.RED}'{' '.join(cmd)}'{c.END}")
print(f"{c.GREEN}Running command: {c.PURPLE}'{' '.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)
print(f"{c.GREEN} 󱞩 in directory: {c.YELLOW}'{format_link(link="file://" + str(cwd), text=cwd)}'{c.END}")
result = subprocess.run(cmd, cwd=cwd, check=False, **kwargs)
if result.returncode != 0:
print(f"{c.RED}Command exited with non-zero exit code {c.CYAN}{c.BOLD}{result.returncode}{c.END}")
if exit_on_error is True:
result.check_returncode()
else:
print(f"{c.GREEN}Command exited with exit code {c.CYAN}{c.BOLD}{result.returncode}{c.END}")
return result
@aliases.register
@aliases.return_command
@ -28,19 +40,54 @@ def _vm(args):
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)
print(f"{c.BLUE}Running virtual vachine {c.YELLOW}{vm_name}{c.END}")
print(f"{c.BLUE}Starting 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
def _upd(args: list):
path = Path("/etc/nixos")
if path.exists():
c = colors.Colors
print(f"{c.BLUE}Updating {c.YELLOW}NixOS{c.BLUE} hardware configuration file for {c.YELLOW}{gethostname()}{c.BLUE}{c.END}")
run(["sudo", "nixos-generate-config", "--dir", ".",], cwd=path / "hosts")
print(f"{c.BLUE}Deleting redundant {c.YELLOW}NixOS{c.BLUE} configuration file{c.END}")
run(["sudo", "rm", "configuration.nix"], cwd=path / "hosts")
print(f"{c.BLUE}Moving {c.YELLOW}NixOS{c.BLUE} hardware configuration file{c.END}")
run(["sudo", "mv", "hardware-configuration.nix", "{hostname}.nix".format(hostname=gethostname())], cwd=path / "hosts")
print(f"{c.BLUE}Adding {c.YELLOW}NixOS{c.BLUE} hardware configuration file for {c.YELLOW}{gethostname()}{c.BLUE} to git{c.END}")
run(["git", "add", "hosts/{hostname}.nix".format(hostname=gethostname())], cwd=path)
print(f"{c.BLUE}Deleting {c.YELLOW}Visual Studio Code{c.BLUE} user settings backup file{c.END}")
run(["rm", ".config/Code/User/settings.json.bak"], exit_on_error=False, cwd="/home/cswimr")
if "--purge-vscode-extensions" in args:
args.remove("--purge-vscode-extensions")
print(f"{c.BLUE}Killing {c.YELLOW}Visual Studio Code{c.BLUE} processes{c.END}")
run(["killall", "code"])
print(f"{c.BLUE}Purging {c.YELLOW}Visual Studio Code{c.BLUE} extensions{c.END}")
run(["rm", "-rf", ".vscode/extensions"], cwd="/home/cswimr")
print(f"{c.BLUE}Rebuilding {c.YELLOW}NixOS{c.BLUE} configuration{c.END}")
#TODO: Remove --impure once the Starship module is merged - see ../../nixos/shell.nix for more information
args.append("--impure")
if "--impure" in args:
print(f"{c.RED}WARNING: The --impure flag is set!{c.END}")
run(["sudo", "nixos-rebuild", "switch", *args], cwd=path)
@aliases.register
def _lock(args):
path = Path("/etc/nixos")
if path.exists():
c = colors.Colors
print(f"{c.BLUE}Updating {c.YELLOW}Nix Flake{c.BLUE} lock file{c.END}")
run(["nix", "flake", "update", *args], cwd=path)
@aliases.register
@aliases.return_command
def _edit(args):
if not args:
args = ["."]
if "$SSH_CONNECTION":
if environ.get("SSH_CONNECTION") is not None:
return ["$EDITOR", *args]
return ["$VISUAL", *args]
@ -71,13 +118,6 @@ alias_dictionary = {
"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",