summaryrefslogtreecommitdiffstats
path: root/pkgs/haskell/default.nix
blob: 73c917e4e7d99707bed0124c6108caca769f71a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
self: super: let
  inherit (super) lib mylib;

  # Haskell projects in the development environment, i.e. directories in
  # ~/sync/src that contain <pname>.cabal.  They take precedence over
  # pkgs/haskell and nixpkgs, and can depend on each other.  On hosts without
  # ~/sync/src, there are none.
  automaticPackages =
    let
      root = /home/tv/sync/src;
    in
    lib.optionalAttrs (builtins.pathExists root)
      (lib.mapAttrs (pname: _type: root + "/${pname}")
        (lib.filterAttrs
          (pname: type: type == "directory" && builtins.pathExists (root + "/${pname}/${pname}.cabal"))
          (builtins.readDir root)));

  # Copies a Haskell project into the store, leaving out what the build
  # doesn't need.
  cleanHaskellSource = name: src: mylib.copyWithout {
    inherit name src;
    removeNames = [ ".git" ".graveyard" "dist-newstyle" ];
  };

  # An automatic package, from the checkout's cabal.nix if it has one,
  # else from cabal2nix run during evaluation.  cabal.nix starts with
  # the sha256 of the .cabal file it was made from, and fails evaluation
  # when that changed.
  automaticPackage = hself: pname: dir: let
    src = cleanHaskellSource pname dir;
    stored = dir + "/cabal.nix";
    header = "# cabal-sha256: ${builtins.hashFile "sha256" (dir + "/${pname}.cabal")}";
    regenerate = ''{ printf '%s\n' '${header}'; cabal2nix .; } > cabal.nix'';
  in
    if !builtins.pathExists stored then
      hself.callCabal2nix pname src {}
    else if builtins.head (lib.splitString "\n" (builtins.readFile stored)) != header then
      throw "pkgs/haskell: ${toString stored} is stale against ${pname}.cabal; in ${toString dir}, run: ${regenerate}"
    else
      self.haskell.lib.overrideSrc (hself.callPackage stored {}) { inherit src; };
in {
  # nixpkgs applies packageOverrides to every Haskell package set, including
  # haskellPackages.
  haskell = super.haskell // {
    packageOverrides = lib.composeExtensions super.haskell.packageOverrides
      (hself: hsuper:
        mylib.mapNixDir (path: hself.callPackage path {}) [ ./. ]
        //
        lib.mapAttrs (automaticPackage hself) automaticPackages);
  };
}