1{
2 config,
3 lib,
4 stdenv,
5 fetchFromGitHub,
6 erlang,
7 makeWrapper,
8 nix-update-script,
9 coreutils,
10 curl,
11 bash,
12 debugInfo ? false,
13}@inputs:
14
15{
16 baseName ? "elixir",
17 version,
18 erlang ? inputs.erlang,
19 minimumOTPVersion,
20 maximumOTPVersion ? null,
21 sha256 ? null,
22 rev ? "v${version}",
23 src ? fetchFromGitHub {
24 inherit rev sha256;
25 owner = "elixir-lang";
26 repo = "elixir";
27 },
28 escriptPath ? "lib/elixir/generate_app.escript",
29}@args:
30
31let
32 inherit (lib)
33 assertMsg
34 concatStringsSep
35 getVersion
36 optionals
37 optionalString
38 toInt
39 versions
40 versionAtLeast
41 versionOlder
42 ;
43
44 compatibilityMsg = ''
45 Unsupported elixir and erlang OTP combination.
46
47 elixir ${version}
48 erlang OTP ${getVersion erlang} is not >= ${minimumOTPVersion} ${
49 optionalString (maximumOTPVersion != null) "and <= ${maximumOTPVersion}"
50 }
51
52 See https://hexdocs.pm/elixir/${version}/compatibility-and-deprecations.html
53 '';
54
55 maxShiftMajor = builtins.toString ((toInt (versions.major maximumOTPVersion)) + 1);
56 maxAssert =
57 if (maximumOTPVersion == null) then
58 true
59 else
60 versionOlder (versions.major (getVersion erlang)) maxShiftMajor;
61 minAssert = versionAtLeast (getVersion erlang) minimumOTPVersion;
62 bothAssert = minAssert && maxAssert;
63
64 elixirShebang =
65 if stdenv.hostPlatform.isDarwin then
66 # Darwin disallows shebang scripts from using other scripts as their
67 # command. Use env as an intermediary instead of calling elixir directly
68 # (another shebang script).
69 # See https://github.com/NixOS/nixpkgs/pull/9671
70 "${coreutils}/bin/env $out/bin/elixir"
71 else
72 "$out/bin/elixir";
73
74 erlc_opts = [ "deterministic" ] ++ optionals debugInfo [ "debug_info" ];
75in
76if !config.allowAliases && !bothAssert then
77 # Don't throw without aliases to not break CI.
78 null
79else
80 assert assertMsg bothAssert compatibilityMsg;
81 stdenv.mkDerivation {
82 pname = "${baseName}";
83
84 inherit src version debugInfo;
85
86 nativeBuildInputs = [ makeWrapper ];
87 buildInputs = [ erlang ];
88
89 env = {
90 LANG = "C.UTF-8";
91 LC_TYPE = "C.UTF-8";
92 DESTDIR = placeholder "out";
93 PREFIX = "/";
94 ERL_COMPILER_OPTIONS = "[${concatStringsSep "," erlc_opts}]";
95 };
96
97 preBuild = ''
98 patchShebangs ${escriptPath} || true
99 '';
100
101 # copy stdlib source files for LSP access
102 postInstall = ''
103 for d in lib/*; do
104 cp -R "$d/lib" "$out/lib/elixir/$d"
105 done
106 '';
107
108 postFixup = ''
109 # Elixir binaries are shell scripts which run erl. Add some stuff
110 # to PATH so the scripts can run without problems.
111
112 for f in $out/bin/*; do
113 b=$(basename $f)
114 if [ "$b" = mix ]; then continue; fi
115 wrapProgram $f \
116 --prefix PATH ":" "${
117 lib.makeBinPath [
118 erlang
119 coreutils
120 curl
121 bash
122 ]
123 }"
124 done
125
126 substituteInPlace $out/bin/mix \
127 --replace "/usr/bin/env elixir" "${elixirShebang}"
128 '';
129
130 passthru.updateScript = nix-update-script {
131 extraArgs = [
132 "--version-regex"
133 "v(${lib.versions.major version}\\.${lib.versions.minor version}\\.[0-9\\-rc.]+)"
134 "--override-filename"
135 "pkgs/development/interpreters/elixir/${lib.versions.major version}.${lib.versions.minor version}.nix"
136 ];
137 };
138
139 pos = builtins.unsafeGetAttrPos "sha256" args;
140 meta = with lib; {
141 homepage = "https://elixir-lang.org/";
142 description = "Functional, meta-programming aware language built on top of the Erlang VM";
143
144 longDescription = ''
145 Elixir is a functional, meta-programming aware language built on
146 top of the Erlang VM. It is a dynamic language with flexible
147 syntax and macro support that leverages Erlang's abilities to
148 build concurrent, distributed and fault-tolerant applications
149 with hot code upgrades.
150 '';
151
152 license = licenses.asl20;
153 platforms = platforms.unix;
154 teams = [ teams.beam ];
155 };
156 }