-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen-templates.py
More file actions
129 lines (93 loc) · 3.79 KB
/
gen-templates.py
File metadata and controls
129 lines (93 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
# gen-templates.py [templates-dir] [out-nix] [out-readme]
# defaults: ./templates ./templates.nix ./README.md
import json, subprocess, sys
from pathlib import Path
templates_dir = Path(sys.argv[1] if len(sys.argv) > 1 else "templates")
out_nix = Path(sys.argv[2] if len(sys.argv) > 2 else "templates.nix")
out_readme = Path(sys.argv[3] if len(sys.argv) > 3 else "README.md")
def get_description(path: Path) -> str:
r = subprocess.run(
["nix", "flake", "metadata", "--json", f"path:{path}"],
capture_output=True, text=True
)
if r.returncode != 0:
return ""
return json.loads(r.stdout).get("description", "")
def screenshots(path: Path) -> list[Path]:
return [p for p in (path / "pics" / f"{n}.png" for n in (1, 2)) if p.exists()]
def demo(path: Path) -> Path | None:
p = path / "pics" / "demo.gif"
return p if p.exists() else None
templates = sorted(
(d for d in templates_dir.iterdir() if d.is_dir() and (d / "flake.nix").exists()),
key=lambda d: d.name
)
if not templates:
sys.exit(f"no templates found in {templates_dir}")
entries = [(t.name, get_description(t), screenshots(t), demo(t)) for t in templates]
# ── templates.nix ─────────────────────────────────────────────────────────────
nix_attrs = "\n".join(
f' {name} = {{\n path = ./templates/{name};\n description = "{desc}";\n }};'
for name, desc, _, __ in entries
)
out_nix.write_text(
f"# auto-generated by gen-templates.py — do not edit manually\n"
f"{{\n{nix_attrs}\n}}\n"
)
# ── README.md ─────────────────────────────────────────────────────────────────
sections = []
for name, desc, pics, vid in entries:
lines = [f"### `{name}`", "", desc or "_no description_"]
for p in ([] if vid else pics):
lines += ["", f""]
if vid:
lines += ["", f'']
sections.append("\n".join(lines))
table_rows = "\n".join(
f"| [`{n}`](#{n}) | {d} |" for n, d, _, __ in entries
)
out_readme.write_text(f"""\
# nut-shells
NixOS flake templates for [nut](https://github.com/Francesco149/nut).
## usage
```sh
nix shell nixpkgs#git # if you don't have git
mkdir my-config && cd my-config
# replace <template> with the template name. see below for a list
nix flake init -t github:Francesco149/nut-shells#<template>
git init
git add .
# generate hardware config for your current machine
nixos-generate-config --dir ./hosts/nixos/ --force
sudo nixos-rebuild boot --flake .#nixos
sudo reboot
# log in as headpats with password changeme and change your password.
# NOTE: some templates like the default one have no users,
# so you just log in as root with your root password
passwd
```
if there's a non-root user, the default username is always `headpats` with
`changeme` as the initial password. remember to run `passwd` to change it, then
remove `users.users.headpats.initialPassword = "changeme";` in
`hosts/nixos/nixos.nix` .
if for whatever reason the config doesn't boot, you can select the previous
generation in grub to roll back. NixOS perks!
refer to the [nut docs](https://github.com/Francesco149/nut) for next steps.
---
## templates
| name | description |
| ---- | ----------- |
{table_rows}
{"---".join(f"\n\n{s}\n\n" for s in sections).strip()}
---
## maintaining this repo
after adding, removing, or renaming a template, regenerate the nix attrset and
this README:
```sh
nix run nixpkgs#python3 -- ./gen-templates.py
```
then open `README.md` in vscode and save to auto-format.
""")
print(f"wrote {out_nix}")
print(f"wrote {out_readme}")