summaryrefslogtreecommitdiff
path: root/Com/Simatime/buildHaskellApp.nix
blob: c7bdd1fc9d72e3fec9d17113393b70063fe36fec (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

nixpkgs:

{  name  # the namespace
,  nick  # a short name, for the executable
,  deps  # deps get passed to the compilers
}:

with nixpkgs;

let
  nsToPath = ns: builtins.toString (builtins.replaceStrings ["."] ["/"] ns);
  pathToNs = p: builtins.replaceStrings ["/"] ["."] p;
  basePath = nsToPath name;
  apexPath = basePath + "/Apex"; # compiled with ghc
  aeroPath = basePath + "/Aero"; # compiled with ghcjs

  depsToPackageSet = packageSet: deps:
    map (s: builtins.getAttr s packageSet) deps;

  claySrc = pkgs.fetchFromGitHub {
    owner = "sebastiaanvisser";
    repo = "clay";
    rev = "cc7729b1b42a79e261091ff7835f7fc2a7ae3cee";
    sha256 = "1vd67976lvi5l4qq18zy6j44apynkl44ps04p8vwfx4gzr895dyp";
  };

  ghc865_ = pkgs.haskell.packages.ghc865.override (oldAttrs: {
    overrides = with pkgs.haskell.lib; self: super: {
      clay = self.callCabal2nix "clay" claySrc {};
      wai-middleware-metrics = dontCheck super.wai-middleware-metrics;
    };
  });

  ghc = ghc865_.ghcWithPackages (hp: depsToPackageSet hp
    (deps.both ++ deps.apex));

  # ghcjs-8.6.0.1
  ghcjs_ = pkgs.haskell.packages.ghcjs.override (oldAttrs: {
    overrides = with pkgs.haskell.lib; self: super: {
      clay = dontCheck (self.callCabal2nix "clay" claySrc {});
      http-types = dontCheck super.http-types;
      tasty-quickcheck = dontCheck super.tasty-quickcheck;
      scientific = dontCheck super.scientific; # takes forever
      servant = dontCheck super.servant;
      comonad = dontCheck super.comonad;
      QuickCheck = dontCheck super.QuickCheck;
    };
  });

  ghcjs = ghcjs_.ghcWithPackages (hp:
    depsToPackageSet hp (deps.both ++ deps.aero));

in {
  inherit ghc ghcjs;
  app = stdenv.mkDerivation {
    name = name;
    version = "0";
    src = ../../.; # this is the git root
    nativeBuildInputs = [
      ghc ghcjs guile
    ];
    strictDeps = true;
    buildPhase = ''
      #
      mkdir -p $out/{bin,static} ${basePath}
      #
      # compile with ghc
      #
      ${ghc}/bin/ghc -Werror -i. \
          --make ${apexPath}.hs \
          -main-is ${pathToNs apexPath} \
          -o $out/bin/${nick}
      #
      # compile with ghcjs
      #
      ${ghcjs}/bin/ghcjs -Werror -i. \
          --make ${aeroPath}.hs \
          -main-is ${pathToNs aeroPath} \
          -o ${aeroPath}
      #
      # optimize js output
      #
      ${pkgs.closurecompiler}/bin/closure-compiler \
          ${aeroPath}.jsexe/all.js > $out/static/${nick}.js
    '';
    # the install process was handled above
    installPhase = "exit 0";
  };
}