How to read a file in a nixpkgs test?

I’ve started some work to test the contents of some configuration files in nixpkgs, but I’m running into a strange issue:

machine: waiting for file ‘/etc/pam.d/chfn‘
(0.02 seconds)
kill machine (pid 8)
machine # qemu-system-x86_64: terminating on signal 15 from pid 6 (/nix/store/fkzla307l4mlcvfyshsrccwl7szihx2z-python3-3.9.6/bin/python3)
(0.02 seconds)
Traceback (most recent call last):
  File "/nix/store/xry4g85wdm7jq0fwg7bx41l2zshi1cdz-nixos-test-driver/bin/.nixos-test-driver-wrapped", line 1334, in <module>
  File "/nix/store/xry4g85wdm7jq0fwg7bx41l2zshi1cdz-nixos-test-driver/bin/.nixos-test-driver-wrapped", line 1210, in run_tests
  File "/nix/store/xry4g85wdm7jq0fwg7bx41l2zshi1cdz-nixos-test-driver/bin/.nixos-test-driver-wrapped", line 1206, in test_script
    exec(self.tests, symbols, None)
  File "<string>", line 4, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/etc/pam.d/chfn'

So machine.wait_for_file("/etc/pam.d/chfn") on line 3 succeeds but open("/etc/pam.d/chfn") on line 4 fails. What gives? I tried machine.send_chars("ls -lA $(readlink -f /etc/static/pam.d/chfn)\n") followed by machine.screenshot("foo"), and the target file is there, readable by everyone. I also tried opening with mode rb, but no go.

What am I doing wrong?

1 Like

testScript is not run inside the machine so open will not have access to files inside it. You could use machine.copy_from_vm or machine.succeed("cat /etc/pam.d/chfn"), the latter returns stdout (i.e. file contents).