{ nixpkgs ? import ./Bild/Nixpkgs.nix }:


let
  inherit (nixpkgs) lib stdenv;
  ghcCompiler = "ghc884";
  ghcjsCompiler = "ghcjs86";

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

  # general functions to put in a lib
  lines = s: lib.pipe s [
    (builtins.split "\n")
    (builtins.filter (x: builtins.typeOf x == "string"))
  ];
  removeNull = ls: builtins.filter (x: x != null) ls;

  selectAttrs = deps: packageSet:
    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;

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

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

in rec {
  # gather data needed for compiling by analyzing the main module
  analyze = main: rec {
    # path to the module relative to the git root
    relpath = builtins.replaceStrings ["${root}/"] [""]
        (builtins.toString main);
    # Haskell-appropriate name of the module
    module = builtins.replaceStrings ["/" ".hs"] ["." ""] relpath;
    # file contents
    content = builtins.readFile main;
    # search for the ': out' declaration
    out = lib.pipe content [
      lines
      (map (builtins.match "^-- : out ([[:alnum:]._-]*)$"))
      removeNull
      lib.lists.flatten
      builtins.head
    ];
    # collect all of the ': dep' declarations
    deps = lib.pipe content [
      lines
      (map (builtins.match "^-- : dep ([[:alnum:]._-]*)$"))
      removeNull
      lib.lists.flatten
    ];
    # collect ': sys' declarations
    sysdeps = lib.pipe content [
      lines
      (map (builtins.match "^-- : sys ([[:alnum:]._-]*)$"))
      removeNull
      lib.lists.flatten
    ];
  };

  ghcPackageSetFull = mkGhcPackageSet haskellDeps;

  ghc = main:
    let
      data = analyze main;
      ghc = mkGhcPackageSet (hp: selectAttrs data.deps hp);
    in stdenv.mkDerivation {
      name = data.module;
      src = ../.;
      nativeBuildInputs = [ ghc ] ++ selectAttrs data.sysdeps nixpkgs.pkgs;
      strictDeps = true;
      buildPhase = ''
        mkdir -p $out/bin
        # compile with ghc
        ${ghc}/bin/ghc \
          -Werror \
          -i. \
          --make ${main} \
          -main-is ${data.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;

  sources = nixpkgs.sources;

  pkgs = nixpkgs.pkgs;
}