-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.sh
219 lines (187 loc) · 5.12 KB
/
helper.sh
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
# RUN THIS SCRIPT WITH `nix run .#helper -- <args...>`
set -euo pipefail
# You can change this to your own cache.
CACHIX_NAME="spitulax"
command -v nom >/dev/null
NOM=$?
_nix () {
nix --experimental-features 'nix-command flakes' $@
}
echoErr () {
echo -en "\033[31m" 1>&2
echo $@ 1>&2
echo -en "\033[0m" 1>&2
}
decolor () {
sed -e 's/\x1b\[[0-9;]*m//g'
}
paths () {
_nix path-info --json result/ | jq -r '.[].references.[]'
}
build () {
echo -e '\033[1mBuilding packages...\033[0m'
if [ $NOM -eq 0 ]; then
_nix build .#cached --log-format internal-json -v --accept-flake-config |& nom --json
else
_nix build .#cached --accept-flake-config
fi
}
push () {
echo -e '\033[1mPushing packages...\033[0m'
cachix push "$CACHIX_NAME" $(paths)
}
upinput () {
echo -e '\033[1mUpdating flake inputs...\033[0m'
_nix flake update --accept-flake-config
}
uplist () {
echo -e '\033[1mUpdating package list...\033[0m'
local path=$(_nix build .#mypkgs-list --accept-flake-config --json | jq -r '.[].outputs.out')
install -m644 "$path" list.md
}
# Arguments:
# - DIRNAME: one or more directories to update (pkgs/* or flakes/*)
# - SKIP_EXIST: if 1, skip directories where pkg.json already exists
# - FORCE: if 1, update even if the found version is the same as the old version
# - FLAKE_ONLY: if 1, only update the flakes
upscript () {
up () {
local type="$1"
local dirname="$2"
local script="$3"
if ! [ -d "${dirname}" ]; then
echoErr "${dirname} did not exist"
return
fi
local json_path
case "$type" in
"flake")
json_path="${dirname}/flake.json"
;;
"pkg")
json_path="${dirname}/pkg.json"
;;
*)
echoErr "Unknown type: ${type}"
return
esac
if [ "${SKIP_EXIST:-0}" -eq 1 ] && [ -f "$json_path" ]; then
return
fi
echo -e "\033[1mUpdating ${dirname}...\033[0m"
local oldver
if [ -r "$json_path" ]; then
if [ "$type" == "flake" ]; then
oldver=$(cat "$json_path" | jq -r '.rev')
elif [ "$type" == "pkg" ]; then
oldver=$(cat "$json_path" | jq -r '.version')
fi
fi
local json
set +e
export FORCE="${FORCE:-0}"
if json=$($script "${oldver:-}"); then
echo "$json" > "$json_path"
else
echo "Skipped"
fi
set -e
}
echo -e '\033[1mRunning update scripts...\033[0m'
local pkgs_drv
local flakes_drv=$(_nix build .#flakes-update-scripts --accept-flake-config --json | jq -r '.[].outputs.out')
if [ "${FLAKE_ONLY:-0}" -ne 1 ]; then
pkgs_drv=$(_nix build .#pkgs-update-scripts --accept-flake-config --json | jq -r '.[].outputs.out')
fi
for x in $(find -L "$flakes_drv" -type f -executable); do
if [ -v DIRNAME ]; then
for dirname in $DIRNAME; do
if [[ "$dirname" != flakes/* ]]; then
continue
fi
if [ "$dirname" == "flakes/$(basename "$x")" ]; then
up "flake" "$dirname" "$x"
break
fi
done
else
up "flake" "flakes/$(basename "$x")" "$x"
fi
done
[ "${FLAKE_ONLY:-0}" -eq 1 ] && return
for x in $(find -L "$pkgs_drv" -type f -executable); do
if [ -v DIRNAME ]; then
for dirname in $DIRNAME; do
if [[ "$dirname" != pkgs/* ]]; then
continue
fi
if [ "$dirname" == "pkgs/$(basename "$x")" ]; then
up "pkg" "$dirname" "$x"
break
fi
done
else
up "pkg" "pkgs/$(basename "$x")" "$x"
fi
done
}
commitup () {
# TODO: Implement the new `commitup`
echoErr "Unimplemented"
exit 1
}
usage () {
echo "Arguments are passed via environment variables."
echo "Subcommands:"
echo "- build"
echo "- commitup"
echo "- pushinput"
echo "- pushpkgs"
echo "- up"
echo "- upinput"
echo "- uplist"
echo "- upscript"
}
[ $# -ne 1 ] && usage && exit 1
case "$1" in
# Update this flake's inputs.
"upinput")
upinput
;;
# Build cached packages.
"build")
build
;;
# Push this flake's inputs to cachix.
"pushinput")
echo -e '\033[1mPushing inputs to cachix...\033[0m'
_nix flake archive --accept-flake-config --json \
| jq -r '.path,(.inputs|to_entries[].value.path)' \
| cachix push "$CACHIX_NAME"
;;
# Push recently built packages to cachix.
# Typically used right after calling `build`.
"pushpkgs")
push
;;
# Update `list.md`.
"uplist")
uplist
;;
# Run the update scipt of maintained packages and flakes.
"upscript")
upscript
;;
# Full update routine.
"up")
upinput && upscript && build && push && uplist
;;
# Commit current changes as an update.
"commitup")
commitup
;;
*)
usage
exit 1
;;
esac