lol

nim: wrap compiler for cross-compilation support

+259 -51
+2 -1
pkgs/applications/science/biology/mosdepth/default.nix
··· 26 26 sha256 = "01gm9gj2x2zs4yx6wk761fi1papi7qr3gp4ln1kkn8n2f9y9h849"; 27 27 }; 28 28 29 - buildInputs = [ nim htslib pcre ]; 29 + nativeBuildInputs = [ nim ]; 30 + buildInputs = [ htslib pcre ]; 30 31 31 32 buildPhase = '' 32 33 HOME=$TMPDIR
+23
pkgs/development/compilers/nim/NIM_CONFIG_DIR.patch
··· 1 + diff --git a/compiler/nimconf.nim b/compiler/nimconf.nim 2 + index a470179bd..73cfa1a23 100644 3 + --- a/compiler/nimconf.nim 4 + +++ b/compiler/nimconf.nim 5 + @@ -225,10 +225,15 @@ proc getUserConfigPath*(filename: RelativeFile): AbsoluteFile = 6 + proc getSystemConfigPath*(conf: ConfigRef; filename: RelativeFile): AbsoluteFile = 7 + # try standard configuration file (installation did not distribute files 8 + # the UNIX way) 9 + - let p = getPrefixDir(conf) 10 + - result = p / RelativeDir"config" / filename 11 + + let 12 + + prefix = getPrefixDir(conf) 13 + + env = getEnv("NIM_CONFIG_PATH") 14 + + if env != "": 15 + + result = env.toAbsoluteDir / filename 16 + + else: 17 + + result = prefix / RelativeDir"config" / filename 18 + when defined(unix): 19 + - if not fileExists(result): result = p / RelativeDir"etc/nim" / filename 20 + + if not fileExists(result): result = prefix / RelativeDir"etc/nim" / filename 21 + if not fileExists(result): result = AbsoluteDir"/etc/nim" / filename 22 + 23 + proc loadConfigs*(cfg: RelativeFile; cache: IdentCache; conf: ConfigRef) =
+232 -50
pkgs/development/compilers/nim/default.nix
··· 1 - # based on https://github.com/nim-lang/Nim/blob/v0.18.0/.travis.yml 1 + # https://nim-lang.github.io/Nim/packaging.html 2 2 3 - { stdenv, lib, fetchurl, makeWrapper, openssl, pcre, readline, 4 - boehmgc, sfml, sqlite }: 3 + { stdenv, lib, fetchgit, fetchurl, makeWrapper, gdb, openssl, pcre, readline 4 + , boehmgc, sqlite, nim-unwrapped, nim-stdlib, nim }: 5 5 6 - stdenv.mkDerivation rec { 7 - pname = "nim"; 6 + let 8 7 version = "1.2.6"; 9 - 10 8 src = fetchurl { 11 - url = "https://nim-lang.org/download/${pname}-${version}.tar.xz"; 9 + url = "https://nim-lang.org/download/nim-${version}.tar.xz"; 12 10 sha256 = "0zk5qzxayqjw7kq6p92j4008g9bbyilyymhdc5xq9sln5rqym26z"; 13 11 }; 14 12 15 - enableParallelBuilding = true; 13 + meta = with lib; { 14 + description = "Statically typed, imperative programming language"; 15 + homepage = "https://nim-lang.org/"; 16 + license = licenses.mit; 17 + maintainers = with maintainers; [ ehmry ]; 18 + }; 19 + 20 + parseCpu = platform: 21 + with platform; 22 + # Derive a Nim CPU identifier 23 + if isAarch32 then 24 + "arm" 25 + else if isAarch64 then 26 + "arm64" 27 + else if isAlpha then 28 + "alpha" 29 + else if isAvr then 30 + "avr" 31 + else if isMips && is32bit then 32 + "mips" 33 + else if isMips && is64bit then 34 + "mips64" 35 + else if isMsp430 then 36 + "msp430" 37 + else if isPowerPC && is32bit then 38 + "powerpc" 39 + else if isPowerPC && is64bit then 40 + "powerpc64" 41 + else if isRiscV && is64bit then 42 + "riscv64" 43 + else if isSparc then 44 + "sparc" 45 + else if isx86_32 then 46 + "i386" 47 + else if isx86_64 then 48 + "amd64" 49 + else 50 + abort "no Nim CPU support known for ${config}"; 51 + 52 + parseOs = platform: 53 + with platform; 54 + # Derive a Nim OS identifier 55 + if isAndroid then 56 + "Android" 57 + else if isDarwin then 58 + "MacOSX" 59 + else if isFreeBSD then 60 + "FreeBSD" 61 + else if isGenode then 62 + "Genode" 63 + else if isLinux then 64 + "Linux" 65 + else if isNetBSD then 66 + "NetBSD" 67 + else if isNone then 68 + "Standalone" 69 + else if isOpenBSD then 70 + "OpenBSD" 71 + else if isWindows then 72 + "Windows" 73 + else if isiOS then 74 + "iOS" 75 + else 76 + abort "no Nim OS support known for ${config}"; 16 77 17 - NIX_LDFLAGS = "-lcrypto -lpcre -lreadline -lgc -lsqlite3"; 78 + parsePlatform = p: { 79 + cpu = parseCpu p; 80 + os = parseOs p; 81 + }; 18 82 19 - # we could create a separate derivation for the "written in c" version of nim 20 - # used for bootstrapping, but koch insists on moving the nim compiler around 21 - # as part of building it, so it cannot be read-only 83 + nimHost = parsePlatform stdenv.hostPlatform; 84 + nimTarget = parsePlatform stdenv.targetPlatform; 22 85 23 - nativeBuildInputs = [ 24 - makeWrapper 25 - ]; 86 + wrapperInputs = rec { 26 87 27 - buildInputs = [ 28 - openssl pcre readline boehmgc sfml sqlite 29 - ]; 88 + bootstrap = stdenv.mkDerivation rec { 89 + pname = "nim-bootstrap"; 90 + version = "0.20.0"; 30 91 31 - patches = [ ./nixbuild.patch ]; 92 + src = fetchgit { 93 + # A Git checkout is much smaller than a GitHub tarball. 94 + url = "https://github.com/nim-lang/csources.git"; 95 + rev = "v" + version; 96 + sha256 = "0i6vsfy1sgapx43n226q8m0pvn159sw2mhp50zm3hhb9zfijanis"; 97 + }; 32 98 33 - postPatch = "echo define:nixbuild >> config/nim.cfg"; 99 + enableParallelBuilding = true; 34 100 35 - buildPhase = '' 36 - runHook preBuild 101 + installPhase = '' 102 + runHook preInstall 103 + install -Dt $out/bin bin/nim 104 + runHook postInstall 105 + ''; 106 + }; 37 107 38 - # build.sh wants to write to $HOME/.cache 39 - HOME=$TMPDIR 40 - sh build.sh 41 - ./bin/nim c koch 42 - ./koch boot -d:release \ 43 - -d:useGnuReadline \ 44 - ${lib.optionals (stdenv.isDarwin || stdenv.isLinux) "-d:nativeStacktrace"} 45 - ./koch tools -d:release 108 + unwrapped = stdenv.mkDerivation { 109 + # https://nim-lang.github.io/Nim/packaging.html 110 + pname = "nim-unwrapped"; 111 + inherit version src; 46 112 47 - runHook postBuild 48 - ''; 113 + buildInputs = [ boehmgc openssl pcre readline sqlite ]; 49 114 50 - installPhase = '' 51 - runHook preInstall 115 + patches = [ 116 + ./NIM_CONFIG_DIR.patch 117 + # Override compiler configuration via an environmental variable 52 118 53 - install -Dt $out/bin bin/* koch 54 - ./koch install $out 55 - mv $out/nim/bin/* $out/bin/ && rmdir $out/nim/bin 56 - mv $out/nim/* $out/ && rmdir $out/nim 119 + ./nixbuild.patch 120 + # Load libraries at runtime by absolute path 121 + ]; 57 122 123 + configurePhase = '' 124 + runHook preConfigure 125 + cp ${bootstrap}/bin/nim bin/ 126 + echo 'define:nixbuild' >> config/nim.cfg 127 + runHook postConfigure 128 + ''; 129 + 130 + kochArgs = [ 131 + "--cpu:${nimHost.cpu}" 132 + "--os:${nimHost.os}" 133 + "-d:release" 134 + "-d:useGnuReadline" 135 + ] ++ lib.optional (stdenv.isDarwin || stdenv.isLinux) 136 + "-d:nativeStacktrace"; 137 + 138 + buildPhase = '' 139 + runHook preBuild 140 + local HOME=$TMPDIR 141 + ./bin/nim c koch 142 + ./koch boot $kochArgs --parallelBuild:$NIX_BUILD_CORES 143 + ./koch tools $kochArgs --parallelBuild:$NIX_BUILD_CORES 144 + runHook postBuild 145 + ''; 146 + 147 + installPhase = '' 148 + runHook preInstall 149 + install -Dt $out/bin bin/* 150 + runHook postInstall 151 + ''; 152 + 153 + inherit meta; 154 + }; 155 + 156 + stdlib = stdenv.mkDerivation { 157 + pname = "nim-stdlib"; 158 + inherit (nim-unwrapped) version src patches; 159 + 160 + dontConfigure = true; 161 + dontBuild = true; 162 + 163 + installPhase = '' 164 + runHook preInstall 165 + touch bin/nim 166 + ./install.sh $TMPDIR 167 + cp -r $TMPDIR/nim/lib $out 168 + runHook postInstall 169 + ''; 170 + 171 + meta = meta // { 172 + description = meta.description + " (standard library)"; 173 + }; 174 + }; 175 + }; 176 + 177 + wrapped = let 178 + nim = nim-unwrapped; 179 + inherit (stdenv) targetPlatform; 180 + in stdenv.mkDerivation { 181 + name = "${targetPlatform.config}-nim-wrapper-${nim.version}"; 182 + inherit (nim) version; 183 + preferLocalBuild = true; 184 + 185 + nativeBuildInputs = [ makeWrapper ]; 186 + 187 + unpackPhase = '' 188 + runHook preUnpack 189 + tar xf ${nim.src} nim-$version/config/nim.cfg 190 + cd nim-$version 191 + runHook postUnpack 192 + ''; 193 + 194 + dontConfigure = true; 195 + 196 + wrapperArgs = [ 197 + "--prefix PATH : ${lib.makeBinPath [ stdenv.cc gdb ]}:${ 198 + placeholder "out" 199 + }/bin" 200 + "--prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath [ stdenv.cc.libc openssl ]}" 201 + "--set NIM_CONFIG_PATH ${placeholder "out"}/etc/nim" 202 + ]; 203 + 204 + buildPhase = with stdenv; 205 + let 206 + ccType = if cc.isGNU then 207 + "gcc" 208 + else if cc.isClang then 209 + "clang" 210 + else 211 + abort "no Nim configuration available for ${cc.name}"; 212 + in '' 213 + runHook preBuild 214 + cat >> config/nim.cfg << EOF 215 + 216 + define:nixbuild 217 + os = ${nimTarget.os} 218 + cpu = ${nimTarget.cpu} 219 + cc = ${ccType} 220 + EOF 221 + 222 + mkdir -p $out/bin $out/etc/nim 223 + export cc=$CC 224 + export cxx=$CXX 225 + substituteAll config/nim.cfg $out/etc/nim/nim.cfg \ 226 + --replace "cc = gcc" "" 227 + 228 + for binpath in ${nim}/bin/nim?*; do 229 + local binname=`basename $binpath` 230 + makeWrapper $binpath $out/bin/${targetPlatform.config}-$binname \ 231 + $wrapperArgs 232 + ln -s $out/bin/${targetPlatform.config}-$binname $out/bin/$binname 233 + done 234 + 235 + makeWrapper ${nim}/bin/nim $out/bin/${targetPlatform.config}-nim \ 236 + $wrapperArgs \ 237 + --set NIX_HARDENING_ENABLE "''${NIX_HARDENING_ENABLE/fortify}" \ 238 + --add-flags --lib:${nim-stdlib} 239 + ln -s $out/bin/${targetPlatform.config}-nim $out/bin/nim 240 + 241 + runHook postBuild 242 + ''; 58 243 # Fortify hardening appends -O2 to gcc flags which is unwanted for unoptimized nim builds. 59 - wrapProgram $out/bin/nim \ 60 - --run 'NIX_HARDENING_ENABLE=''${NIX_HARDENING_ENABLE/fortify/}' \ 61 - --suffix PATH : ${lib.makeBinPath [ stdenv.cc ]} 244 + 245 + dontInstall = true; 62 246 63 - runHook postInstall 64 - ''; 247 + meta = meta // { 248 + description = nim.meta.description 249 + + " (${targetPlatform.config} wrapper)"; 250 + platforms = lib.platforms.unix; 251 + }; 65 252 66 - meta = with stdenv.lib; { 67 - description = "Statically typed, imperative programming language"; 68 - homepage = "https://nim-lang.org/"; 69 - license = licenses.mit; 70 - maintainers = with maintainers; [ ehmry ]; 71 - platforms = with platforms; linux ++ darwin; # arbitrary 72 253 }; 73 - } 254 + 255 + in wrapped // wrapperInputs
+2
pkgs/top-level/all-packages.nix
··· 9445 9445 mozart2-binary = callPackage ../development/compilers/mozart/binary.nix { }; 9446 9446 9447 9447 nim = callPackage ../development/compilers/nim { }; 9448 + nim-unwrapped = nim.unwrapped; 9449 + nim-stdlib = nim.stdlib; 9448 9450 nrpl = callPackage ../development/tools/nrpl { }; 9449 9451 9450 9452 neko = callPackage ../development/compilers/neko { };