DEV Community

more-urgent-jest
more-urgent-jest

Posted on • Edited on

Deving with elixir on windows 10

elixir on windows 10

I have had a pretty good experience learning elixir on windows 10, until after the holidays, when I tried going back to .Net and found that nuget was trashed. Either that or I was just too impatient to wait for nuget packet manager to load. I don't remember it being that slow.

Anyway, I couldn't work it out and had my PC rebuilt. I'm going to be more careful and use docker, but that's not that straight forward on windows 10. There are some good resources on the web, e.g. running elixir in docker containers, but finding one for windows 10 was proving to be challenging. I have been working through this tutorial, but some of the commands don't work in powershell as they are.

Assuming that you have docker installed and know your way around it a bit you should be able to follow the first part of the poeticoding tutorial to get a development environment going using these instructions to supplement the unix specific commands used in that tutorial. I use powershell to run the commands.

setting up

To develop in elixir via docker you need to be able to edit the code and have it run in the docker container. This can be done using bind mounts.

To run a docker container using a bind mount and create a project in that location run this in the location in your file system where you want the project to be created:

docker container run --rm -v ${pwd}:/data -w /data elixir mix new crypto

where src is the location in your local file system that you want to mount in the container and dst is the location in the file system on the container.

After running this you should have a project folder created in your local file system in the location you specified named using the project name you specified.

The -w flag sets the working directory of the container.

Then add the dependency to the mix.exs file inside the crypto directory, cd into that directory and run this to get mix to install the dependencies:

docker container run --rm -v ${pwd}:/app -w /app -it elixir:1.7.4 mix deps.get

Then create the volume and run:

docker container run --rm -v elixir-mix:/root/.mix -v ${pwd}:/app -w /app -it  elixir:1.7.4 mix deps.get

That should work unless you are behind a proxy.

links

https://rominirani.com/docker-on-windows-mounting-host-directories-d96f3f056a2c
https://docs.docker.com/storage/bind-mounts/
https://docs.docker.com/storage/volumes/

proxy settings

If you're behind a proxy server this won't work without some configuration. If your proxy server MITM the HTTPS connection then you can either configure your proxy not to do so or use the HEX_UNSAFE_HTTPS environment variable. Since I am running this on a corporate LAN I am using this:

docker container run --rm --env HTTP_PROXY=http://proxy.where.ever:8080 --env HTTPS_PROXY=http://proxy.where.ever:8080 --env HEX_UNSAFE_HTTPS=1 -v elixir-mix:/root/.mix -v ${pwd}:/app -w /app -it elixir mix deps.get

That worked for me and the dependency installed.

links

https://github.com/hexpm/hex/issues/283
https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file

how to use the environment

Once you have your project generated and dependencies installed you can run the container interactively:

docker container run --name elixir-crypto --env HTTP_PROXY=http://proxy.where.ever:8080 --env HTTPS_PROXY=http://proxy.where.ever:8080 --env HEX_UNSAFE_HTTPS=1 -v elixir-mix:/root/.mix -v ${pwd}:/app -w /app -it elixir

and use the shell from the container to run commands like:

mix test

No more trashed work dev environment, I love it!

Remember to change the container name to reflect what code it is for, but docker will tell you if you are trying to reuse the same name.

If you have suggestions for improving this setup please mention them in the comments.

Much appreciated.

PS: to naively time the execution of a function from the command line:

:timer.tc(fn -> code_to_measure end, [])

https://groups.google.com/d/msg/elixir-lang-core/lhxQyTLzN0Y/SiE5HsarCCoJ
https://erlang.org/doc/man/timer.html

further reading

https://ilhub.io/blog/2019/05/30/vscode-remote
https://medium.com/@pentacent/getting-started-with-elixir-docker-982e2a16213c
https://pspdfkit.com/blog/2018/how-to-run-your-phoenix-application-with-docker/

Top comments (0)