summaryrefslogtreecommitdiff
path: root/Biz/Bild.nix
blob: 7a07f3658e7890e9cc190fcc9214a59b1ea7a8bd (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
{ nixpkgs ? import ./Bild/Nixpkgs.nix }:

let
  constants = import ./Bild/Constants.nix;
  lib = nixpkgs.lib;
  # put all of our stuff in the 'bild' namespace
in nixpkgs // {
  bild = rec {
    # provided by .envrc
    root = builtins.getEnv "CODEROOT";

    inherit (nixpkgs) sources;

    haskell = rec {
      inherit (constants) ghcCompiler;

      # all available packages
      deps = import ./Bild/Deps/Haskell.nix;
      packages = lib.attrsets.getAttrs deps nixpkgs.haskellPackages;

      # make a ghc with dependencies
      ghcWith = nixpkgs.haskell.packages.${ghcCompiler}.ghcWithHoogle;

      # ghc with all packages, used for generating bild's package database
      ghcPackageSetFull = ghcWith (p: lib.attrsets.attrVals deps p);

      # bild's dependencies, needs to be hand-written
      ghcPackageSetBild = ghcWith (hpkgs:
        with hpkgs; [
          aeson
          async
          base
          bytestring
          conduit
          conduit-extra
          containers
          directory
          docopt
          filepath
          process
          protolude
          rainbow
          regex-applicative
          split
          tasty
          tasty-hunit
          tasty-quickcheck
          text
          hostname
          wai # can remove when removed from Biz.Log
        ]);
    };

    lisp = { sbclWith = nixpkgs.lispPackages_new.sbclWithPackages; };

    python = {
      packages = nixpkgs.python3Packages;
      pythonWith = nixpkgs.python3.withPackages;
      buildPythonApplication = nixpkgs.python3.pkgs.buildPythonApplication;
    };

    # c packages are just nixpkgs, filtered to just the list of deps i want
    c.packages = lib.attrsets.getAttrs (import ./Bild/Deps/C.nix) nixpkgs.pkgs;

    # expose some packages for inclusion in os/image builds
    pkgs = with nixpkgs.pkgs; {
      inherit deadnix git hlint indent ormolu shellcheck nixfmt mypy pkg-config;
      ruff = unstable.ruff;
    };

    # a standard nix build for bild, for bootstrapping. this should be the only
    # hand-written builder we need
    bild = nixpkgs.stdenv.mkDerivation {
      name = "bild";
      src = ../.;
      nativeBuildInputs = [ haskell.ghcPackageSetBild ];
      buildInputs = [ nixpkgs.makeWrapper ];
      propagatedBuildInputs = with nixpkgs; [
        pkg-config
        git
        # this is just to get access to ghc-pkg in bild
        (haskell.ghcWith (_: [ ]))

        # lisp deps, remove this when i implement nix builds for lisp
        guile
        (lisp.sbclWith
          (p: with p; [ asdf alexandria ])) # just enough to build Example.lisp
      ];
      strictDeps = true;
      buildPhase = ''
        mkdir -p $out/bin $out/lib/ghc-${haskell.ghcPackageSetFull.version}
        cp -r \
          ${haskell.ghcPackageSetFull}/lib/ghc-${haskell.ghcPackageSetFull.version}/package.conf.d \
          $out/lib/ghc-${haskell.ghcPackageSetFull.version}
        ghc \
          -threaded \
          -Werror \
          -i. \
          --make Biz/Bild.hs \
          -main-is Biz.Bild \
          -o $out/bin/bild
      '';
      installPhase = ''
        wrapProgram $out/bin/bild \
          --prefix PATH : ${
            lib.makeBinPath [ haskell.ghcPackageSetBild pkgs.git ]
          } \
          --set GHC_PACKAGE_PATH \
          $out/lib/ghc-${haskell.ghcPackageSetFull.version}/package.conf.d
      '';
    };

    # wrapper around bild
    runBildAnalyze = target:
      nixpkgs.stdenv.mkDerivation rec {
        name = "bild-analysis";
        src = ../.;
        USER = "nixbld";
        HOSTNAME = "nix-sandbox";
        # we need to remove the $src root because bild expects paths relative to the
        # working directory:
        TARGET = "."
          + lib.strings.removePrefix (toString src) (toString target);
        buildPhase = ''
          export CODEROOT=$(pwd)
          mkdir $out
          ${bild}/bin/bild --plan "$TARGET" 1> $out/analysis.json \
            2> >(tee -a $out/stderr >&2)
        '';
        installPhase = "exit 0";
      };

    # gather data needed for compiling by analyzing the main module. returns the
    # json object of the build
    analyze = target:
      builtins.readFile (runBildAnalyze target + "/analysis.json");

    # this does a bild build for the given target, but entirely in nix. its kinda
    # like IFD, but not as costly, i think
    run = target: import ./Bild/Builder.nix { analysisJSON = analyze target; };

    # the main development environment
    env = nixpkgs.pkgs.mkShell {
      name = "bizdev";
      # this should just be dev tools
      buildInputs = with nixpkgs.pkgs; [
        bat
        bc
        bild
        ctags
        fd
        figlet
        fzf
        git
        git-branchless
        gitlint
        jq
        lolcat
        nixpkgs.haskell.packages.${constants.ghcCompiler}.fast-tags
        ormolu
        ripgrep
        tree
        wemux
      ];
      shellHook = ''
        export GHC_PACKAGE_PATH=${bild}/lib/ghc-${haskell.ghcPackageSetFull.version}/package.conf.d
      '';
    };

    # build an operating system. 'cfg' is the NixOS config
    os = cfg: (nixpkgs.nixos (_args: cfg)).toplevel;

    # build a docker image
    image = nixpkgs.pkgs.dockerTools.buildImage;
  };
}