···
`mixRelease` is used to make a release in the mix sense. Dependencies will need to be fetched with `fetchMixDeps` and passed to it.
71
-
#### mixRelease - Elixir Phoenix example {#mixrelease---elixir-phoenix-example}
71
+
#### mixRelease - Elixir Phoenix example {#mix-release-elixir-phoenix-example}
73
+
there are 3 steps, frontend dependencies (javascript), backend dependencies (elixir) and the final derivation that puts both of those together
75
+
##### mixRelease - Frontend dependencies (javascript) {#mix-release-javascript-deps}
77
+
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)
79
+
##### mixRelease - backend dependencies (mix) {#mix-release-mix-deps}
81
+
There are 2 ways to package backend dependencies. With mix2nix and with a fixed-output-derivation (FOD).
83
+
###### mix2nix {#mix2nix}
85
+
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.
87
+
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.
89
+
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.
93
+
- run `mix2nix > mix_deps.nix` in the upstream repo.
94
+
- pass `mixNixDeps = with pkgs; import ./mix_deps.nix { inherit lib beamPackages; };` as an argument to mixRelease.
96
+
If there are git depencencies.
98
+
- 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`.
99
+
- From the mix_deps.nix file, remove the dependencies that had git versions and pass them as an override to the import function.
102
+
mixNixDeps = import ./mix.nix {
103
+
inherit beamPackages lib;
104
+
overrides = (final: prev: {
105
+
# mix2nix does not support git dependencies yet,
106
+
# so we need to add them manually
107
+
prometheus_ex = beamPackages.buildMix rec {
108
+
name = "prometheus_ex";
111
+
# Change the argument src with the git src that you actually need
112
+
src = fetchFromGitLab {
113
+
domain = "git.pleroma.social";
115
+
owner = "elixir-libraries";
116
+
repo = "prometheus.ex";
117
+
rev = "a4e9beb3c1c479d14b352fd9d6dd7b1f6d7deee5";
118
+
sha256 = "1v0q4bi7sb253i8q016l7gwlv5562wk5zy3l2sa446csvsacnpjk";
120
+
# you can re-use the same beamDeps argument as generated
121
+
beamDeps = with final; [ prometheus ];
127
+
You will need to run the build process once to fix the sha256 to correspond to your new git src.
129
+
###### FOD {#fixed-output-derivation}
131
+
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.
135
+
- start with the following argument to mixRelease
138
+
mixFodDeps = fetchMixDeps {
139
+
pname = "mix-deps-${pname}";
140
+
inherit src version;
141
+
sha256 = lib.fakeSha256;
145
+
The first build will complain about the sha256 value, you can replace with the suggested value after that.
147
+
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.
73
-
Here is how your `default.nix` file would look.
149
+
##### mixRelease - example {#mix-release-example}
151
+
Here is how your `default.nix` file would look for a phoenix project.
with import <nixpkgs> { };
157
+
# beam.interpreters.erlangR23 is available if you need a particular version
packages = beam.packagesWith beam.interpreters.erlang;
160
+
pname = "your_project";
src = builtins.fetchgit {
url = "ssh://git@github.com/your_id/your_repo";
rev = "replace_with_your_commit";
85
-
pname = "your_project";
168
+
# if using mix2nix you can use the mixNixDeps attribute
mixFodDeps = packages.fetchMixDeps {
pname = "mix-deps-${pname}";
91
-
inherit src mixEnv version;
171
+
inherit src version;
# nix will complain and tell you the right value to replace this with
# if you have build time environment variables add them here
···
nodeDependencies = (pkgs.callPackage ./assets/default.nix { }).shell.nodeDependencies;
100
-
frontEndFiles = stdenvNoCC.mkDerivation {
101
-
pname = "frontend-${pname}";
103
-
nativeBuildInputs = [ nodejs ];
105
-
inherit version src;
108
-
cp -r ./assets $TEMPDIR
110
-
mkdir -p $TEMPDIR/assets/node_modules/.cache
111
-
cp -r ${nodeDependencies}/lib/node_modules $TEMPDIR/assets
112
-
export PATH="${nodeDependencies}/bin:$PATH"
115
-
webpack --config ./webpack.config.js
120
-
cp -r ./priv/static $out/
123
-
outputHashAlgo = "sha256";
124
-
outputHashMode = "recursive";
125
-
# nix will complain and tell you the right value to replace this with
126
-
outputHash = lib.fakeSha256;
128
-
impureEnvVars = lib.fetchers.proxyImpureEnvVars;
133
-
inherit src pname version mixEnv mixFodDeps;
181
+
inherit src pname version mixFodDeps;
# if you have build time environment variables add them here
137
-
mkdir -p ./priv/static
138
-
cp -r ${frontEndFiles} ./priv/static
186
+
ln -sf ${nodeDependencies}/lib/node_modules assets/node_modules
187
+
npm run deploy --prefix ./assets
189
+
# for external task you need a workaround for the no deps check flag
190
+
# https://github.com/phoenixframework/phoenix/issues/2690
191
+
mix do deps.loadpaths --no-deps-check, phx.digest
192
+
mix phx.digest --no-deps-check
···
systemd.services.${release_name} = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "postgresql.service" ];
222
+
# note that if you are connecting to a postgres instance on a different host
223
+
# postgresql.service should not be included in the requires.
requires = [ "network-online.target" "postgresql.service" ];
···
260
+
# in case you have migration scripts or you want to use a remote shell
environment.systemPackages = [ release ];
···
{ pkgs ? import <nixpkgs> {} }:
221
-
elixir = beam.packages.erlangR22.elixir_1_9;
276
+
elixir = beam.packages.erlangR24.elixir_1_12;
buildInputs = [ elixir ];
227
-
ERL_INCLUDE_PATH="${erlang}/lib/erlang/usr/include";
···
# TODO: not sure how to make hex available without installing it afterwards.
mix local.hex --if-missing
319
+
# keep your shell history in iex
export ERL_AFLAGS="-kernel shell_history enabled"