forked from nix-community/nixpkgs-wayland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nu
executable file
·205 lines (175 loc) · 6.31 KB
/
main.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
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
#!/usr/bin/env nu
let system = "x86_64-linux";
let forceCheck = false; # use for development to re-update all pkgs
$env.CACHIX_CACHE = (
if "CACHIX_CACHE" in $env { $env.CACHIX_CACHE }
else "nixpkgs-wayland"
)
$env.CACHIX_SIGNING_KEY = (
if "CACHIX_SIGNING_KEY_NIXPKGS_WAYLAND" in $env { $env.CACHIX_SIGNING_KEY_NIXPKGS_WAYLAND }
else "null"
)
def header [ color: string text: string spacer="▒": string ] {
let text = $"($text) "
let header = $"("" | fill -c $spacer -w 2) ($text | fill -c $spacer -w 100)"
print -e $"(ansi $color)($header)(ansi reset)"
}
def getBadHash [ attrName: string ] {
let val = ((do -i { ^nix build --no-link $attrName }| complete)
| get stderr
| split row "\n"
| where ($it | str contains "got:")
| str replace --regex '\s+got:(.*)(sha256-.*)' '$2'
| get 0
)
$val
}
def replaceHash [ packageName: string, position: string, hashName: string, oldHash: string ] {
let fakeSha256 = "0000000000000000000000000000000000000000000000000000";
do -c { ^sd -s $"($oldHash)" $"($fakeSha256)" $"($position)" }
let newHash = (getBadHash $".#($packageName)")
do -c { ^sd -s $"($fakeSha256)" $"($newHash)" $"($position)" }
print -e {packageName: $packageName, hashName: $hashName, oldHash: $oldHash, newHash: $newHash}
}
def updatePkgs [] {
header "light_yellow_reverse" "update packages"
let pkgs = (^nix eval --json $".#packages.($system)" --apply 'x: builtins.attrNames x' | str trim | from json)
let pkgs = ($pkgs | where ($it != "default"))
$pkgs | each { |packageName|
let position = $"pkgs/($packageName)/metadata.nix"
let verinfo = (^nix eval --json -f $position | str trim | from json)
let skip = (("skip" in ($verinfo | transpose | get column0)) and $verinfo.skip)
if $skip {
print -e $"(ansi light_yellow) update ($packageName) - (ansi light_cyan_underline)skipped(ansi reset)"
} else {
# Try update rev
let newrev = (
if ("repo_git" in ($verinfo | transpose | get column0)) {
(do -c {
^git ls-remote $verinfo.repo_git $"refs/heads/($verinfo.branch)"
} | complete | get stdout | str trim | str replace --regex '(\s+)(.*)$' "")
} else if ( "repo_hg" in ($verinfo | transpose | get column0) ) {
(do -c {
^hg identify $verinfo.repo_hg -r $verinfo.branch
} | complete | get stdout | str trim)
} else {
error make { msg: "unknown repo type" }
}
)
let shouldUpdate = (if ($forceCheck) {
print -e $"(ansi light_yellow) update ($packageName) - (ansi light_yellow_underline)forced(ansi reset)"
true
} else if ($newrev != $verinfo.rev) {
print -e $"(ansi light_yellow) update ($packageName) - (ansi light_yellow_underline)update to ($newrev)(ansi reset)"
true
} else {
print -e $"(ansi dark_gray) update ($packageName) - noop(ansi reset)"
false
})
if ($shouldUpdate) {
do -c { ^sd -s $"($verinfo.rev)" $"($newrev)" $"($position)" }
print -e {packageName: $packageName, oldrev: $verinfo.rev, newrev: $newrev}
replaceHash $packageName $position "sha256" $verinfo.sha256
if "vendorSha256" in ($verinfo | transpose | get column0) {
replaceHash $packageName $position "vendorSha256" $verinfo.vendorSha256
}
do -c {
^git commit $position -m $"auto-update: ($packageName): ($verinfo.rev) => ($newrev)"
} | complete
}
null
} # end !skip
} # end each-pkg loop
}
def buildDrv [ drvRef: string ] {
header "white_reverse" $"build ($drvRef)" "░"
header "blue_reverse" $"eval ($drvRef)"
let evalJobs = (
^nix-eval-jobs
--flake $".#($drvRef)"
--check-cache-status
| from json --objects
)
header "green_reverse" $"build ($drvRef)"
print -e ($evalJobs
| where isCached == false
| select name isCached)
$evalJobs
| where isCached == false
| each { |drv| do -c { ^nix build $'($drv.drvPath)^*' } }
header "purple_reverse" $"cache: calculate paths: ($drvRef)"
let pushPaths = ($evalJobs | each { |drv|
$drv.outputs | each { |outPath|
if ($outPath.out | path exists) {
$outPath.out
}
}
})
print -e $pushPaths
if ($env.CACHIX_SIGNING_KEY != "null") {
let cachePathsStr = ($pushPaths | each {|it| $"($it)(char nl)"} | str join)
let cacheResults = (echo $cachePathsStr | ^cachix push $env.CACHIX_CACHE | complete)
header "purple_reverse" $"cache/push ($drvRef)"
print -e $cacheResults
} else {
print -e "'$CACHIX_SIGNING_KEY_NIXPKGS_WAYLAND' not set, not pushing to cachix."
}
}
def "main rereadme" [] {
let color = "yellow"
header $"($color)_reverse" $"readme"
let packageNames = (nix eval --json $".#packages.($system)" --apply 'x: builtins.attrNames x' | str trim | from json)
let pkgList = ($packageNames | where ($it != "default"))
let delimStart = "<!--pkgs-start-->"
let delimEnd = "<!--pkgs-end-->"
let pkgrows = ($pkgList | each { |packageName|
let meta = (do -c {
nix eval --json $".#packages.($system).($packageName).meta" | str trim | from json
})
let home = (if "homepage" in ($meta | transpose | get column0) {
$meta.homepage
} else { "__missing__" })
($"| [($packageName)]\(($home)\) | ($meta.description) |")
})
let rows = [
$delimStart
"| Package | Description |"
"| --- | --- |"
$pkgrows
$delimEnd
]
let tableText = ($rows | flatten | str join "\n")
let regexString = ([ '(?s)(.*)' $delimStart '(.*)' $delimEnd '(.*)' ] | str join '')
let replaceText = $"\$1($tableText)\$3"
^rg --multiline $regexString "README.md" --replace $replaceText | save --raw README2.md
mv -f README2.md README.md
do -i { ^git commit -m "auto-update: updated readme" "./README.md" }
}
def "main build" [] {
buildDrv $"packages.($system)"
print -e ""
buildDrv $"devShells.($system).default.inputDerivation"
}
def flakeAdvance [] {
header "purple_reverse" "advance flake inputs"
^nix flake lock --recreate-lock-file --commit-lock-file
}
def gitPush [] {
header "purple_reverse" "git push origin HEAD"
^git push origin HEAD
}
def "main advance" [] {
flakeAdvance
main build
gitPush
}
def "main update" [] {
flakeAdvance
updatePkgs
main build
main rereadme
gitPush
}
def main [] {
print -e "commands: [advance, update, build]"
}