-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotfiles.nu
More file actions
401 lines (386 loc) · 12.5 KB
/
dotfiles.nu
File metadata and controls
401 lines (386 loc) · 12.5 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
const dotfiles_path = path self .
const config_file_name = "qd-config.yml"
const global_config_path = $dotfiles_path | path join "global-config.yml"
const master_rec_path = $dotfiles_path | path join master.rec
const master_key_path = $dotfiles_path | path join master.key
const tmp_path = $dotfiles_path | path join .tmp
# Dotfiles location
export def pwd []: nothing -> path { $dotfiles_path }
def make-tmp-file []: nothing -> path {
mkdir $tmp_path
let file = $tmp_path | path join (random uuid -v 7)
touch $file
$file
}
def cleanup-tmp [] { rm -r $tmp_path }
const known_hosts = [posix darwin ubuntu win windows]
const host_aliases = {darwin: posix, ubuntu: posix, windows: win}
def sys-host-name []: nothing -> string { sys host | get name }
def home-path [] { "~" | path expand }
def interpolate-path [path?: string]: list<string> -> path {
$in | each {|it|
match $it {
"%dotfiles%" => (pwd)
"%path%" => $path
"%dotfiles-cache%" => (home-path | path join .dotfiles-cache)
"%home%" => (home-path)
"%config%" => (home-path | path join .config)
"%application-support%" => (home-path | path join Library 'Application Support')
"%local-appdata%" => $env.LOCALAPPDATA
"%appdata%" => $env.APPDATA
_ => $it
}
}
| path join
}
def interpolate-string-list-field [config: record, field: string]: [
list<string> -> list<string>
list<any> -> list<any>
] {
each --flatten {|it|
match $it {
"%root%" => ($config | get $field)
_ => $it
}
}
}
def match-tags [machine_tags: list<string>]: string -> bool {
if ($in starts-with "!") {
($in | str substring 1..) not-in $machine_tags
} else {
$in in $machine_tags
}
}
def global-config []: nothing -> record {
mut auto_tags = []
if ("WSL_DISTRO_NAME" in $env) {
$auto_tags = $auto_tags | append wsl
}
let auto_tags = $auto_tags
open $global_config_path
| insert machine_tags {
$env.DOTFILES_TAGS? | default [] | append $auto_tags
}
}
def get-configs []: nothing -> list<path> {
cd $dotfiles_path
glob --no-dir $"*/($config_file_name)"
}
# Config schema (per config dir):
# - path: path
# - files: list<record<src: list<string>, dest: list<string>, if: list<stirng>>>
# - include: list<list<string>> # path segments / tokens to interpolate
# - encrypt: list<glob>
# - ignore: list<glob>
# - dotfile_include: list<list<string>>
# - dotfile_source: list<list<string>>
# - dotfile_env_include: list<list<string>>
# - dotfile_env_source: list<list<string>>
# - brew: list<string | { name: string, tap: string } | {"%root%": ...}>
# - scoop: list<string | { name: string, bucket: string } | {"%root%": ...}>
# - <host> / <host-alias>: same structure for host-specific overrides
def load-config []: path -> record {
let host = sys-host-name
let host_alias = (
$host_aliases
| get -o --ignore-case $host
| default 'unknown'
)
let parent = $in | path parse | get parent
let name = $parent | path basename
let global_config = global-config
let raw_config = open $in
if not ($raw_config.if? | default [] | all {match-tags $global_config.machine_tags}) {
return ($global_config | insert name $name | insert should_ignore true)
}
# Merge order is important
global-config
| merge deep --strategy append $raw_config
| merge ($raw_config | get -o --ignore-case $host_alias | default {})
| merge ($raw_config | get -o --ignore-case $host | default {})
| reject -o --ignore-case "if" ...$known_hosts
| upsert brew { default [] | interpolate-string-list-field $raw_config brew }
| upsert scoop { default [] | interpolate-string-list-field $raw_config scoop }
| upsert path { default [] | interpolate-path }
| rename --column {path: dest}
| upsert include {|c|
$c.include? | default []
| each {|row| $row | interpolate-path $c.dest }
}
| upsert files {|c|
$c.files? | default []
| interpolate-string-list-field $raw_config files
| where {|row| $row.if? | default [] | all {match-tags $c.machine_tags}}
| each {|row|
{
src: ($row.src | interpolate-path $c.dest)
dest: ($row.dest | interpolate-path $c.dest)
}
}
}
| insert name $name
| insert src $parent
}
def config-src []: [record -> path, record -> nothing] { $in.src? }
def config-dest []: [record -> path, record -> nothing] { $in.dest? }
def load-configs []: nothing -> list<record> { get-configs | each {load-config} | where ("should_ignore" not-in $it)}
def config-names []: nothing -> list<string> { load-configs | get name }
def config-file-path [name: string@config-names]: nothing -> path { $dotfiles_path | path join $name $config_file_name }
# Load config to check structure
export def verify-config [config_name: string@config-names] { config-file-path $config_name | load-config }
# Pull remote updates
export def remote-pull [] {
cd $dotfiles_path
fossil update
}
# Push updates to remote
export def remote-push [message: string] {
cd $dotfiles_path
fossil addremove
fossil commit --comment $message
}
# Uncommited diff
export def remote-diff [] {
cd $dotfiles_path
fossil diff
}
# Compile dotfile
export def compile-home-dotfile [] {
let imports = load-configs
| append (global-config)
| each --flatten {|c|
let dest = $c | config-dest
$c.dotfile_include? | default [] | each {interpolate-path $dest | $'use `($in)`'}
| append ($c.dotfile_source? | default [] | each {interpolate-path $dest | $'source `($in)`'})
| wrap path | insert env false
| append (
$c.dotfile_env_include? | default [] | each {interpolate-path $dest | $'use `($in)`'}
| append ($c.dotfile_env_source? | default [] | each {interpolate-path $dest | $'source `($in)`'})
| wrap path | insert env true
)
}
$imports | where env == false | get path | uniq | sort -r | str join (char newline) | save -f (home-path | path join .dotfiles.local.nu)
$imports | where env == true | get path | uniq | sort -r | str join (char newline) | save -f (home-path | path join .dotfiles-env.local.nu)
}
def syncable-configs []: nothing -> list<string> { load-configs | where ($it | config-dest | is-not-empty) | get name }
def path-strip [strip_path: path]: list<path> -> list<path> { each {path relative-to $strip_path} }
def files [--ignore: list<glob> = [], --encrypt: list<glob> = []]: path -> list<record> {
let input = $in
if ($input | is-empty) {
error make {msg: "No path provided", help: "Config is missing path", label: {
text: "Path is empty",
span: (metadata $input).span
}}
}
cd $input
glob --no-dir --exclude (
["**/*.age" $"**/($config_file_name)"]
| append $ignore | append $encrypt
) */**
| path-strip $input
| wrap path
| insert encrypted false
| append (
$encrypt
| append **/*.age
| each --flatten {|it|
glob --no-dir --exclude $ignore $it
}
| path-strip $input
| str replace -r ".age$" ""
| wrap path
| insert encrypted true
)
}
def files-in-src []: record -> list<record> { config-src | files }
def files-in-dest []: record -> list<record> {
let config = $in
$config | config-dest
| files --ignore ($config.ignore? | default []) --encrypt ($config.encrypt? | default [])
}
# Difference (INPUT ∖ ARG) - all extra files in INPUT as a result
def files-difference-with [exclude: list<record>]: list<record> -> list<record> {
let exclude = $exclude | get path
$in | where $it.path not-in $exclude
}
# Pull local config files into dotfiles
export def pull [
config_name: string@syncable-configs
--sync-with-remote (-s): string # Push updates to remote after pulling from local
--no-recompile
] {
let config = config-file-path $config_name | load-config
let dest_files = $config | files-in-dest
$dest_files | each {|it|
let from = $config | config-dest | path join $it.path
let to = $config | config-src | path join $it.path
mkdir ($to | path parse | get parent)
if $it.encrypted {
let target = $"($to).age"
let enc = {|| age --encrypt -R $master_rec_path -o $target $from }
if not ($target | path exists) {
do $enc
return
}
let out = make-tmp-file
age --decrypt -i $master_key_path -o $out $target
if not (same-files $out $from) {
do $enc
}
rm $out
} else {
cp --update $from $to
}
}
$config.files | each {|it| cp --update $it.dest $it.src}
$config | files-in-src | files-difference-with $dest_files | each {|it|
let file_name = if $it.encrypted { $"($it.path).age" } else { $it.path }
rm ($config | config-src | path join $file_name)
}
if ($sync_with_remote | is-not-empty) {
remote-push $sync_with_remote
}
if not $no_recompile {
compile-home-dotfile
}
}
# Pull all local configs into dotfiles
export def pull-all [
--sync-with-remote (-s): string # Push updates to remote after pulling from local
] {
syncable-configs | each {|c| pull --no-recompile $c}
if ($sync_with_remote | is-not-empty) {
remote-push $sync_with_remote
}
compile-home-dotfile
cleanup-tmp
}
# Push stored config files into local
export def push [
config_name: string@syncable-configs
--sync-with-remote (-s) # Pull remote updates before pushing to local
--no-recompile
] {
if $sync_with_remote {
remote-pull
}
let config = config-file-path $config_name | load-config
let is_first_push = not ($config | config-dest | path exists)
let pre_init_script_path = $config | config-dest | path join qd-pre-init.nu
if $is_first_push and ($pre_init_script_path | path exists) {
nu $pre_init_script_path
}
let src_files = $config | files-in-src
$src_files
| each {|it|
let from = $config | config-src | path join $it.path
let to = $config | config-dest | path join $it.path
mkdir ($to | path parse | get parent)
if $it.encrypted {
let out = make-tmp-file
age --decrypt -i $master_key_path -o $out $"($from).age"
cp --force $out $to
rm $out
} else {
cp --update $from $to
}
}
$config.files | each {|it| cp --update $it.src $it.dest}
$config.include | each {|from|
let to = $config | config-dest | path join ($from | path basename)
cp --update $from $to
}
$config | files-in-dest | files-difference-with $src_files | each {|f| rm ($config | config-dest | path join $f.path)}
let init_script_path = $config | config-dest | path join qd-init.nu
if $is_first_push and ($init_script_path | path exists) {
nu $init_script_path
}
if not $no_recompile {
compile-home-dotfile
}
ignore
}
# Push all stored configs into local
export def push-all [
--sync-with-remote (-s) # Pull remote updates before pushing to local
] {
if $sync_with_remote {
remote-pull
}
syncable-configs | each {|c| push --no-recompile $c}
compile-home-dotfile
cleanup-tmp
}
def load-brew-config []: nothing -> record<packages: list<string>, taps: list<string>> {
load-configs
| get brew
| flatten
| each {|it|
if ($it | describe) == "string" {
{
name: $it
tap: null
}
} else {
$it
}
}
| uniq-by name
| {
packages: ($in | get name)
taps: ($in | get tap | uniq | where (is-not-empty))
}
}
# Install packages from list
export def brew-install [] {
let config = load-brew-config
$config.taps | each {|it| brew tap $it}
if ($config.packages | is-not-empty) {
brew install ...$config.packages
}
}
# Upgrade packages from list
export def brew-upgrade [] { load-brew-config | get packages | brew upgrade ...$in }
def load-scoop-config []: nothing -> record<packages: list<string>, buckets: list<string>> {
load-configs
| get scoop
| flatten
| each {|it|
if ($it | describe) == "string" {
{
name: $it
bucket: main
}
} else {
$it
}
}
| uniq-by name
| {
packages: ($in | each {|it| $"($it.bucket)/($it.name)"})
buckets: ($in | get bucket | uniq)
}
}
# Install packages from list
export def scoop-install [] {
let config = load-scoop-config
$config.buckets | each {|buck| scoop bucket add $buck}
if ($config.packages | is-not-empty) {
$config.packages | scoop install ...$in
}
}
# Upgrade packages from list
export def scoop-upgrade [] { load-scoop-config | get packages | scoop update ...$in }
# Install packages and push all configs
export def init [] {
if (which brew | is-not-empty) {
brew-install
}
if (which scoop | is-not-empty) {
scoop-install
}
push-all
}
def same-files [file1: path, file2: path]: nothing -> bool {
(open -r $file1) == (open -r $file2)
}