switch back to zsh

I like `xonsh` and all, but integrating it with neovim without incurring
a major performance penalty is difficult to say the least. I think
switching back to `zsh` is my best option to solve this issue for now.
This commit is contained in:
cswimr 2025-02-04 11:39:40 -06:00
parent 184afebce1
commit 95ac8522db
Signed by: cswimr
GPG key ID: 0EC431A8DA8F8087
9 changed files with 362 additions and 14 deletions

View file

@ -0,0 +1,49 @@
# 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))

View file

@ -3,6 +3,50 @@ import subprocess
from pathlib import Path
from shutil import which
from common.colors import Colors as c
def format_link(link: str, text: str) -> str:
return f"\033]8;;{link}\033\\{text}\033]8;;\033\\"
def get_environment() -> dict[str, str]:
env = dict(os.environ)
env["DIRENV_DISABLE"] = "1"
return env
def run(
cmd: list[str],
cwd: Path = Path.cwd(),
exit_on_error: bool = True,
env: dict[str, str] | None = None,
**kwargs,
) -> subprocess.CompletedProcess:
if not env:
env = get_environment()
print(f"{c.GREEN}Running command: {c.PURPLE}'{' '.join(cmd)}'{c.END}")
if cwd != Path.cwd():
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, env=env, **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
def notify(
application_name: str,
@ -30,7 +74,7 @@ def notify(
args.append(title)
args.append(message)
print(args)
subprocess.run(args)
run(cmd=args)
def read_secret_file(secret: str, home: bool = False) -> str:
@ -57,8 +101,8 @@ def does_desktop_entry_exist(desktop_entry: str, show_all_paths: bool = True) ->
entry_paths = []
if which("qtpaths"):
result = subprocess.run(
["qtpaths", "--paths", "ApplicationsLocation"],
result = run(
cmd=["qtpaths", "--paths", "ApplicationsLocation"],
stdout=subprocess.PIPE,
text=True,
)
@ -73,7 +117,7 @@ def does_desktop_entry_exist(desktop_entry: str, show_all_paths: bool = True) ->
if show_all_paths:
print(
f"Checking the following paths for {desktop_entry}:\n{entry_paths}\n{'-'*20}"
f"Checking the following paths for {desktop_entry}:\n{entry_paths}\n{'-' * 20}"
)
for entry_path in entry_paths:

0
scripts/py/edit.py Normal file
View file

View file

@ -8,7 +8,7 @@ import tempfile
from pathlib import Path
from shutil import which
from common.common import notify # type: ignore
from common.common import notify, run
from PySide6.QtGui import QImageWriter # type: ignore
@ -56,7 +56,7 @@ def spectacle_screenshot(
"--pointer",
"--copy-image",
"--output",
file_path,
str(file_path),
]
if record:
@ -65,7 +65,7 @@ def spectacle_screenshot(
command.append("--region")
try:
subprocess.run(command, check=True) # type: ignore
run(command)
except subprocess.CalledProcessError as e:
if Path(file_path).exists() and use_temp_file:
os.remove(file_path)
@ -78,7 +78,7 @@ def spectacle_screenshot(
try:
opts = [
"/etc/nixos/scripts/py/zipline.py",
file_path,
str(file_path),
"--application-name",
"Spectacle",
"--desktop-entry",
@ -86,7 +86,7 @@ def spectacle_screenshot(
]
if url:
opts.extend(["--url", url])
subprocess.run(opts) # type: ignore
run(cmd=opts) # type: ignore
finally:
if Path(file_path).exists() and use_temp_file:
os.remove(file_path)

135
scripts/py/upd.py Executable file
View file

@ -0,0 +1,135 @@
#! /usr/bin/env nix-shell
#! nix-shell /etc/nixos/scripts/nix/python.nix -i python
from argparse import ArgumentParser
from pathlib import Path
from socket import gethostname
from common.colors import Colors as c
from common.common import run
def _upd(
subcommand: str = "switch",
pull: bool = False,
lock: bool = False,
rewrite_hardware_configuration: bool = False,
purge_vscode_extensions: bool = False,
extra_args: list = [],
):
path = Path("/etc/nixos")
if subcommand not in ("switch", "boot", "test", "build-vm"):
raise ValueError(f"{subcommand} is not a valid subcommand!")
if path.exists():
files_to_delete = {
"Visual Studio Code user settings": ".config/Code/User/settings.json.bak",
"fontconfig": ".config/fontconfig/conf.d/10-hm-fonts.conf.bak",
}
if pull:
print(
f"{c.BLUE}Pulling {c.YELLOW}NixOS{c.BLUE} configuration from remote{c.END}"
)
run(["git", "pull"], cwd=path)
if lock:
print(f"{c.BLUE}Updating {c.YELLOW}Nix Flake{c.BLUE} lock file{c.END}")
run(["nix", "flake", "update", *extra_args], cwd=path)
if rewrite_hardware_configuration:
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,
)
for file_description, file_path in files_to_delete.items():
print(
f"{c.BLUE}Deleting {c.YELLOW}{file_description}{c.BLUE} backup file{c.END}"
)
run(["rm", file_path], exit_on_error=False, cwd="/home/cswimr")
if purge_vscode_extensions:
print(
f"{c.BLUE}Killing {c.YELLOW}Visual Studio Code{c.BLUE} processes{c.END}"
)
run(["killall", "code"], exit_on_error=False)
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
extra_args.append("--impure")
if "--impure" in extra_args:
print(f"{c.RED}WARNING: The --impure flag is set!{c.END}")
run(["sudo", "nixos-rebuild", subcommand, *extra_args], cwd=path)
if __name__ == "__main__":
parser = ArgumentParser(
prog="upd.py",
description="Update your NixOS system via a convenient wrapper script.",
epilog="Example usage: upd.py",
)
parser.add_argument(
"--subcommand",
help="Which subcommand of `nixos-rebuild` to run.",
default="switch",
)
parser.add_argument(
"--pull",
help="If this is set, the script will run `git pull` in the flake directory before updating.",
action="store_true",
)
parser.add_argument(
"--lock",
help="If this is set, `nix flake update` will be ran before the system is updated.",
action="store_true",
)
parser.add_argument(
"--rewrite-hardware-configuration",
help="If this is set, `nixos-generate-config` will be ran before the system is updated.",
action="store_true",
)
parser.add_argument(
"--purge-vscode-extensions",
help="If this is set, extensions in the `~/.vscode/extensions` folder will be deleted before the system is updated.",
action="store_true",
)
args = parser.parse_args()
_upd(
subcommand=args.subcommand,
pull=args.pull,
lock=args.lock,
rewrite_hardware_configuration=args.rewrite_hardware_configuration,
purge_vscode_extensions=args.purge_vscode_extensions,
# extra_args = args.extra_args
)

41
scripts/py/vm.py Executable file
View file

@ -0,0 +1,41 @@
#! /usr/bin/env nix-shell
#! nix-shell /etc/nixos/scripts/nix/python.nix -i python
from argparse import ArgumentParser
from pathlib import Path
from common.colors import Colors as c
from common.common import run
def _vm(name: str = "nixpkgs", args: list = []):
if name == "nixpkgs":
args.extend(["-I", "nixpkgs=/bulk/home/cswimr/Projects/nixpkgs"])
path = Path(f"/etc/nixos/hosts/virtual-machines/{name}")
if path.exists():
if (path / "flake.nix").exists():
args.extend(["--flake", f"{path}#nixos"])
elif (path / "default.nix").exists():
args.extend(["-I", f"nixos-config={path}/default.nix", "--no-flake"])
print(f"{c.BLUE}Building virtual machine {c.YELLOW}{name}{c.END}")
run(["nixos-rebuild", "build-vm", *args], cwd=path)
print(f"{c.BLUE}Starting virtual vachine {c.YELLOW}{name}{c.END}")
run(["./result/bin/run-nixos-vm"], cwd=path)
print(
f"{c.BLUE}Virtual machine {c.YELLOW}{name} {c.BLUE}has {c.RED}stopped.{c.END}"
)
else:
raise FileNotFoundError(f"Virtual machine {name} does not exist.")
if __name__ == "__main__":
parser = ArgumentParser(
prog="vm.py",
description="Run a NixOS virtual machine from the system flake.",
epilog="Example usage: vm.py nixpkgs",
)
parser.add_argument("name", help="Which virtual machine to run.")
args = parser.parse_args()
_vm(name=args.name)