How to create a build to connect to specific wifi AP on the first boot automatically?

I am doing an experiment to create a Dragonboard image that can connect to a specific wifi AP on the first boot automatically.
I am using the embedded Linux version and need some suggestions on how to implement this.
What I plan to do is to run this command somewhere during the system initialization.
$ nmcli dev wifi connect “mywifi” password “mypassword”
But I don’t know where and how to add this command. And if there are dependencies of this command, how do I specify that? I also want to know if there are any permission I need to specify to issue this command.

There are several ways to achieve this. Personally, I would add the AP config file to the image.
When you perform your nmcli command, a persistent conf file is created in /etc/NetworkManager/system-connections/apname.

You can then the copy it to your yocto build tree, and add a networkmanager.bbapend in which you add this config file to the image.

Note that your password will be clearly visible.

Loic,
I can add the AP config file to my build. After I flash the build, will nmcli pickup the config file and connect to the specific AP automatically the first time it powers up? or Do I need to run some nmcli command manually to direct it to the AP config file we integrated in?

Sure, this should configure network-manage in the same way as with the command line.