voughtdq

voughtdq

Escript: can not load argon2_nif - wrong approach for CLI?

I’m trying to create a command line utility for creating administrative users in a back office application.

defmodule Admin.CLI do

  def main(args) do
    handle_command(args)
  end

  def handle_command(["createadmin" | rest]) do
    {opts, _, _} = OptionParser.parse(rest, switches: [username: :string, password: :string],
      aliases: [u: :username, p: :password])

    username = Keyword.get(opts, :username) || raise "You must supply a username"
    password = Keyword.get(opts, :password) || raise "You must supply a password"

    case Admin.create_user(%{"username" => username, "password" => password}) do
      {:ok, _user} ->
        IO.puts("User #{username} created with password #{password}")
      {:error, _} ->
        IO.puts(:stderr, "Error creating user.")
    end
  end

  def handle_command(_) do
    IO.puts(:stderr, "Invalid command.")
  end
end

But this is the output that I receive when running the command:

$ ./admin createadmin --username test --password test
[warn] The on_load function for module Elixir.Argon2.Base returned:
{:function_clause, [{:filename, :join, [{:error, :bad_name}, 'argon2_nif'], [file: 'filename.erl', line: 446]}, {Argon2.Base, :load_nif, 0, [file: 'lib/argon2/base.ex', line: 115]}, {Argon2.Base, :init, 0, [file: 'lib/argon2/base.ex', ...]}, {:code_server, :"-handle_on_load/5-fun-0-", 1, [...]}]}

[error] Process #PID<0.243.0> raised an exception
** (FunctionClauseError) no function clause matching in :filename.join/2
    (stdlib) filename.erl:446: :filename.join({:error, :bad_name}, 'argon2_nif')
    (argon2_elixir) lib/argon2/base.ex:115: Argon2.Base.load_nif/0
    (argon2_elixir) lib/argon2/base.ex:14: Argon2.Base.init/0
    (kernel) code_server.erl:1340: anonymous fn/1 in :code_server.handle_on_load/5
** (UndefinedFunctionError) function Argon2.Base.hash_password/3 is undefined (module Argon2.Base is not available)
    (argon2_elixir) Argon2.Base.hash_password("test", <<158, 41, 41, 255, 119, 126, 67, 223, 90, 157, 199, 83, 101, 28, 67, 206>>, [])
    (argon2_elixir) lib/argon2.ex:140: Argon2.add_hash/2
    (admin) lib/admin/user.ex:30: Admin.User.maybe_add_hash/1
    (admin) lib/admin.ex:14: Admin.create_user/1
    (admin) lib/admin/cli.ex:14: Admin.CLI.handle_command/1
    (elixir) lib/kernel/cli.ex:121: anonymous fn/3 in Kernel.CLI.exec_fun/2

When running with the same input from iex -S mix, it works as expected:

iex(1)> Admin.CLI.handle_command(["createadmin", "--username", "test", "--password", "test"])
User test created with password test
[debug] QUERY OK db=25.1ms decode=0.8ms queue=0.6ms
INSERT INTO "users" ("password_hash","username") VALUES ($1,$2) RETURNING "id" ["$argon2id$v=19$m=131072,t=8,p=4$dXaxDD2S7LG2sAaxfj8u9w$iJw1bEMSwZCPS3jIFZZ4Fzg/5sGf0hSEh8izr1hlyCY", "test"]

Is my approach wrong here? Should the escript just be sending messages to a handler process that’s already running inside the application?

Marked As Solved

NobbZ

NobbZ

You can not use NIFs in an escript.


edit

Strictly speaking, you can’t use priv_dir in an escript, which most NIFs rely on to ship the shared object which contains the actual NIF.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

We're in Beta

About us Mission Statement