on e94de40 help sign in
haskell.nix/lib/call-stack-to-nix.nix
79 lines 3.1 kB history raw
1/* The function obtained when this is applied to a package set calls
2 * the stack-to-nix tool on a supplied source set and then
3 * imports the resulting pkgs.nix. The application of this function
4 * to a source path can thus be used directly as the input to mkStackPackageSet
5 *
6 * see also `call-cabal-project-to-nix`!
7 */
8{ runCommand, pkgs, mkCacheFile, materialize, haskellLib }:
9{ name ? src.name or null # optional name for better error messages
10, src
11, stackYaml ? "stack.yaml"
12, ignorePackageYaml ? false
13, cache ? null
14, stack-sha256 ? null
15, resolverSha256 ? null
16, materialized ? null # Location of a materialized copy of the nix files
17, checkMaterialization ? null # If true the nix files will be generated used to check plan-sha256 and material
18, nix-tools ? pkgs.haskell-nix.internal-nix-tools # When building stack projects we use the internal nix-tools (compiled with a fixed GHC version)
19, ... }:
20let
21 inherit (haskellLib.fetchResolver {
22 inherit src stackYaml resolverSha256;
23 }) resolver fetchedResolver;
25 subDir' = src.origSubDir or "";
26 stackToNixArgs = builtins.concatStringsSep " " [
27 "--full"
28 "--stack-yaml=$SRC/${stackYaml}"
29 (if ignorePackageYaml then "--ignore-package-yaml" else "")
30 "-o ."
31 ];
32 stack = materialize ({
33 inherit materialized;
34 sha256 = stack-sha256;
35 sha256Arg = "stack-sha256";
36 reasonNotSafe = null;
37 } // pkgs.lib.optionalAttrs (checkMaterialization != null) {
38 inherit checkMaterialization;
39 }) (runCommand (if name == null then "stack-to-nix-pkgs" else name + "-stack-to-nix-pkgs") {
40 nativeBuildInputs = [ nix-tools pkgs.nix-prefetch-git pkgs.cacert pkgs.xorg.lndir ];
41 # Needed or stack-to-nix will die on unicode inputs
42 LOCALE_ARCHIVE = pkgs.lib.optionalString (pkgs.stdenv.hostPlatform.libc == "glibc") "${pkgs.glibcLocales}/lib/locale/locale-archive";
43 LANG = "en_US.UTF-8";
44 LC_ALL = "en_US.UTF-8";
45 preferLocalBuild = false;
46 } (''
47 mkdir -p $out${subDir'}
48 ${
49 # If no resolver was fetched use the original stack.yaml
50 if fetchedResolver == null
51 then ''
52 SRC=${src}
53 ''
54 else
55 # Replace the resolver path in the stack.yaml with the fetched version
56 ''
57 SRC=$(mktemp -d)
58 cd $SRC
59 lndir -silent "${src}/." $SRC
60 rm ${stackYaml}
61 cp ${src}/${stackYaml} .
62 chmod +w ${stackYaml}
63 substituteInPlace ${stackYaml} --replace "${resolver}" "${fetchedResolver}"
64 ''}
65 ${pkgs.lib.optionalString (cache != null) ''
66 cp ${mkCacheFile cache}/.stack-to-nix.cache* $out${subDir'}
67 ''}
68 (cd $out${subDir'} && stack-to-nix ${stackToNixArgs})
70 # We need to strip out any references to $src, as those won't
71 # be accessable in restricted mode.
72 for nixf in $(find $out -name "*.nix" -type f); do
73 substituteInPlace $nixf --replace "$SRC" "."
74 done
76 # move pkgs.nix to default.nix ensure we can just nix `import` the result.
77 mv $out${subDir'}/pkgs.nix $out${subDir'}/default.nix
78 ''));
79in { projectNix = stack; inherit src; sourceRepos = []; }