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

haskell-nix.extraPkgconfigMappings documentation #1670

Conversation

ramirez7
Copy link
Contributor

Following up on #1667

  • Added an example of when and how to use this feature to docs/tutorials/pkg-map.md.
  • Got rid of my TODO Nix comment and put a real one in.

@@ -11,7 +11,7 @@ final: prev: {
# overlays.
defaultModules = [];

# TODO: doc etc
# Additional user-provided mappings to augment ./../lib/pkgconf-nixpkgs-map.nix
extraPkgconfigMappings = {};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

re: @L-as

This should probably be part of the project-common module.

What did you mean by this? I'm not familiar with the whole structure of haskell.nix, but would that also make it easy to set these options globally?

Copy link
Contributor

Choose a reason for hiding this comment

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

@ramirez7 Sorry for the late response, yeah, essentially you add an option for it in modules/project-common.nix, then it's in the result of evaluating the module (see cabalProject') .

Copy link
Contributor

Choose a reason for hiding this comment

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

    extraPkgconfigMappings = mkOption {
      type = submodule [ fill this out ];
      default = {};
      description = ''
        Extra pkgconfig mappings
      '';
    };

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm I'll try this in a branch and get a good comparison going

@@ -29,6 +29,23 @@ nixpkgs.overlays = [
];
```

The user can map package(s) in Nixpkgs to a `pkgconfig-depends` name by
overlaying the `haskell-nix.extraPkgconfigMappings` attribute:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

re: @yvan-sraka

Should we document that directly on the https://input-output-hk.github.io/haskell.nix/ manual?

I added this to the "Nixpkgs overlay" section of this tutorial since it also uses overlays. I guess it's more specifically a haskell-nix overlay, but still 🤔

nixpkgs.overlays = [
(self: super: {
haskell-nix = super.haskell-nix // {
extraPkgconfigMappings = super.haskell-nix.extraPkgconfigMappings // {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You have to take care to not clobber other overlays that add mappings.

I have some helpers in my Nix code since this gets tedious:

{
    # See https://stackoverflow.com/a/54505212
    recursiveMerge = x: y: recursiveMergeAll [x y];
    recursiveMergeAll = attrList:
      with super.lib;
      let f = attrPath:
            zipAttrsWith (n: values:
              if tail values == []
              then head values
              else if all isList values
              then unique (concatLists values)
              else if all isAttrs values
              then f (attrPath ++ [n]) values
              else last values
            );
      in f [] attrList;

    mapExtraPkgconfig = super-haskell-nix: name: mappings: recursiveMerge super-haskell-nix {
      extraPkgconfigMappings = { "${name}" = mappings; };
    };
}

So my extraPkgconfigMappings overlays end up looking like this:

self: super:
{
  haskell-nix = super.macaroniLib.mapExtraPkgconfig super.haskell-nix "freeglut" ["freeglut"];
}

@exarkun
Copy link

exarkun commented Sep 15, 2022

Thanks for these docs, @ramirez7 .

I tried to use them to update my Haskell.nix-based packaging to current master@HEAD (05c2012) but I didn't succeed.

Here's my attempt - PrivateStorageio/PaymentServer@0ea0037

Note I couldn't follow the example in these docs extremely closely because I am setting up overlays different (I don't have a place to put nixpkgs.overlays = ... in the current structure). Perhaps that's related to the failure.

@ramirez7
Copy link
Contributor Author

I actually do my overlays like you do (as an arg to the nixpkgs import).

The problem you ran into was subtle: Overlays are applied left-to-right, so your overlay was being applied before the haskell.nix overlay, which is what provides the haskell-nix attribute. So it was clobbering your extraPkgconfigMappings. If you switch the concat order, nix-shell no longer fails during planning on pkgconfig errors at least.

diff --git a/nix/default.nix b/nix/default.nix
index cafdd6c..b0d5d39 100644
--- a/nix/default.nix
+++ b/nix/default.nix
@@ -14,7 +14,7 @@ let
 
   # Haskell.nix is delivery as an overlay.  Add our own overlay, which
   # provides one of our crypto dependencies, in a non-destructive way.
-  allOverlays = moreOverlays ++ haskellNix.nixpkgsArgs.overlays;
+  allOverlays = haskellNix.nixpkgsArgs.overlays ++ moreOverlays;
 
   # Import nixpkgs and pass the haskell.nix provided nixpkgsArgs
   pkgs = import

@exarkun
Copy link

exarkun commented Sep 15, 2022

Yep, that does it, thanks! I was aware of the possibility of overlays clobbering each other but I overlooked the fact that this applies even to my single overlay, since I'm combining it with Haskell.nix's overlay. I'm not sure if the docs could do a better job keeping people away from this pitfall... overlays seem a bit tricky to get right in Nix in general, not just with Haskell.nix. The only thing that comes to mind is providing the feature as more of a "regular" argument somewhere, maybe to pkgs.haskell-nix.project (but I'm only familiar with a couple small corners of Haskell.nix so I dunno if that's really better or not, and also I recognize this is a documentation PR not the implementation PR).

Thanks again.

@Ashe
Copy link

Ashe commented Oct 23, 2022

Sorry for the comment, but is there a chance you could add context to where nixpkgs.overlays can be placed? I'm following the flakes guide and there's a section on scaffolding which seems to just replace the template's flake.nix file, and I have no idea how to fix my error:

error: The Nixpkgs package set does not contain the package: c (system dependency).
       You may need to augment the system package mapping in haskell.nix so that it can be found.

It seems like this documentation might help, but would love to see an example of how exactly to do this and where nixpkgs.overlays should go in context of the scaffolding. I've tried in the let block, in the modules block, in the outputs but since I'm a bit of a nix-newbie I need a bit of handholding with this, so perhaps other people might appreciate it too? Thanks

@ramirez7
Copy link
Contributor Author

@Ashe I'm actually not sure where this nixpkgs.overlays thing would go either - I agree those docs could be improved.

I knew what to do due to past Nix experience - the place to put overlays are in the nixpkgs import:

import haskellNix.sources.nixpkgs-unstable
  {
    config = haskellNix.nixpkgsArgs.config;
    overlays = haskellNix.nixpkgsArgs.overlays ++ [ overlay1 overlay2 overlay_etc ];
  }

And then your overlay would just have to define c as a top level attribute. I'm not sure exactly what that library is supposed to be, but I did run into something maybe similar with m when x-compiling hgeometry and I just set it to null to paper over it until I sent haskell.nix a PR to handle the mapping.

self: super:
{
  c = null;
}

So in full you could do this I think:

import haskellNix.sources.nixpkgs-unstable
  {
    config = haskellNix.nixpkgsArgs.config;
    overlays = haskellNix.nixpkgsArgs.overlays ++ [ (self: super: { c = null; }) ];
  }

(assuming mapping c to null is correct and it isn't some actual library that needs packaging)

@Ashe
Copy link

Ashe commented Oct 24, 2022

Thank you @ramirez7 , I appreciate it ! I just thought I'd give my perspective as someone who's probably over-reliant on documentation :)

@hamishmack
Copy link
Collaborator

bors try

iohk-bors bot added a commit that referenced this pull request Oct 24, 2022
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Oct 24, 2022

@hamishmack hamishmack merged commit d54e282 into input-output-hk:master Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants