Skip to content

run-nixos-vm breaks with spaces in paths #134318

@piegamesde

Description

@piegamesde

Describe the bug

I generated a VM with nix-build '<nixpkgs/nixos>' -A vm -I nixos-config=./vm.nix:

#! /nix/store/xvvgw9sb8wk6d2c0j3ybn7sll67s3s4z-bash-4.4-p23/bin/bash

NIX_DISK_IMAGE=$(readlink -f ${NIX_DISK_IMAGE:-./nixos.qcow2})

if ! test -e "$NIX_DISK_IMAGE"; then
    /nix/store/ffdmpzdh77bggj7hj02jpal0wn3s5wnn-qemu-6.0.0/bin/qemu-img create -f qcow2 "$NIX_DISK_IMAGE" \
      512M || exit 1
fi

# Create a directory for storing temporary data of the running VM.
if [ -z "$TMPDIR" -o -z "$USE_TMPDIR" ]; then
    TMPDIR=$(mktemp -d nix-vm.XXXXXXXXXX --tmpdir)
fi

# Create a directory for exchanging data with the VM.
mkdir -p $TMPDIR/xchg



cd $TMPDIR
idx=0


# Start QEMU.
exec /nix/store/ffdmpzdh77bggj7hj02jpal0wn3s5wnn-qemu-6.0.0/bin/qemu-kvm -cpu max \
    -name nixos \
    -m 2048 \
    -smp 1 \
    -device virtio-rng-pci \
    -net nic,netdev=user.0,model=virtio -netdev user,id=user.0${QEMU_NET_OPTS:+,$QEMU_NET_OPTS} \
    -virtfs local,path=/nix/store,security_model=none,mount_tag=store \
    -virtfs local,path=$TMPDIR/xchg,security_model=none,mount_tag=xchg \
    -virtfs local,path=${SHARED_DIR:-$TMPDIR/xchg},security_model=none,mount_tag=shared \
    -drive cache=writeback,file=$NIX_DISK_IMAGE,id=drive1,if=none,index=1,werror=report -device virtio-blk-pci,drive=drive1 \
    -usb -device usb-tablet,bus=usb-bus.0 -kernel /nix/store/z38jqqijckiwlnl05j4bvqhzacvsa3fc-nixos-system-nixos-21.05.2428.2d6ab6c6b92/kernel -initrd /nix/store/z38jqqijckiwlnl05j4bvqhzacvsa3fc-nixos-system-nixos-21.05.2428.2d6ab6c6b92/initrd -append "$(cat /nix/store/z38jqqijckiwlnl05j4bvqhzacvsa3fc-nixos-system-nixos-21.05.2428.2d6ab6c6b92/kernel-params) init=/nix/store/z38jqqijckiwlnl05j4bvqhzacvsa3fc-nixos-system-nixos-21.05.2428.2d6ab6c6b92/init regInfo=/nix/store/yc79bjdbndmnv9q0fwlp2dxg69zap15n-closure-info/registration console=ttyS0,115200n8 console=tty0 $QEMU_KERNEL_PARAMS" \
    $QEMU_OPTS \
    "$@"

The specific issue is caused by the unquoted file=$NIX_DISK_IMAGE in the exec command. But in general, shellcheck.net reports a lot more of these potential issues, and they should all get fixed.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Build a VM in a folder with spaces or other funny things in the file name
  2. Try to run it

Expected behavior

shellcheck.net reports no issues on the generated script.

Notify maintainers

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

  • system: "x86_64-linux"
  • host os: Linux 5.12.13-arch1-1, Arch Linux, noversion
  • multi-user?: no
  • sandbox: yes
  • version: nix-env (Nix) 2.3.10
  • channels: "nixpkgs-21.05.2428.2d6ab6c6b92, nixpkgs-unstable-21.11pre307920.6df7b8f3985"
  • nixpkgs: ~/.nix-defexpr/channels/nixpkgs

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute:
# a list of nixos modules affected by the problem
module:

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bugSomething is broken6.topic: nixosIssues or PRs affecting NixOS modules, or package usability issues specific to NixOS
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions