@ -1,22 +1,10 @@
# https://nim-lang.github.io/Nim/packaging.html
# https://nim-lang.org/docs/nimc.html
{ stdenv , lib , fetchurl , fetchgit , fetchFromGitHub , makeWrapper , gdb , openssl
, pcre , readline , boehmgc , sqlite , nim-unwrapped , nimble -unwrapped } :
{ lib , buildPackages , stdenv , fetchurl , fetchgit , fetchFromGitHub , makeWrapper
, openssl , pcre , readline , boehmgc , sqlite , nim-unwrapped } :
let
version = " 1 . 4 . 4 " ;
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 " ;
sha256 = " 0 3 k 6 4 2 n n j c a 0 s 6 j l b n 1 v 4 j l d 5 1 m b k i x 9 7 j l i 4 k y 7 4 g q l x y f p 4 w v d " ;
} ;
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 ;
maintainers = with maintainers ; [ ehmry ] ;
} ;
parseCpu = platform :
with platform ;
# Derive a Nim CPU identifier
@ -83,197 +71,251 @@ let
nimHost = parsePlatform stdenv . hostPlatform ;
nimTarget = parsePlatform stdenv . targetPlatform ;
wrapperInputs = rec {
bootstrapCompiler = stdenv . mkDerivation rec {
pname = " n i m - b o o t s t r a p " ;
version = " 0 . 2 0 . 0 " ;
bootstrap = stdenv . mkDerivation rec {
pname = " n i m - b o o t s t r a p " ;
version = " 0 . 2 0 . 0 " ;
src = fetchgit {
# A Git checkout is much smaller than a GitHub tarball.
url = " h t t p s : / / g i t h u b . c o m / n i m - l a n g / c s o u r c e s . g i t " ;
rev = " v ${ version } " ;
sha256 = " 0 i 6 v s f y 1 s g a p x 4 3 n 2 2 6 q 8 m 0 p v n 1 5 9 s w 2 m h p 5 0 z m 3 h h b 9 z f i j a n i s " ;
} ;
src = fetchgit {
# A Git checkout is much smaller than a GitHub tarball.
url = " h t t p s : / / g i t h u b . c o m / n i m - l a n g / c s o u r c e s . g i t " ;
rev = " v " + version ;
sha256 = " 0 i 6 v s f y 1 s g a p x 4 3 n 2 2 6 q 8 m 0 p v n 1 5 9 s w 2 m h p 5 0 z m 3 h h b 9 z f i j a n i s " ;
} ;
enableParallelBuilding = true ;
enableParallelBuilding = true ;
installPhase = ''
runHook preInstall
install - Dt $ out/bin bin/nim
runHook postInstall
'' ;
} ;
installPhase = ''
runHook preInstall
install - Dt $ out/bin bin/nim
runHook postInstall
'' ;
} ;
in {
unwrapped = stdenv . mkDerivation {
pname = " n i m - u n w r a p p e d " ;
inherit version src ;
nim-unwrapped = stdenv . mkDerivation rec {
pname = " n i m - u n w r a p p e d " ;
version = " 1 . 4 . 4 " ;
strictDeps = true ;
buildInputs = [ boehmgc openssl pcre readline sqlite ] ;
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 " ;
sha256 = " 0 3 k 6 4 2 n n j c a 0 s 6 j l b n 1 v 4 j l d 5 1 m b k i x 9 7 j l i 4 k y 7 4 g q l x y f p 4 w v d " ;
} ;
patches = [
./NIM_CONFIG_DIR.patch
# Override compiler configuration via an environmental variable
buildInputs = [ boehmgc openssl pcre readline sqlite ] ;
./nixbuild.patch
# Load libraries at runtime by absolute pat h
] ;
patches = [
./NIM_CONFIG_DIR.patc h
# Override compiler configuration via an environmental variable
configurePhase = ''
runHook preConfigure
cp $ { bootstrap } /bin/nim bin /
echo ' define:nixbuild' > > config/nim.cfg
runHook postConfigure
'' ;
./nixbuild.patch
# Load libraries at runtime by absolute path
] ;
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 " ;
configurePhase = ''
runHook preConfigure
cp $ { bootstrapCompiler } /bin/nim bin /
echo ' define:nixbuild' > > config/nim.cfg
runHook postConfigure
'' ;
buildPhase = ''
runHook preBuild
local HOME = $ TMPDIR
./bin/nim c koch
./koch boot $ kochArgs - - parallelBuild:$NIX_BUILD_CORES
./koch toolsNoExternal $ kochArgs - - parallelBuild:$NIX_BUILD_CORES
runHook postBuild
'' ;
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 " ;
buildPhase = ''
runHook preBuild
local HOME = $ TMPDIR
./bin/nim c koch
./koch boot $ kochArgs - - parallelBuild:$NIX_BUILD_CORES
./koch toolsNoExternal $ kochArgs - - parallelBuild:$NIX_BUILD_CORES
runHook postBuild
'' ;
installPhase = ''
runHook preInstall
install - Dt $ out/bin bin /*
ln - sf $ out/nim/bin/nim $ out/bin/nim
./install.sh $ out
runHook postInstall
'' ;
installPhase = ''
runHook preInstall
install - Dt $ out/bin bin /*
ln - sf $ out/nim/bin/nim $ out/bin/nim
./install.sh $ out
runHook postInstall
'' ;
inherit meta ;
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 ;
maintainers = with maintainers ; [ ehmry ] ;
} ;
} ;
nimble-unwrapped = stdenv . mkDerivation rec {
pname = " n i m b l e - u n w r a p p e d " ;
version = " 0 . 1 3 . 1 " ;
src = fetchFromGitHub {
owner = " n i m - l a n g " ;
repo = " n i m b l e " ;
rev = " v " + version ;
sha256 = " 1 i d b 4 r 0 k j b q v 1 6 r 6 b g m x l r 1 3 w 2 v g q 5 3 3 2 h m n c 8 p j b x i y f w m 0 7 5 x 8 " ;
} ;
nimble-unwrapped = stdenv . mkDerivation rec {
pname = " n i m b l e - u n w r a p p e d " ;
version = " 0 . 1 3 . 1 " ;
strictDeps = true ;
nativeBuildInputs = [ nim-unwrapped ] ;
buildInputs = [ openssl ] ;
src = fetchFromGitHub {
owner = " n i m - l a n g " ;
repo = " n i m b l e " ;
rev = " v ${ version } " ;
sha256 = " 1 i d b 4 r 0 k j b q v 1 6 r 6 b g m x l r 1 3 w 2 v g q 5 3 3 2 h m n c 8 p j b x i y f w m 0 7 5 x 8 " ;
} ;
nimFlags = [ " - - c p u : ${ nimHost . cpu } " " - - o s : ${ nimHost . os } " " - d : r e l e a s e " ] ;
depsBuildBuild = [ nim-unwrapped ] ;
buildInputs = [ openssl ] ;
buildPhase = ''
runHook preBuild
HOME = $ NIX_BUILD_TOP nim c $ nimFlags src/nimble
runHook postBuild
'' ;
nimFlags = [ " - - c p u : ${ nimHost . cpu } " " - - o s : ${ nimHost . os } " " - d : r e l e a s e " ] ;
installPhase = ''
runHook preBuild
install - Dt $ out/bin src/nimble
runHook postBuild
'' ;
} ;
buildPhase = ''
runHook preBuild
HOME = $ NIX_BUILD_TOP nim c $ nimFlags src/nimble
runHook postBuild
'' ;
installPhase = ''
runHook preBuild
install - Dt $ out/bin src/nimble
runHook postBuild
'' ;
} ;
wrapped = let
nim' = nim-unwrapped ;
nimble' = nimble-unwrapped ;
nim = let
nim' = buildPackages . nim-unwrapped ;
nimble' = buildPackages . nimble-unwrapped ;
inherit ( stdenv ) targetPlatform ;
in stdenv . mkDerivation {
name = " ${ targetPlatform . config } - n i m - w r a p p e r - ${ nim' . version } " ;
inherit ( nim' ) version ;
preferLocalBuild = true ;
strictDeps = true ;
nativeBuildInputs = [ makeWrapper ] ;
patches = [
./nim.cfg.patch
# Remove configurations that clash with ours
] ;
unpackPhase = ''
runHook preUnpack
tar xf $ { nim' . src } nim- $ version/config/nim.cfg
tar xf $ { nim' . src } nim- $ version/config
cd nim- $ version
runHook postUnpack
'' ;
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 = [
" - - p r e f i x P A T H : ${ lib . makeBinPath [ stdenv . cc gdb ] } : ${
" - - p r e f i x P A T H : ${ lib . makeBinPath [ buildPackages . gdb ] } : ${
placeholder " o u t "
} /bin "
" - - p r e f i x L D _ L I B R A R Y _ P A T H : ${
lib . makeLibraryPath [ stdenv . cc . libc openssl ]
} "
# 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
'' - - s e t N I X _ H A R D E N I N G _ E N A B L E " ''$ { N I X _ H A R D E N I N G _ E N A B L E / f o r t i f y } " ''
# Fortify hardening appends -O2 to gcc flags which is unwanted for unoptimized nim builds.
] ;
buildPhase = with stdenv ;
let
ccType = if cc . isGNU then
" g c c "
else if cc . isClang then
" c l a n g "
else
abort " n o N i m c o n f i g u r a t i o n a v a i l a b l e f o r ${ cc . name } " ;
in ''
runHook preBuild
cat > > config/nim.cfg < < EOF
define:nixbuild
os = $ { nimTarget . os }
cpu = $ { nimTarget . cpu }
cc = $ { ccType }
EOF
mkdir - p $ out/bin $ out/etc/nim
export cc = $ CC
export cxx = $ CXX
substituteAll config/nim.cfg $ out/etc/nim/nim.cfg \
- - replace " c c = g c c " " "
for binpath in $ { nim' } /bin/nim ? * ; do
local binname = ` basename $ binpath `
makeWrapper \
$ binpath $ out/bin / $ { targetPlatform . config } - $ binname \
$ wrapperArgs
ln - s $ out/bin / $ { targetPlatform . config } - $ binname $ out/bin / $ binname
done
installPhase = ''
runHook preInstall
makeWrapper \
$ { nim' } /nim/bin/nim $ out/bin / $ { targetPlatform . config } - nim \
$ wrapperArgs
ln - s $ out/bin / $ { targetPlatform . config } - nim $ out/bin/nim
mkdir - p $ out/bin $ out/etc
cp - r config $ out/etc/nim
for binpath in $ { nim' } /bin/nim ? * ; do
local binname = ` basename $ binpath `
makeWrapper \
$ { nim' } /bin/testament $ out/bin / $ { targetPlatform . config } - testament \
$ binpath $ out/bin / $ { targetPlatform . config } - $ binname \
$ wrapperArgs
ln - s $ out/bin / $ { targetPlatform . config } - testament $ out/bin/testament
ln - s $ out/bin / $ { targetPlatform . config } - $ binname $ out/bin / $ binname
done
makeWrapper \
$ { nimble ' } /bin/nimble $ out/bin / $ { targetPlatform . config } - nimble \
- - suffix PATH : $ out/bin
ln - s $ out/bin / $ { targetPlatform . config } - nimble $ out/bin/nimble
makeWrapper \
$ { nim' } /nim/bin/nim $ out/bin / $ { targetPlatform . config } - nim \
$ wrapperArgs
ln - s $ out/bin / $ { targetPlatform . config } - nim $ out/bin/nim
runHook postBuild
'' ;
makeWrapper \
$ { nim' } /bin/testament $ out/bin / $ { targetPlatform . config } - testament \
$ wrapperArgs
ln - s $ out/bin / $ { targetPlatform . config } - testament $ out/bin/testament
makeWrapper \
$ { nimble' } /bin/nimble $ out/bin / $ { targetPlatform . config } - nimble \
- - suffix PATH : $ out/bin
ln - s $ out/bin / $ { targetPlatform . config } - nimble $ out/bin/nimble
dontInstall = true ;
runHook postInstall
'' ;
passthru = {
nim = nim' ;
nimble = nimble' ;
} ;
meta = meta // {
meta = nim' . meta // {
description = nim' . meta . description
+ " ( ${ targetPlatform . config } w r a p p e r ) " ;
platforms = lib . platforms . unix ;
platforms = with lib . platforms ; unix ++ genode ;
} ;
} ;
in wrapped // wrapperInputs
}