Search code examples
elixirmqttnerves-project

How to copy CRT file on a Nerves device


I want to copy ca file to establish ssl connection. I am placing the CA inside lib

lib/
   mymodule.ex #using the ca path here
   mycrt.crt

and then using mix firmware I am giving the relative path like this

 def getPath() do
    {path,0} = System.cmd("pwd",[])
    String.replace(path,"\n","/lib/mycrt.crt")
   end

But we run on host using iex -S mix so I have to prefix the /lib before mycrt.crt

I am not sure whether it is properly getting copied in /lib or pwd is getting the right path after burning on device. Is this a proper way of doing? Because when I am doing ls on device I cannot see mycrt.crt copied in directory

What is the right way to copy files over device using Nerves?


Solution

  • Instead of lib use priv, so:

    priv/mycrt.crt
    

    Then in code:

    Path.join(:code.priv_dir(:my_app), "mycrt.crt")
    

    See https://erlang.org/doc/man/code.html#priv_dir-1.

    Alternatively you could use a module attribute to read the file contents at compile time:

    @crt_bin File.read!("lib/mycrt.crt")
    

    Whether you just want the file to be available on the target, or want the module attribute version, I would recommend storing the file in priv per: http://erlang.org/doc/design_principles/applications.html#7.4. In particular:

    • priv - Optional. Used for application specific files.

    and:

    The priv directory holds assets that the application needs during runtime. Executables should reside in priv/bin and dynamically-linked libraries should reside in priv/lib. Other assets are free to reside within the priv directory but it is recommended it does so in a structured manner.