This repo contains a generic ~config.nims~ that adds a Nim
“sub-command” or task named musl. You can simply add that file to
your Nim project, and run nim musl foo.nim with optional -d:pcre
and/or -d:libressl / -d:openssl switches (assuming that the below
prerequisites are met).
- OS: One of Linux x86 (32/64), ARM (32/64), MIPS (32/64), PowerPC (32/64), S390X, SuperH, Microblaze, OpenRISC (ref)
- Nim: https://nim-lang.org/ (built from *devel* branch as of <2018-09-13 Thu>)
- musl library: https://www.musl-libc.org/download.html
curland GNUtarare needed on the system for the-d:pcre/-d:libressl/-d:opensslswitches to work.
These optional command-line utilities for binary size optimization will be run automatically one by one, if present.
strip(frombinutils): https://sourceware.org/binutils/docs/binutils/strip.htmlupx: https://github.com/upx/upx
git clone https://github.com/kaushalmodi/hello_muslcd hello_musl- Run
nim musl [-d:pcre] [-d:libressl|-d:openssl] <FILE>.nim
nim musl src/hello_musl.nim
That will generate hello_musl binary in src/ directory.
file ./src/hello_muslwill print:src/hello_musl: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped./src/hello_muslwill print:Hello, World!
nim musl -d:pcre src/hello_musl_pcre.nim
That will generate hello_musl_pcre binary in src/ directory.
file ./src/hello_musl_pcrewill print:src/hello_musl_pcre: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped./src/hello_musl_pcrewill print:Hello, World! Bye, World!
nim musl -d:libressl src/hello_musl_ssl.nim
That will generate hello_musl_ssl binary in src/ directory.
file ./src/hello_musl_sslwill print:src/hello_musl_ssl: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped./src/hello_musl_sslwill print:{"name":"Kaushal Modi","type":"card","url":"https://scripter.co/"} { "name": "Kaushal Modi", "type": "card", "url": "https://scripter.co/" } "Kaushal Modi"
- Note
- Binary built using
-d:libresslon RHEL 6.8 is known to crash with just a “Killed” message. But the same, when built on Travis, run fine on RHEL 6.8 (mystery). So a RHEL 6.8 user may look at the slightly less secure option of building using with-d:opensslswitch instead.
nim musl -d:openssl src/hello_musl_ssl.nim
That will generate hello_musl_ssl binary in src/ directory, and
the outputs would be same as those you see for the -d:libressl
switch.
When building with -d:openssl, a statically linked version of
OpenSSL library is first built with the -DOPENSSL_NO_SECURE_MEMORY
Configure option, because of an issue with it getting built using
MUSL.
This security laxing switch is not added if LibreSSL is used instead (~-d:libressl~)
- Copy the ~config.nims~ to your Nim project.
- While being the same directory as
config.nims, do:nim musl <path/to/your/nim/file> # without static pcre lib linking nim musl -d:pcre <path/to/your/nim/file> # *with* static pcre lib linking nim musl -d:libressl <path/to/your/nim/file> # *with* static libressl lib linking nim musl -d:openssl <path/to/your/nim/file> # *with* static openssl lib linking (less secure)
- https://github.com/nim-lang/Nim/wiki/Using-nimscript-for-configuration
- https://nim-lang.org/docs/nims.html
- https://hookrace.net/blog/nim-binary-size/#using-the-c-standard-library
- https://www.reddit.com/r/programming/comments/2wk7q6/static_linking_with_nim/corwtl7/
- [ ] Figure out how to have
nimble installinstall the binary generated bynim musl. - [X] Don’t hard-code the
muslGccconst inconfig.nims - [X] Not require
hello_musl.nimble- Currently that is needed just to add the
-d:musland-d:releaseswitches, and then to auto-runstrip -s. - So to remove dependency on this file, I need to figure out how to
get the current foo.nim file name from within the
config.nims. - Eventual goal is to reuse the same
config.nimsfor all projects. So I cannot hardcode thepkgNameas I do inhello_musl.nimble.
- Currently that is needed just to add the