Flake nixos-install fails: mktemp: failed to create file via template ‘/run/user/0/tmp.XXXXXXXXXX’: No such file or directory

Happened

When trying to install a NixOS flake setup in a QEMU/KVM virtual machine, I get

mktemp: failed to create file via template ‘/run/user/0/tmp.XXXXXXXXXX’: No such file or directory
Traceback (most recent call last):
  File "/nix/store/yb390224d24pp41qya0s9w20jkfcrwp8-systemd-boot", line 305, in <module>
    main()
  File "/nix/store/yb390224d24pp41qya0s9w20jkfcrwp8-systemd-boot", line 293, in main
    subprocess.check_call("/nix/store/iqa5zghnjhxasfl1w3xigk6mbi0qlfbd-copy-extra-files")
  File "/nix/store/dn4fwp0yx6nsa85cr20cwvdmg64xwmcy-python3-3.9.9/lib/python3.9/subprocess.py", line 373, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '/nix/store/iqa5zghnjhxasfl1w3xigk6mbi0qlfbd-copy-extra-files' returned non-zero exit status 1.

Even though I’m able too boot in this installation afterwards, this terminates the installation in seemingly a bad state and I can’t login in the root user since I wasn’t asked to change its password.

Reproduce

  1. Get NixOS 21.11 minimal ISO image at https://nixos.org/download.html

  2. Use iso image to create new QEMU/KVM virtual machine on virt-manager, and configure to use UEFI instead of BIOS

  3. Boot virtual machine

  4. Partition

    sudo parted /dev/vda mklabel gpt
    sudo parted /dev/vda mkpart ESP fat32 0% 200MiB
    sudo parted /dev/vda mkpart CRY 200MiB 100%
    sudo parted /dev/vda set 1 esp on
    
  5. Encrypt vda2 and open it

    sudo cryptsetup --verbose --type luks2 --hash sha512 --cipher aes-xts-plain64 --key-size 512 luksFormat /dev/vda2
    sudo cryptsetup luksOpen --type luks2 /dev/vda2 cry
    
  6. Create LVM

    sudo pvcreate /dev/mapper/cry
    sudo vgcreate vg /dev/mapper/cry
    sudo lvcreate -L 30G vg -n root
    
  7. Format partitions

    sudo mkfs.vfat -F 32 -n esp /dev/vda1
    sudo mkfs.xfs -L root /dev/vg/root
    
  8. Mount

    sudo mount /dev/vg/root /mnt
    sudo mkdir /mnt/efi /etc/nixos
    sudo mount /dev/vda1 /mnt/efi
    
  9. Clone and install

    nix-shell -p git nixFlakes
    sudo git clone https://github.com/yuuyins/cnix.git /mnt/etc/nixos
    
  10. Change UUIDs in /mnt/etc/nixos/cnix/host/hardware-configuration.nix to match accordingly blkid output

  11. Install

    sudo nixos-install --impure --root /mnt --flake /mnt/etc/nixos#cnix
    

Log

nixos-install --impure --root /mnt --flake /mnt/etc/nixos#cnix
warning: creating lock file '/mnt/etc/nixos/flake.lock'
warning: Git tree '/mnt/etc/nixos' is dirty
building the flake in git+file:///mnt/etc/nixos...
warning: Git tree '/mnt/etc/nixos' is dirty
copying channel...
installing the boot loader...
warning: user ‘cnix’ has unknown group ‘cnix’
setting up /etc...
Warning: something's wrong at /nix/store/cz6na7w751iv7z78fb9ms8hhvnsd0l8z-setup-etc.pl line 120.
/etc/tmpfiles.d/journal-nocow.conf:26: Failed to resolve specifier: uninitialized /etc detected, skipping
All rules containing unresolvable specifiers will be skipped.
Initializing machine ID from VM UUID.
Created "/efi/EFI".
Created "/efi/EFI/systemd".
Created "/efi/EFI/BOOT".
Created "/efi/loader".
Created "/efi/loader/entries".
Created "/efi/EFI/Linux".
Copied "/nix/store/cfhpzlarbhfw3scj91dcz5ai04ayfzik-systemd-249.7/lib/systemd/boot/efi/systemd-bootx64.efi" to "/efi/EFI/systemd/systemd-bootx64.efi".
Copied "/nix/store/cfhpzlarbhfw3scj91dcz5ai04ayfzik-systemd-249.7/lib/systemd/boot/efi/systemd-bootx64.efi" to "/efi/EFI/BOOT/BOOTX64.EFI".
Created "/efi/d2fee85a986d49c29a422c3dd1435341".
Random seed file /efi/loader/random-seed successfully written (512 bytes).
Not installing system token, since we are running in a virtualized environment.
Created EFI boot entry "Linux Boot Manager".
mktemp: failed to create file via template ‘/run/user/0/tmp.XXXXXXXXXX’: No such file or directory
Traceback (most recent call last):
  File "/nix/store/yb390224d24pp41qya0s9w20jkfcrwp8-systemd-boot", line 305, in <module>
    main()
  File "/nix/store/yb390224d24pp41qya0s9w20jkfcrwp8-systemd-boot", line 293, in main
    subprocess.check_call("/nix/store/iqa5zghnjhxasfl1w3xigk6mbi0qlfbd-copy-extra-files")
  File "/nix/store/dn4fwp0yx6nsa85cr20cwvdmg64xwmcy-python3-3.9.9/lib/python3.9/subprocess.py", line 373, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '/nix/store/iqa5zghnjhxasfl1w3xigk6mbi0qlfbd-copy-extra-files' returned non-zero exit status 1.

I had that issue a few days ago trying on physical hardware and using the 21.11 ISO, with a similar disk setup (using cryptsetup). I’ve found this GitHub issue and Shados solution “solved” it (I put this on quotes because the installation finished successfully, but somehow I was not able to get into the login screen, which I’m not sure if it is totally related to the issue).

The way I could solve my problem it was using an old ISO (21.05). I was able to set up my machine smoothly.

I tried using nixos-minimal-21.05.4719.0fd9ee1aa36-x86_64-linux.iso, but I got the same issue.

nixos-install --impure --root /mnt --flake /mnt/etc/nixos#cnix
warning: creating lock file '/mnt/etc/nixos/flake.lock'
warning: Git tree '/mnt/etc/nixos' is dirty
building the flake in git+file:///mnt/etc/nixos...
warning: Git tree '/mnt/etc/nixos' is dirty
copying channel...
installing the boot loader...
setting up /etc...
/etc/tmpfiles.d/journal-nocow.conf:26: Failed to resolve specifier: uninitialized /etc detected, skipping
All rules containing unresolvable specifiers will be skipped.
Initializing machine ID from VM UUID.
Created "/efi/EFI".
Created "/efi/EFI/systemd".
Created "/efi/EFI/BOOT".
Created "/efi/loader".
Created "/efi/loader/entries".
Created "/efi/EFI/Linux".
Copied "/nix/store/cfhpzlarbhfw3scj91dcz5ai04ayfzik-systemd-249.7/lib/systemd/boot/efi/systemd-bootx64.efi" to "/efi/EFI/systemd/systemd-bootx64.efi".
Copied "/nix/store/cfhpzlarbhfw3scj91dcz5ai04ayfzik-systemd-249.7/lib/systemd/boot/efi/systemd-bootx64.efi" to "/efi/EFI/BOOT/BOOTX64.EFI".
Created "/efi/d2fee85a986d49c29a422c3dd1435341".
Random seed file /efi/loader/random-seed successfully written (512 bytes).
Not installing system token, since we are running in a virtualized environment.
Created EFI boot entry "Linux Boot Manager".
mktemp: failed to create file via template ‘/run/user/0/tmp.XXXXXXXXXX’: No such file or directory
Traceback (most recent call last):
  File "/nix/store/yb390224d24pp41qya0s9w20jkfcrwp8-systemd-boot", line 305, in <module>
    main()
  File "/nix/store/yb390224d24pp41qya0s9w20jkfcrwp8-systemd-boot", line 293, in main
    subprocess.check_call("/nix/store/iqa5zghnjhxasfl1w3xigk6mbi0qlfbd-copy-extra-files")
  File "/nix/store/dn4fwp0yx6nsa85cr20cwvdmg64xwmcy-python3-3.9.9/lib/python3.9/subprocess.py", line 373, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '/nix/store/iqa5zghnjhxasfl1w3xigk6mbi0qlfbd-copy-extra-files' returned non-zero exit status 1.

I workarounded it by installing with configuration.nix, and once installed, converted the config to a flake.