refactor: cleanup scripts

This commit is contained in:
Alex Maldonado
2026-05-12 15:21:31 -04:00
parent 9bdd8bd9d5
commit 74dc3215fd
10 changed files with 350 additions and 300 deletions
+5 -12
View File
@@ -13,7 +13,6 @@ from pathlib import Path
from textwrap import dedent
### Color helpers ###
def _supports_color() -> bool:
return sys.stderr.isatty()
@@ -42,8 +41,6 @@ def die(msg: str, code: int = 1) -> None:
### Validation ###
# RFC 1123 hostname: 1-63 chars, [a-z0-9-], no leading/trailing dash.
HOST_RE = re.compile(r"^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$")
@@ -53,8 +50,6 @@ def valid_hostname(name: str) -> bool:
### Templates ###
DEFAULT_NIX_TEMPLATE = dedent("""\
{{ pkgs, ... }}: {{
imports = [
@@ -105,7 +100,6 @@ PLACEHOLDER_HARDWARE = dedent("""\
### Helpers ###
def add_to_flake(flake_path: Path, name: str, *, template_host: str | None = None) -> None:
"""
@@ -119,13 +113,11 @@ def add_to_flake(flake_path: Path, name: str, *, template_host: str | None = Non
"""
src = flake_path.read_text()
# Find `nixosConfigurations = {`; be flexible about whitespace.
m = re.search(r"nixosConfigurations\s*=\s*\{", src)
if not m:
die(f"could not find `nixosConfigurations = {{` in {flake_path}")
open_pos = m.end() - 1 # index of the '{'
# Brace-match to find the closing '}' of this attrset.
depth = 0
close_pos = None
@@ -250,7 +242,7 @@ def add_to_flake(flake_path: Path, name: str, *, template_host: str | None = Non
def host_in_flake(flake_path: Path, name: str) -> bool:
"""Cheap textual check; does flake.nix already define this host?"""
"""Cheap textual check, does flake.nix already define this host?"""
src = flake_path.read_text()
return bool(
re.search(rf"(^|\W){re.escape(name)}\s*=\s*nixpkgs\.lib\.nixosSystem", src)
@@ -269,6 +261,7 @@ def find_template_host(flake_path: Path) -> str | None:
return m2.group(1) if m2 else None
def main() -> int:
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("name", help="hostname for the new host (lowercase, RFC 1123)")
@@ -291,9 +284,9 @@ def main() -> int:
die(f"host `{args.name}` already defined in {flake} — refusing to touch anything")
template_host = find_template_host(flake)
log(f"repo: {repo}")
log(f"new host: {args.name}")
log(f"template host: {template_host or '<none — using default block>'}")
log(f"repo: {repo}")
log(f"new host: {args.name}")
log(f"template host: {template_host or '<none, using default block>'}")
if args.dry_run:
warn("dry-run: nothing written")