{"id":16581773,"url":"https://github.com/kaushalmodi/hello_musl","last_synced_at":"2025-04-09T16:20:38.947Z","repository":{"id":39536347,"uuid":"148636836","full_name":"kaushalmodi/hello_musl","owner":"kaushalmodi","description":"NimScript (config.nims) for building a static binary using Nim + musl + pcre + libressl/openssl","archived":false,"fork":false,"pushed_at":"2023-08-07T15:56:13.000Z","size":62,"stargazers_count":91,"open_issues_count":4,"forks_count":10,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-04T15:44:30.495Z","etag":null,"topics":["binary","libressl","musl","nim","nimscript","openssl","pcre","ssl","static"],"latest_commit_sha":null,"homepage":"https://scripter.co/nim-deploying-static-binaries/","language":"Nim","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaushalmodi.png","metadata":{"files":{"readme":"README.org","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-13T12:49:34.000Z","updated_at":"2025-02-20T19:27:53.000Z","dependencies_parsed_at":"2022-09-15T22:00:15.699Z","dependency_job_id":null,"html_url":"https://github.com/kaushalmodi/hello_musl","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalmodi%2Fhello_musl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalmodi%2Fhello_musl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalmodi%2Fhello_musl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaushalmodi%2Fhello_musl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaushalmodi","download_url":"https://codeload.github.com/kaushalmodi/hello_musl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065285,"owners_count":21041872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary","libressl","musl","nim","nimscript","openssl","pcre","ssl","static"],"created_at":"2024-10-11T22:29:51.082Z","updated_at":"2025-04-09T16:20:38.896Z","avatar_url":"https://github.com/kaushalmodi.png","language":"Nim","funding_links":[],"categories":[],"sub_categories":[],"readme":"#+title: Nim + musl + pcre + libressl/openssl\n#+author: Kaushal Modi\n\n[[https://travis-ci.org/kaushalmodi/hello_musl][https://travis-ci.org/kaushalmodi/hello_musl.svg?branch=master]]\n\nThis repo contains a generic [[https://github.com/kaushalmodi/hello_musl/blob/master/config.nims][~config.nims~]] that adds a Nim\n\"sub-command\" or task named *musl*. You can simply add that file to\nyour Nim project, and run ~nim musl foo.nim~ with optional ~-d:pcre~\nand/or ~-d:libressl~ / ~-d:openssl~ switches (assuming that the below\nprerequisites are met).\n\n* Prerequisites\n- OS: One of Linux x86 (32/64), ARM (32/64), MIPS (32/64), PowerPC\n  (32/64), S390X, SuperH, Microblaze, OpenRISC ([[https://www.musl-libc.org/intro.html][ref]])\n- Nim: [[https://nim-lang.org/]] (built from [[https://github.com/nim-lang/Nim/tree/devel][*devel*]] branch as of \u003c2018-09-13 Thu\u003e)\n- *musl* library: [[https://www.musl-libc.org/download.html]]\n- ~curl~ and GNU ~tar~ are needed on the system for the ~-d:pcre~ /\n  ~-d:libressl~ / ~-d:openssl~ switches to work.\n** Optional\nThese optional command-line utilities for binary size optimization\nwill be run automatically one by one, if present.\n- ~strip~ (from ~binutils~): [[https://sourceware.org/binutils/docs/binutils/strip.html]]\n- ~upx~: [[https://github.com/upx/upx]]\n* Generating static binary\n** For this example ~hello_musl~ project\n1. ~git clone https://github.com/kaushalmodi/hello_musl~\n2. ~cd hello_musl~\n3. Run ~nim musl [-d:pcre] [-d:libressl|-d:openssl] \u003cFILE\u003e.nim~\n** Static links with just ~musl~ lib\n#+begin_example\nnim musl src/hello_musl.nim\n#+end_example\n\nThat will generate ~hello_musl~ binary in ~src/~ directory.\n- ~file ./src/hello_musl~ will print:\n  #+begin_example\n  src/hello_musl: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped\n  #+end_example\n- ~./src/hello_musl~ will print:\n  #+begin_example\n  Hello, World!\n  #+end_example\n** Static linking with ~musl~ + ~pcre~ libs\n#+begin_example\nnim musl -d:pcre src/hello_musl_pcre.nim\n#+end_example\n\nThat will generate ~hello_musl_pcre~ binary in ~src/~ directory.\n- ~file ./src/hello_musl_pcre~ will print:\n  #+begin_example\n  src/hello_musl_pcre: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped\n  #+end_example\n- ~./src/hello_musl_pcre~ will print:\n  #+begin_example\n  Hello, World!\n  Bye, World!\n  #+end_example\n** Static linking with ~musl~ + ~libressl~ libs\n#+begin_example\nnim musl -d:libressl src/hello_musl_ssl.nim\n#+end_example\n\nThat will generate ~hello_musl_ssl~ binary in ~src/~ directory.\n- ~file ./src/hello_musl_ssl~ will print:\n  #+begin_example\n  src/hello_musl_ssl: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped\n  #+end_example\n- ~./src/hello_musl_ssl~ will print:\n  #+begin_example\n  {\"name\":\"Kaushal Modi\",\"type\":\"card\",\"url\":\"https://scripter.co/\"}\n  {\n    \"name\": \"Kaushal Modi\",\n    \"type\": \"card\",\n    \"url\": \"https://scripter.co/\"\n  }\n  \"Kaushal Modi\"\n  #+end_example\n\n- Note :: Binary built using ~-d:libressl~ on RHEL 6.8 is known to\n          crash with just a \"Killed\" message. But the same, when built\n          on Travis, run fine on RHEL 6.8 (mystery). So a RHEL 6.8\n          user may look at the slightly less secure option of building\n          using with ~-d:openssl~ switch instead.\n** Static linking with ~musl~ + ~openssl~ libs\n#+begin_example\nnim musl -d:openssl src/hello_musl_ssl.nim\n#+end_example\n\nThat will generate ~hello_musl_ssl~ binary in ~src/~ directory, and\nthe outputs would be same as those you see for the ~-d:libressl~\nswitch.\n*** Note about compiling with statically linked OpenSSL (~-d:openssl~)\nWhen building with ~-d:openssl~, a statically linked version of\nOpenSSL library is *first* built with the ~-DOPENSSL_NO_SECURE_MEMORY~\nConfigure option, because of an [[https://github.com/openssl/openssl/issues/7207#issuecomment-420814524][issue with it getting built using\nMUSL]].\n\n*This security laxing switch is not added if LibreSSL is used instead\n (~-d:libressl~)*\n** For your Nim project\n- Copy the [[https://github.com/kaushalmodi/hello_musl/blob/master/config.nims][~config.nims~]] to your Nim project.\n- While being the same directory as ~config.nims~, do:\n  #+begin_example\n  nim musl \u003cpath/to/your/nim/file\u003e             # without static pcre lib linking\n  nim musl -d:pcre \u003cpath/to/your/nim/file\u003e     # *with* static pcre lib linking\n  nim musl -d:libressl \u003cpath/to/your/nim/file\u003e # *with* static libressl lib linking\n  nim musl -d:openssl \u003cpath/to/your/nim/file\u003e  # *with* static openssl lib linking (less secure)\n  #+end_example\n* References\n- [[https://github.com/nim-lang/Nim/wiki/Using-nimscript-for-configuration]]\n- [[https://nim-lang.org/docs/nims.html]]\n  - [[https://nim-lang.org/docs/nimscript.html]]\n- [[https://hookrace.net/blog/nim-binary-size/#using-the-c-standard-library]]\n- [[https://www.reddit.com/r/programming/comments/2wk7q6/static_linking_with_nim/corwtl7/]]\n* TODO Todo-List\n- [ ] Figure out how to have ~nimble install~ install the binary\n  generated by ~nim musl~.\n- [X] Don't hard-code the ~muslGcc~ const in ~config.nims~\n- [X] Not require ~hello_musl.nimble~\n  - Currently that is needed just to add the ~-d:musl~ and\n    ~-d:release~ switches, and then to auto-run ~strip -s~.\n  - So to remove dependency on this file, I need to figure out how to\n    get the current /foo.nim/ file name from within the ~config.nims~.\n  - Eventual goal is to reuse the same ~config.nims~ for all\n    projects. So I cannot hardcode the ~pkgName~ as I do in\n    ~hello_musl.nimble~.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaushalmodi%2Fhello_musl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaushalmodi%2Fhello_musl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaushalmodi%2Fhello_musl/lists"}