mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-10 19:55:41 +03:00
nixos/qemu-vm: minor readability improvements
The script generation is using the *lib.imap* functions in several other places already so this spot using a shell script variable instead seems a bit off. Moving the previous shell script code to Nix improves upon the Nix code by removing the additional *lib.optionalString* for the variable initialisation making the code more concise. The shell code is reduced to a one-liner per disk image, making it much easier to determine that this is a templated loop. Compare the previous: ```bash idx=0 if ! test -e "empty$idx.qcow2"; then /nix/store/73n3qwfazqw8zwr1z840jsirjllqpg9v-qemu-host-cpu-only-for-vm-tests-9.0.2/bin/qemu-img create -f qcow2 "empty$idx.qcow2" "20480M" fi idx=$((idx + 1)) if ! test -e "empty$idx.qcow2"; then /nix/store/73n3qwfazqw8zwr1z840jsirjllqpg9v-qemu-host-cpu-only-for-vm-tests-9.0.2/bin/qemu-img create -f qcow2 "empty$idx.qcow2" "20480M" fi idx=$((idx + 1)) if ! test -e "empty$idx.qcow2"; then /nix/store/73n3qwfazqw8zwr1z840jsirjllqpg9v-qemu-host-cpu-only-for-vm-tests-9.0.2/bin/qemu-img create -f qcow2 "empty$idx.qcow2" "20480M" fi idx=$((idx + 1)) ``` and the new: ```bash test -e "empty0.qcow2" || /nix/store/73n3qwfazqw8zwr1z840jsirjllqpg9v-qemu-host-cpu-only-for-vm-tests-9.0.2/bin/qemu-img create -f qcow2 "empty0.qcow2" "20480M" test -e "empty1.qcow2" || /nix/store/73n3qwfazqw8zwr1z840jsirjllqpg9v-qemu-host-cpu-only-for-vm-tests-9.0.2/bin/qemu-img create -f qcow2 "empty1.qcow2" "20480M" test -e "empty2.qcow2" || /nix/store/73n3qwfazqw8zwr1z840jsirjllqpg9v-qemu-host-cpu-only-for-vm-tests-9.0.2/bin/qemu-img create -f qcow2 "empty2.qcow2" "20480M" ``` While the line becomes slightly longer it also becomes immediately obvious on a visual level which parts are changing for each invocation (i.e. different disk sizes as well as the incremented counter stick out). Since the "idx" variable is now embedded, this also becomes copy&pastable, and also shows the maximum index readily in the last line, as opposed to having to count the number of if statements otherwise. None of this is *needed* of course. Signed-off-by: benaryorg <binary@benary.org>
This commit is contained in:
parent
edd7f43442
commit
048d8cceee
1 changed files with 8 additions and 7 deletions
|
@ -257,13 +257,14 @@ let
|
|||
|
||||
cd "$TMPDIR"
|
||||
|
||||
${lib.optionalString (cfg.emptyDiskImages != [ ]) "idx=0"}
|
||||
${flip concatMapStrings cfg.emptyDiskImages (size: ''
|
||||
if ! test -e "empty$idx.qcow2"; then
|
||||
${qemu}/bin/qemu-img create -f qcow2 "empty$idx.qcow2" "${toString size}M"
|
||||
fi
|
||||
idx=$((idx + 1))
|
||||
'')}
|
||||
${lib.pipe cfg.emptyDiskImages [
|
||||
(lib.imap0 (
|
||||
idx: size: ''
|
||||
test -e "empty${builtins.toString idx}.qcow2" || ${qemu}/bin/qemu-img create -f qcow2 "empty${builtins.toString idx}.qcow2" "${builtins.toString size}M"
|
||||
''
|
||||
))
|
||||
(builtins.concatStringsSep "")
|
||||
]}
|
||||
|
||||
# Start QEMU.
|
||||
exec ${qemu-common.qemuBinary qemu} \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue