jononomo

jononomo

Where / How does the Mix environment variable get set?

I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?

Thanks.

Most Liked

BrightEyesDavid

BrightEyesDavid

And for anyone who isn’t already aware (like me until relatively recently), this means you can set the environment on each mix command.

Unix-like:

MIX_ENV=prod mix {task}

Windows:

set "MIX_ENV=prod" && mix {task}
10
Post #3
hubertlepicki

hubertlepicki

https://github.com/elixir-lang/elixir/blob/50293b46f13a86328f0ffabdcbb8592e29ac24c6/lib/mix/lib/mix/state.ex#L11

it gets set here. It’s a compile-time variable, gets fetched fron shell environment, and default value is “dev”.

nathanl

nathanl

A related concept: you may have noticed that when you run mix test, it assumes MIX_ENV=test, but most tasks assume MIX_ENV=dev unless otherwise specified.

To set the default environment for a Miix task (eg, a task to run JS tests) or task alias, you can use :preferred_cli_env. For example:

defmodule MyProject.Mixfile do
  use Mix.Project

  def project do
    [
      # ...
      aliases: aliases(),
      preferred_cli_env: [
        # note that all task names are atoms
        test: :test,
        jstest: :test,
        "test.all": :test
      ]
    ]
  end

  # ...

  defp aliases do
    [
      "test.all": ["test", "jstest"]
    ]
  end
end
NobbZ

NobbZ

It is not 100% equivalent and one needs to know the differences when using the windows version:

$ echo %MIX_ENV%
%MIX_ENV%

$ set MIX_ENV=test && mix deps.get
Running dependency resolution...
All dependencies up to date

$ echo %MIX_ENV%
test

Linux:

$ echo $MIX_ENV

$ MIX_ENV=test mix deps.get
Running dependency resolution...
All dependencies up to date

$ echo $MIX_ENV

As you can see, in linux the value of MIX_ENV is only overwritten for that single command and recovered afterwards, while the windows version changes the value permanently.

OvermindDL1

OvermindDL1

I just use a bash/cygwin shell on Windows, much more uniform. ^.^

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

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 record...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement