2020-09-25 04:45:31 +00:00
# https://nim-lang.github.io/Nim/packaging.html
2021-03-20 04:20:00 +00:00
# https://nim-lang.org/docs/nimc.html
2020-04-24 23:36:52 +00:00
2021-09-23 15:35:13 +00:00
{ lib , callPackage , buildPackages , stdenv , fetchurl , fetchgit , fetchFromGitHub
2023-08-22 20:05:09 +00:00
, makeWrapper , openssl , pcre , readline , boehmgc , sqlite , Security , nim-unwrapped
2023-10-09 19:29:22 +00:00
, nim-unwrapped-2 , nim } :
2020-04-24 23:36:52 +00:00
2020-09-25 04:45:31 +00:00
let
parseCpu = platform :
with platform ;
# Derive a Nim CPU identifier
if isAarch32 then
" a r m "
else if isAarch64 then
" a r m 6 4 "
else if isAlpha then
" a l p h a "
else if isAvr then
" a v r "
else if isMips && is32bit then
" m i p s "
else if isMips && is64bit then
" m i p s 6 4 "
else if isMsp430 then
" m s p 4 3 0 "
2021-09-22 15:38:15 +00:00
else if isPower && is32bit then
2020-09-25 04:45:31 +00:00
" p o w e r p c "
2021-09-22 15:38:15 +00:00
else if isPower && is64bit then
2020-09-25 04:45:31 +00:00
" p o w e r p c 6 4 "
else if isRiscV && is64bit then
" r i s c v 6 4 "
else if isSparc then
" s p a r c "
else if isx86_32 then
" i 3 8 6 "
else if isx86_64 then
" a m d 6 4 "
else
abort " n o N i m C P U s u p p o r t k n o w n f o r ${ config } " ;
parseOs = platform :
with platform ;
# Derive a Nim OS identifier
if isAndroid then
" A n d r o i d "
else if isDarwin then
" M a c O S X "
else if isFreeBSD then
" F r e e B S D "
else if isGenode then
" G e n o d e "
else if isLinux then
" L i n u x "
else if isNetBSD then
" N e t B S D "
else if isNone then
" S t a n d a l o n e "
else if isOpenBSD then
" O p e n B S D "
else if isWindows then
" W i n d o w s "
else if isiOS then
" i O S "
else
abort " n o N i m O S s u p p o r t k n o w n f o r ${ config } " ;
parsePlatform = p : {
cpu = parseCpu p ;
os = parseOs p ;
} ;
nimHost = parsePlatform stdenv . hostPlatform ;
nimTarget = parsePlatform stdenv . targetPlatform ;
2022-12-02 08:20:57 +00:00
2021-03-20 04:20:00 +00:00
in {
2020-09-25 04:45:31 +00:00
2023-08-22 20:05:09 +00:00
nim-unwrapped = stdenv . mkDerivation ( finalAttrs : {
2021-03-20 04:20:00 +00:00
pname = " n i m - u n w r a p p e d " ;
2023-07-15 17:15:38 +00:00
version = " 1 . 6 . 1 4 " ;
2021-03-20 04:20:00 +00:00
strictDeps = true ;
2020-09-25 04:45:31 +00:00
2021-03-20 04:20:00 +00:00
src = fetchurl {
2023-08-22 20:05:09 +00:00
url = " h t t p s : / / n i m - l a n g . o r g / d o w n l o a d / n i m - ${ finalAttrs . version } . t a r . x z " ;
2023-07-15 17:15:38 +00:00
hash = " s h a 2 5 6 - 0 H D S 8 o r i Q A 3 3 / k p J 7 O u f R c 1 T m Q a x B 0 g Y V q C v e o + o L c k = " ;
2021-03-20 04:20:00 +00:00
} ;
2020-09-25 04:45:31 +00:00
2023-03-30 22:05:00 +00:00
buildInputs = [ boehmgc openssl pcre readline sqlite ]
++ lib . optional stdenv . isDarwin Security ;
2020-09-25 04:45:31 +00:00
2021-03-20 04:20:00 +00:00
patches = [
./NIM_CONFIG_DIR.patch
# Override compiler configuration via an environmental variable
2020-09-25 04:45:31 +00:00
2021-03-20 04:20:00 +00:00
./nixbuild.patch
# Load libraries at runtime by absolute path
2023-07-15 17:15:38 +00:00
./extra-mangling.patch
# Mangle store paths of modules to prevent runtime dependence.
2021-10-17 09:34:42 +00:00
] ++ lib . optional ( ! stdenv . hostPlatform . isWindows ) ./toLocation.patch ;
2020-09-25 04:45:31 +00:00
2023-08-22 20:05:09 +00:00
configurePhase = let
bootstrapCompiler = stdenv . mkDerivation {
pname = " n i m - b o o t s t r a p " ;
inherit ( finalAttrs ) version src preBuild ;
enableParallelBuilding = true ;
installPhase = ''
runHook preInstall
install - Dt $ out/bin bin/nim
runHook postInstall
'' ;
} ;
in ''
2021-03-20 04:20:00 +00:00
runHook preConfigure
2022-12-02 08:20:57 +00:00
cp $ { bootstrapCompiler } /bin/nim bin /
2021-03-20 04:20:00 +00:00
echo ' define:nixbuild' > > config/nim.cfg
runHook postConfigure
'' ;
2020-09-25 04:45:31 +00:00
2021-03-20 04:20:00 +00:00
kochArgs = [
" - - c p u : ${ nimHost . cpu } "
" - - o s : ${ nimHost . os } "
" - d : r e l e a s e "
" - d : u s e G n u R e a d l i n e "
] ++ lib . optional ( stdenv . isDarwin || stdenv . isLinux ) " - d : n a t i v e S t a c k t r a c e " ;
2022-12-17 10:02:37 +00:00
preBuild = lib . optionalString ( stdenv . isDarwin && stdenv . isAarch64 ) ''
substituteInPlace makefile \
- - replace " a a r c h 6 4 " " a r m 6 4 "
'' ;
2021-03-20 04:20:00 +00:00
buildPhase = ''
runHook preBuild
local HOME = $ TMPDIR
2022-10-21 18:38:19 +00:00
./bin/nim c - - parallelBuild:$NIX_BUILD_CORES koch
2021-03-20 04:20:00 +00:00
./koch boot $ kochArgs - - parallelBuild:$NIX_BUILD_CORES
./koch toolsNoExternal $ kochArgs - - parallelBuild:$NIX_BUILD_CORES
2022-10-21 18:38:19 +00:00
./bin/nim js - d:release tools/dochack/dochack.nim
2021-03-20 04:20:00 +00:00
runHook postBuild
'' ;
2020-09-25 04:45:31 +00:00
2021-03-20 04:20:00 +00:00
installPhase = ''
runHook preInstall
install - Dt $ out/bin bin /*
ln - sf $ out/nim/bin/nim $ out/bin/nim
2022-09-09 14:08:57 +00:00
ln - sf $ out/nim/lib $ out/lib
2021-03-20 04:20:00 +00:00
./install.sh $ out
2022-10-21 18:38:19 +00:00
cp - a tools $ out/nim /
2021-03-20 04:20:00 +00:00
runHook postInstall
'' ;
2020-09-25 04:45:31 +00:00
2021-03-20 04:20:00 +00:00
meta = with lib ; {
description = " S t a t i c a l l y t y p e d , i m p e r a t i v e p r o g r a m m i n g l a n g u a g e " ;
homepage = " h t t p s : / / n i m - l a n g . o r g / " ;
license = licenses . mit ;
2023-05-24 13:37:59 +00:00
mainProgram = " n i m " ;
2021-03-20 04:20:00 +00:00
maintainers = with maintainers ; [ ehmry ] ;
2020-09-25 04:45:31 +00:00
} ;
2023-08-22 20:05:09 +00:00
} ) ;
nim-unwrapped-2 = nim-unwrapped . overrideAttrs ( finalAttrs : rec {
version = " 2 . 0 . 0 " ;
src = fetchurl {
url = " h t t p s : / / n i m - l a n g . o r g / d o w n l o a d / n i m - ${ version } . t a r . x z " ;
hash = " s h a 2 5 6 - v W E B 2 E A D b 7 e O k 6 a d 9 s 8 / n 9 D C H N d U t p X / h K O 0 r d j t C v c = " ;
} ;
2023-10-09 19:29:22 +00:00
2023-08-22 20:05:09 +00:00
patches = [
./NIM_CONFIG_DIR.patch
# Override compiler configuration via an environmental variable
./nixbuild.patch
# Load libraries at runtime by absolute path
./extra-mangling.patch
# Mangle store paths of modules to prevent runtime dependence.
2023-10-09 19:29:22 +00:00
./openssl.patch
# 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.
2023-08-22 20:05:09 +00:00
] ;
} ) ;
2020-11-30 08:33:03 +00:00
2023-08-22 20:05:09 +00:00
} // ( let
2023-10-09 19:29:22 +00:00
wrapNim = { nim' , patches }:
2023-08-22 20:05:09 +00:00
let
targetPlatformConfig = stdenv . targetPlatform . config ;
self = stdenv . mkDerivation ( finalAttrs : {
name = " ${ targetPlatformConfig } - n i m - w r a p p e r - ${ nim' . version } " ;
inherit ( nim' ) version ;
preferLocalBuild = true ;
strictDeps = true ;
nativeBuildInputs = [ makeWrapper ] ;
# Needed for any nim package that uses the standard library's
# 'std/sysrand' module.
depsTargetTargetPropagated = lib . optional stdenv . isDarwin Security ;
inherit patches ;
unpackPhase = ''
runHook preUnpack
tar xf $ { nim' . src } nim- $ version/config
cd nim- $ version
runHook postUnpack
2021-09-23 15:35:13 +00:00
'' ;
2023-08-22 20:05:09 +00:00
dontConfigure = true ;
buildPhase =
# Configure the Nim compiler to use $CC and $CXX as backends
# The compiler is configured by two configuration files, each with
# a different DSL. The order of evaluation matters and that order
# is not documented, so duplicate the configuration across both files.
''
runHook preBuild
cat > > config/config.nims < < WTF
switch ( " o s " , " ${ nimTarget . os } " )
switch ( " c p u " , " ${ nimTarget . cpu } " )
switch ( " d e f i n e " , " n i x b u i l d " )
# Configure the compiler using the $CC set by Nix at build time
import strutils
let cc = getEnv " C C "
if cc . contains ( " g c c " ) :
switch ( " c c " , " g c c " )
elif cc . contains ( " c l a n g " ) :
switch ( " c c " , " c l a n g " )
WTF
mv config/nim.cfg config/nim.cfg.old
cat > config/nim.cfg < < WTF
os = " ${ nimTarget . os } "
cpu = " ${ nimTarget . cpu } "
define : " n i x b u i l d "
WTF
cat > > config/nim.cfg < config/nim.cfg.old
rm config/nim.cfg.old
cat > > config/nim.cfg < < WTF
clang . cpp . exe % = " \$ C X X "
clang . cpp . linkerexe % = " \$ C X X "
clang . exe % = " \$ C C "
clang . linkerexe % = " \$ C C "
gcc . cpp . exe % = " \$ C X X "
gcc . cpp . linkerexe % = " \$ C X X "
gcc . exe % = " \$ C C "
gcc . linkerexe % = " \$ C C "
WTF
runHook postBuild
'' ;
wrapperArgs = lib . optionals ( ! ( stdenv . isDarwin && stdenv . isAarch64 ) ) [
" - - p r e f i x P A T H : ${ lib . makeBinPath [ buildPackages . gdb ] } : ${
placeholder " o u t "
} /bin "
# Used by nim-gdb
" - - p r e f i x L D _ L I B R A R Y _ P A T H : ${ lib . makeLibraryPath [ openssl pcre ] } "
# These libraries may be referred to by the standard library.
# This is broken for cross-compilation because the package
# set will be shifted back by nativeBuildInputs.
" - - s e t N I M _ C O N F I G _ P A T H ${ placeholder " o u t " } / e t c / n i m "
# Use the custom configuration
] ;
installPhase = ''
runHook preInstall
mkdir - p $ out/bin $ out/etc
cp - r config $ out/etc/nim
for binpath in $ { nim' } /bin/nim ? * ; do
local binname = ` basename $ binpath `
makeWrapper \
$ binpath $ out/bin / $ { targetPlatformConfig } - $ binname \
$ wrapperArgs
ln - s $ out/bin / $ { targetPlatformConfig } - $ binname $ out/bin / $ binname
done
2021-09-23 15:35:13 +00:00
2023-08-22 20:05:09 +00:00
makeWrapper \
$ { nim' } /nim/bin/nim $ out/bin / $ { targetPlatformConfig } - nim \
- - set-default CC $ ( command - v $ CC ) \
- - set-default CXX $ ( command - v $ CXX ) \
$ wrapperArgs
ln - s $ out/bin / $ { targetPlatformConfig } - nim $ out/bin/nim
2021-09-23 15:35:13 +00:00
makeWrapper \
2023-08-22 20:05:09 +00:00
$ { nim' } /bin/testament $ out/bin / $ { targetPlatformConfig } - testament \
2021-09-23 15:35:13 +00:00
$ wrapperArgs
2023-08-22 20:05:09 +00:00
ln - s $ out/bin / $ { targetPlatformConfig } - testament $ out/bin/testament
2020-09-25 04:45:31 +00:00
2023-08-22 20:05:09 +00:00
'' + ''
runHook postInstall
'' ;
2023-10-09 19:29:22 +00:00
passthru = { nim = nim' ; } ;
2023-08-22 20:05:09 +00:00
meta = nim' . meta // {
description = nim' . meta . description
+ " ( ${ targetPlatformConfig } w r a p p e r ) " ;
platforms = with lib . platforms ; unix ++ genode ;
} ;
} ) ;
in self // {
pkgs = callPackage ../../../top-level/nim-packages.nix { nim = self ; } ;
2020-09-25 04:45:31 +00:00
} ;
2023-08-22 20:05:09 +00:00
in {
nim = wrapNim {
nim' = buildPackages . nim-unwrapped ;
patches = [ ./nim.cfg.patch ] ;
2020-09-25 04:45:31 +00:00
} ;
2023-08-22 20:05:09 +00:00
nim2 = wrapNim {
nim' = buildPackages . nim-unwrapped-2 ;
patches = [ ./nim2.cfg.patch ] ;
} ;
2023-10-09 19:29:22 +00:00
2023-08-22 20:05:09 +00:00
} )