-
Notifications
You must be signed in to change notification settings - Fork 17
/
nix-copy-closure.sh
286 lines (251 loc) · 10.1 KB
/
nix-copy-closure.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
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
#!/usr/bin/env bash
# Automatic generated, DON'T MODIFY IT.
# @flag --to Copy the closure of paths from a Nix store accessible from the local machine to the Nix store on the remote machine.
# @flag --from Copy the closure of paths from the Nix store on the remote machine to the local machine’s specified Nix store.
# @flag --gzip Enable compression of the SSH connection.
# @flag --include-outputs Also copy the outputs of store derivations included in the closure.
# @flag -s --use-substitutes Attempt to download missing store objects on the target from substituters.
# @flag --help Prints out a summary of the command syntax and exits.
# @flag --version Prints out the Nix version number on standard output and exits.
# @flag --quiet Decreases the level of verbosity of diagnostic messages printed on standard error.
# @option --log-format[`_module_nix_log_format`] <format> This option can be used to change the output of the log format, with format being one of:
# @flag -Q --no-build-output By default, output written by builders to standard output and standard error is echoed to the Nix command’s standard error.
# @option -j --max-jobs <number> Sets the maximum number of build jobs that Nix will perform in parallel to the specified number.
# @flag --cores Sets the value of the NIX_BUILD_CORES environment variable in the invocation of builders.
# @flag --max-silent-time Sets the maximum number of seconds that a builder can go without producing any data on standard output or standard error.
# @flag --timeout Sets the maximum number of seconds that a builder can run.
# @flag -k --keep-going Keep going in case of failed builds, to the greatest extent possible.
# @flag -K --keep-failed Specifies that in case of a build failure, the temporary directory (usually in /tmp) in which the build takes place should not be deleted.
# @flag --fallback Whenever Nix attempts to build a derivation for which substitutes are known for each output path, but realising the output paths through the substitutes fails, fall back on building the derivation.
# @flag --readonly-mode When this option is used, no attempt is made to open the Nix database.
# @option --arg*[`_module_nix_arg_name`] <name> <value> This option is accepted by nix-env, nix-instantiate, nix-shell and nix-build.
# @option --argstr*[`_module_nix_arg_name`] <name> <value> This option is like --arg, only the value is not a Nix expression but a string.
# @option -A --attr[`_module_nix_attr`] <attrPath> Select an attribute from the top-level Nix expression being evaluated.
# @flag -E --expr Interpret the command line arguments as a list of Nix expressions to be parsed and evaluated, rather than as a list of file names of Nix expressions.
# @option -I --include <path> Add an entry to the list of search paths used to resolve lookup paths.
# @option --option <name> <value> Set the Nix configuration option name to value.
# @flag --repair Fix corrupted or missing store paths by redownloading or rebuilding them.
# @arg user-machine[`_module_ssh_host`]
# @arg paths*[`_choice_remote_path`]
. "$ARGC_COMPLETIONS_ROOT/utils/_argc_utils.sh"
_choice_remote_path() {
if [[ -z "$argc_user_machine" ]]; then
return;
fi
ssh -o 'Batchmode yes' "$argc_user_machine" command ls -a1dp "$ARGC_CWORD*" 2>/dev/null | \
_argc_util_comp_parts /
}
_module_nix_arg_name() {
if [[ "${argc__args[-2]}" != @(--arg|--argstr) ]]; then
return
fi
local func opt
if [[ -n "$argc_expr" ]]; then
func="${argc__positionals[0]}"
else
local file="$(_module_nix_get_file)"
func="${file:+"import $file"}"
fi
if [[ -z "$func" ]]; then
return
fi
names=($(_module_nix_eval_stdin 2>&1 <<NIX_FILE
if builtins.typeOf ($func) == "lambda" then
builtins.attrNames (builtins.functionArgs ($func))
else
""
NIX_FILE
))
if [[ $? -gt 0 ]]; then
return 1
fi
printf "%s\n" "${names[@]}"
}
_module_nix_attr() {
local defexpr func
if [[ -n "$argc_expr" ]]; then
func="${argc__positionals[0]}"
else
local file="$(_module_nix_get_file)"
if [[ -n "$file" ]]; then
func="import $file"
fi
fi
if [[ -n "$func" ]]; then
local i len args=""
len="${#argc_arg[@]}"
for ((i=0; i<$len; i+=2)); do
args+="${argc_arg[$i]} = ${argc_arg[$((i+1))]};"
done
len="${#argc_argstr[@]}"
for ((i=0; i<$len; i+=2)); do
args+="${argc_arg[$i]} = \"${argc_arg[$((i+1))]}\";"
done
defexpr="$func $args"
else
local cmd_name="$(_module_nix_cmd_name)"
if [[ "$cmd_name" == nix-env ]]; then
defexpr=$(_module_nix_gen_defexpr ~/.nix-defexpr)
elif [[ "$cmd_name" == nix ]]; then
local channels
IFS=':' read -ra channels <<< "$NIX_PATH"
channels+=( ${argc_I[@]} )
declare -A names
for channel in "${channels[@]}"; do
name="${channel%%=*}"
nix_path="${channel#*=}"
if [[ "$name" != "$channel" ]]; then
names["$name"]=1
fi
done
defexpr=$'{ '
for name in "${!names[@]}"; do
[[ "$name" == nixos-config ]] && continue
defexpr+="$name = import <${name}>; "
done
defexpr+=' }'
fi
fi
if [[ -n "$defexpr" ]]; then
_module_nix_gen_attr "$defexpr"
fi
}
_module_nix_cmd_name() {
if [[ -z "$nix_cmd_name" ]]; then
nix_cmd_name="$(basename ${argc__args[0]})"
nix_cmd_name="${nix_cmd_name%%.*}"
fi
echo "$nix_cmd_name"
}
_module_nix_eval_stdin() {
local i override
if [[ -n "$argc_I" ]]; then
override=$(IFS=':'; echo "${argc_I[*]}")
fi
override+=${override:+:}${NIX_PATH}
while [[ "$override" == *@(=|:)channel:* ]]; do
local channel=${override#*channel:}
channel="channel:"${channel%%:*}
local url="https://nixos.org/channels/"${channel:8}"/nixexprs.tar.xz"
override=${override/"$channel"/"$url"}
done
while [[ "$override" == *https://* ]]; do
local url=${override#*https://}
url="https://"${url%%:*}
local cache=$(_module_nix_resolve_url "$url")
override=${override/"$url"/"$cache"}
done
NIX_PATH=$override nix-instantiate --eval - 2> /dev/null | tr '"[]' ' '
}
_module_nix_gen_attr() {
local cur="$ARGC_CWORD"
if [[ $cur == .* ]]; then
return
fi
local defexpr="$1"
_argc_util_comp_parts .
local -a paths=(${argc__parts_prefix//./ })
paths=(${paths[*]/%/\"})
paths=(${paths[*]/#/\"})
local -a result
result=($(_module_nix_eval_stdin <<NIX_FILE
let
autocall = setOrLambda:
if builtins.isFunction setOrLambda then
setOrLambda {}
else
setOrLambda;
top = autocall ($defexpr);
names = [ ${paths[*]} ];
# Returns attr.name calling it if it's a function
reducer = set: name:
autocall (builtins.getAttr name set);
result = builtins.foldl' reducer top names; #'
in
if builtins.isAttrs result then
builtins.attrNames result
else
""
NIX_FILE
))
if [[ $? -gt 0 ]]; then
return 1
fi
if [[ -z "$argc__parts_prefix" ]]; then
printf "%s\n" "${result[@]}" | _argc_util_transform suffix=. nospace
else
printf "%s\n" "${result[@]}"
fi
}
_module_nix_gen_defexpr() {
shopt -s nullglob
declare -A result
local -a initialQueue=("$1/channels" "$1/channels_root" $(echo "$1"/*))
local -a queue=("${initialQueue[@]}")
while [[ ${#queue[@]} -gt 0 ]]; do
local current="${queue[0]}"
queue=(${queue[@]:1})
if [[ -e "$current/default.nix" ]]; then
local name
name="$(basename "$current")"
if [[ -z "${result[$name]}" ]]; then
result["$name"]=$current
fi
else
queue=($(echo "$current"/*) "${queue[@]}")
fi
done
local nix_expr="{ "
for name in "${!result[@]}"; do
local expr_path="${result[$name]}"
nix_expr+="$name = import $expr_path;"
nix_expr+=" "
done
nix_expr+="}"
echo "$nix_expr"
}
_module_nix_get_file() {
local file
local cmd_name="$(_module_nix_cmd_name)"
if [[ "$cmd_name" == @(nix-env|nix) ]]; then
file="$argc_file"
elif [[ -e shell.nix && "$cmd_name" == nix-shell ]]; then
file="shell.nix"
elif [[ -e default.nix ]]; then
file="default.nix"
fi
if [[ "$file" ]]; then
if [[ "$file" == channel:* ]]; then
file="https://nixos.org/channels/"${file:8}"/nixexprs.tar.xz"
fi
if [[ -e $file ]]; then
file=$(realpath "$file" 2>/dev/null)
elif [[ "$file" == https://* ]]; then
file=$(_module_nix_resolve_url $file)
fi
fi
printf -- "$file"
}
_module_nix_log_format() {
cat <<-'EOF'
raw This is the raw format, as outputted by nix-build.
internal-json Outputs the logs in a structured manner.
bar Only display a progress bar during the builds.
bar-with-logs Display the raw logs, with the progress bar at the bottom.
EOF
}
_module_nix_resolve_url() {
local url=$1
local input="${url##*/}\0$url"
local sha
sha=$(nix-hash --flat --base32 --type sha256 <(printf "$input"))
local cache=${XDG_CACHE_HOME:-~/.cache}/nix/tarballs
local link="$cache"/"$sha"-file
if [[ -e "$link" ]]; then
echo "$cache/$(basename $(readlink $link))-unpacked"
fi
}
_module_ssh_host() {
cat ~/.ssh/config | sed -n 's/^\s*Host\s\+\(\S.*\?\)\s*$/\1/Ip'
}
command eval "$(argc --argc-eval "$0" "$@")"