Gabriella439 / haskell-nix

Nix and Haskell in production
1.14k stars 117 forks source link

Talk about how to use GHC-specific tools (e.g. ghc-mod) #30

Open adelbertc opened 6 years ago

adelbertc commented 6 years ago

Tools like ghc-mod, as far as I understand, need to be built with the same version of GHC as the project you intend to use it in. Curious how you handle it with the setup described in this tutorial.

Gabriella439 commented 6 years ago

I think this is as simple as adding ghc-mod to your record of project build products and then installing it

For example, if you had a project like this one from the tutorial:

let
  config = {
    packageOverrides = pkgs: rec {
      haskellPackages = pkgs.haskellPackages.override {
        overrides = haskellPackagesNew: haskellPackagesOld: rec {
          optparse-applicative =
            haskellPackagesNew.callPackage ./optparse-applicative-2.nix { };

          project1 =
            haskellPackagesNew.callPackage ./default.nix { };

          turtle =
            haskellPackagesNew.callPackage ./turtle.nix { };
        };
      };
    };
  };

  pkgs = import <nixpkgs> { inherit config; };

in
  { project1 = pkgs.haskellPackages.project1;
  }

... then I think you can just add ghc-mod to the output:

let
  config = {
    packageOverrides = pkgs: rec {
      haskellPackages = pkgs.haskellPackages.override {
        overrides = haskellPackagesNew: haskellPackagesOld: rec {
          optparse-applicative =
            haskellPackagesNew.callPackage ./optparse-applicative-2.nix { };

          project1 =
            haskellPackagesNew.callPackage ./default.nix { };

          turtle =
            haskellPackagesNew.callPackage ./turtle.nix { };
        };
      };
    };
  };

  pkgs = import <nixpkgs> { inherit config; };

in
  { project1 = pkgs.haskellPackages.project1;
    ghc-mod  = pkgs.haskellPackages.ghc-mod;
  }

... and then you can install the ghc-mod provided by your project by running:

$ nix-env --install --attr ghc-mod --file release.nix

@adelbertc: Could you try that out and let me know if it works for you? If it does, then I can add this to the tutorial

adelbertc commented 6 years ago

Hmm wouldn't that globally install ghc-mod? For something like ghc-mod where the version of GHC it's compiled against matters, you'd want to do it on a per project basis right? At work we have it available once we drop into a project-specific shell.nix, curious how you would approach it.

Gabriella439 commented 6 years ago

The problem is that nix-shell doesn't support loading multiple derivations from a release.nix file

adelbertc commented 6 years ago

Right, so I guess if possible you would need a shell.nix that stays in sync with default.nix (ruling out cabal2nix --shell since the generated expression seems to largely duplicate default.nix) but also lets you add in tooling in buildDepends or buildTools or something?

alpmestan commented 6 years ago

Have you tried indeed simply adding ghc-mod to your buildDepends/buildTools? If not, you might want to verify that it allows you to have a working ghc-mod that's aware of your project. Then you can automate that by maybe overriding your project's derivation at the last minute, right before calling callPackage, by adding a buildDepends/buildTools entry. Anything that guarantees that ghc-mod will be built, with the same ghc version as everything else in the project, and that it ends up in your PATH.