1#! /usr/bin/env nix-shell
2#! nix-shell -i bash -p coreutils haskellPackages.cabal2nix-unstable git nix nixfmt-rfc-style -I nixpkgs=.
3
4set -euo pipefail
5
6self=$0
7
8print_help () {
9cat <<END_HELP
10Usage: $self [options]
11
12Options:
13 --do-commit Commit changes to this file.
14 -f | --fast Do not update the transitive-broken.yaml file.
15 -h | --help Show this help.
16
17This script is used to regenerate nixpkgs' Haskell package set, using the
18tool hackage2nix from the nixos/cabal2nix repo. hackage2nix looks at the
19config files in pkgs/development/haskell-modules/configuration-hackage2nix
20and generates a Nix expression for package version specified there, using the
21Cabal files from the Hackage database (available under all-cabal-hashes) and
22its companion tool cabal2nix.
23
24Unless --fast is used, it will then use the generated nix expression by
25running regenerate-transitive-broken-packages.sh which updates the transitive-broken.yaml
26file. Then it re-runs hackage2nix.
27
28Related scripts are update-hackage.sh, for updating the snapshot of the
29Hackage database used by hackage2nix, and update-cabal2nix-unstable.sh,
30for updating the version of hackage2nix used to perform this task.
31
32Note that this script doesn't gcroot anything, so it may be broken by an
33unfortunately timed nix-store --gc.
34
35END_HELP
36}
37
38DO_COMMIT=0
39REGENERATE_TRANSITIVE=1
40
41options=$(getopt -o "fh" -l "help,fast,do-commit" -- "$@")
42
43eval set -- "$options"
44
45while true; do
46 case "$1" in
47 --do-commit)
48 DO_COMMIT=1
49 ;;
50 -f|--fast)
51 REGENERATE_TRANSITIVE=0
52 ;;
53 -h|--help)
54 print_help
55 exit 0
56 ;;
57 --)
58 break;;
59 *)
60 print_help
61 exit 1
62 ;;
63 esac
64 shift
65done
66
67HACKAGE2NIX="${HACKAGE2NIX:-hackage2nix}"
68
69config_dir=pkgs/development/haskell-modules/configuration-hackage2nix
70
71run_hackage2nix() {
72"$HACKAGE2NIX" \
73 --hackage "$unpacked_hackage" \
74 --preferred-versions <(for n in "$unpacked_hackage"/*/preferred-versions; do cat "$n"; echo; done) \
75 --nixpkgs "$PWD" \
76 --config "$compiler_config" \
77 --config "$config_dir/main.yaml" \
78 --config "$config_dir/stackage.yaml" \
79 --config "$config_dir/broken.yaml" \
80 --config "$config_dir/transitive-broken.yaml"
81}
82
83echo "Obtaining Hackage data …"
84extraction_derivation='with import ./. {}; runCommandLocal "unpacked-cabal-hashes" { } "tar xf ${all-cabal-hashes} --strip-components=1 --one-top-level=$out"'
85unpacked_hackage="$(nix-build -E "$extraction_derivation" --no-out-link)"
86
87echo "Generating compiler configuration …"
88compiler_config="$(nix-build -A haskellPackages.cabal2nix-unstable.compilerConfig --no-out-link)"
89
90echo "Running hackage2nix to regenerate pkgs/development/haskell-modules/hackage-packages.nix …"
91run_hackage2nix
92
93if [[ "$REGENERATE_TRANSITIVE" -eq 1 ]]; then
94
95echo "Regenerating transitive-broken.yaml … (pass --fast to $self to skip this step)"
96
97maintainers/scripts/haskell/regenerate-transitive-broken-packages.sh
98
99echo "Running hackage2nix again to reflect changes in transitive-broken.yaml …"
100
101run_hackage2nix
102
103fi
104
105nixfmt pkgs/development/haskell-modules/hackage-packages.nix
106
107if [[ "$DO_COMMIT" -eq 1 ]]; then
108git add pkgs/development/haskell-modules/configuration-hackage2nix/transitive-broken.yaml
109git add pkgs/development/haskell-modules/hackage-packages.nix
110git commit -F - << EOF
111haskellPackages: regenerate package set based on current config
112
113This commit has been generated by maintainers/scripts/haskell/regenerate-hackage-packages.sh
114EOF
115fi
116
117echo "Regeneration of hackage-packages.nix finished."