-
Notifications
You must be signed in to change notification settings - Fork 43
/
do.py
executable file
·330 lines (285 loc) · 9.69 KB
/
do.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import os
import subprocess
from enum import Enum
from typing import List
import platform
import typer
app = typer.Typer()
class FlakeOutputs(Enum):
NIXOS = "nixosConfigurations"
DARWIN = "darwinConfigurations"
HOME_MANAGER = "homeConfigurations"
class Colors(Enum):
SUCCESS = typer.colors.GREEN
INFO = typer.colors.BLUE
ERROR = typer.colors.RED
check_git = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True)
LOCAL_FLAKE = os.path.realpath(check_git.stdout.decode().strip())
REMOTE_FLAKE = "github:kclejeune/system"
is_local = check_git.returncode == 0 and os.path.isfile(
os.path.join(LOCAL_FLAKE, "flake.nix")
)
FLAKE_PATH = LOCAL_FLAKE if is_local else REMOTE_FLAKE
UNAME = platform.uname()
check_nixos = subprocess.run(
["/usr/bin/env", "type", "nixos-rebuild"], capture_output=True
)
check_darwin = subprocess.run(
["/usr/bin/env", "type", "darwin-rebuild"], capture_output=True
)
if check_nixos.returncode == 0:
# if we're on nixos, this command is built in
PLATFORM = FlakeOutputs.NIXOS
elif check_darwin.returncode == 0 or UNAME.system.lower() == "darwin":
# if we're on darwin, we might have darwin-rebuild or the distro id will be 'darwin'
PLATFORM = FlakeOutputs.DARWIN
else:
# in all other cases of linux
PLATFORM = FlakeOutputs.HOME_MANAGER
USERNAME = os.getenv(
"USER", subprocess.run(["id", "-un"], capture_output=True).stdout.decode().strip()
)
SYSTEM_ARCH = "aarch64" if UNAME.machine == "arm64" else UNAME.machine
SYSTEM_OS = UNAME.system.lower()
DEFAULT_HOST = f"{USERNAME}@{SYSTEM_ARCH}-{SYSTEM_OS}"
def fmt_command(cmd: List[str]):
cmd_str = " ".join(cmd)
return f"$ {cmd_str}"
def test_cmd(cmd: List[str]):
return subprocess.run(cmd).returncode == 0
def run_cmd(cmd: List[str], shell=False):
typer.secho(fmt_command(cmd), fg=Colors.INFO.value)
return (
subprocess.run(" ".join(cmd), shell=True)
if shell
else subprocess.run(cmd, shell=False)
)
def select(nixos: bool, darwin: bool, home_manager: bool):
if sum([nixos, darwin, home_manager]) > 1:
typer.secho(
"cannot apply more than one of [--nixos, --darwin, --home-manager]. aborting...",
fg=Colors.ERROR.value,
)
raise typer.Abort()
if nixos:
return FlakeOutputs.NIXOS
elif darwin:
return FlakeOutputs.DARWIN
elif home_manager:
return FlakeOutputs.HOME_MANAGER
else:
return PLATFORM
@app.command(
help="builds an initial configuration",
hidden=PLATFORM == FlakeOutputs.NIXOS,
)
def bootstrap(
host: str = typer.Argument(
DEFAULT_HOST, help="the hostname of the configuration to build"
),
remote: bool = typer.Option(
default=False,
hidden=not is_local,
help="whether to fetch current changes from the remote",
),
nixos: bool = False,
darwin: bool = False,
home_manager: bool = False,
):
cfg = select(nixos=nixos, darwin=darwin, home_manager=home_manager)
flags = [
"-v",
"--experimental-features",
"nix-command flakes",
"--extra-substituters",
"https://kclejeune.cachix.org",
]
bootstrap_flake = REMOTE_FLAKE if remote else FLAKE_PATH
if host is None:
typer.secho("host unspecified", fg=Colors.ERROR.value)
return
if cfg is None:
typer.secho("missing configuration", fg=Colors.ERROR.value)
elif cfg == FlakeOutputs.NIXOS:
typer.secho(
"boostrap does not apply to nixos systems.",
fg=Colors.ERROR.value,
)
raise typer.Abort()
elif cfg == FlakeOutputs.DARWIN:
disk_setup()
flake = f"{bootstrap_flake}#{cfg.value}.{host}.config.system.build.toplevel"
run_cmd(["nix", "build", flake] + flags)
run_cmd(
f"./result/sw/bin/darwin-rebuild switch --flake {FLAKE_PATH}#{host}".split()
)
elif cfg == FlakeOutputs.HOME_MANAGER:
flake = f"{bootstrap_flake}#{host}"
run_cmd(
["nix", "run"]
+ flags
+ [
"github:nix-community/home-manager",
"--no-write-lock-file",
"--",
"switch",
"--flake",
flake,
"-b",
"backup",
]
)
else:
typer.secho("could not infer system type.", fg=Colors.ERROR.value)
raise typer.Abort()
@app.command(
help="builds the specified flake output; infers correct platform to use if not specified",
)
def build(
host: str = typer.Argument(
DEFAULT_HOST, help="the hostname of the configuration to build"
),
remote: bool = typer.Option(
default=False,
hidden=not is_local,
help="whether to fetch current changes from the remote",
),
nixos: bool = False,
darwin: bool = False,
home_manager: bool = False,
):
cfg = select(nixos=nixos, darwin=darwin, home_manager=home_manager)
if cfg is None:
return
elif cfg == FlakeOutputs.NIXOS:
cmd = ["sudo", "nixos-rebuild", "build", "--flake"]
elif cfg == FlakeOutputs.DARWIN:
cmd = ["darwin-rebuild", "build", "--flake"]
elif cfg == FlakeOutputs.HOME_MANAGER:
cmd = ["home-manager", "build", "--flake"]
else:
typer.secho("could not infer system type.", fg=Colors.ERROR.value)
raise typer.Abort()
if remote:
flake = f"{REMOTE_FLAKE}#{host}"
else:
flake = f"{FLAKE_PATH}#{host}"
flags = ["--show-trace"]
run_cmd(cmd + [flake] + flags)
@app.command(
hidden=not is_local,
help="remove previously built configurations and symlinks from the current directory",
)
def clean(
filename: str = typer.Argument(
"result", help="the filename to be cleaned, or '*' for all files"
),
):
run_cmd(f"find . -type l -maxdepth 1 -name {filename} -exec rm {{}} +".split())
@app.command(
hidden=PLATFORM != FlakeOutputs.DARWIN,
help="configure disk setup for nix-darwin",
)
def disk_setup():
if not test_cmd("grep -q ^run\\b /etc/synthetic.conf".split()):
APFS_UTIL = "/System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util"
typer.secho("setting up /etc/synthetic.conf", fg=Colors.INFO.value)
run_cmd(
"echo 'run\tprivate/var/run' | sudo tee -a /etc/synthetic.conf".split(),
shell=True,
)
run_cmd([APFS_UTIL, "-B"])
run_cmd([APFS_UTIL, "-t"])
if not test_cmd(["test", "-L", "/run"]):
typer.secho("linking /run directory", fg=Colors.INFO.value)
run_cmd("sudo ln -sfn private/var/run /run".split())
typer.secho("disk setup complete", fg=Colors.SUCCESS.value)
@app.command(
help="run garbage collection on unused nix store paths",
no_args_is_help=True,
)
def gc(
delete_older_than: str = typer.Option(
None,
"--delete-older-than",
"-d",
metavar="[AGE]",
help="specify minimum age for deleting store paths",
),
dry_run: bool = typer.Option(False, help="test the result of garbage collection"),
):
cmd = f"nix-collect-garbage --delete-older-than {delete_older_than} {'--dry-run' if dry_run else ''}"
run_cmd(cmd.split())
@app.command(
hidden=not is_local,
help="update all flake inputs or optionally specific flakes",
)
def update(
flake: List[str] = typer.Option(
None,
"--flake",
"-f",
metavar="[FLAKE]",
help="specify an individual flake to be updated",
),
commit: bool = typer.Option(False, help="commit the updated lockfile"),
):
flags = ["--commit-lock-file"] if commit else []
if not flake:
typer.secho("updating all flake inputs")
run_cmd(["nix", "flake", "update"] + flags)
else:
inputs = []
for input in flake:
inputs.append("--update-input")
inputs.append(input)
typer.secho(f"updating {', '.join(flake)}")
run_cmd(["nix", "flake", "lock"] + inputs + flags)
@app.command(help="pull changes from remote repo", hidden=not is_local)
def pull():
cmd = f"git stash && git pull && git stash apply"
run_cmd(cmd.split())
@app.command(
help="builds and activates the specified flake output; infers correct platform to use if not specified",
)
def switch(
host: str = typer.Argument(
DEFAULT_HOST,
help="the hostname of the configuration to build",
),
remote: bool = typer.Option(
default=False,
hidden=not is_local,
help="whether to fetch current changes from the remote",
),
nixos: bool = False,
darwin: bool = False,
home_manager: bool = False,
):
if not host:
typer.secho("Error: host configuration not specified.", fg=Colors.ERROR.value)
raise typer.Abort()
cfg = select(nixos=nixos, darwin=darwin, home_manager=home_manager)
if cfg is None:
return
elif cfg == FlakeOutputs.NIXOS:
cmd = f"sudo nixos-rebuild switch --flake"
elif cfg == FlakeOutputs.DARWIN:
cmd = f"darwin-rebuild switch --flake"
elif cfg == FlakeOutputs.HOME_MANAGER:
cmd = f"home-manager switch --flake"
else:
typer.secho("could not infer system type.", fg=Colors.ERROR.value)
raise typer.Abort()
if remote:
flake = f"{REMOTE_FLAKE}#{host}"
else:
flake = f"{FLAKE_PATH}#{host}"
flags = ["--show-trace"]
run_cmd(cmd.split() + [flake] + flags)
@app.command(hidden=not is_local, help="cache the output environment of flake.nix")
def cache(cache_name: str = "kclejeune"):
cmd = f"nix flake archive --json | jq -r '.path,(.inputs|to_entries[].value.path)' | cachix push {cache_name}"
run_cmd(cmd.split(), shell=True)
if __name__ == "__main__":
app()