-
Notifications
You must be signed in to change notification settings - Fork 1
/
vyxnix.fish
134 lines (116 loc) · 4.46 KB
/
vyxnix.fish
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
function vyxnix -d "vyxos nix3 launcher"
# Principal nix3 launcher.
#
# "develop" and "shell" are treated specially:
# vyxnix develop -> nix develop --command fish
# vyxnix develop x y z -> nix develop x y z --command fish
# vyxnix develop x -- y z -> nix develop x --command fish -c "y z"
# vyxnix develop x --command y z -> nix develop x --command y z
# "d/s" below refers to these cases.
#
# "run" and "shell" are also treated specially:
# vyxnix run xyz -> nix run nixpkgs#xyz
# vyxnix run abc#def -> nix run abc#def
# vyxnix shell uvw -- nyonk -> nix shell nixpkgs#uvw -- nyonk
#
# !x=y translates to --override-input x y.
# !! translates to --offline.
# Default options (specify --vyx-no-defaults to omit):
set -f defaultargs -L --keep-going
# Specify --vyx-dry-run to echo the command that would be executed.
set -f dryrun 0
# $state: "args", "command", "run"
# - "args" is the initial state.
# - (d/s only) "command" is entered when "--command" is encountered.
# All remaining arguments are appended to $args.
# - (d/s only) "run" is entered when "--" is encountered.
# All remaining arguments are appended to $run.
set -f state args
# $args: arguments passed to the command, not including any 'run'.
set -f args
# $run: run arguments, passed to the command (fish).
set -f run
# $command: the nix3 subcommand to run.
set -f command $argv[1]
set -e argv[1]
# $ds: 1 if develop or shell, 0 otherwise.
if test "$command" = develop -o "$command" = shell
set -f ds 1
else
set -f ds 0
end
for arg in $argv
switch $state
case args
if test "$arg" = --vyx-no-defaults
set defaultargs
continue
end
if test "$arg" = --vyx-dry-run
set dryrun 1
continue
end
if string match -qr '\A!(?<input>[^=]+)=(?<override>.+)\z' -- "$arg"
set -a args --override-input "$input" "$override"
continue
end
if test "$arg" = "!!"
set -a args --offline
continue
end
if test "$arg" = --command -a "$ds" -eq 1
set -a args --command
set state command
else if test "$arg" = -- -a "$ds" -eq 1
set state run
else if test "$command" = run -o "$command" = shell
if string match -qr '#' -- "$arg"
# nr abc#def ...
# ns abc#def ...
set -a args $arg
set state command
else if test "$arg" = --
# nr ... -- ...
set -a args $arg
set state command
else if string match -qr '\A-' -- "$arg"
# nr --xyz ...
# ns --xyz ...
set -a args $arg
else if string match -qr ':' -- "$arg"
# nr github:xyz/abc ...
set -a args $arg
else if test "$arg" = '.'
# nr . ...
set -a args $arg
else
# nr abc ...
# ns abc ...
set -a args "nixpkgs#$arg"
end
else
set -a args $arg
end
case command
set -a args $arg
case run
set -a run $arg
end
end
if test "$state" = run
set cmd nix $command $defaultargs $args --command fish -c (string escape -- $run | string join ' ')
else
if test "$state" = args -a "$ds" -eq 1
set -a args --command fish
end
set cmd nix $command $defaultargs $args
end
if test "$dryrun" -eq 1
echo (string escape -- $cmd)
else
$cmd
end
end
complete -c vyxnix -l vyx-no-defaults -d "omit the default arguments passed by vyxnix"
complete -c vyxnix -l vyx-dry-run -d "do a dry run of this command; i.e. echo what would be run"
complete -c vyxnix -w nix