1 # https://nim-lang.github.io/Nim/packaging.html
2 # https://nim-lang.org/docs/nimc.html
4 { lib, callPackage, buildPackages, stdenv, fetchurl, fetchgit, fetchFromGitHub
5 , makeWrapper, openssl, pcre, readline, boehmgc, sqlite, Security
6 , nim-unwrapped-2, nim-unwrapped-1, nim }:
11 # Derive a Nim CPU identifier
14 else if isAarch64 then
20 else if isMips && is32bit then
22 else if isMips && is64bit then
26 else if isPower && is32bit then
28 else if isPower && is64bit then
30 else if isRiscV && is64bit then
39 abort "no Nim CPU support known for ${config}";
43 # Derive a Nim OS identifier
48 else if isFreeBSD then
58 else if isOpenBSD then
60 else if isWindows then
65 abort "no Nim OS support known for ${config}";
72 nimHost = parsePlatform stdenv.hostPlatform;
73 nimTarget = parsePlatform stdenv.targetPlatform;
77 nim-unwrapped-2 = stdenv.mkDerivation (finalAttrs: {
78 pname = "nim-unwrapped";
83 url = "https://nim-lang.org/download/nim-${finalAttrs.version}.tar.xz";
84 hash = "sha256-cVJr0HQ53I43j6Gm60B+2hKY8fPU30R23KDjyjy+Pwk=";
87 buildInputs = [ boehmgc openssl pcre readline sqlite ]
88 ++ lib.optional stdenv.isDarwin Security;
91 ./NIM_CONFIG_DIR.patch
92 # Override compiler configuration via an environmental variable
95 # Load libraries at runtime by absolute path
97 ./extra-mangling.patch
98 # Mangle store paths of modules to prevent runtime dependence.
101 # dlopen is widely used by Python, Ruby, Perl, ... what you're really telling me here is that your OS is fundamentally broken. That might be news for you, but it isn't for me.
105 bootstrapCompiler = stdenv.mkDerivation {
106 pname = "nim-bootstrap";
107 inherit (finalAttrs) version src preBuild;
108 enableParallelBuilding = true;
111 install -Dt $out/bin bin/nim
117 cp ${bootstrapCompiler}/bin/nim bin/
118 echo 'define:nixbuild' >> config/nim.cfg
119 runHook postConfigure
123 "--cpu:${nimHost.cpu}"
127 ] ++ lib.optional (stdenv.isDarwin || stdenv.isLinux) "-d:nativeStacktrace";
129 preBuild = lib.optionalString (stdenv.isDarwin && stdenv.isAarch64) ''
130 substituteInPlace makefile \
131 --replace "aarch64" "arm64"
137 ./bin/nim c --parallelBuild:$NIX_BUILD_CORES koch
138 ./koch boot $kochArgs --parallelBuild:$NIX_BUILD_CORES
139 ./koch toolsNoExternal $kochArgs --parallelBuild:$NIX_BUILD_CORES
140 ./bin/nim js -d:release tools/dochack/dochack.nim
146 install -Dt $out/bin bin/*
147 ln -sf $out/nim/bin/nim $out/bin/nim
148 ln -sf $out/nim/lib $out/lib
150 cp -a tools dist $out/nim/
155 description = "Statically typed, imperative programming language";
156 homepage = "https://nim-lang.org/";
157 license = licenses.mit;
159 maintainers = with maintainers; [ ehmry ];
163 nim-unwrapped-1 = nim-unwrapped-2.overrideAttrs (finalAttrs: prevAttrs: {
166 url = "https://nim-lang.org/download/nim-${finalAttrs.version}.tar.xz";
167 hash = "sha256-/+0EdQTR/K9hDw3Xzz4Ce+kaKSsMnFEWFQTC87mE/7k=";
171 ./NIM_CONFIG_DIR.patch
172 # Override compiler configuration via an environmental variable
175 # Load libraries at runtime by absolute path
177 ./extra-mangling.patch
178 # Mangle store paths of modules to prevent runtime dependence.
179 ] ++ lib.optional (!stdenv.hostPlatform.isWindows) ./toLocation.patch;
183 wrapNim = { nim', patches }:
184 let targetPlatformConfig = stdenv.targetPlatform.config;
185 in stdenv.mkDerivation (finalAttrs: {
186 name = "${targetPlatformConfig}-nim-wrapper-${nim'.version}";
187 inherit (nim') version;
188 preferLocalBuild = true;
191 nativeBuildInputs = [ makeWrapper ];
193 # Needed for any nim package that uses the standard library's
194 # 'std/sysrand' module.
195 depsTargetTargetPropagated = lib.optional stdenv.isDarwin Security;
201 tar xf ${nim'.src} nim-$version/config
206 dontConfigure = true;
209 # Configure the Nim compiler to use $CC and $CXX as backends
210 # The compiler is configured by two configuration files, each with
211 # a different DSL. The order of evaluation matters and that order
212 # is not documented, so duplicate the configuration across both files.
215 cat >> config/config.nims << WTF
217 switch("os", "${nimTarget.os}")
218 switch("cpu", "${nimTarget.cpu}")
219 switch("define", "nixbuild")
221 # Configure the compiler using the $CC set by Nix at build time
224 if cc.contains("gcc"):
226 elif cc.contains("clang"):
227 switch("cc", "clang")
230 mv config/nim.cfg config/nim.cfg.old
231 cat > config/nim.cfg << WTF
232 os = "${nimTarget.os}"
233 cpu = "${nimTarget.cpu}"
237 cat >> config/nim.cfg < config/nim.cfg.old
238 rm config/nim.cfg.old
240 cat >> config/nim.cfg << WTF
242 clang.cpp.exe %= "\$CXX"
243 clang.cpp.linkerexe %= "\$CXX"
245 clang.linkerexe %= "\$CC"
246 gcc.cpp.exe %= "\$CXX"
247 gcc.cpp.linkerexe %= "\$CXX"
249 gcc.linkerexe %= "\$CC"
255 wrapperArgs = lib.optionals (!(stdenv.isDarwin && stdenv.isAarch64)) [
256 "--prefix PATH : ${lib.makeBinPath [ buildPackages.gdb ]}:${
261 "--prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath [ openssl pcre ]}"
262 # These libraries may be referred to by the standard library.
263 # This is broken for cross-compilation because the package
264 # set will be shifted back by nativeBuildInputs.
266 "--set NIM_CONFIG_PATH ${placeholder "out"}/etc/nim"
267 # Use the custom configuration
273 mkdir -p $out/bin $out/etc
275 cp -r config $out/etc/nim
277 for binpath in ${nim'}/bin/nim?*; do
278 local binname=`basename $binpath`
280 $binpath $out/bin/${targetPlatformConfig}-$binname \
282 ln -s $out/bin/${targetPlatformConfig}-$binname $out/bin/$binname
286 ${nim'}/nim/bin/nim $out/bin/${targetPlatformConfig}-nim \
287 --set-default CC $(command -v $CC) \
288 --set-default CXX $(command -v $CXX) \
290 ln -s $out/bin/${targetPlatformConfig}-nim $out/bin/nim
293 ${nim'}/bin/testament $out/bin/${targetPlatformConfig}-testament \
295 ln -s $out/bin/${targetPlatformConfig}-testament $out/bin/testament
301 passthru = { nim = nim'; };
303 meta = nim'.meta // {
304 description = nim'.meta.description
305 + " (${targetPlatformConfig} wrapper)";
306 platforms = with lib.platforms; unix ++ genode;
312 nim' = buildPackages.nim-unwrapped-2;
313 patches = [ ./nim2.cfg.patch ];
317 nim' = buildPackages.nim-unwrapped-1;
318 patches = [ ./nim.cfg.patch ];