improve the vm alias

This commit is contained in:
Seaswimmer 2024-11-27 13:12:29 -05:00
parent 13aa63b444
commit 122ee3035a
Signed by: cswimr
GPG key ID: 0EC431A8DA8F8087
4 changed files with 62 additions and 13 deletions

View file

@ -1,9 +1,14 @@
from os.path import dirname
from pathlib import Path from pathlib import Path
import subprocess import subprocess
import colors
def run(cmd: list[str]) -> subprocess.CompletedProcess: def run(cmd: list[str], cwd: Path = Path.cwd(), **kwargs) -> subprocess.CompletedProcess:
print(f"* {' '.join(cmd)}") c = colors.Colors
return subprocess.run(cmd, check=True) 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.register
@aliases.return_command @aliases.return_command
@ -20,11 +25,12 @@ def _vm(args):
build_vm_args.extend(["-I", "nixpkgs=/bulk/home/cswimr/Projects/nixpkgs"]) build_vm_args.extend(["-I", "nixpkgs=/bulk/home/cswimr/Projects/nixpkgs"])
vm_path = Path(f"/etc/nixos/hosts/virtual-machines/{vm_name}") vm_path = Path(f"/etc/nixos/hosts/virtual-machines/{vm_name}")
if vm_path.exists(): if vm_path.exists():
print(f"Building virtual machine {vm_name}") 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) 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}") print(f"{c.BLUE}Running virtual vachine {c.YELLOW}{vm_name}{c.END}")
run([str(vm_path / "result" / "bin" / "run-nixos-vm")], cwd=vm_path) run(["./result/bin/run-nixos-vm"], cwd=vm_path)
print(f"Virtual machine {vm_name} has stopped.") print(f"{c.BLUE}Virtual machine {c.YELLOW}{vm_name} {c.BLUE}has {c.RED}stopped.{c.END}")
else: else:
raise FileNotFoundError(f"Virtual machine {vm_name} does not exist.") raise FileNotFoundError(f"Virtual machine {vm_name} does not exist.")

47
config/xonsh/colors.py Normal file
View file

@ -0,0 +1,47 @@
# SGR color constants
# rene-d 2018
# https://gist.github.com/rene-d/9e584a7dd2935d0f461904b9f2950007
class Colors:
""" ANSI color codes """
BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BROWN = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
CYAN = "\033[0;36m"
LIGHT_GRAY = "\033[0;37m"
DARK_GRAY = "\033[1;30m"
LIGHT_RED = "\033[1;31m"
LIGHT_GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
LIGHT_BLUE = "\033[1;34m"
LIGHT_PURPLE = "\033[1;35m"
LIGHT_CYAN = "\033[1;36m"
LIGHT_WHITE = "\033[1;37m"
BOLD = "\033[1m"
FAINT = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
NEGATIVE = "\033[7m"
CROSSED = "\033[9m"
END = "\033[0m"
# cancel SGR codes if we don't write to a terminal
if not __import__("sys").stdout.isatty():
for _ in dir():
if isinstance(_, str) and _[0] != "_":
locals()[_] = ""
else:
# set Windows console in VT mode
if __import__("platform").system() == "Windows":
kernel32 = __import__("ctypes").windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
del kernel32
if __name__ == '__main__':
for i in dir(Colors):
if i[0:1] != "_" and i != "END":
print("{:>16} {}".format(i, getattr(Colors, i) + i + Colors.END))

7
nixos/pkg.nix Executable file → Normal file
View file

@ -46,11 +46,8 @@ let
packwiz packwiz
xclip xclip
starship starship
sops
# for xonsh ssh-to-age
python311
python311Packages.pip
python311Packages.python-lsp-server
]; ];
flakePackages = with inputs; [ compose2nix.packages.${system}.default ]; flakePackages = with inputs; [ compose2nix.packages.${system}.default ];
in { in {

View file

@ -73,7 +73,6 @@
enable = true; enable = true;
config = '' config = ''
$BASH_COMPLETIONS = ('${bashcfg.completion.package}/etc/profile.d/bash_completion.sh') $BASH_COMPLETIONS = ('${bashcfg.completion.package}/etc/profile.d/bash_completion.sh')
#execx($(starship init xonsh))
#xontrib load cd #xontrib load cd
xontrib load direnv xontrib load direnv
xontrib load sh xontrib load sh