Readit News logoReadit News
losvedir · 2 years ago
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.

elixxir123 · 2 years ago
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.

h0l0cube · 2 years ago
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.
melx · 2 years ago
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

omniscient_oce · 2 years ago
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?
fredrikholm · 2 years ago
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.
afhammad · 2 years ago
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.

ithkuil · 2 years ago
> frequency bias

aka Baader-Meinhof phenomenon

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

stareatgoats · 2 years ago
I see what you are doing there. I'm actually seeing it more and more lately
networked · 2 years ago
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.

plainOldText · 2 years ago
A nice collection of Elixir scripts in the repository linked at the end of the article: https://github.com/wojtekmach/mix_install_examples
pmarreck · 2 years ago
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...

di4na · 2 years ago
Wojtek would probably accept PR ;)
elixxir123 · 2 years ago
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.

thefreeman · 2 years ago
You need to install elixir and erlang with asdf. ubuntu packages outdated / incomplete versions.
kulkarniniraj · 2 years ago
I'd be more interested if I can access at least sqlite using single page scripts (better if using ORM)
h0l0cube · 2 years ago
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

kulkarniniraj · 2 years ago
That is cool. Didn't know I could use ecto in single file. I'll check it out, thanks.
rftyuikdjhgvcf · 2 years ago
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.

h0l0cube · 2 years ago
> 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

rftyuikdjhgvcf · 2 years ago
"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?

sph · 2 years ago
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.

rftyuikdjhgvcf · 2 years ago
yeah. exactly because it download depencies. I want my distro owner to decide that.

most languages dont havea built in package manager.

rkangel · 2 years ago
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.

rftyuikdjhgvcf · 2 years ago
do you read every release note to see what security improvements were added to phoenix template? yeah, nobody does.
mercer · 2 years ago
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.

rftyuikdjhgvcf · 2 years ago
almost as if you wanted ecto packaged system wide, like, say a distro maintained package? ;)