nh2 / static-haskell-nix

easily build most Haskell programs into fully static Linux executables
388 stars 36 forks source link

Can't build Haskell project with PostgreSQL #57

Closed tfausak closed 4 years ago

tfausak commented 4 years ago

I am trying to build a simple Haskell project that uses PostgreSQL. I can't get it to statically link successfully. Here are the exact steps I'm going through:

# I'm running Nix inside of Docker.
# I'd imagine the results are the same regardless.
docker run \
  --interactive \
  --rm \
  --tty \
  --volume "$( pwd ):/opt/example" \
  --volume nix:/nix \
  --workdir /opt/example \
  nixos/nix:2.3 \
  sh

# The `allow-different-user` setting is for Docker.
echo 'resolver: lts-14.0
allow-different-user: true' > stack.yaml

# I called this `example-project` so it would match the `default.nix` without changes.
echo 'name: example-project
version: 1
executable:
  main: example-project.hs
  dependencies: [ base, postgresql-simple ]' > package.yaml

# Program won't do anything useful, but it forces libpq to be linked.
echo 'import Database.PostgreSQL.Simple
main = connectPostgreSQL mempty' > example-project.hs

# Get the current latest example Stack project.
nix-env --install wget
sha=ff7715e0e13fb3f615e64a8d8c2e43faa4429b0f
wget --output-document default.nix \
  https://raw.githubusercontent.com/nh2/static-haskell-nix/$sha/static-stack2nix-builder-example/default.nix

# Set up Cachix for caching.
nix-env -i cachix
cachix use static-haskell-nix

# Try to build.
nix-env --install bash
$( nix-build --no-link -A fullBuildScript )

After all of the normal output I get these errors:

Preprocessing executable 'example-project' for example-project-1..
Building executable 'example-project' for example-project-1..
[1 of 2] Compiling Main             ( example-project.hs, dist/build/example-project/example-project-tmp/Main.o )
[2 of 2] Compiling Paths_example_project ( dist/build/example-project/autogen/Paths_example_project.hs, dist/build/example-project/example-project-tmp/Paths_example_project.o )
Linking dist/build/example-project/example-project ...
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `SSLerrmessage':
(.text+0x3b): undefined reference to `ERR_reason_error_string'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `initialize_SSL':
(.text+0x102): undefined reference to `SSLv23_method'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x10a): undefined reference to `SSL_CTX_new'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x12a): undefined reference to `SSL_CTX_ctrl'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x13e): undefined reference to `SSL_CTX_ctrl'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1a8): undefined reference to `SSL_new'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1c5): undefined reference to `SSL_set_ex_data'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1e9): undefined reference to `BIO_new'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x20d): undefined reference to `SSL_set_bio'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x224): undefined reference to `BIO_int_ctrl'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x233): undefined reference to `SSL_CTX_free'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x29c): undefined reference to `ENGINE_by_id'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x2b4): undefined reference to `ENGINE_init'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x2cf): undefined reference to `ENGINE_load_private_key'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x2e7): undefined reference to `SSL_use_PrivateKey'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x381): undefined reference to `SSL_check_private_key'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x38b): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x439): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x4fa): undefined reference to `SSL_CTX_free'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x5c4): undefined reference to `SSL_use_PrivateKey_file'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x5d2): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x6a4): undefined reference to `SSL_set_verify'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x6cd): undefined reference to `ERR_put_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x6d2): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x716): undefined reference to `SSL_CTX_free'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x727): undefined reference to `SSL_CTX_use_certificate_chain_file'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x741): undefined reference to `BIO_s_socket'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x804): undefined reference to `SSL_ctrl'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x819): undefined reference to `SSL_CTX_load_verify_locations'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x830): undefined reference to `SSL_CTX_get_cert_store'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x863): undefined reference to `ERR_clear_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x8d0): undefined reference to `X509_STORE_load_locations'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x8e2): undefined reference to `X509_STORE_set_flags'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x922): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x96a): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x9b3): undefined reference to `ENGINE_finish'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x9bf): undefined reference to `ENGINE_free'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0xa06): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0xa2f): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0xa99): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0xafe): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `verify_peer_name_matches_certificate_name':
(.text+0xb55): undefined reference to `ASN1_STRING_data'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0xb60): undefined reference to `ASN1_STRING_length'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `my_sock_write':
(.text+0xd41): undefined reference to `BIO_clear_flags'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0xd72): undefined reference to `BIO_set_flags'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `my_sock_read':
(.text+0xda1): undefined reference to `BIO_clear_flags'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0xdd2): undefined reference to `BIO_set_flags'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `pgtls_read_pending':
(.text+0xe7c): undefined reference to `SSL_pending'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `pgtls_read':
(.text+0xed5): undefined reference to `ERR_clear_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0xee6): undefined reference to `SSL_read'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0xef7): undefined reference to `SSL_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0xf45): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `pgtls_write':
(.text+0x112a): undefined reference to `ERR_clear_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x113c): undefined reference to `SSL_write'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x114d): undefined reference to `SSL_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1191): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `pgtls_init':
(.text+0x13b1): undefined reference to `CRYPTO_get_id_callback'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x13bf): undefined reference to `CRYPTO_get_locking_callback'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x13d0): undefined reference to `CRYPTO_set_locking_callback'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x13e3): undefined reference to `OPENSSL_config'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x13e8): undefined reference to `SSL_library_init'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x13ed): undefined reference to `SSL_load_error_strings'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x13f9): undefined reference to `CRYPTO_num_locks'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1424): undefined reference to `CRYPTO_num_locks'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1488): undefined reference to `CRYPTO_set_id_callback'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `pgtls_close':
(.text+0x14c8): undefined reference to `SSL_shutdown'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x14d9): undefined reference to `SSL_free'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x14fc): undefined reference to `X509_free'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1518): undefined reference to `ENGINE_finish'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1524): undefined reference to `ENGINE_free'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1591): undefined reference to `CRYPTO_get_locking_callback'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x15a2): undefined reference to `CRYPTO_get_id_callback'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x15b5): undefined reference to `CRYPTO_set_id_callback'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x15be): undefined reference to `CRYPTO_set_locking_callback'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `pgtls_open_client':
(.text+0x1606): undefined reference to `ERR_clear_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1612): undefined reference to `SSL_connect'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1628): undefined reference to `SSL_get_peer_certificate'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x16d3): undefined reference to `SSL_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x16db): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1755): undefined reference to `X509_get_ext_d2i'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1769): undefined reference to `sk_num'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x17a4): undefined reference to `sk_value'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x180d): undefined reference to `sk_free'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1851): undefined reference to `ERR_get_error'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x18f8): undefined reference to `X509_get_subject_name'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1916): undefined reference to `X509_NAME_get_index_by_NID'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1930): undefined reference to `X509_NAME_get_entry'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1938): undefined reference to `X509_NAME_ENTRY_get_data'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x19fc): undefined reference to `sk_free'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1a83): undefined reference to `sk_free'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1a8f): undefined reference to `X509_get_subject_name'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1aad): undefined reference to `X509_NAME_get_index_by_NID'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1ae8): undefined reference to `X509_get_subject_name'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1b04): undefined reference to `X509_NAME_get_index_by_NID'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: /nix/store/s382qh3qk52ldqygi6xjsjm1s36jmrn5-postgresql-9.6.15-lib/lib/libpq.a(fe-secure-openssl.o): in function `PQsslAttribute':
(.text+0x1c7b): undefined reference to `SSL_get_version'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1ca4): undefined reference to `SSL_get_current_compression'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1cc4): undefined reference to `SSL_get_current_cipher'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1cd1): undefined reference to `SSL_CIPHER_get_bits'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1d14): undefined reference to `SSL_get_current_cipher'
/nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/ld: (.text+0x1d1c): undefined reference to `SSL_CIPHER_get_name'
collect2: error: ld returned 1 exit status
`cc' failed in phase `Linker'. (Exit code: 1)
builder for '/nix/store/5kg1ld5x70ca6f6nazjgpjhfxnwlgz4l-example-project-1.drv' failed with exit code 1
error: build of '/nix/store/5kg1ld5x70ca6f6nazjgpjhfxnwlgz4l-example-project-1.drv' failed
nh2 commented 4 years ago

