Skip to content
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

Caching for Haskell projects #318

Closed
1 task done
commandodev opened this issue Jul 15, 2022 · 4 comments · Fixed by #331
Closed
1 task done

Caching for Haskell projects #318

commandodev opened this issue Jul 15, 2022 · 4 comments · Fixed by #331
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@commandodev
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm trying to get caching working for Haskell projects.

Using the same basic haskell/servant project as #312 time I run

NIXPACKS_INSTALL_CACHE_DIRS=/root/.stack NIXPACKS_BUILD_CACHE_DIRS=/app/.stack-work nixpacks build --env NIXPACKS_INSTALL_CACHE_DIRS NIXPACKS_BUILD_CACHE_DIRS -o ../rw-out .

This gives me the following Dockerfile

FROM ghcr.io/railwayapp/nixpacks:debian-1657820962

WORKDIR /app/

# Setup
COPY environment.nix /app/
RUN nix-env -if environment.nix
RUN apt-get update && apt-get install -y libgmp-dev gcc binutils make zlib1g-dev

# Load environment variables
ARG NIXPACKS_BUILD_CACHE_DIRS NIXPACKS_INSTALL_CACHE_DIRS
ENV NIXPACKS_BUILD_CACHE_DIRS=$NIXPACKS_BUILD_CACHE_DIRS NIXPACKS_INSTALL_CACHE_DIRS=$NIXPACKS_INSTALL_CACHE_DIRS

# Install
COPY . /app/
RUN --mount=type=cache,id=zUXEhU0xtPA-/root/.stack,target=/root/.stack stack setup

# Build

RUN --mount=type=cache,id=zUXEhU0xtPA-/app/.stack-work,target=/app/.stack-work stack build

# Start
COPY . /app/
CMD stack exec railway-exe

This works building locally, but the cache doesn't appear to make any speed up (ghc is still downloaded and all library dependencies are rebuilt).

I think the haskell might work better if it did stack install and then called the executable from /root/local/bin (I think).

Or even better - use a two stage build and just copy the executable to the (much smaller) image that's pushed.

To reproduce

Use the command above to build a local docker file

Expected behavior

Caching should work

Environment

ubuntu

❯ nixpacks --version
nixpacks 0.2.4
@commandodev commandodev added the bug Something isn't working label Jul 15, 2022
@commandodev
Copy link
Author

commandodev commented Jul 15, 2022

OK, looks like I've cracked it:

NIXPACKS_BUILD_CACHE_DIRS:   /root/.stack,/app/.stack-work
NIXPACKS_BUILD_CMD:          stack install
NIXPACKS_INSTALL_CACHE_DIRS: /root/.stack
NIXPACKS_START_CMD:          /root/.local/bin/railway-exe

Build time: 7.94 seconds

Key changes:

Cache the /root/.stack and /app/.stack-work directories
Change build to stack install (I think this should be the default behaviour)
Change the run command to /root/.local/bin/<exe-name> the $(head packageYaml.executables) trick should work here to set the default.

Optional improvements:

Multi stage build that just copies the /root/.local/bin directory to the image that's published.

cc @coffee-cup

@coffee-cup coffee-cup added enhancement New feature or request and removed bug Something isn't working labels Jul 15, 2022
@coffee-cup
Copy link
Contributor

Incredible! I can make that change today (unless you want to take it on). Thanks for doing the hard work and figuring this out.

@commandodev
Copy link
Author

commandodev commented Jul 15, 2022 via email

@aarnphm
Copy link
Contributor

aarnphm commented Jul 17, 2022

actually contributing for this is relatively straightforward. You can take a look at src/providers/haskell.rs 😃

@coffee-cup coffee-cup added the good first issue Good for newcomers label Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants