summaryrefslogtreecommitdiff
path: root/Biz/Bild.nix
blob: 3cd502653739a430d14dba74f9dd086de6b91504 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
{ nixpkgs ? import ./Bild/Nixpkgs.nix }:


let
  ghcCompiler = "ghc884";
  ghcjsCompiler = "ghcjs86";

  # provided by .envrc
  root = builtins.getEnv "BIZ_ROOT";

  selectAttrs = deps: packageSet:
    nixpkgs.lib.attrsets.attrVals deps packageSet;

  # returns true if a is a subset of b, where a and b are attrsets
  subset = a: b: builtins.all
    (x: builtins.elem x b) a;

  # 44 = lib.strings.stringLength "/nix/store/gia2r9mxhc900y1m97dlmr1g3rm3ich3-"
  dropNixStore = s: nixpkgs.lib.strings.substring 44 (nixpkgs.lib.strings.stringLength s) s;

  haskellDeps = hpkgs: import ./Bild/Deps/Haskell.nix hpkgs;

  mkGhcPackageSet = nixpkgs.haskell.packages.${ghcCompiler}.ghcWithHoogle;
  #mkGhcjsPackageSet = nixpkgs.haskell.packages.${ghcjsCompiler}.ghcWithPackages;

in rec {
  inherit (nixpkgs) lib stdenv pkgs sources;

  # a standard nix build for `bild` - this should be the only hand-written
  # builder we need
  bild = stdenv.mkDerivation {
    name = "bild";
    src = ../.;
    nativeBuildInputs = [ ghcPackageSetFull ];
    buildInputs = [ ghcPackageSetFull nixpkgs.makeWrapper ];
    propagatedBuildInputs = [ ghcPackageSetFull ];
    strictDeps = true;
    buildPhase = ''
      mkdir -p $out/bin
      ghc \
        -Werror \
        -i. \
        --make Biz/Bild.hs \
        -main-is Biz.Bild \
        -o $out/bin/bild
    '';
    installPhase = ''
      wrapProgram $out/bin/bild --prefix PATH : ${lib.makeBinPath [ ghcPackageSetFull ]}
    '';
  };

  # wrapper around bild
  runBildAnalyze = main: stdenv.mkDerivation {
    name = "bild-analysis";
    src = ../.;
    USER = "nixbld";
    HOSTNAME = "nix-sandbox";
    BIZ_ROOT = "$src";
    buildPhase = ''
      set -eux
      mkdir $out
      : analyzing with bild
      ${bild}/bin/bild --analyze ${main} 1> $out/analysis.json 2> $out/stderr
      set +eux
    '';
    installPhase = "exit 0";
  };

  # gather data needed for compiling by analyzing the main module
  analyze = main:
    builtins.head
      (lib.trivial.importJSON
        (runBildAnalyze main + "/analysis.json"));

  ghcPackageSetFull = mkGhcPackageSet haskellDeps;

  ghc = main:
    let
      data = analyze main;
      ghc = mkGhcPackageSet (hp: selectAttrs data.langdeps hp);
      module = lib.strings.concatStringsSep "." data.namespace.path;
    in stdenv.mkDerivation {
      name = module;
      src = ../.;
      nativeBuildInputs = [ ghc ] ++ selectAttrs data.sysdeps nixpkgs.pkgs;
      strictDeps = true;
      buildPhase = ''
        set -eux
        mkdir -p $out/bin
        : compiling with ghc
        ${ghc}/bin/ghc \
          -Werror \
          -i. \
          --make ${main} \
          -main-is ${module} \
          -o $out/bin/${data.out}
      '';
      # the install process was handled above
      installPhase = "exit 0";
    } // { env = ghc; };

  #ghcjs = main:
  #  let
  #    data = analyze main;
  #    ghcjs = mkGhcjsPackageSet (hp: selectAttrs data.deps hp);
  #  in stdenv.mkDerivation {
  #    name = data.module;
  #    src = ../.;
  #    nativeBuildInputs = [ ghcjs ];
  #    strictDeps = true;
  #    buildPhase = ''
  #      mkdir -p $out/static
  #      # compile with ghcjs
  #      ${ghcjs}/bin/ghcjs \
  #        -Werror \
  #        -i. \
  #        --make ${main} \
  #        -main-is ${data.module} \
  #        -o ${data.out}
  #      # optimize js output
  #      ${nixpkgs.pkgs.closurecompiler}/bin/closure-compiler \
  #        ${data.out}/all.js > $out/static/${data.out}
  #    '';
  #    installPhase = "exit 0";
  #  } // { env = ghcjs; };

  env = pkgs.mkShell {
    name = "bizdev";
    buildInputs = with nixpkgs.pkgs; [
      # haskell deps
      (mkGhcPackageSet haskellDeps)
      # ghcjs doesn't need everything, and many things fail to build
      #(mkGhcjsPackageSet (hp: with hp; [
      #  aeson
      #  clay
      #  containers
      #  miso
      #  protolude
      #  servant
      #  split
      #  text
      #  ghcjs-base
      #]))

      # python deps
      (python38.withPackages (p:
        [ p.black p.pylint ]))

      # tools
      haskell.packages.${ghcCompiler}.apply-refact
      cmark
      figlet
      haskell.packages.${ghcCompiler}.fast-tags
      hlint
      lolcat
      niv.niv
      nixops
      ormolu
      python37Packages.black
      python37Packages.pylint
      shellcheck
      wemux
      gmnisrv
      gmni
    ] ++ lib.optional nixpkgs.stdenv.isLinux [
      # scheme deps (i think these are broken on macOS)
      guile
      #inspekt3d
      #libfive

      ccze
    ];
    shellHook = ". ${./Bild/ShellHook.sh}";
  };

  os = cfg: (nixpkgs.nixos (args: cfg)).toplevel;
}