Looks like we have to do the same as for https://github.com/nh2/static-haskell-nix/blob/ff7715e0e13fb3f615e64a8d8c2e43faa4429b0f/survey/default.nix#L988-L992

Using this slightly modified default.nix for your project fixes it:

# Run using:
#
#     $(nix-build --no-link -A fullBuildScript)
{
  stack2nix-output-path ? "custom-stack2nix-output.nix",
}:
let
  cabalPackageName = "example-project";
  compiler = "ghc865"; # matching stack.yaml

  # Pin static-haskell-nix version.
  static-haskell-nix =
    if builtins.pathExists ../.in-static-haskell-nix
      then toString ../. # for the case that we're in static-haskell-nix itself, so that CI always builds the latest version.
      # Update this hash to use a different `static-haskell-nix` version:
      else fetchTarball https://github.com/nh2/static-haskell-nix/archive/8e73cdc0bebb58577978f319a5c3c8a3b0da98f4.tar.gz;

  # Pin nixpkgs version
  # By default to the one `static-haskell-nix` provides, but you may also give
  # your own as long as it has the necessary patches, using e.g.
  #     pkgs = import (fetchTarball https://github.com/nh2/nixpkgs/archive/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa123.tar.gz) {};
  pkgs = import "${static-haskell-nix}/nixpkgs.nix";

  stack2nix-script = import "${static-haskell-nix}/static-stack2nix-builder/stack2nix-script.nix" {
    inherit pkgs;
    stack-project-dir = toString ./.; # where stack.yaml is
    hackageSnapshot = "2019-10-08T00:00:00Z"; # pins e.g. extra-deps without hashes or revisions
  };

  static-stack2nix-builder = import "${static-haskell-nix}/static-stack2nix-builder/default.nix" {
    normalPkgs = pkgs;
    inherit cabalPackageName compiler stack2nix-output-path;
    # disableOptimization = true; # for compile speed
  };

  # Full invocation, including pinning `nix` version itself.
  fullBuildScript = pkgs.writeScript "stack2nix-and-build-script.sh" ''
    #!/usr/bin/env bash
    set -eu -o pipefail
    STACK2NIX_OUTPUT_PATH=$(${stack2nix-script})
    export NIX_PATH=nixpkgs=${pkgs.path}
    ${pkgs.nix}/bin/nix-build --no-link -A static_package --argstr stack2nix-output-path "$STACK2NIX_OUTPUT_PATH" "$@"
  '';

  static_package =
    static-stack2nix-builder.haskell-static-nix_output.pkgs.staticHaskellHelpers.addStaticLinkerFlagsWithPkgconfig
      static-stack2nix-builder.static_package
      (with static-stack2nix-builder.haskell-static-nix_output.pkgs; [ openssl postgresql ])
      "--libs libpq";
in
  {
    inherit static_package;
    inherit fullBuildScript;
    # For debugging:
    inherit stack2nix-script;
    inherit static-stack2nix-builder;
  }

The new part in it is:

  static_package =
    static-stack2nix-builder.haskell-static-nix_output.pkgs.staticHaskellHelpers.addStaticLinkerFlagsWithPkgconfig
      static-stack2nix-builder.static_package
      (with static-stack2nix-builder.haskell-static-nix_output.pkgs; [ openssl postgresql ])
      "--libs libpq";
in
  {
    inherit static_package;

Here we modify the static_package returned by the static-stack2nix-builder a little bit before building (using the addStaticLinkerFlagsWithPkgconfig function I've just exported in PR #58): We force the libpq libs to be linked in, which also includes its openssl dependency libraries.

This is currently required for all apps that link in libpq.

A better, fundamental solution would be if we could somehow remember in postgresql-simple itself that any package that uses it will have to link those libs in, but I haven't figured out yet how to do that.

tfausak commented 4 years ago

Hmm, I tried that and ended up with a different error:

Preprocessing executable 'example-project' for example-project-1..
Building executable 'example-project' for example-project-1..
[1 of 2] Compiling Main             ( example-project.hs, dist/build/example-project/example-project-tmp/Main.o )
[2 of 2] Compiling Paths_example_project ( dist/build/example-project/autogen/Paths_example_project.hs, dist/build/example-project/example-project-tmp/Paths_example_project.o )
Linking dist/build/example-project/example-project ...
haddockPhase
installing
Installing executable example-project in /nix/store/gr764dvk09mb2xg10pwz4yximnf80kgy-example-project-1/bin
Warning: The directory
/nix/store/gr764dvk09mb2xg10pwz4yximnf80kgy-example-project-1/bin is not in
the system search path.
post-installation fixup
shrinking RPATHs of ELF executables and libraries in /nix/store/gr764dvk09mb2xg10pwz4yximnf80kgy-example-project-1
shrinking /nix/store/gr764dvk09mb2xg10pwz4yximnf80kgy-example-project-1/bin/example-project
cannot find section .dynamic
strip is /nix/store/bprb8jwawnsfnrmjwvazl3r84869f0g3-binutils-2.31.1/bin/strip
stripping (with command strip and flags -S) in /nix/store/gr764dvk09mb2xg10pwz4yximnf80kgy-example-project-1/bin
patching script interpreter paths in /nix/store/gr764dvk09mb2xg10pwz4yximnf80kgy-example-project-1
checking for references to /tmp/nix-build-example-project-1.drv-0/ in /nix/store/gr764dvk09mb2xg10pwz4yximnf80kgy-example-project-1...
cannot find section .dynamic
/nix/store/gr764dvk09mb2xg10pwz4yximnf80kgy-example-project-1
nh2 commented 4 years ago

@tfausak Is that an error?

It seems it printed the result output path /nix/store/gr764dvk09mb2xg10pwz4yximnf80kgy-example-project-1 at the end.

tfausak commented 4 years ago

Oops! You’re absolutely right. I skimmed through that output too quickly. The “cannot find section .dynamic” threw me for a loop.

The build succeeded and I was able to run the executable. And ldd reported it as not dynamic. Thanks so much!

wedens commented 4 years ago

I've tried to apply the same workaround but the build fails with:

Linking dist/build/my-project/my-project ...
/nix/store/f1v9hcm7pxdrgmmarf5ldss9xc480l0n-binutils-2.31.1/bin/ld: /nix/store/9lyjgijs7cix0scxql46lik6v57qbydq-openssl-1.1.1d/lib/libcrypto.a(f_impl.o): in function `gf_mul':

/build/openssl-1.1.1d/crypto/ec/curve448/arch_32/f_impl.c:16:0: error:
     multiple definition of `gf_mul'; /nix/store/qhnylp3ia57kwzihghrnmlng758mpcni-cipher-aes-0.2.11/lib/ghc-8.6.5/x86_64-linux-ghc-8.6.5/cipher-aes-0.2.11-B94prXb0Uot719rucDUpNG/libHScipher-aes-0.2.11-B94prXb0Uot719rucDUpNG.a(gf.o):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
`cc' failed in phase `Linker'. (Exit code: 1)
builder for '/nix/store/483dzvsmmdawv6gjsajnbp5p1zr8lgjn-my-project-0.0.0.drv' failed with exit code 1
error: build of '/nix/store/483dzvsmmdawv6gjsajnbp5p1zr8lgjn-my-project-0.0.0.drv' failed
wedens commented 4 years ago

apparently cipher-aes-0.2.11 defines gf_mul which conflicts with openssl.

cipher-aes is deprecated but it's still being used by clientsession which is yesod-core dependency :/

nh2 commented 4 years ago

@wedens The simplest fix to that would likely be a PR to cipher-aes, namespacing the function, calling it e.g. hs_cipher_aes_gf_mul.