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
2024-06-05 15:53:02 +00:00
{ lib , callPackage , buildPackages , stdenv , fetchurl , fetchgit
2024-01-02 11:29:13 +00:00
, makeWrapper , openssl , pcre , readline , boehmgc , sqlite , Security
, nim-unwrapped-2 , nim-unwrapped-1 , 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
2024-01-02 11:29:13 +00:00
nim-unwrapped-2 = stdenv . mkDerivation ( finalAttrs : {
2021-03-20 04:20:00 +00:00
pname = " n i m - u n w r a p p e d " ;
2024-10-04 16:56:33 +00:00
version = " 2 . 2 . 0 " ;
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 " ;
2024-10-04 16:56:33 +00:00
hash = " s h a 2 5 6 - z p h C h J y X Y O S H 7 N 0 c 2 t 9 8 D y h E y v r m B U A c f H K u J X Z E i T w = " ;
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 ]
2024-09-26 11:04:55 +00:00
++ lib . optional stdenv . hostPlatform . 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
2024-10-04 16:56:33 +00:00
./extra-mangling-2.patch
2023-07-15 17:15:38 +00:00
# Mangle store paths of modules to prevent runtime dependence.
2024-01-02 11:29:13 +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.
] ;
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 "
2024-09-26 11:04:55 +00:00
] ++ lib . optional ( stdenv . hostPlatform . isDarwin || stdenv . hostPlatform . isLinux ) " - d : n a t i v e S t a c k t r a c e " ;
2021-03-20 04:20:00 +00:00
2024-09-26 11:04:55 +00:00
preBuild = lib . optionalString ( stdenv . hostPlatform . isDarwin && stdenv . hostPlatform . isAarch64 ) ''
2022-12-17 10:02:37 +00:00
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
2024-01-13 08:15:51 +00:00
cp - a tools dist $ out/nim /
2021-03-20 04:20:00 +00:00
runHook postInstall
'' ;
2020-09-25 04:45:31 +00:00
2024-10-04 16:56:33 +00:00
passthru = {
updateScript . command = [ ./update.sh ] ;
} ;
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 " ;
2024-10-04 16:56:33 +00:00
maintainers = with maintainers ; [ ehmry eveeifyeve ] ;
2020-09-25 04:45:31 +00:00
} ;
2023-08-22 20:05:09 +00:00
} ) ;
2024-01-02 11:29:13 +00:00
nim-unwrapped-1 = nim-unwrapped-2 . overrideAttrs ( finalAttrs : prevAttrs : {
2024-05-15 15:35:15 +00:00
version = " 1 . 6 . 2 0 " ;
2023-08-22 20:05:09 +00:00
src = fetchurl {
2024-01-02 11:29:13 +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 " ;
2024-05-15 15:35:15 +00:00
hash = " s h a 2 5 6 - / + 0 E d Q T R / K 9 h D w 3 X z z 4 C e + k a K S s M n F E W F Q T C 8 7 m E / 7 k = " ;
2023-08-22 20:05:09 +00:00
} ;
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.
2024-01-02 11:29:13 +00:00
] ++ lib . optional ( ! stdenv . hostPlatform . isWindows ) ./toLocation.patch ;
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 }:
2024-01-02 11:29:13 +00:00
let targetPlatformConfig = stdenv . targetPlatform . config ;
in stdenv . mkDerivation ( finalAttrs : {
2023-08-22 20:05:09 +00:00
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.
2024-09-26 11:04:55 +00:00
depsTargetTargetPropagated = lib . optional stdenv . hostPlatform . isDarwin Security ;
2023-08-22 20:05:09 +00:00
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
'' ;
2024-09-26 11:04:55 +00:00
wrapperArgs = lib . optionals ( ! ( stdenv . hostPlatform . isDarwin && stdenv . hostPlatform . isAarch64 ) ) [
2023-08-22 20:05:09 +00:00
" - - 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 ) " ;
2024-07-27 06:49:29 +00:00
platforms = with lib . platforms ; unix ++ genode ++ windows ;
2023-08-22 20:05:09 +00:00
} ;
} ) ;
in {
nim2 = wrapNim {
nim' = buildPackages . nim-unwrapped-2 ;
patches = [ ./nim2.cfg.patch ] ;
} ;
2023-10-09 19:29:22 +00:00
2024-01-02 11:29:13 +00:00
nim1 = wrapNim {
nim' = buildPackages . nim-unwrapped-1 ;
patches = [ ./nim.cfg.patch ] ;
} ;
2023-08-22 20:05:09 +00:00
} )