···68686969`mixRelease` is used to make a release in the mix sense. Dependencies will need to be fetched with `fetchMixDeps` and passed to it.
70707171-#### mixRelease - Elixir Phoenix example {#mixrelease---elixir-phoenix-example}
7171+#### mixRelease - Elixir Phoenix example {#mix-release-elixir-phoenix-example}
7272+7373+there are 3 steps, frontend dependencies (javascript), backend dependencies (elixir) and the final derivation that puts both of those together
7474+7575+##### mixRelease - Frontend dependencies (javascript) {#mix-release-javascript-deps}
7676+7777+for phoenix projects, inside of nixpkgs you can either use yarn2nix (mkYarnModule) or node2nix. An example with yarn2nix can be found [here](https://github.com/NixOS/nixpkgs/blob/master/pkgs/servers/web-apps/plausible/default.nix#L39). An example with node2nix will follow. To package something outside of nixpkgs, you have alternatives like [npmlock2nix](https://github.com/nix-community/npmlock2nix) or [nix-npm-buildpackage](https://github.com/serokell/nix-npm-buildpackage)
7878+7979+##### mixRelease - backend dependencies (mix) {#mix-release-mix-deps}
8080+8181+There are 2 ways to package backend dependencies. With mix2nix and with a fixed-output-derivation (FOD).
8282+8383+###### mix2nix {#mix2nix}
8484+8585+mix2nix is a cli tool available in nixpkgs. it will generate a nix expression from a mix.lock file. It is quite standard in the 2nix tool series.
8686+8787+Note that currently mix2nix can't handle git dependencies inside the mix.lock file. If you have git dependencies, you can either add them manually (see [example](https://github.com/NixOS/nixpkgs/blob/master/pkgs/servers/pleroma/default.nix#L20)) or use the FOD method.
8888+8989+The advantage of using mix2nix is that nix will know your whole dependency graph. On a dependency update, this won't trigger a full rebuild and download of all the dependencies, where FOD will do so.
9090+9191+practical steps:
9292+9393+- run `mix2nix > mix_deps.nix` in the upstream repo.
9494+- pass `mixNixDeps = with pkgs; import ./mix_deps.nix { inherit lib beamPackages; };` as an argument to mixRelease.
9595+9696+If there are git depencencies.
9797+9898+- You'll need to fix the version artificially in mix.exs and regenerate the mix.lock with fixed version (on upstream). This will enable you to run `mix2nix > mix_deps.nix`.
9999+- From the mix_deps.nix file, remove the dependencies that had git versions and pass them as an override to the import function.
100100+101101+```nix
102102+ mixNixDeps = import ./mix.nix {
103103+ inherit beamPackages lib;
104104+ overrides = (final: prev: {
105105+ # mix2nix does not support git dependencies yet,
106106+ # so we need to add them manually
107107+ prometheus_ex = beamPackages.buildMix rec {
108108+ name = "prometheus_ex";
109109+ version = "3.0.5";
110110+111111+ # Change the argument src with the git src that you actually need
112112+ src = fetchFromGitLab {
113113+ domain = "git.pleroma.social";
114114+ group = "pleroma";
115115+ owner = "elixir-libraries";
116116+ repo = "prometheus.ex";
117117+ rev = "a4e9beb3c1c479d14b352fd9d6dd7b1f6d7deee5";
118118+ sha256 = "1v0q4bi7sb253i8q016l7gwlv5562wk5zy3l2sa446csvsacnpjk";
119119+ };
120120+ # you can re-use the same beamDeps argument as generated
121121+ beamDeps = with final; [ prometheus ];
122122+ };
123123+ });
124124+};
125125+```
126126+127127+You will need to run the build process once to fix the sha256 to correspond to your new git src.
128128+129129+###### FOD {#fixed-output-derivation}
130130+131131+A fixed output derivation will download mix dependencies from the internet. To ensure reproducibility, a hash will be supplied. Note that mix is relatively reproducible. An FOD generating a different hash on each run hasn't been observed (as opposed to npm where the chances are relatively high). See [elixir_ls](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/beam-modules/elixir_ls.nix) for a usage example of FOD.
132132+133133+Practical steps
134134+135135+- start with the following argument to mixRelease
136136+137137+```nix
138138+ mixFodDeps = fetchMixDeps {
139139+ pname = "mix-deps-${pname}";
140140+ inherit src version;
141141+ sha256 = lib.fakeSha256;
142142+ };
143143+```
144144+145145+The first build will complain about the sha256 value, you can replace with the suggested value after that.
146146+147147+Note that if after you've replaced the value, nix suggests another sha256, then mix is not fetching the dependencies reproducibly. An FOD will not work in that case and you will have to use mix2nix.
721487373-Here is how your `default.nix` file would look.
149149+##### mixRelease - example {#mix-release-example}
150150+151151+Here is how your `default.nix` file would look for a phoenix project.
7415275153```nix
76154with import <nixpkgs> { };
7715578156let
157157+ # beam.interpreters.erlangR23 is available if you need a particular version
79158 packages = beam.packagesWith beam.interpreters.erlang;
159159+160160+ pname = "your_project";
161161+ version = "0.0.1";
162162+80163 src = builtins.fetchgit {
81164 url = "ssh://git@github.com/your_id/your_repo";
82165 rev = "replace_with_your_commit";
83166 };
841678585- pname = "your_project";
8686- version = "0.0.1";
8787- mixEnv = "prod";
8888-168168+ # if using mix2nix you can use the mixNixDeps attribute
89169 mixFodDeps = packages.fetchMixDeps {
90170 pname = "mix-deps-${pname}";
9191- inherit src mixEnv version;
171171+ inherit src version;
92172 # nix will complain and tell you the right value to replace this with
93173 sha256 = lib.fakeSha256;
94174 # if you have build time environment variables add them here
···9717798178 nodeDependencies = (pkgs.callPackage ./assets/default.nix { }).shell.nodeDependencies;
99179100100- frontEndFiles = stdenvNoCC.mkDerivation {
101101- pname = "frontend-${pname}";
102102-103103- nativeBuildInputs = [ nodejs ];
104104-105105- inherit version src;
106106-107107- buildPhase = ''
108108- cp -r ./assets $TEMPDIR
109109-110110- mkdir -p $TEMPDIR/assets/node_modules/.cache
111111- cp -r ${nodeDependencies}/lib/node_modules $TEMPDIR/assets
112112- export PATH="${nodeDependencies}/bin:$PATH"
113113-114114- cd $TEMPDIR/assets
115115- webpack --config ./webpack.config.js
116116- cd ..
117117- '';
118118-119119- installPhase = ''
120120- cp -r ./priv/static $out/
121121- '';
122122-123123- outputHashAlgo = "sha256";
124124- outputHashMode = "recursive";
125125- # nix will complain and tell you the right value to replace this with
126126- outputHash = lib.fakeSha256;
127127-128128- impureEnvVars = lib.fetchers.proxyImpureEnvVars;
129129- };
130130-131131-132180in packages.mixRelease {
133133- inherit src pname version mixEnv mixFodDeps;
181181+ inherit src pname version mixFodDeps;
134182 # if you have build time environment variables add them here
135183 MY_ENV_VAR="my_value";
136136- preInstall = ''
137137- mkdir -p ./priv/static
138138- cp -r ${frontEndFiles} ./priv/static
184184+185185+ postBuild = ''
186186+ ln -sf ${nodeDependencies}/lib/node_modules assets/node_modules
187187+ npm run deploy --prefix ./assets
188188+189189+ # for external task you need a workaround for the no deps check flag
190190+ # https://github.com/phoenixframework/phoenix/issues/2690
191191+ mix do deps.loadpaths --no-deps-check, phx.digest
192192+ mix phx.digest --no-deps-check
139193 '';
140194}
141195```
···165219 systemd.services.${release_name} = {
166220 wantedBy = [ "multi-user.target" ];
167221 after = [ "network.target" "postgresql.service" ];
222222+ # note that if you are connecting to a postgres instance on a different host
223223+ # postgresql.service should not be included in the requires.
168224 requires = [ "network-online.target" "postgresql.service" ];
169225 description = "my app";
170226 environment = {
···201257 path = [ pkgs.bash ];
202258 };
203259260260+ # in case you have migration scripts or you want to use a remote shell
204261 environment.systemPackages = [ release ];
205262}
206263```
···215272{ pkgs ? import <nixpkgs> {} }:
216273217274with pkgs;
218218-219275let
220220-221221- elixir = beam.packages.erlangR22.elixir_1_9;
222222-276276+ elixir = beam.packages.erlangR24.elixir_1_12;
223277in
224278mkShell {
225279 buildInputs = [ elixir ];
226226-227227- ERL_INCLUDE_PATH="${erlang}/lib/erlang/usr/include";
228280}
229281```
230282···264316 # TODO: not sure how to make hex available without installing it afterwards.
265317 mix local.hex --if-missing
266318 export LANG=en_US.UTF-8
319319+ # keep your shell history in iex
267320 export ERL_AFLAGS="-kernel shell_history enabled"
268321269322 # postges related