Nature40 / pimod

Reconfigure Raspberry Pi images with an easy, Docker-like configuration file
GNU General Public License v3.0
122 stars 19 forks source link

RUN export VARIABLE=foo gives error "unbound variable" #45

Closed patrickelectric closed 2 years ago

patrickelectric commented 2 years ago

When trying to export a variable the program just fail with unbound variable

oxzi commented 2 years ago

Each RUN command is executed within a new ad hoc shell within the chrooted environment. Thus, exporting a variable in one RUN command and using it in a second RUN command is not possible.

However, I could think of an ENV command which exports environment variables to the chroot. You could also export your variable directly in the Pifile as it is a Bash script itself and use the exported variable later.

patrickelectric commented 2 years ago

That's a great idea !

oxzi commented 2 years ago

Please excuse the delay. I have just pushed f9c137d463e884b9efff2dddda4e6d731d250e52 to master which adds an ENV command.

As a Pifile is just a Bash script itself, using the dollar sign is hard. So there is now the possibility to either use the variable within another sh -c "echo $VAR" or let pimod do the substitution for you via @@VAR@@. Please read the ENV documentation or this example: https://github.com/Nature40/pimod/blob/f9c137d463e884b9efff2dddda4e6d731d250e52/examples/RPi-RaspberryPiOSLite.Pifile#L16-L24

Btw, just while typing the commit message, I just thought about an easier hack. As a Pifile is just Bash, one could use Bashism within the Pifile and export VAR=WHATEVER and use $VAR within the Pifile which would be replaced by the Bash on the host. However, this would not result in an exported variable on the guest resp. in the image. So at least it was worth something.

Please try out the current master branch and report back. Thanks!

sensslen commented 2 years ago

I just noticed that the environment variables only get replaced in the RUN command and not in the HOST command

oxzi commented 2 years ago

@sensslen: Thanks for reporting. I just pushed a commit adding HOST support for ENV.

I will close this issue for now. Anyone, please feel free to reopen, if necessary.