-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrebuild.nu
executable file
·136 lines (111 loc) · 3.2 KB
/
rebuild.nu
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
#!/usr/bin/env nu
def --wrapped sync [...arguments] {
(rsync
--rsh "ssh -q"
--compress
--delete --recursive --force
--delete-excluded
--delete-missing-args
...$arguments)
}
# Rebuild a NixOS / Darwin config.
def main --wrapped [
host: string = "" # The host to build.
...arguments # The arguments to pass to `nixos-rebuild switch`.
]: nothing -> nothing {
let host = if ($host | is-not-empty) {
$host
} else {
(hostname)
}
if $host != (hostname) {
ssh -q -tt $host $"
rm -rf ncc
"
git ls-files
| sync --files-from - ./ ($host + ":ncc")
ssh -q -tt $host $"
cd ncc
./rebuild.nu ($host) ($arguments | str join ' ')
"
return
}
let args_split = $arguments | prepend "" | split list "--"
let nh_flags = [
"--hostname" $host
] | append ($args_split | get 0 | filter { $in != "" })
let nix_flags = [
"--option" "accept-flake-config" "true"
"--option" "eval-cache" "false"
] | append ($args_split | get --ignore-errors 1 | default [])
if (uname | get kernel-name) == "Darwin" {
darwin-rebuild switch --flake (".#" + $host) ...$nix_flags
if not (xcode-select --install e>| str contains "Command line tools are already installed") {
darwin-shadow-xcode-popup
}
darwin-set-zshrc
} else {
nh os switch . ...$nh_flags -- ...$nix_flags
}
}
# Replace with the command that has been triggering
# the "install developer tools" popup.
#
# Set by default to "SplitForks" because who even uses that?
const original_trigger = "/usr/bin/SplitForks"
# Where the symbolic links to `/usr/bin/false` will
# be created in to shadow all popup-triggering binaries.
#
# Place this in your $env.PATH right before /usr/bin
# to never get the "install developer tools" popup ever again:
#
# ```nu
# let usr_bin_index = $env.PATH
# | enumerate
# | where item == /usr/bin
# | get 0.index
#
# $env.PATH = $env.PATH | insert $usr_bin_index $shadow_path
# ```
#
# Do NOT set this to a path that you use for other things,
# it will get deleted if it exists to only have the shadowers.
const shadow_path = "~/.local/shadow" | path expand # Did you read the comment?
def darwin-shadow-xcode-popup [] {
print "shadowing xcode popup binaries..."
let original_size = ls $original_trigger | get 0.size
let shadoweds = ls /usr/bin
| flatten
| filter {
# All xcode-select binaries are the same size, so we can narrow down and not run weird stuff.
$in.size == $original_size
}
| filter {
^$in.name e>| str contains "xcode-select: note: No developer tools were found, requesting install."
}
| get name
| each { path basename }
rm -rf $shadow_path
mkdir $shadow_path
for shadowed in $shadoweds {
let shadow_path = $shadow_path | path join $shadowed
ln --symbolic /usr/bin/false $shadow_path
}
}
def darwin-set-zshrc [] {
print "setting zshrc..."
let nu_command = $"
let usr_bin_index = $env.PATH
| enumerate
| where item == /usr/bin
| get 0.index
$env.PATH = $env.PATH | insert $usr_bin_index ($shadow_path | path expand)
$env.SHELL = which nu | get 0.path
"
let zshrc = $"
exec nu --execute '
($nu_command)
'
"
$zshrc | save --force ~/.zshrc
}