Skip to content

Instantly share code, notes, and snippets.

@Lucus16
Created October 30, 2023 16:21
Show Gist options
  • Select an option

  • Save Lucus16/e71e318b973eb46806a81f14580dc902 to your computer and use it in GitHub Desktop.

Select an option

Save Lucus16/e71e318b973eb46806a81f14580dc902 to your computer and use it in GitHub Desktop.
Experiment to build haskell in pure nix
{ pkgs ? import <nixpkgs> { } }:
with builtins;
rec {
inherit (pkgs) ghc lib;
uniqueStrings = strings:
attrNames (listToAttrs (map (name: {
inherit name;
value = null;
}) strings));
getImportedModules = path:
uniqueStrings (concatLists (filter isList (split ''
import +([A-Za-z.]+)'' (readFile path))));
buildModule = { src, path, deps ? [ ] }:
derivation {
name = baseNameOf path;
builder = pkgs.writeShellScript "ghc" ''
${pkgs.coreutils}/bin/mkdir -p "$out/$dir"
${pkgs.ghc}/bin/ghc -odir "$out" -ohi "$out/$path.hi" "$@"
'';
system = currentSystem;
dir = dirOf path;
inherit path;
args = [ (src + "/${path}.hs") "-c" "-O1" ]
++ map (dep: "-i ${dep}") deps;
};
init = buildModule {
src = splitmix/src;
path = "System/Random/SplitMix/Init";
};
compat = buildModule {
src = splitmix/src-compat;
path = "Data/Bits/Compat";
};
splitmix = buildModule {
src = splitmix/src;
path = "System/Random/SplitMix";
deps = [ compat init ];
};
montecarlo-pi-o = buildModule {
src = splitmix/tests;
path = "SplitMixPi";
deps = [ splitmix ];
};
time = rec {
pname = "time";
version = "1.12.2";
name = "${pname}-${version}";
aPath = "${ghc}/lib/${ghc.name}/${name}/libHS${name}.a";
};
montecarlo-pi = derivation {
name = "montecarlo-pi";
builder = pkgs.writeShellScript "ghc" ''
${pkgs.ghc}/bin/ghc -o "$out" "$@"
'';
system = currentSystem;
args = [
"${montecarlo-pi-o}/Main.o"
"${splitmix}/System/Random/SplitMix.o"
"${init}/System/Random/SplitMix/Init.o"
time.aPath
];
};
test = pkgs.runCommand "test-montecarlo-pi" { } ''
${montecarlo-pi}
touch $out
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment