haskell.nix/mk-local-hackage-repo/default.nix
Hamish Mackenzie a443611ecf
Add evalSystem and evalPackages project args (#1546)
This adds a way to specify the `evalSystem` or `evalPackages` explicitly when calling the `project` functions.

Currently if we want to make a `flake` that supports multiple systems we have few options:

* Require builders for all the supported systems (even just for `nix flake show`).

* Pass `--impure` so that haskell.nix can see `builtins.currentSystem` to set up `pkgs.evalPackages` to use that.  Unfortunately this prevents nix from caching some of the work it does and often results in it recalculating for each supported system when it would otherwise be cached and take no time at all.

* Add an overlay to replace `evalPackages`.  This works, but it is not straight forward.

* Materialize the nix files for the project.

This change allows `evalSystem = "x86_64-linux";` to be passed telling `haskell.nix` to run `cabal` and `nix-tools` on that system.  The user will have to have a builder for that system, but does not need to have builders for the others (unless building outputs for them).
2022-07-28 20:03:05 +12:00

56 lines
2.2 KiB
Nix

# Create a local hackage repo, we can use as a repository in a cabal config
#
# This will include:
# - 01-index.tar.gz (the index file)
# - root.json and
# - mirrors.json as metadata items.
# - snapshot.json that records the index, root and mirrors.
# - timestamp.json that will record the snapshot.json
#
# This is all part of The Update Framework (TUF) and the specific implementation
# cabal-install (via hackage-security) does of it.
#
# We will create a completely unsigned bare repository. Using signing keys within
# nix would be pointless as we'd have to hardcode them to produce the same output
# reproducibly.
#
pkgs:
{ name, index }:
pkgs.buildPackages.runCommand "hackage-repo-${name}" { preferLocalBuild = true; } ''
mkdir -p $out
export expires="4000-01-01T00:00:00Z"
ln -sf ${index} $out/01-index.tar.gz
export index_md5=$(md5sum ${index} | awk '{ print $1 }')
export index_sha256=$(sha256sum ${index} | awk '{ print $1 }')
${
# When possible check the hash we calculate here against the `outputHash`
# of the index derivation (when the `extra-hackages` feature is used the index
# may not have an outputHash).
pkgs.lib.optionalString (index ? outputHash) ''
if [[ "${index.outputHash}" != "$index_sha256" ]]; then
echo "ERROR See https://github.com/input-output-hk/haskell.nix/issues/884"
exit 1
fi
''}
export index_length=$(stat --printf="%s" ${index})
substituteAll ${./root.json} $out/root.json
export root_md5=$(md5sum $out/root.json | awk '{ print $1 }')
export root_sha256=$(sha256sum $out/root.json | awk '{ print $1 }')
export root_length=$(stat --printf="%s" $out/root.json)
substituteAll ${./mirrors.json} $out/mirrors.json
export mirrors_md5=$(md5sum $out/mirrors.json | awk '{ print $1 }')
export mirrors_sha256=$(sha256sum $out/mirrors.json | awk '{ print $1 }')
export mirrors_length=$(stat --printf="%s" $out/mirrors.json)
substituteAll ${./snapshot.json} $out/snapshot.json
export snapshot_md5=$(md5sum $out/snapshot.json | awk '{ print $1 }')
export snapshot_sha256=$(sha256sum $out/snapshot.json | awk '{ print $1 }')
export snapshot_length=$(stat --printf="%s" $out/snapshot.json)
substituteAll ${./timestamp.json} $out/timestamp.json
''