Now Reading
Single File Elixir Scripts · Fly

Single File Elixir Scripts · Fly

2023-03-08 18:39:08

Man fitting an entire script into his head as he runs.
Picture by Annie Ruygt

This text’s about operating single file Elixir scripts. We even present a Phoenix LiveView Instance! is a good place to run your Phoenix purposes. Try find out how to get started!

Elixir has highly effective in-built scripting performance, permitting us to jot down Elixir to a file—say my_script.exs— and execute it immediately by elixir my_script.exs.

The overwhelming majority of manufacturing Elixir tasks can be immediately compiled by way of combine with all accessible optimizations and efficiency enhancements enabled. However let’s discover what we will accomplish after we go on script and throw out compilation!


The first command to know is Mix.install/2. If you are familiar with Livebook this can be a evaluate, however this command allows set up of any hex package deal. Let’s leap in:

Combine.set up([ 
  {:jason, "~> 1.0"} 

|> dbg()

Right here we set up the newest model of the fantastic req HTTP consumer and model 1 for the peerlessly named JSON library jason. As soon as put in, you possibly can instantly use them. Technically we did not want to put in jason as a result of req included it, however I did for instance.


The second function we will need is Application.put_env/4. This operate permits us to place values into the worldwide Software config at runtime. Right here is the bottom setting configuration we want if we need to configure a Phoenix Endpoint:

Software.put_env(:pattern, SamplePhoenix.Endpoint,
    http: [ip: {127, 0, 0, 1}, port: 5001],
    server: true,
    live_view: [signing_salt: "aaaaaaaa"],
    secret_key_base: String.duplicate("a", 64)

This is not the solely strategy to configure one thing. We might have included an choice to Combine.set up like so:

Combine.set up([ 
      {:jason, "~> 1.0"} 
    config: [
        sample: [
            SamplePhoenix.Endpoint: [
                http: [ip: {127, 0, 0, 1}, port: 5001],
                server: true,
                live_view: [signing_salt: "aaaaaaaa"],
                secret_key_base: String.duplicate("a", 64)

Now What?

With those two functions we have the basic foundation to do anything Elixir can do but in a single, portable file!

We can do…

System Administration

retirement = Path.join([System.user_home!(), "retirement"])

# Get rid of those old .ex files who needs em!
|> Enum.filter(fn f -> 
      {{year, _, _,}, _} = File.stat!(f).mtime 
      year < 2023
|> Enum.each(fn compiled_file ->!(compiled_file, retirement) 
    # we only need .exs files now

Data Processing

# Req will parse CSVs for us!
|> Enum.reduce(0, fn row, count -> 
    death_increase = String.to_integer(, 19))
    count + death_increase
|> IO.puts()

Report Phoenix LiveView Bugs

Let’s say you’ve discovered a bug in LiveView and want to report it. You can increase the odds of it getting fixed quickly by providing a bare-bones example. You could mix a project and push it up to GitHub, or you could make a single file example and put it in a gist! In fact, Phoenix core contributor Gary Rennie does this so usually that I affectionately name these recordsdata Garyfiles.

Software.put_env(:pattern, SamplePhoenix.Endpoint,
  http: [ip: {127, 0, 0, 1}, port: 5001],
  server: true,
  live_view: [signing_salt: "aaaaaaaa"],
  secret_key_base: String.duplicate("a", 64)

Combine.set up([
  {:plug_cowboy, "~> 2.5"},
  {:jason, "~> 1.0"},
  {:phoenix, "~> 1.7.0-rc.2", override: true},
  {:phoenix_live_view, "~> 0.18.2"}

defmodule SamplePhoenix.ErrorView do
  def render(template, _), do: Phoenix.Controller.status_message_from_template(template)

defmodule SamplePhoenix.SampleLive do
  use Phoenix.LiveView, structure: {__MODULE__, :dwell}

  def mount(_params, _session, socket) do
    {:oops, assign(socket, :depend, 0)}

  def render("dwell.html", assigns) do
    <script src=""></script>
    <script src=""></script>
      let liveSocket = new window.LiveView.LiveSocket("", window.Phoenix.Socket)
      * { font-size: 1.1em; }


  def render(assigns) do

    <button phx-click="">+</button>
    <button phx-click="">-</button>

  def handle_event("inc", _params, socket) do
    {:noreply, assign(socket, :depend, socket.assigns.depend + 1)}

  def handle_event("dec", _params, socket) do
    {:noreply, assign(socket, :depend, socket.assigns.depend - 1)}

defmodule Router do
  use Phoenix.Router
  import Phoenix.LiveView.Router

  pipeline :browser do
    plug(:accepts, ["html"])

  scope "", SamplePhoenix do

    dwell("", SampleLive, :index)

defmodule SamplePhoenix.Endpoint do
  use Phoenix.Endpoint, otp_app: :pattern
  socket("/dwell", Phoenix.LiveView.Socket)

{:okay, _} = Supervisor.start_link([SamplePhoenix.Endpoint], technique: :one_for_one)
Course of.sleep(:infinity)

Seems the bug wasn’t in Phoenix in any respect and was an oopsie on my half. Can you notice it?

This one is barely extra concerned and relies on the wojtekmach/mix_install_examples venture. With this file you’ve got a completely practical Phoenix LiveView utility in a single file operating on port 5001!

And you’ll see all the stuff you want to make Phoenix Work, and albeit it isn’t that a lot. When folks say we want a “light-weight net framework” ask them what’s pointless on this file!

Report Issues

Here at we try to be super responsive on the questions on our community forum. For instance we’ve a problem with utilizing mnesia and fly volumes, like some customers recently posted. If we needed to publish an remoted bug report, we might arrange a minimal venture to assist actually get the eye of the help group.

First, we might need a Dockerfile that may run Elixir scripts

# syntax = docker/dockerfile:1
FROM "hexpm/elixir:1.14.2-erlang-25.2-debian-bullseye-20221004-slim"

# set up dependencies
RUN apt-get replace -y && apt-get set up -y build-essential git libstdc++6 openssl libncurses5 locales 
    && apt-get clear && rm -f /var/lib/apt/lists/*_*

# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /and so on/locale.gen && locale-gen

# Env variables we would need
ENV ERL_AFLAGS "-proto_dist inet6_tcp"

WORKDIR "/app"

# Copy our recordsdata over
COPY bug.exs /app

# set up hex + rebar when you plan on utilizing Combine.set up
RUN combine native.hex --force && 
    combine native.rebar --force

CMD elixir /app/bug.exs

Lastly add our bug.exs

vol_dir = System.get_env("VOL_DIR" || "/knowledge"

# Setup mnesiua
Software.put_env(:mnesia, :dir, to_charlist(vol_dir))
:okay = Software.begin(:mnesia)

# Examine that mnesia is working
dbg(:mnesia.change_table_copy_type(:schema, node(), :disc_copies))

# Perhaps attempt writing a file to see whatsup
path = part of([vol_dir, "hello.txt"])
File.write!(path, "Hi there from elixir!"

Course of.sleep(:infinity) # Hold it operating so fly is aware of its okay

And our fly.toml

See Also

app = "APP NAME"

supply = "knowledge"
vacation spot = "/knowledge"

Now we will fly create APP_NAME, fly volumes create knowledge, fly deploy after which verify the logs fly logs to see what failed.

On this case, I could not reproduce the error they had been seeing. However it’s useful to have some code that is remoted to solely the issue you’re having. We might additionally see beginning up a Phoenix server this fashion and deploying a weekend tiny app. I would not suggest it, however you possibly can!

In Conclusion

If you take nothing else away from this post, I hope you click around Wojtek Mach‘s FANTASTIC mix_install_examples repository for Elixir script inspiration. You are able to do absolutely anything from Machine Studying to low stage Methods Programming, all from a single file and the Elixir runtime.

And at last, please do not be afraid to make use of them as a growth instruments. For those who encounter a nasty bug in a library or your code, it will probably actually assist to isolate it to JUST the failing code and construct out a easy repeatable take a look at case like this.

Or possibly as a substitute of asking ChatGPT to jot down you a shell script, write it in Elixir, so a human can learn it. is a good way to run your Phoenix LiveView app near your customers. It is very easy to get began. You may be operating in minutes.

Deploy a Phoenix app today!  

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top