blob: fc19f402583f04ab27e9d7b0e6e15ec7456b8a3e (
plain)
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
|
#!/run/current-system/profile/bin/guile -s
!#
(use-modules (guix)
(guix channels)
(guix import json)
(guix git)
(ice-9 popen)
(ice-9 textual-ports)
(ice-9 pretty-print)
(srfi srfi-1)) ;; alist
;;
;; config
;;
(define %dotfiles-dir
(dirname (dirname (current-filename))))
(define %guix-channels-file
(string-append %dotfiles-dir "/channels.scm"))
(define %nix-flake-file
(string-append %dotfiles-dir "/home/nix/flake.nix"))
(define %guix-cuirass-servers
(list "https://cuirass.nonguix.org"
"https://cuirass.freya.cat"))
;;
;; io functions
;;
(define (invoke . args)
(let* ((command (string-join args " "))
(err-cons (pipe))
(port (with-error-to-port (cdr err-cons)
(lambda _ (open-input-pipe command))))
(_ (setvbuf (car err-cons) 'block
(* 1024 16))) ; 16kib buffer
(result (get-string-all port)))
(close-port (cdr err-cons))
result))
(define (read-file path)
(call-with-input-file path get-string-all))
(define (write-file path contents)
(call-with-output-file path (lambda (port)
(put-string port contents))))
;;
;; git functions
;;
(define (git-checkout git-url)
;; create or update a local bare git checkout from <git-url>
;; - clones checkout if it doesnt exist
;; - fetches newset commits
(let* ((home (getenv "HOME"))
(name (string-append
(string-replace-substring
(last (string-split git-url #\/))
".git"
"")
".git"))
(dir (string-append home
"/.cache/checkouts/"
name)))
(if (not (access? dir F_OK))
(begin
(format #t "checking out ~a...\n" name)
(invoke "mkdir" "-p" dir)
(invoke "git" "clone" "--bare" git-url dir)))
(invoke "git" "-C" dir "fetch")
dir))
(define (git-latest-commit git-url)
(car (string-split
(invoke "git" "ls-remote" git-url "HEAD")
#\tab)))
(define (git-rev-list git-url local-commit remote-commit)
;; get how many commits ahead the remote commit is
;; from the current local commit
;; - returns 0 if remote-commit is the same or older
(if (and (string? local-commit)
(string? remote-commit))
(let* ((dir (git-checkout git-url))
(timestamp
;; git rev-list counts #commits
;; - returns zero if commits out of order (remote older)
(invoke "git" "-C" dir "rev-list" "--count"
(string-append local-commit
".."
remote-commit))))
(string->number (string-trim-both timestamp #\newline)))
;; return zero of one of the commit
;; hashes provided is invalid
0))
;;
;; core functions
;;
(define (merge-commits local-commits remote-commits)
;; create a zipped pair of each <loacl> and <remote> commits
;; - ignores the commit if the remote its older than the local commit
;;
;; local commit format:
;; ('id "git-url" "commit")
;;
;; remote commit format:
;; ('id . "commit")
;;
;; return format:
;; ('id "local-commit" "newest-commit") ;; neweset commit is commit with
;; ;; newer timestamp
(fold (lambda (local-repo commits)
(let* ((repo-id (car local-repo))
(git-url (cadr local-repo))
(local-commit (caddr local-repo))
(remote-commit (assoc-ref remote-commits repo-id))
(commit-diff (git-rev-list git-url
local-commit
remote-commit)))
;; dont append these commits to the updated
;; merge list if the remote-commit is not better then
;; what we already have locally
(if (> commit-diff 0)
(cons (list repo-id local-commit remote-commit)
commits)
commits)))
'()
local-commits))
(define (update-file local-commits remote-commits path)
(let* ((contents (read-file path))
(commits (merge-commits local-commits remote-commits))
(result (fold (lambda (pair contents)
(let ((repo-id (car pair))
(local-commit (cadr pair))
(remote-commit (caddr pair)))
(format #t "~s\t~a -> ~a\n"
repo-id
(string-take local-commit 7)
(string-take remote-commit 7))
(string-replace-substring contents
local-commit
remote-commit)))
(read-file path)
commits)))
(write-file path result)
(length commits)))
;;
;; guix functions
;;
(define (cuirass-latest-evaluation cuirass-url)
(json-fetch
(string-append
cuirass-url
"/api/evaluations?nr=1")))
(define (guix-local-commits channels)
(map (lambda (channel)
(list (channel-name channel)
(channel-url channel)
(channel-commit channel)))
channels))
(define (cuirass-commits cuirass-servers)
(fold (lambda (cuirass-url env)
(let* ((evaluation (cuirass-latest-evaluation cuirass-url))
(checkouts (vector->list
(assoc-ref
(car (vector->list evaluation))
"checkouts"))))
(fold (lambda (checkout env)
(let* ((channel (string->symbol
(assoc-ref checkout "channel")))
(commit (assoc-ref checkout "commit")))
(if (assoc-ref env channel)
;; channel already set, skip
env
;; add commit to env
(assoc-set! env channel commit))))
env
checkouts)))
'()
cuirass-servers))
;;
;; nix functions
;;
(define (nix-local-commits flake-file)
(let ((lines (filter
(lambda (line)
(string-contains line "github:"))
(string-split (read-file flake-file)
#\newline))))
(map
;; valid line, parse out repo-id, git-url, and commit
(lambda (line)
(let* ((trimmed (substring line
(+ (string-index line #\:) 1)
(string-rindex line #\")))
(pair (string-split trimmed #\/))
(repo-id (string->symbol (cadr pair)))
(git-url (string-append "https://github.com/"
(car pair) "/"
(cadr pair) ".git"))
(commit (caddr pair)))
(list repo-id git-url commit)))
lines)))
(define (nix-nixpkgs-commit)
(let ((json (json-fetch
(string-append
"https://raw.githubusercontent.com/"
"nix-community/home-manager"
"/refs/heads/master/flake.lock")
#:timeout 1)))
(fold
(lambda (key json)
(assoc-ref json key))
json
(list "nodes"
"nixpkgs"
"locked"
"rev"))))
(define (nix-remote-commits local-commits)
(fold (lambda (pair env)
(let* ((repo-id (car pair))
(git-url (cadr pair))
(commit (if (eq? repo-id 'nixpkgs)
(nix-nixpkgs-commit)
(git-latest-commit git-url))))
(assoc-set! env repo-id commit)))
'()
local-commits))
;;
;; update
;;
(define %guix-local-commits
(guix-local-commits
(load %guix-channels-file)))
(define %guix-remote-commits
(cuirass-commits %guix-cuirass-servers))
(define %nix-local-commits
(nix-local-commits %nix-flake-file))
(define %nix-remote-commits
(nix-remote-commits %nix-local-commits))
(if (> (update-file
%guix-local-commits
%guix-remote-commits
%guix-channels-file) 0)
(system* "guix-retry" "guix" "pull" "-C" %guix-channels-file))
(if (> (update-file
%nix-local-commits
%nix-remote-commits
%nix-flake-file) 0)
(system* "nix" "flake" "lock" (dirname %nix-flake-file)))
|