Skip to content

Setup hooks are polluting NIX_LDFLAGS #40046

Closed

Description

@matthewbauer

I just realized that we don't have a good way to stop setup hooks from being run multiple times. For instance, this is the output for gpgme on my computer:

-lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -L/nix/store/zl3vsrqfwx4xvxagdrdqav73yl53cxps-libc++-5.0.1/lib -lintl -L/nix/store/15jap16aibnk81idwlf37g91mx8akaqv-gpgme-1.11.1-dev/lib -lintl -lintl -L/nix/store/8rcmp881wmbh18mzrkd9kmjxh2w8cn5z-libgpg-error-1.28/lib -lintl -L/nix/store/dkmxfrzsi0bzg4a4b976ww08243fslbs-glib-2.56.0-dev/lib -lintl -L/nix/store/byjz3zsj2397x4bllp4axqcnc517jdl7-zlib-1.2.11-dev/lib -lintl -L/nix/store/553hwxk54h8z87m4xgga4n8dznd6n8vp-zlib-1.2.11/lib -lintl -L/nix/store/si0g5b52xhrpdd8xjd67xdpskfgfscid-libffi-3.2.1-dev/lib -lintl -L/nix/store/qm51pj3fsvvb34kl7s9p4vvis92vq3ml-libffi-3.2.1/lib -lintl -L/nix/store/s8yvb556vw7l73qzx2wmhc2qmdw972dy-gettext-0.19.8/lib -lintl -L/nix/store/6xismd1mcjqnsj873wmx1fr0p62v13f2-libiconv-osx-10.11.6/lib -lintl -L/nix/store/7cb7305cliycr62pkcc8yzpjl50jncad-glib-2.56.0/lib -lintl -lintl -L/nix/store/yr6dzd26vd5crjfczv5w5kpw0g3la7qg-libassuan-2.5.1/lib -lintl -L/nix/store/8ipd3lbwabrxwbm1f7sxi8xj54sn0a44-pth-2.0.7/lib -lintl -L/nix/store/6ilwnfqar3k2vx9baazzagbfh25xgcfg-gpgme-1.11.1/lib -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -L/nix/store/zl3vsrqfwx4xvxagdrdqav73yl53cxps-libc++-5.0.1/lib -lintl -L/nix/store/15jap16aibnk81idwlf37g91mx8akaqv-gpgme-1.11.1-dev/lib -lintl -lintl -L/nix/store/8rcmp881wmbh18mzrkd9kmjxh2w8cn5z-libgpg-error-1.28/lib -lintl -L/nix/store/dkmxfrzsi0bzg4a4b976ww08243fslbs-glib-2.56.0-dev/lib -lintl -L/nix/store/byjz3zsj2397x4bllp4axqcnc517jdl7-zlib-1.2.11-dev/lib -lintl -L/nix/store/553hwxk54h8z87m4xgga4n8dznd6n8vp-zlib-1.2.11/lib -lintl -L/nix/store/si0g5b52xhrpdd8xjd67xdpskfgfscid-libffi-3.2.1-dev/lib -lintl -L/nix/store/qm51pj3fsvvb34kl7s9p4vvis92vq3ml-libffi-3.2.1/lib -lintl -L/nix/store/s8yvb556vw7l73qzx2wmhc2qmdw972dy-gettext-0.19.8/lib -lintl -L/nix/store/6xismd1mcjqnsj873wmx1fr0p62v13f2-libiconv-osx-10.11.6/lib -lintl -L/nix/store/7cb7305cliycr62pkcc8yzpjl50jncad-glib-2.56.0/lib -lintl -lintl -L/nix/store/yr6dzd26vd5crjfczv5w5kpw0g3la7qg-libassuan-2.5.1/lib -lintl -L/nix/store/8ipd3lbwabrxwbm1f7sxi8xj54sn0a44-pth-2.0.7/lib -lintl -L/nix/store/6ilwnfqar3k2vx9baazzagbfh25xgcfg-gpgme-1.11.1/lib -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -L/nix/store/zl3vsrqfwx4xvxagdrdqav73yl53cxps-libc++-5.0.1/lib -lintl -L/nix/store/15jap16aibnk81idwlf37g91mx8akaqv-gpgme-1.11.1-dev/lib -lintl -lintl -L/nix/store/8rcmp881wmbh18mzrkd9kmjxh2w8cn5z-libgpg-error-1.28/lib -lintl -L/nix/store/dkmxfrzsi0bzg4a4b976ww08243fslbs-glib-2.56.0-dev/lib -lintl -L/nix/store/byjz3zsj2397x4bllp4axqcnc517jdl7-zlib-1.2.11-dev/lib -lintl -L/nix/store/553hwxk54h8z87m4xgga4n8dznd6n8vp-zlib-1.2.11/lib -lintl -L/nix/store/si0g5b52xhrpdd8xjd67xdpskfgfscid-libffi-3.2.1-dev/lib -lintl -L/nix/store/qm51pj3fsvvb34kl7s9p4vvis92vq3ml-libffi-3.2.1/lib -lintl -L/nix/store/s8yvb556vw7l73qzx2wmhc2qmdw972dy-gettext-0.19.8/lib -lintl -L/nix/store/6xismd1mcjqnsj873wmx1fr0p62v13f2-libiconv-osx-10.11.6/lib -lintl -L/nix/store/7cb7305cliycr62pkcc8yzpjl50jncad-glib-2.56.0/lib -lintl -lintl -L/nix/store/yr6dzd26vd5crjfczv5w5kpw0g3la7qg-libassuan-2.5.1/lib -lintl -L/nix/store/8ipd3lbwabrxwbm1f7sxi8xj54sn0a44-pth-2.0.7/lib -lintl -L/nix/store/6ilwnfqar3k2vx9baazzagbfh25xgcfg-gpgme-1.11.1/lib -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -lintl -L/nix/store/zl3vsrqfwx4xvxagdrdqav73yl53cxps-libc++-5.0.1/lib -lintl -L/nix/store/15jap16aibnk81idwlf37g91mx8akaqv-gpgme-1.11.1-dev/lib -lintl -lintl -L/nix/store/8rcmp881wmbh18mzrkd9kmjxh2w8cn5z-libgpg-error-1.28/lib -lintl -L/nix/store/dkmxfrzsi0bzg4a4b976ww08243fslbs-glib-2.56.0-dev/lib -lintl -L/nix/store/byjz3zsj2397x4bllp4axqcnc517jdl7-zlib-1.2.11-dev/lib -lintl -L/nix/store/553hwxk54h8z87m4xgga4n8dznd6n8vp-zlib-1.2.11/lib -lintl -L/nix/store/si0g5b52xhrpdd8xjd67xdpskfgfscid-libffi-3.2.1-dev/lib -lintl -L/nix/store/qm51pj3fsvvb34kl7s9p4vvis92vq3ml-libffi-3.2.1/lib -lintl -L/nix/store/s8yvb556vw7l73qzx2wmhc2qmdw972dy-gettext-0.19.8/lib -lintl -L/nix/store/6xismd1mcjqnsj873wmx1fr0p62v13f2-libiconv-osx-10.11.6/lib -lintl -L/nix/store/7cb7305cliycr62pkcc8yzpjl50jncad-glib-2.56.0/lib -lintl -lintl -L/nix/store/yr6dzd26vd5crjfczv5w5kpw0g3la7qg-libassuan-2.5.1/lib -lintl -L/nix/store/8ipd3lbwabrxwbm1f7sxi8xj54sn0a44-pth-2.0.7/lib -lintl -L/nix/store/6ilwnfqar3k2vx9baazzagbfh25xgcfg-gpgme-1.11.1/lib -lintl -lintl

