1 # This expression takes a file like `hackage-packages.nix` and constructs
2 # a full package set out of that.
4 { # package-set used for build tools (all of nixpkgs)
7 , # A haskell package set for Setup.hs, compiler plugins, and similar
11 , # package-set used for non-haskell dependencies (all of nixpkgs)
14 , # stdenv provides our build and host platforms
17 , # this module provides the list of known licenses and maintainers
20 # needed for overrideCabal & packageSourceOverrides
23 , # hashes for downloading Hackage packages
24 # This is either a directory or a .tar.gz containing the cabal files and
25 # hashes of Hackage as exemplified by this repository:
26 # https://github.com/commercialhaskell/all-cabal-hashes/tree/hackage
32 , # A function that takes `{ pkgs, lib, callPackage }` as the first arg and
33 # `self` as second, and returns a set of haskell packages
36 , # The final, fully overridden package set usable with the nixpkgs fixpoint
37 # overriding functionality
41 # return value: a function from self to the package set
45 inherit (stdenv) buildPlatform hostPlatform;
47 inherit (lib) fix' extends makeOverridable;
48 inherit (haskellLib) overrideCabal;
50 mkDerivationImpl = pkgs.callPackage ./generic-builder.nix {
52 nodejs = buildPackages.nodejs-slim;
53 inherit (self) buildHaskellPackages ghc ghcWithHoogle ghcWithPackages;
54 inherit (self.buildHaskellPackages) jailbreak-cabal;
55 hscolour = overrideCabal (drv: {
58 hyperlinkSource = false; # Avoid depending on hscolour for this build.
59 postFixup = "rm -rf $out/lib $out/share $out/nix-support";
60 }) self.buildHaskellPackages.hscolour;
61 cpphs = overrideCabal (drv: {
63 postFixup = "rm -rf $out/lib $out/share $out/nix-support";
64 }) (self.cpphs.overrideScope (self: super: {
65 mkDerivation = drv: super.mkDerivation (drv // {
66 enableSharedExecutables = false;
67 enableSharedLibraries = false;
74 mkDerivation = makeOverridable mkDerivationImpl;
76 # manualArgs are the arguments that were explicitly passed to `callPackage`, like:
78 # callPackage foo { bar = null; };
80 # here `bar` is a manual argument.
81 callPackageWithScope = scope: fn: manualArgs:
83 # this code is copied from callPackage in lib/customisation.nix
85 # we cannot use `callPackage` here because we want to call `makeOverridable`
86 # on `drvScope` (we cannot add `overrideScope` after calling `callPackage` because then it is
87 # lost on `.override`) but determine the auto-args based on `drv` (the problem here
88 # is that nix has no way to "passthrough" args while preserving the reflection
89 # info that callPackage uses to determine the arguments).
90 drv = if lib.isFunction fn then fn else import fn;
91 auto = builtins.intersectAttrs (lib.functionArgs drv) scope;
93 # Converts a returned function to a functor attribute set if necessary
94 ensureAttrs = v: if builtins.isFunction v then { __functor = _: v; } else v;
96 # this wraps the `drv` function to add `scope` and `overrideScope` to the result.
97 drvScope = allArgs: ensureAttrs (drv allArgs) // {
100 let newScope = mkScope (fix' (extends f scope.__unfix__));
101 # note that we have to be careful here: `allArgs` includes the auto-arguments that
102 # weren't manually specified. If we would just pass `allArgs` to the recursive call here,
103 # then we wouldn't look up any packages in the scope in the next interation, because it
104 # appears as if all arguments were already manually passed, so the scope change would do
106 in callPackageWithScope newScope drv manualArgs;
108 in lib.makeOverridable drvScope (auto // manualArgs);
111 ps = pkgs.__splicedPackages;
112 scopeSpliced = pkgs.splicePackages {
113 pkgsBuildBuild = scope.buildHaskellPackages.buildHaskellPackages;
114 pkgsBuildHost = scope.buildHaskellPackages;
115 pkgsBuildTarget = {};
117 pkgsHostTarget = scope;
118 pkgsTargetTarget = {};
121 inherit (scope) ghc buildHaskellPackages;
123 in ps // ps.xorg // ps.gnome2 // { inherit stdenv; } // scopeSpliced;
124 defaultScope = mkScope self;
125 callPackage = drv: args: callPackageWithScope defaultScope drv args;
127 # Use cabal2nix to create a default.nix for the package sources found at 'src'.
128 haskellSrc2nix = { name, src, sha256 ? null, extraCabal2nixOptions ? "" }:
130 sha256Arg = if sha256 == null then "--sha256=" else ''--sha256="${sha256}"'';
131 in buildPackages.runCommand "cabal2nix-${name}" {
132 nativeBuildInputs = [ buildPackages.cabal2nix-unwrapped ];
133 preferLocalBuild = true;
134 allowSubstitutes = false;
135 LANG = "en_US.UTF-8";
136 LOCALE_ARCHIVE = pkgs.lib.optionalString (buildPlatform.libc == "glibc") "${buildPackages.glibcLocales}/lib/locale/locale-archive";
140 cabal2nix --compiler=${self.ghc.haskellCompilerName} --system=${hostPlatform.config} ${sha256Arg} "${src}" ${extraCabal2nixOptions} > "$out/default.nix"
143 # Given a package name and version, e.g. name = "async", version = "2.2.4",
144 # gives its cabal file and hashes (JSON file) as discovered from the
145 # all-cabal-hashes value. If that's a directory, it will copy the relevant
146 # files to $out; if it's a tarball, it will extract and move them to $out.
147 all-cabal-hashes-component = name: version: buildPackages.runCommand "all-cabal-hashes-component-${name}-${version}" {} ''
149 if [ -d ${all-cabal-hashes} ]
151 cp ${all-cabal-hashes}/${name}/${version}/${name}.json $out
152 cp ${all-cabal-hashes}/${name}/${version}/${name}.cabal $out
154 tar --wildcards -xzvf ${all-cabal-hashes} \*/${name}/${version}/${name}.{json,cabal}
155 mv */${name}/${version}/${name}.{json,cabal} $out
159 hackage2nix = name: version: let component = all-cabal-hashes-component name version; in self.haskellSrc2nix {
160 name = "${name}-${version}";
161 sha256 = ''$(sed -e 's/.*"SHA256":"//' -e 's/".*$//' "${component}/${name}.json")'';
162 src = "${component}/${name}.cabal";
165 # Adds a nix file derived from cabal2nix in the passthru of the derivation it
166 # produces. This is useful to debug callHackage / callCabal2nix by looking at
167 # the content of the nix file pointed by `cabal2nixDeriver`.
168 # However, it does not keep a reference to that file, which may be garbage
169 # collected, which may be an annoyance.
170 callPackageKeepDeriver = src: args:
171 overrideCabal (orig: {
172 passthru = orig.passthru or {} // {
173 # When using callCabal2nix or callHackage, it is often useful
174 # to debug a failure by inspecting the Nix expression
175 # generated by cabal2nix. This can be accessed via this
176 # cabal2nixDeriver field.
177 cabal2nixDeriver = src;
179 }) (self.callPackage src args);
181 in package-set { inherit pkgs lib callPackage; } self // {
183 inherit mkDerivation callPackage haskellSrc2nix hackage2nix buildHaskellPackages;
185 inherit (haskellLib) packageSourceOverrides;
187 # callHackage :: Text -> Text -> AttrSet -> HaskellPackage
189 # e.g., while overriding a package set:
190 # '... foo = self.callHackage "foo" "1.5.3" {}; ...'
191 callHackage = name: version: callPackageKeepDeriver (self.hackage2nix name version);
194 # :: { pkg :: Text, ver :: Text, sha256 :: Text }
198 # This function does not depend on all-cabal-hashes and therefore will work
199 # for any version that has been released on hackage as opposed to only
200 # versions released before whatever version of all-cabal-hashes you happen
201 # to be currently using.
202 callHackageDirect = {pkg, ver, sha256}:
203 let pkgver = "${pkg}-${ver}";
204 in self.callCabal2nix pkg (pkgs.fetchzip {
205 url = "mirror://hackage/${pkgver}/${pkgver}.tar.gz";
209 # Creates a Haskell package from a source package by calling cabal2nix on the source.
210 callCabal2nixWithOptions = name: src: extraCabal2nixOptions: args:
213 pkgs.lib.hasSuffix ".cabal" path ||
214 baseNameOf path == "package.yaml";
215 expr = self.haskellSrc2nix {
216 inherit name extraCabal2nixOptions;
217 src = if pkgs.lib.canCleanSource src
218 then pkgs.lib.cleanSourceWith { inherit src filter; }
221 in overrideCabal (orig: {
223 }) (callPackageKeepDeriver expr args);
225 callCabal2nix = name: src: args: self.callCabal2nixWithOptions name src "" args;
228 # , name : Defaulted String
229 # , source-overrides : Defaulted (Either Path VersionNumber)
230 # , overrides : Defaulted (HaskellPackageOverrideSet)
231 # , modifier : Defaulted
232 # , returnShellEnv : Defaulted
233 # , withHoogle : Defaulted
234 # , cabal2nixOptions : Defaulted
235 # } -> NixShellAwareDerivation
237 # Given a path to a haskell package directory, an optional package name
238 # which defaults to the base name of the path, an optional set of source
239 # overrides as appropriate for the 'packageSourceOverrides' function, an
240 # optional set of arbitrary overrides, and an optional haskell package
241 # modifier, return a derivation appropriate for nix-build or nix-shell to
242 # build that package.
244 # If 'returnShellEnv' is true this returns a derivation which will give you
245 # an environment suitable for developing the listed packages with an
246 # incremental tool like cabal-install.
248 # If 'withHoogle' is true (the default if a shell environment is requested)
249 # then 'ghcWithHoogle' is used to generate the derivation (instead of
250 # 'ghcWithPackages'), see the documentation there for more information.
252 # 'cabal2nixOptions' can contain extra command line arguments to pass to
253 # 'cabal2nix' when generating the package derivation, for example setting
254 # a cabal flag with '--flag=myflag'.
257 , name ? lib.optionalString (builtins.typeOf root == "path") (builtins.baseNameOf root)
258 , source-overrides ? {}
259 , overrides ? self: super: {}
260 , modifier ? drv: drv
261 , returnShellEnv ? pkgs.lib.inNixShell
262 , withHoogle ? returnShellEnv
263 , cabal2nixOptions ? "" }:
265 (extensible-self.extend
266 (pkgs.lib.composeExtensions
267 (self.packageSourceOverrides source-overrides)
269 .callCabal2nixWithOptions name root cabal2nixOptions {};
271 then (modifier drv).envFunc {inherit withHoogle;}
274 # This can be used to easily create a derivation containing GHC and the specified set of Haskell packages.
277 # $ nix-shell -p 'haskellPackages.ghcWithPackages (hpkgs: [ hpkgs.mtl hpkgs.lens ])'
278 # $ ghci # in the nix-shell
279 # Prelude > import Control.Lens
281 # GHC is setup with a package database with all the specified Haskell packages.
283 # ghcWithPackages :: (HaskellPkgSet -> [ HaskellPkg ]) -> Derivation
284 ghcWithPackages = buildHaskellPackages.callPackage ./with-packages-wrapper.nix {
285 haskellPackages = self;
286 inherit (self) hoogleWithPackages;
290 # Put 'hoogle' into the derivation's PATH with a database containing all
291 # the package's dependencies; run 'hoogle server --local' in a shell to
292 # host a search engine for the dependencies.
295 # $ nix-shell -p 'haskellPackages.hoogleWithPackages (p: [ p.mtl p.lens ])'
296 # [nix-shell] $ hoogle server
298 # hoogleWithPackages :: (HaskellPkgSet -> [ HaskellPkg ]) -> Derivation
300 # To reload the Hoogle server automatically on .cabal file changes try
302 # echo *.cabal | entr -r -- nix-shell --run 'hoogle server --local'
303 hoogleWithPackages = self.callPackage ./hoogle.nix {
304 haskellPackages = self;
308 lib.warn "hoogleLocal is deprecated, use hoogleWithPackages instead" (
309 self.hoogleWithPackages (_: packages)
311 # This is like a combination of ghcWithPackages and hoogleWithPackages.
312 # It provides a derivation containing both GHC and Hoogle with an index of
313 # the given Haskell package database.
316 # $ nix-shell -p 'haskellPackages.ghcWithHoogle (hpkgs: [ hpkgs.conduit hpkgs.lens ])'
318 # ghcWithHoogle :: (HaskellPkgSet -> [ HaskellPkg ]) -> Derivation
319 ghcWithHoogle = self.ghcWithPackages.override {
323 # Returns a derivation whose environment contains a GHC with only
324 # the dependencies of packages listed in `packages`, not the
325 # packages themselves. Using nix-shell on this derivation will
326 # give you an environment suitable for developing the listed
327 # packages with an incremental tool like cabal-install.
329 # In addition to the "packages" arg and "withHoogle" arg, anything that
330 # can be passed into stdenv.mkDerivation can be included in the input attrset
333 # with import <nixpkgs> {};
334 # haskellPackages.extend (haskell.lib.compose.packageSourceOverrides {
335 # frontend = ./frontend;
336 # backend = ./backend;
341 # let pkgs = import <nixpkgs> {} in
342 # (import ./.).shellFor {
343 # packages = p: [p.frontend p.backend p.common];
345 # buildInputs = [ pkgs.python pkgs.cabal-install ];
354 # bash$ nix-shell --run "cabal new-build all"
355 # bash$ nix-shell --run "python"
357 { # Packages to create this development shell for. These are usually
358 # your local packages.
360 , # Whether or not to generate a Hoogle database for all the
363 , # Whether or not to include benchmark dependencies of your local
364 # packages. You should set this to true if you have benchmarks defined
365 # in your local packages that you want to be able to run with cabal benchmark
367 # An optional function that can modify the generic builder arguments
368 # for the fake package that shellFor uses to construct its environment.
373 # haskellPkgs = pkgs.haskell.packages.ghc884.override (hpArgs: {
374 # overrides = pkgs.lib.composeExtensions (hpArgs.overrides or (_: _: { })) (
376 # mkDerivation = args: hprev.mkDerivation ({
378 # doBenchmark = false;
381 # enableLibraryProfiling = false;
382 # enableExecutableProfiling = false;
388 # haskellPkgs.shellFor {
389 # packages = p: [ p.foo ];
390 # genericBuilderArgsModifier = args: args // { doCheck = true; doBenchmark = true };
393 # This will disable tests and benchmarks for everything in "haskellPkgs"
394 # (which will invalidate the binary cache), and then re-enable them
395 # for the "shellFor" environment (ensuring that any test/benchmark
396 # dependencies for "foo" will be available within the nix-shell).
397 , genericBuilderArgsModifier ? (args: args)
399 # Extra dependencies, in the form of cabal2nix build attributes.
401 # An example use case is when you have Haskell scripts that use
402 # libraries that don't occur in your packages' dependencies.
406 # extraDependencies = p: {
407 # libraryHaskellDepends = [ p.releaser ];
409 , extraDependencies ? p: {}
413 # A list of the packages we want to build a development shell for.
414 # This is a list of Haskell package derivations.
415 selected = packages self;
417 # This is a list of attribute sets, where each attribute set
418 # corresponds to the build inputs of one of the packages input to shellFor.
420 # Each attribute has keys like buildDepends, executableHaskellDepends,
421 # testPkgconfigDepends, etc. The values for the keys of the attribute
422 # set are lists of dependencies.
425 # cabalDepsForSelected
427 # # This may be the attribute set corresponding to the `backend`
428 # # package in the example above.
429 # { buildDepends = [ gcc ... ];
430 # libraryHaskellDepends = [ lens conduit ... ];
433 # # This may be the attribute set corresponding to the `common`
434 # # package in the example above.
435 # { testHaskellDepends = [ tasty hspec ... ];
436 # libraryHaskellDepends = [ lens aeson ];
437 # benchmarkHaskellDepends = [ criterion ... ];
442 cabalDepsForSelected = map (p: p.getCabalDeps) selected;
444 # A predicate that takes a derivation as input, and tests whether it is
445 # the same as any of the `selected` packages.
447 # Returns true if the input derivation is not in the list of `selected`
450 # isNotSelected :: Derivation -> Bool
454 # isNotSelected common [ frontend backend common ]
457 # isNotSelected lens [ frontend backend common ]
459 isNotSelected = input: pkgs.lib.all (p: input.outPath or null != p.outPath) selected;
461 # A function that takes a list of list of derivations, filters out all
462 # the `selected` packages from each list, and concats the results.
464 # zipperCombinedPkgs :: [[Derivation]] -> [Derivation]
467 # zipperCombinedPkgs [ [ lens conduit ] [ aeson frontend ] ]
468 # => [ lens conduit aeson ]
470 # Note: The reason this isn't just the function `pkgs.lib.concat` is
471 # that we need to be careful to remove dependencies that are in the
472 # `selected` packages.
474 # For instance, in the above example, if `common` is a dependency of
475 # `backend`, then zipperCombinedPkgs needs to be careful to filter out
476 # `common`, because cabal will end up ignoring that built version,
477 # assuming new-style commands.
478 zipperCombinedPkgs = vals:
480 (drvList: pkgs.lib.filter isNotSelected drvList)
483 # Zip `cabalDepsForSelected` into a single attribute list, combining
484 # the derivations in all the individual attributes.
488 # => # Assuming the value of cabalDepsForSelected is the same as
489 # # the example in cabalDepsForSelected:
490 # { buildDepends = [ gcc ... ];
491 # libraryHaskellDepends = [ lens conduit aeson ... ];
492 # testHaskellDepends = [ tasty hspec ... ];
493 # benchmarkHaskellDepends = [ criterion ... ];
497 # See the Note in `zipperCombinedPkgs` for what gets filtered out from
498 # each of these dependency lists.
500 pkgs.lib.zipAttrsWith (_name: zipperCombinedPkgs) (cabalDepsForSelected ++ [ (extraDependencies self) ]);
502 # A attribute set to pass to `haskellPackages.mkDerivation`.
504 # The important thing to note here is that all the fields from
505 # packageInputs are set correctly.
506 genericBuilderArgs = {
508 if pkgs.lib.length selected == 1
509 then (pkgs.lib.head selected).name
515 // pkgs.lib.optionalAttrs doBenchmark {
516 # `doBenchmark` needs to explicitly be set here because haskellPackages.mkDerivation defaults it to `false`. If the user wants benchmark dependencies included in their development shell, it has to be explicitly enabled here.
520 # This is a pseudo Haskell package derivation that contains all the
521 # dependencies for the packages in `selected`.
523 # This is a derivation created with `haskellPackages.mkDerivation`.
525 # pkgWithCombinedDeps :: HaskellDerivation
526 pkgWithCombinedDeps = self.mkDerivation (genericBuilderArgsModifier genericBuilderArgs);
528 # The derivation returned from `envFunc` for `pkgWithCombinedDeps`.
530 # This is a derivation that can be run with `nix-shell`. It provides a
531 # GHC with a package database with all the dependencies of our
532 # `selected` packages.
534 # This is a derivation created with `stdenv.mkDerivation` (not
535 # `haskellPackages.mkDerivation`).
537 # pkgWithCombinedDepsDevDrv :: Derivation
538 pkgWithCombinedDepsDevDrv = pkgWithCombinedDeps.envFunc { inherit withHoogle; };
540 mkDerivationArgs = builtins.removeAttrs args [ "genericBuilderArgsModifier" "packages" "withHoogle" "doBenchmark" "extraDependencies" ];
542 in pkgWithCombinedDepsDevDrv.overrideAttrs (old: mkDerivationArgs // {
543 nativeBuildInputs = old.nativeBuildInputs ++ mkDerivationArgs.nativeBuildInputs or [];
544 buildInputs = old.buildInputs ++ mkDerivationArgs.buildInputs or [];
548 withPackages = self.ghcWithPackages;
549 withHoogle = self.ghcWithHoogle;
553 Run `cabal sdist` on a source.
555 Unlike `haskell.lib.sdistTarball`, this does not require any dependencies
556 to be present, as it uses `cabal-install` instead of building `Setup.hs`.
557 This makes `cabalSdist` faster than `sdistTarball`.
561 name ? if src?name then "${src.name}-sdist.tar.gz" else "source.tar.gz"
563 pkgs.runCommandLocal name
566 nativeBuildInputs = [
567 buildHaskellPackages.cabal-install
569 # TODO after https://github.com/haskell/cabal/issues/8352
576 cd "''${sourceRoot:-.}"
579 HOME=$PWD cabal sdist --output-directory out
584 Like `haskell.lib.buildFromSdist`, but using `cabal sdist` instead of
587 Unlike `haskell.lib.buildFromSdist`, this does not require any dependencies
588 to be present. This makes `buildFromCabalSdist` faster than `haskell.lib.buildFromSdist`.
590 buildFromCabalSdist = pkg:
591 haskellLib.overrideSrc
593 src = self.cabalSdist { inherit (pkg) src; };
594 version = pkg.version;
599 Modify a Haskell package to add shell completion scripts for the
600 given executables produced by it. These completion scripts will be
601 picked up automatically if the resulting derivation is installed,
602 e.g. by `nix-env -i`.
604 This depends on the `--*-completion` flag `optparse-applicative` provides
605 automatically. Since we need to invoke installed executables, completions
606 are not generated if we are cross-compiling.
608 commands: names of the executables built by the derivation
609 pkg: Haskell package that builds the executables
612 generateOptparseApplicativeCompletions [ "exec1" "exec2" ] pkg
614 Type: [str] -> drv -> drv
616 generateOptparseApplicativeCompletions =
623 if stdenv.buildPlatform.canExecute stdenv.hostPlatform
624 then lib.foldr haskellLib.__generateOptparseApplicativeCompletion pkg commands
629 Modify given Haskell package to force GHC to employ the LLVM
630 codegen backend when compiling. Useful when working around bugs
631 in a native codegen backend GHC defaults to.
634 forceLlvmCodegenBackend tls
638 forceLlvmCodegenBackend = haskellLib.overrideCabal (drv: {
639 configureFlags = drv.configureFlags or [ ] ++ [ "--ghc-option=-fllvm" ];
640 buildTools = drv.buildTools or [ ] ++ [ self.llvmPackages.llvm ];