Merge pull request #28744 from LumiGuide/checkUnusedPackages

haskell: add the checkUnusedPackages function including docs

authored by

Peter Simons and committed by
GitHub
8a0566e4 97a4088d

+73 -1
+56
doc/languages-frameworks/haskell.md
··· 867 867 nix-build -A haskell.packages.integer-simple.ghc802.scientific 868 868 ``` 869 869 870 + ### Quality assurance 871 + 872 + The `haskell.lib` library includes a number of functions for checking for 873 + various imperfections in Haskell packages. It's useful to apply these functions 874 + to your own Haskell packages and integrate that in a Continuous Integration 875 + server like [hydra](https://nixos.org/hydra/) to assure your packages maintain a 876 + minimum level of quality. This section discusses some of these functions. 877 + 878 + #### buildStrictly 879 + 880 + Applying `haskell.lib.buildStrictly` to a Haskell package enables the `-Wall` 881 + and `-Werror` GHC options to turn all warnings into build failures. Additionally 882 + the source of your package is gotten from first invoking `cabal sdist` to ensure 883 + all needed files are listed in the Cabal file. 884 + 885 + #### checkUnusedPackages 886 + 887 + Applying `haskell.lib.checkUnusedPackages` to a Haskell package invokes 888 + the [packunused](http://hackage.haskell.org/package/packunused) tool on the 889 + package. `packunused` complains when it finds packages listed as build-depends 890 + in the Cabal file which are redundant. For example: 891 + 892 + ``` 893 + $ nix-build -E 'let pkgs = import <nixpkgs> {}; in pkgs.haskell.lib.checkUnusedPackages {} pkgs.haskellPackages.scientific' 894 + these derivations will be built: 895 + /nix/store/3lc51cxj2j57y3zfpq5i69qbzjpvyci1-scientific-0.3.5.1.drv 896 + ... 897 + detected package components 898 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 899 + 900 + - library 901 + - testsuite(s): test-scientific 902 + - benchmark(s): bench-scientific* 903 + 904 + (component names suffixed with '*' are not configured to be built) 905 + 906 + library 907 + ~~~~~~~ 908 + 909 + The following package dependencies seem redundant: 910 + 911 + - ghc-prim-0.5.0.0 912 + 913 + testsuite(test-scientific) 914 + ~~~~~~~~~~~~~~~~~~~~~~~~~~ 915 + 916 + no redundant packages dependencies found 917 + 918 + builder for ‘/nix/store/3lc51cxj2j57y3zfpq5i69qbzjpvyci1-scientific-0.3.5.1.drv’ failed with exit code 1 919 + error: build of ‘/nix/store/3lc51cxj2j57y3zfpq5i69qbzjpvyci1-scientific-0.3.5.1.drv’ failed 920 + ``` 921 + 922 + As you can see, `packunused` finds out that although the testsuite component has 923 + no redundant dependencies the library component of `scientific-0.3.5.1` depends 924 + on `ghc-prim` which is unused in the library. 925 + 870 926 ## Other resources 871 927 872 928 - The Youtube video [Nix Loves Haskell](https://www.youtube.com/watch?v=BsBhi_r-OeE)
+17 -1
pkgs/development/haskell-modules/lib.nix
··· 1 1 # TODO(@Ericson2314): Remove `pkgs` param, which is only used for 2 - # `buildStackProject` and `justStaticExecutables` 2 + # `buildStackProject`, `justStaticExecutables` and `checkUnusedPackages` 3 3 { pkgs, lib }: 4 4 5 5 rec { ··· 107 107 }); 108 108 109 109 buildStrictly = pkg: buildFromSdist (appendConfigureFlag pkg "--ghc-option=-Wall --ghc-option=-Werror"); 110 + 111 + checkUnusedPackages = 112 + { ignoreEmptyImports ? false 113 + , ignoreMainModule ? false 114 + , ignorePackages ? [] 115 + } : drv : 116 + overrideCabal (appendConfigureFlag drv "--ghc-option=-ddump-minimal-imports") (_drv: { 117 + postBuild = with lib; 118 + let args = concatStringsSep " " ( 119 + optional ignoreEmptyImports "--ignore-empty-imports" ++ 120 + optional ignoreMainModule "--ignore-main-module" ++ 121 + map (pkg: "--ignore-package ${pkg}") ignorePackages 122 + ); 123 + in "${pkgs.haskellPackages.packunused}/bin/packunused" + 124 + optionalString (args != "") " ${args}"; 125 + }); 110 126 111 127 buildStackProject = pkgs.callPackage ./generic-stack-builder.nix { }; 112 128