-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Build a statically linked binary #40
Comments
No, I don't know, sorry. |
Alright, thanks for your quick reply, I will try to dig a bit further 😄 |
Just a quick note, it's actually working using "old" cabal commands: # build static executable binary
FROM haskell:8
RUN git clone https://github.com/BurntSushi/erd.git
WORKDIR erd
# use "old" commands on purpose to workaround an issue with static link
RUN cabal update
RUN cabal install --only-dependencies --force-reinstalls
RUN cabal configure --disable-executable-dynamic --disable-shared --ghc-option=-optl=-static \
&& cabal build @BurntSushi If you want I can submit a pull request with this Dockerfile ? Or add a documentation page somewhere if you think that it can be useful ? 😉 |
I'd be happy to add a link, but I definitely do not want to adopt a |
Oh I didn't know that the project is unmaintained. |
Hey, I found this because I got pinged on haskell/cabal#5847. What you want to do is really easy with https://github.com/nh2/static-haskell-nix: Just check out the repo, do what it says in the README, and run:
This will generate a 3.8 MB static I have attached it here: erd-0.1.3.0-static-linux-x86_64.zip Building it took only 2 minutes due to my binary cache. I had to make a miniscule change nh2/static-haskell-nix@3729754 to get it compile, because the I also recommend you adding |
Hello @nh2 As mentioned, I'm not familiar with the whole Haskell ecosystem. I finally got this working using What are the advantages of using The dependency should be Regarding Stackage, I don't really know because @BurntSushi already mentioned that the project is unmaintained. So ultimately he will decide. |
@Mogztter
Yep, understood.
Reproducibility to a high degree, and getting a
Small correction: musl instead of
Yes, likely. As long as your build toolchain is built on musl, using this approach is equivalent to my nixpkgs approach. You also need to ensure that all system libraries a given Haskell package needs provide
I think this is only the case in
You don't need to be the author to add something to Stackage (like you don't need to be an author to add something to Debian). If @BurntSushi does not need to project any more, but you have an interest in I hadn't heard of Hope this answers all questions! |
If someone wanted to take over this code and manage the Stack package, that'd be great! |
@BurntSushi mind making a short, pinned "looking for new maintainer" issue? That would make it obvious for people looking at issues. I'd then link it for you on Haskell reddit and mailing lists unless you want to do that. The package seems beginner friendly so chances are high somebody would like to take it over. |
@mmzx I have seen that the project can now be built using stack. What is the recommended way to create a statically linked binary? Thanks! |
@Mogztter, can you maybe have a look at the link below? |
|
@nh2 I'm using a Docker image to build the project and I'm not familiar with nix so https://github.com/nh2/static-haskell-nix/blob/master/static-stack2nix-builder-example/default.nix is really cryptic for me 🤔 Do you provide a Docker image to build a statically linked binary with FROM xyz
RUN git clone https://github.com/BurntSushi/erd.git
WORKDIR erd
RUN git checkout v0.2.0.0
RUN nixpkgs nix-build |
@nh2 Is that the case now? Can I get a pre-built statically linked binary of erd somewhere? |
For reference the following is working: FROM ubuntu:18.04
RUN apt-get update && apt-get install -y graphviz curl git
RUN curl -sSL https://get.haskellstack.org/ | sh
RUN git clone https://github.com/BurntSushi/erd.git
WORKDIR erd
RUN git checkout v0.2.0.0
RUN /usr/local/bin/stack install --ghc-options="-fPIC" \
--ghc-options="-static" \
--ghc-options="-optl=-static" \
--ghc-options="-optc=-static" |
@Mogztter
It looks not,
No, Docker does not really provide any benefit over using nix directly on a host system, so it is unlikely that anybody will do that.
Be aware that your Docker image is using Ubuntu, which means you are statically linking against glibc, which does not properly support static linking (neither technically, nor do its maintainers consider static linking a use case worth supporting). If you use any networking functionality, you must expect segfaults (e.g. nh2/static-haskell-nix#17), and may experience other odd behaviour. To get a proper static binary without surprises, you must use a libc that supports static linking, such as musl, and the entire dependency chain (including all dependent system C libraries) must be built against it. This is what Building
|
Sorry I misread, it's published on hackage not stackage 😅
Ok but I can install Nix on an Alpine image?
I tried to use an Alpine based image, but
Should we add this file to the
Thanks for the explanation.
Is it possible to get a stable output (ie
Thanks I will give it a try 👍 |
@Mogztter That's possible but the question may not make sense. You need either
Yes.
Ah I see. Indeed NixOS is not necessary. Only the nix build tool, and the nixpkgs repository of software, which you can run on any Linux distro.
Yes, there have been some problems with stack on Alpine with its GHC versions recently (commercialhaskell/stack#2387). This was one of the reasons I made static-haskell-nix: To make reproducible static builds possible on any distro, without having to depend on whether a specific Linux distro's GHC setup is currently in good shape or not. (That's not to say Alpine isn't good -- I think it's great, but nixpkgs is much easier to override with a bit of declarative code than Alpine is.)
Yes: The nix build prints the path to stdout, and it's the only thing it prints to stdout. So you can do e.g.
Let me know if you encounter any issues 👍 |
I am currently working on to put erd into stackage, but having some obstacles on the way. |
FWIW the following gives me a static binary (except for glibc) from current master:
Run with
EDIT: or better, because the container image is actually not needed here: mkdir -p .cabal
podman run \
--volume "$(pwd)":"/src" \
--volume "$(pwd)/.cabal":"/root/.cabal" \
--workdir /src \
--interactive \
--rm \
docker.io/haskell:8 \
sh <<EOF
cabal v2-update
cabal v2-configure --enable-executable-static --enable-optimization=2
cabal v2-build
cabal v2-install --install-method=copy --overwrite-policy=always --installdir .
EOF |
@mmzx I see that https://github.com/BurntSushi/erd/releases/tag/v0.2.1.0-RC1 has binaries attached. Would it be possible to do that for the current and next releases? |
@Mogztter Yes, should be possible. Will do the binaries over the weekend, stay tuned please. |
@Mogztter I've just created a PR to release a new version, it would be due anyway regardless static binary availability. However, I have failed making the compiling work using Would you be fine using a docker container as an alternative? |
For now, we are using a Docker container to produce an executable: Since we are using a tag, it would be better if the result was attached to a release. Currently, we are building it every time 😞 |
We have the same requirement for the official asciidoctor Docker image which provides The recent upgrade to alpine 3.17 break the cabal install so we need to find another static binary. Would be useful to get static binaries on releases. @mmzx I can help if you need some |
Hello and thanks for this project!
I'm trying to build a statically linked binary from the source. I'm not very familiar with the Haskell ecosystem so I've been using this blog post https://vaibhavsagar.com/blog/2018/01/03/static-haskell-nix/ to get me started.
I have the following
Dockerfile
to build the project:As you can see I'm using
new
prefixed commands becausecabal
was complaining.Anyway, the build is failing as you can see below:
Full trace
Do you know if it's possible ? Am I missing something ?
Thanks for your help 👍
The text was updated successfully, but these errors were encountered: