Skip to content

Conversation

@seanstrom
Copy link
Contributor

Summary

  • This PR attempts to integrate Devbox for managing and provisioning the development environment.
    • Additionally, this PR integrates a .envrc file for integrating Direnv with Devbox. This is an optional feature, but it can be convenient for entering a Devbox workspace and automatically have the shell env prepared.
  • This PR was tested locally on macOS using commands like stack install, stack build, and stack ghci.
    • There may be some unresolved issues for other platforms like Linux or Windows.

Copy link
Member

@supermario supermario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks @seanstrom !

Some minor feedback.

Also; when I run devbox shell and then stack ghci I see this error printed out about 120 times:

gcc: error: unrecognized command-line option '--target=arm64-apple-darwin'

Any idea why? I see gcc is version pinned, I guess latest had some issues?

# Automatically sets up your devbox environment whenever you cd into this
# directory via our direnv integration:

eval "$(devbox generate direnv --print-envrc)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice, but I don't think this should be the default for direnv, as I think more folks use direnv than they do devbox so the first thing they'd get greeted with is an odd error upon entering the project dir.

Happy to keep it as .envrc-devbox and add a note in the readme that a devbox setup is available and that optionally you can also use it automatically with direnv if you like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah good point!
I'll rename the file to .envrc-devbox and update the docs too 👍

@seanstrom
Copy link
Contributor Author

Also; when I run devbox shell and then stack ghci I see this error printed out about 120 times:

gcc: error: unrecognized command-line option '--target=arm64-apple-darwin'

Any idea why? I see gcc is version pinned, I guess latest had some issues?

I think this error happens when dependencies use the GCC compiler and attempt to cross-compile. I recall reading that GCC doesn't support the flag --target=arm64-apple-darwin, so I needed to use Clang for the compilation of those dependencies.

At the moment, I'm not able to reproduce that issue again, so I'll need a moment to figure out how to reset my env to test this again.

In the mean time, maybe try overriding / exporting an environment variable like: export CC=clang
I recall this being the workaround that I used during some of my tinkering.

On a side note, what is the reason for needing GCC in this project? Would Clang be okay for compiling the dependencies?

@supermario
Copy link
Member

It's a long time since I set things up so I don't recall if gcc specifically was required.

Just tried running a recompile in ghci and that seems to have a hard failure if that helps repro;

ghci> :rr
gcc: error: unrecognized command-line option '--target=arm64-apple-darwin'
gcc: error: unrecognized command-line option '--target=arm64-apple-darwin'
`gcc' failed in phase `Linker'. (Exit code: 1)
[188 of 218] Compiling Lamdera.Injection ( /Users/mario/dev/projects/lamdera-compiler/extra/Lamdera/Injection.hs, /Users/mario/dev/projects/lamdera-compiler/.stack-work/odir/Lamdera/Injection.o )
gcc: error: unrecognized command-line option '--target=arm64-apple-darwin'
gcc: error: unrecognized command-line option '--target=arm64-apple-darwin'
`gcc' failed in phase `Linker'. (Exit code: 1)

<interactive>:1:1: error:
    Not in scope: ‘Test.target’
    No module named ‘Test’ is imported.

@seanstrom
Copy link
Contributor Author

@supermario I've pushed up some changes that should set the desired C compiler to be Clang during compilation. Can you pull down these changes and try re-entering the devbox shell?

Also it would be good to confirm what running: echo $CC prints before and after entering the devbox shell.

@supermario
Copy link
Member

@seanstrom hrm different issue now 🤔

$ stack install
[1 of 2] Compiling Main             ( /Users/mario/.stack/setup-exe-src/setup-CKvAmRb3.hs, /Users/mario/.stack/setup-exe-src/setup-CKvAmRb3.o )
gcc: error: unrecognized command-line option '--target=arm64-apple-darwin'
`gcc' failed in phase `Assembler'. (Exit code: 1)

Error: [S-6374]
       While building simple Setup.hs (scroll up to its section to see the error) using:
       /Users/mario/.ghcup/ghc/9.2.8/bin/ghc -rtsopts -threaded -clear-package-db -global-package-db -hide-all-packages -package base -main-is StackSetupShim.mainOverride -package Cabal-3.6.3.0 /Users/mario/.stack/setup-exe-src/setup-CKvAmRb3.hs /Users/mario/.stack/setup-exe-src/setup-shim-CKvAmRb3.hs -o /Users/mario/.stack/setup-exe-cache/aarch64-osx/tmp-Cabal-simple_CKvAmRb3_3.6.3.0_ghc-9.2.8
       Process exited with code: ExitFailure 1
$ echo $CC
clang
$ which `echo $CC`
/nix/store/wkadfwbhn9b4kdih88j7izzy20qw79bk-clang-wrapper-19.1.5/bin/clang

@seanstrom
Copy link
Contributor Author

@supermario Alrighty I think I've figured out what's been going on, and I hope to have a solution too.

I think the gist of the issue stems from how Stack is installing GHC and how GHC is possibly linked to a different C compiler. So in this case, I think we need to configure Stack to avoid installing a separate GHC and use one that's provided by the Devbox environment.

Currently, the setup that I'm experimenting with involves using Nix-Flakes to configure the devbox environment with some extra overrides. For example, I'm using a flake to override stack to not install GHC, and I'm using the flake to pin the given GHC version to 9.2.8 (which matches the stack.yml file).

Overall, I think this solution is promising, but it did require a fair amount of tinkering to resolve multiple issues related to Nix, GHC, Apple C Header files, and etc. The good news is that I can now compile the project inside devbox shell --pure (and also without pure) 🙌

Hopefully these new changes resolve those compilation issues you mentioned, please let me know how it goes and what you think when you have a chance 🙏

@supermario
Copy link
Member

Hey @seanstrom – that appears to be working for me now! I was able to build a binary;

$ lamdera --version-full
ls -alh lamdera-1.3.3-0.19.1-darwin-aarch64-aef1f8c56b49a0ca8e1fff96ef9055138a6260e9-dirty (Mon Jan 20 13:48:29 2025 +0000) (branch:seanstrom/devbox-integration)

One last odd thing is this;

ld: warning: object file (/Users/mario/.stack/snapshots/aarch64-osx/4f96fc52e08a172cc22ef55b2a7e0a7ff51a8de11c8ab4ad9ed6618cfad4a643/9.2.8/lib/aarch64-osx-ghc-9.2.8/vector-0.12.3.1-9lAFbeOos5HBZVbPp7MuQ3/libHSvector-0.12.3.1-9lAFbeOos5HBZVbPp7MuQ3.a(Vector.o)) was built for newer macOS version (14.0) than being linked (11.0)

My guess would be this is because I'm on MacOS 14.4 and my ~/.stack caches from previous builds are being at least partially re-used, but it is curious that it's linking for 11.0...

Unsure if that has any impact, but I'm happy to merge as is for now if you're unsure the cause of this.

@seanstrom
Copy link
Contributor Author

@supermario great to hear that this is working 🙌


About the linking warning you mentioned:

ld: warning: object file (/Users/mario/.stack/snapshots/aarch64-osx/4f96fc52e08a172cc22ef55b2a7e0a7ff51a8de11c8ab4ad9ed6618cfad4a643/9.2.8/lib/aarch64-osx-ghc-9.2.8/vector-0.12.3.1-9lAFbeOos5HBZVbPp7MuQ3/libHSvector-0.12.3.1-9lAFbeOos5HBZVbPp7MuQ3.a(Vector.o)) was built for newer macOS version (14.0) than being linked (11.0)

I'm pretty sure this is because we're using the 11.0 Apple headers in the devbox environment, but I think 14.0 is available in NixPkgs, so we would just need to find a way to dynamically choose the best Apple headers for the environment based on the host macOS version.

This might be complicated to do, but it could be a simple change, just need to read through some nix stuff since I imagine someone has needed to do this kind of thing before.

@supermario
Copy link
Member

supermario commented Feb 3, 2025

@seanstrom okay cool – I'll merge this in for now as it's a great start anyway, and if you find any insight on dynamically selecting apple libs then another PR would be 👌

Thank you!

@supermario supermario merged commit 6415988 into lamdera:lamdera-next Feb 3, 2025
@seanstrom seanstrom deleted the seanstrom/devbox-integration branch February 5, 2025 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants