{"id":11033,"date":"2025-01-26T18:56:47","date_gmt":"2025-01-26T18:56:47","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11033"},"modified":"2025-01-26T18:56:47","modified_gmt":"2025-01-26T18:56:47","slug":"mix-and-project-dependencies-in-elixir","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/mix-and-project-dependencies-in-elixir\/","title":{"rendered":"Mix and project dependencies in Elixir"},"content":{"rendered":"<p>Like many other languages, there&#8217;s a lot of third party packages and shared code for the Elixir language.<\/p>\n<p>If we use mix to create a new project you&#8217;ll get a mix.exs script file generated. Let&#8217;s take a look at one created for one of my projects<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndefmodule TestLang.MixProject do\r\n  use Mix.Project\r\n\r\n  def project do\r\n    &#x5B;\r\n      app: :test_lang,\r\n      version: &quot;0.1.0&quot;,\r\n      elixir: &quot;~&gt; 1.17&quot;,\r\n      start_permanent: Mix.env() == :prod,\r\n      deps: deps()\r\n    ]\r\n  end\r\n\r\n  # Run &quot;mix help compile.app&quot; to learn about applications.\r\n  def application do\r\n    &#x5B;\r\n      extra_applications: &#x5B;:logger]\r\n    ]\r\n  end\r\n\r\n  # Run &quot;mix help deps&quot; to learn about dependencies.\r\n  defp deps do\r\n    &#x5B;\r\n      # {:dep_from_hexpm, &quot;~&gt; 0.3.0&quot;},\r\n      # {:dep_from_git, git: &quot;https:\/\/github.com\/elixir-lang\/my_dep.git&quot;, tag: &quot;0.1.0&quot;}\r\n    ]\r\n  end\r\nend\r\n<\/pre>\n<p>The <em>project<\/em> section is where we set the application name, version etc. The <em>deps<\/em> section is where we add dependencies that we want to pull into our project.<\/p>\n<p>We can pull in code from git or from https:\/\/hex.pm\/. <\/p>\n<p>Let&#8217;s start by adding a dependency on a package from hex.pm, I&#8217;m going to add the UUID package https:\/\/hex.pm\/packages\/uuid, so amend the deps section to look like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndefp deps do\r\n  &#x5B;\r\n    {:uuid, &quot;~&gt; 1.1&quot;}\r\n  ]\r\nend\r\n<\/pre>\n<p>The code was copied from the packages hex.pm page where it has code for various configurations. <\/p>\n<p>We can check the state of our dependencies by typing <em>mix deps<\/em>. In my case it tells me the dependency is not available, I need to run <em>mix deps.get<\/em>. <\/p>\n<p>We don&#8217;t need to compiler the dependency as it&#8217;ll automatically compile when we need it, but you can compile it using <em>mix deps.compile<\/em> if you prefer.<\/p>\n<p>When you bring dependencies into your project you&#8217;ll see a deps folder created and similar to node_modules in web\/node development, you&#8217;ll see the dependency code in this folder.<\/p>\n<p>Let&#8217;s try UUID out (as per it&#8217;s documentation https:\/\/hexdocs.pm\/uuid\/readme.html), I created a lib\/try_uuid.ex file and added the following code<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndefmodule TryUuid do\r\n\r\n  def create() do\r\n    UUID.uuid1()\r\n  end\r\nend\r\n<\/pre>\n<p>Run up iex using <em>iex -S mix<\/em> to allow access to the project and it&#8217;s dependencies. Finally run the code <em>TryUuid.create()<\/em> and if all went well you&#8217;ll get a UUI created.<\/p>\n<p>Let&#8217;s add a dependency from GitHub, back to the mix.exs and change the deps section to like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndefp deps do\r\n  &#x5B;\r\n    {:uuid, &quot;~&gt; 1.1&quot;},\r\n    {:better_weighted_random, git: &quot;https:\/\/github.com\/JohnJocoo\/weighted_random.git&quot; }\r\n  ]\r\nend\r\n<\/pre>\n<p>I basically picked the package from GitHub by searching for packages and looking for something fairly simple to try &#8211; this one seemed as good as any. There were no tags so I&#8217;ve not included a version with the dependency.<\/p>\n<p>Now go through the process of <em>mix get.deps<\/em> now add a file, mine&#8217;s try_random.ex with the following code<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndefmodule TryRandom do\r\n  def create() do\r\n    WeightedRandom.take_one(&#x5B;{:&#039;1&#039;, 0.5}, {:&#039;2&#039;, 1.0}, {:&#039;3&#039;, 2.0}])\r\n  end\r\nend\r\n<\/pre>\n<p>Compile using <em>mix compile<\/em> and then run up <em>iex -S mix<\/em> and finally execute the command <em>TryRandom.create()<\/em> and if all went well you&#8217;ll get some randomly selected value from the list give within the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like many other languages, there&#8217;s a lot of third party packages and shared code for the Elixir language. If we use mix to create a new project you&#8217;ll get a mix.exs script file generated. Let&#8217;s take a look at one created for one of my projects defmodule TestLang.MixProject do use Mix.Project def project do &#x5B; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[730,734],"tags":[],"class_list":["post-11033","post","type-post","status-publish","format-standard","hentry","category-elixir","category-hex-pm"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11033"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11033\/revisions"}],"predecessor-version":[{"id":11255,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11033\/revisions\/11255"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}