I don't understand why the setup hook is being run so many times, but it is definitely not good. Is there a way to ensure addEnvHooks is only run once? Otherwise we may need to add a flag like HAS_ADDED_LINTL.

Current packages adding ldflags:

  • libiconv
  • gettext (libintl)
  • musl-fts
  • netbsd.compat

/cc @Ericson2314 @dtzWill

Activity

  1. knedlsepp commented on May 6, 2018

    @knedlsepp
    Member

    There's a paragraph in the docs that I personally read as: While hooks are convenient, they are difficult to do right. There are however no suggestions on how to achieve idempotency in hooks.

    nixpkgs/doc/stdenv.xml

    Lines 1965 to 1978 in eca5c99

    The Setup hook mechanism is a bit of a sledgehammer though: a powerful
    feature with a broad and indiscriminate area of effect. The combination of
    its power and implicit use may be expedient, but isn't without costs. Nix
    itself is unchanged, but the spirit of adding dependencies being effect-free
    is violated even if the letter isn't. For example, if a derivation path is
    mentioned more than once, Nix itself doesn't care and simply makes sure the
    dependency derivation is already built just the same—depending is just
    needing something to exist, and needing is idempotent. However, a dependency
    specified twice will have its setup hook run twice, and that could easily
    change the build environment (though a well-written setup hook will
    therefore strive to be idempotent so this is in fact not observable). More
    broadly, setup hooks are anti-modular in that multiple dependencies, whether
    the same or different, should not interfere and yet their setup hooks may
    well do so.

  2. matthewbauer commented on May 7, 2018

    @matthewbauer
    MemberAuthor

    So, the "theory" behind this is to normalize different C standard libraries. Glibc provides a ton of nonstandard symbols bundled into libc (libiconv & libintl stuff). Since most of our packages assume they are running Glibc/Linux, they will assume those symbols will be provided by libc & not pass the needed flags. Ideally, we could "strip" those out of Glibc, but this can get complicated & the long legacy of Glibc in free software means we will break lots of otherwise good software. So instead, we provide the -l flag automatically when we know the libc will not provide it through the setup hook.

    I really want to figure out why there are so many NIX_LDFLAGS. My understanding is the setup hook should only be run once for each time it is listed as a dependency (also another for each package that propagates it). This is probably effecting anything that uses addEnvHooks & appends to an export variable.

  3. Ericson2314 commented on May 7, 2018

    @Ericson2314
    Member

    This is a consequence of the native back compat hack that I did in 469fd89 which runs setup hooks too many times to ensure improperly categorized dependencies still get processed. It would be nice to remove this altogether.

    469fd89 is indeed problematic, but I think the next post has the real culprit.

  4. Ericson2314 commented on May 7, 2018

    @Ericson2314
    Member

    @matthewbauer also some of the environment hooks should instead be setup hooks. I fix this in #40139.

  5. Ericson2314 commented on May 7, 2018

    @Ericson2314
    Member

    @knedlsepp indeed there is no way easy way to be idempotent. So I hope my PR is sufficient.

    [BTW https://nixos.org/nixos/nix-pills/basic-dependencies-and-hooks.html which I wrote goes into the hooks more slowly than the manual. I'm curious whether it is more readable.]

  6. Ericson2314 commented on May 7, 2018

    @Ericson2314
    Member

    #40139 should be fixed on staging.

  7. knedlsepp commented on May 7, 2018

    @knedlsepp
    Member

    Interesting read. I once read through the nix-pills before they were hosted on nixos.org. Didn't know they were still being updated.
    One question about the setup hooks:
    As far as I understood we get some dependencies multiple times, because of the way the dependency-graph is traversed. What is the reason we cannot simply:

    • collect all inputs
    • sort | uniq the inputs
    • run all the nix-support/setup-hook files in that order?
  8. Ericson2314 commented on May 7, 2018

    @Ericson2314
    Member

    setup hooks are 1 once per "role"; calls with the same role are indeed deduplicated already. This is because using using, e.g. the C compiler, as a nativeBuildInput vs depsBuildBuild is a fundamentally different thing; the setup hook is called with a different dynamic scope. Reverting 469fd89 will make this OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions