summaryrefslogtreecommitdiff
path: root/biz.nix
diff options
context:
space:
mode:
authorBen Sima <ben@bsima.me>2020-04-12 21:02:33 -0700
committerBen Sima <ben@bsima.me>2020-04-12 21:02:33 -0700
commit4d7314f3afaa7f2395cd9a6f7c96d17f27589870 (patch)
tree93df9da126cc56e123cb08572e6a4e8b68869df5 /biz.nix
parentd344d73892b7eebc6c4dca26904df0695a0ee6c3 (diff)
Refactor biz.nix to dry up code
Diffstat (limited to 'biz.nix')
-rw-r--r--biz.nix78
1 files changed, 42 insertions, 36 deletions
diff --git a/biz.nix b/biz.nix
index b6e59ce..6caadfd 100644
--- a/biz.nix
+++ b/biz.nix
@@ -19,25 +19,43 @@ let
(x: builtins.elem x b) a;
allDeps = import ./deps.nix;
+
+ # 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 ': exe' declaration
+ exe = builtins.head (lists.flatten (removeNull
+ (map (builtins.match "^-- : exe ([[:alnum:]._-]*)$")
+ (lines content))));
+ # collect all of the ': dep' declarations
+ deps = lists.flatten (removeNull
+ (map (builtins.match "^-- : dep ([[:alnum:]._-]*)$")
+ (lines content)));
+ };
+
+ mkGhc = compiler: (deps: compiler (hp:
+ if (subset deps allDeps)
+ then depsToPackageSet hp deps
+ else throw ''
+ missing from deps.nix:
+ ${toString (lib.lists.subtractLists allDeps deps)}
+ ''));
+
+ ghc_ = mkGhc pkgs.haskell.packages.ghc865.ghcWithHoogle;
+ ghcjs_ = mkGhc pkgs.haskell.packages.ghcjs.ghcWithPackages;
in {
buildGhc = main:
let
- relpath = builtins.replaceStrings ["${root}/"] [""] (builtins.toString main);
- module = builtins.replaceStrings ["/" ".hs"] ["." ""] relpath;
- content = builtins.readFile main;
- exe = builtins.head (lists.flatten (removeNull
- (map (builtins.match "^-- : exe ([[:alnum:]._-]*)$")
- (lines content))));
- deps = lists.flatten (removeNull
- (map (builtins.match "^-- : dep ([[:alnum:]._-]*)$")
- (lines content)));
- ghc = pkgs.haskell.packages.ghc865.ghcWithHoogle (hp:
- if (subset deps allDeps)
- then depsToPackageSet hp deps
- else throw
- "missing from deps.nix: ${toString (lib.lists.subtractLists allDeps deps)}");
+ data = analyze main;
+ ghc = ghc_ data.deps;
in stdenv.mkDerivation {
- name = module;
+ name = data.module;
src = ./.;
nativeBuildInputs = [ ghc ];
strictDeps = true;
@@ -46,8 +64,8 @@ in {
# compile with ghc
${ghc}/bin/ghc -Werror -i. \
--make ${main} \
- -main-is ${module} \
- -o $out/bin/${exe}
+ -main-is ${data.module} \
+ -o $out/bin/${data.exe}
'';
# the install process was handled above
installPhase = "exit 0";
@@ -55,22 +73,10 @@ in {
buildGhcjs = main:
let
- relpath = builtins.replaceStrings ["${root}/"] [""] (builtins.toString main);
- module = builtins.replaceStrings ["/" ".hs"] ["." ""] relpath;
- content = builtins.readFile main;
- exe = builtins.head (lists.flatten (removeNull
- (map (builtins.match "^-- : exe ([[:alnum:]._-]*)$")
- (lines content))));
- deps = lists.flatten (removeNull
- (map (builtins.match "^-- : dep ([[:alnum:]._-]*)$")
- (lines content)));
- ghcjs = pkgs.haskell.packages.ghcjs.ghcWithPackages (hp:
- if (subset deps allDeps)
- then depsToPackageSet hp deps
- else throw
- "missing from deps.nix: ${toString (lib.lists.subtractLists allDeps deps)}");
+ data = analyze main;
+ ghcjs = ghcjs_ data.deps;
in stdenv.mkDerivation {
- name = module;
+ name = data.module;
src = ./.;
nativeBuildInputs = [ ghcjs ];
strictDeps = true;
@@ -79,14 +85,14 @@ in {
# compile with ghcjs
${ghcjs}/bin/ghcjs -Werror -i. \
--make ${main} \
- -main-is ${module} \
- -o ${exe}
+ -main-is ${data.module} \
+ -o ${data.exe}
# optimize js output
${pkgs.closurecompiler}/bin/closure-compiler \
- ${exe}/all.js > $out/static/${exe}
+ ${data.exe}/all.js > $out/static/${data.exe}
'';
installPhase = "exit 0";
} // { env = ghcjs; };
- globalGhc = pkgs.haskell.packages.ghc865.ghcWithHoogle (hp: depsToPackageSet hp allDeps);
+ globalGhc = ghc_ allDeps;
}