Hacker News new | past | comments | ask | show | jobs | submit login
Single File Elixir Scripts (fly.io)
185 points by clessg on March 9, 2023 | hide | past | favorite | 34 comments



The introduction of Mix.install in Elixir 1.12 was huge. Prior to that single file Elixir scripts could only use the standard library, which is decently sized, counting Erlang but notably misses a good HTTP client and JSON encoding/decoding. Despite using Elixir at my day-job and loving the language in a production environment, I had written off Elixir for scripting and throwaway work until then.

But now, I love it! Mix.install([:req]) at the top and you've got an easy to use, great basis for scripts.

Toss in Livebook and you're well on your way to a wonderful exploratory environment. I've moved off having an open iex window to having a scratch Livebook open, and it's great for little explorations. I've been using it whenever I need to work with my company's API, and over time the little helper modules and functions have been evolving. I almost feel like pretty soon it might as well package it up and release them as a full-fledged Elixir library for use with the API.


Using iex: Erlang/OTP 24 [erts-12.2.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]

Interactive Elixir (1.12.2) - press Ctrl+C to exit (type h() ENTER for help)

Mix.install([:req]) gives could not compile dependency :mint, "mix compile" failed.


That's odd. I downgraded with asdf to 1.12.2 tried the same thing and it worked on my M1. You could try upgrading either Erlang/Elixir. Failing that, there's a lot of helpful people on elixirforum.com (which now features live chat), so it might be best to ask there.


I had the same issue on Ubuntu (had my "random project" server up to date already, but no Erlang or Elixir). Installing Erlang in addition to Elixir resolved the problem. I don't know exactly what interactions resolved it though.


The image classification app[0] written by Chris (creator of Phoenix) is IMO nicer example of single file Elixir/Phoenix app. I had some inspirational moments. Yes, I know it was probably linked on HN before.

[0] https://github.com/chrismccord/single_file_phx_bumblebee_ml


I've only just dipped my toes into Elixir and Phoenix but so far really loving it. I feel like I've noticed it popping up on here way more often lately or is that just the ol' frequency bias kicking in?


Perhaps a bit of both? There's been a bump in Elixir posts in the last year or two, which is awesome given that the hype cycle has died down now that the language has been stable for a few years.


There's been a lot of development in the Elixir ecosystem and so a lot to share.

Embedded with Nerves, the various AI/ML libraries and tooling, LiveBook getting better every day, LiveView innovating and improving DevEx, etc..

Then there's fly.io pushing out content given their target market is developers and they have a big focus on deploying Elixir applications.


> frequency bias

aka Baader-Meinhof phenomenon

(since I learned about it, it keeps popping up everywhere)


I see what you are doing there. I'm actually seeing it more and more lately


If you are interested in the same approach in other languages, including Clojure, Ruby, and Crystal:

Single-file scripts that download their dependencies - https://news.ycombinator.com/item?id=34388826

Disclosure: the story is a link to my site.


A nice collection of Elixir scripts in the repository linked at the end of the article: https://github.com/wojtekmach/mix_install_examples


None of them have tests :(

I have plenty of single-file Elixir examples, many with tests included, in this elixir-snippets repo:

https://github.com/pmarreck/elixir-snippets/blob/master/prin...


Wojtek would probably accept PR ;)


that's great, I wish I had time to write some :)


I just tried to learn some Elixir, in the latest ubuntu version. and

Mix.install([ {:req, "~> 0.3"} ])

could not compile dependency :mint, "mix compile" failed. Also mix deps.get say that all dependencies are to date but then mix run gives again that could not compile dependency :mint, "mix compile" failed.

So no good way to learn elixir.


You need to install elixir and erlang with asdf. ubuntu packages outdated / incomplete versions.


I'd be more interested if I can access at least sqlite using single page scripts (better if using ORM)


You could adapt this script and replace Postgrex with the ecto_sqlite3 adapter:

https://github.com/wojtekmach/mix_install_examples/blob/main...

https://github.com/elixir-sqlite/ecto_sqlite3


That is cool. Didn't know I could use ecto in single file. I'll check it out, thanks.


love elixir. hate Mix.

it's like using Maven or npm all over again. but even worse because it generates a lot of code.

it creates disposable projects by design. No way to keep track of what to change when mix create outputs something different next year.

and now things like https://github.com/wojtekmach/mix_install_examples/blob/main... where is ecto even installed? download from where? so annoying this trend.


> where is ecto even installed?

For Mix.install you can optionally specify the cache directory. For regular projects they are in the deps folder.

> The location of the cache directory can be controlled using the MIX_INSTALL_DIR environment variable.

https://hexdocs.pm/mix/1.14/Mix.html#install/2

> download from where?

https://hex.pm/packages/ecto

Or you can specify a git commit, or a path if you want to alter it locally.

> No way to keep track of what to change when mix create outputs something different next year.

`mix.lock` keeps track of your resolved dependencies and should be checked in along with your code. Mix/Hex can also be provided the exact `==` versions rather than the `~>` or other Version specifiers

https://hexdocs.pm/elixir/1.14/Version.html


"project folder" which is oh so in line of single file utility scripts which you will run from all over the place.

will you have one dep dir on each place you run the script at?


You are complaining because it downloads the dependencies of the libraries you declare?

That's how any modern package manager works (I'm thinking cargo as well). I'm not sure why not doing that would be preferable.


yeah. exactly because it download depencies. I want my distro owner to decide that.

most languages dont havea built in package manager.


What did you feel you needed to change on your project because the default template had changed? If the code works, then it keeps working.

I've run into this with the Phoenix generators a bit because you need to make changes to your project if you want to update to newer versions of the Phoenix libraries, but they produce documentation about what you need to change.


do you read every release note to see what security improvements were added to phoenix template? yeah, nobody does.


See, I feel it's a way to have some 'frameworky' elements in my projects that are wonderfully standardized. the changes that do occur usually are worth it.

If anything I sometimes find myself annoyed that Ecto is not there by default, because it's part of almost all my projects, and often even a one-file script might benefit from bits of it.


almost as if you wanted ecto packaged system wide, like, say a distro maintained package? ;)


What's up with the black bars blocking texts? Is it a bug? I'm on mobile.


It's okay here, Brave on Android.


Dark reader?


Not who you are responding to but yes, I see the same on Dark Reader. Opening the page in incognito mode fixed the issue (since I don't allow Dark Reader to run in that mode).


Thanks, that was it!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: