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, rev ? { revision = null; sha256 = null; }}: args:
203 let pkgver = "${pkg}-${ver}";
204 firstRevision = self.callCabal2nix pkg (pkgs.fetchzip {
205 url = "mirror://hackage/${pkgver}/${pkgver}.tar.gz";
208 in overrideCabal (orig: {
209 revision = rev.revision;
210 editedCabalFile = rev.sha256;
213 # Creates a Haskell package from a source package by calling cabal2nix on the source.
214 callCabal2nixWithOptions = name: src: opts: args:
216 extraCabal2nixOptions = if builtins.isString opts
218 else opts.extraCabal2nixOptions or "";
219 srcModifier = opts.srcModifier or null;
220 defaultFilter = path: type:
221 pkgs.lib.hasSuffix ".cabal" path ||
222 baseNameOf path == "package.yaml";
223 expr = self.haskellSrc2nix {
224 inherit name extraCabal2nixOptions;
225 src = if srcModifier != null
227 else if pkgs.lib.canCleanSource src
228 then pkgs.lib.cleanSourceWith { inherit src; filter = defaultFilter; }
231 in overrideCabal (orig: {
233 }) (callPackageKeepDeriver expr args);
235 callCabal2nix = name: src: args: self.callCabal2nixWithOptions name src "" args;
238 # , name : Defaulted String
239 # , source-overrides : Defaulted (Either Path VersionNumber)
240 # , overrides : Defaulted (HaskellPackageOverrideSet)
241 # , modifier : Defaulted
242 # , returnShellEnv : Defaulted
243 # , withHoogle : Defaulted
244 # , cabal2nixOptions : Defaulted
245 # } -> NixShellAwareDerivation
247 # Given a path to a haskell package directory, an optional package name
248 # which defaults to the base name of the path, an optional set of source
249 # overrides as appropriate for the 'packageSourceOverrides' function, an
250 # optional set of arbitrary overrides, and an optional haskell package
251 # modifier, return a derivation appropriate for nix-build or nix-shell to
252 # build that package.
254 # If 'returnShellEnv' is true this returns a derivation which will give you
255 # an environment suitable for developing the listed packages with an
256 # incremental tool like cabal-install.
258 # If 'withHoogle' is true (the default if a shell environment is requested)
259 # then 'ghcWithHoogle' is used to generate the derivation (instead of
260 # 'ghcWithPackages'), see the documentation there for more information.
262 # 'cabal2nixOptions' can contain extra command line arguments to pass to
263 # 'cabal2nix' when generating the package derivation, for example setting
264 # a cabal flag with '--flag=myflag'.
267 , name ? lib.optionalString (builtins.typeOf root == "path") (builtins.baseNameOf root)
268 , source-overrides ? {}
269 , overrides ? self: super: {}
270 , modifier ? drv: drv
271 , returnShellEnv ? pkgs.lib.inNixShell
272 , withHoogle ? returnShellEnv
273 , cabal2nixOptions ? "" }:
275 (extensible-self.extend
276 (pkgs.lib.composeExtensions
277 (self.packageSourceOverrides source-overrides)
279 .callCabal2nixWithOptions name root cabal2nixOptions {};
281 then (modifier drv).envFunc {inherit withHoogle;}
284 # This can be used to easily create a derivation containing GHC and the specified set of Haskell packages.
287 # $ nix-shell -p 'haskellPackages.ghcWithPackages (hpkgs: [ hpkgs.mtl hpkgs.lens ])'
288 # $ ghci # in the nix-shell
289 # Prelude > import Control.Lens
291 # GHC is setup with a package database with all the specified Haskell packages.
293 # ghcWithPackages :: (HaskellPkgSet -> [ HaskellPkg ]) -> Derivation
294 ghcWithPackages = buildHaskellPackages.callPackage ./with-packages-wrapper.nix {
295 haskellPackages = self;
296 inherit (self) hoogleWithPackages;
300 # Put 'hoogle' into the derivation's PATH with a database containing all
301 # the package's dependencies; run 'hoogle server --local' in a shell to
302 # host a search engine for the dependencies.
305 # $ nix-shell -p 'haskellPackages.hoogleWithPackages (p: [ p.mtl p.lens ])'
306 # [nix-shell] $ hoogle server
308 # hoogleWithPackages :: (HaskellPkgSet -> [ HaskellPkg ]) -> Derivation
310 # To reload the Hoogle server automatically on .cabal file changes try
312 # echo *.cabal | entr -r -- nix-shell --run 'hoogle server --local'
313 hoogleWithPackages = self.callPackage ./hoogle.nix {
314 haskellPackages = self;
318 lib.warn "hoogleLocal is deprecated, use hoogleWithPackages instead" (
319 self.hoogleWithPackages (_: packages)
321 # This is like a combination of ghcWithPackages and hoogleWithPackages.
322 # It provides a derivation containing both GHC and Hoogle with an index of
323 # the given Haskell package database.
326 # $ nix-shell -p 'haskellPackages.ghcWithHoogle (hpkgs: [ hpkgs.conduit hpkgs.lens ])'
328 # ghcWithHoogle :: (HaskellPkgSet -> [ HaskellPkg ]) -> Derivation
329 ghcWithHoogle = self.ghcWithPackages.override {
333 # Returns a derivation whose environment contains a GHC with only
334 # the dependencies of packages listed in `packages`, not the
335 # packages themselves. Using nix-shell on this derivation will
336 # give you an environment suitable for developing the listed
337 # packages with an incremental tool like cabal-install.
339 # In addition to the "packages" arg and "withHoogle" arg, anything that
340 # can be passed into stdenv.mkDerivation can be included in the input attrset
343 # with import <nixpkgs> {};
344 # haskellPackages.extend (haskell.lib.compose.packageSourceOverrides {
345 # frontend = ./frontend;
346 # backend = ./backend;
351 # let pkgs = import <nixpkgs> {} in
352 # (import ./.).shellFor {
353 # packages = p: [p.frontend p.backend p.common];
355 # buildInputs = [ pkgs.python pkgs.cabal-install ];
364 # bash$ nix-shell --run "cabal new-build all"
365 # bash$ nix-shell --run "python"
367 { # Packages to create this development shell for. These are usually
368 # your local packages.
370 , # Whether or not to generate a Hoogle database for all the
373 , # Whether or not to include benchmark dependencies of your local
374 # packages. You should set this to true if you have benchmarks defined
375 # in your local packages that you want to be able to run with cabal benchmark
377 # An optional function that can modify the generic builder arguments
378 # for the fake package that shellFor uses to construct its environment.
383 # haskellPkgs = pkgs.haskell.packages.ghc884.override (hpArgs: {
384 # overrides = pkgs.lib.composeExtensions (hpArgs.overrides or (_: _: { })) (
386 # mkDerivation = args: hprev.mkDerivation ({
388 # doBenchmark = false;
391 # enableLibraryProfiling = false;
392 # enableExecutableProfiling = false;
398 # haskellPkgs.shellFor {
399 # packages = p: [ p.foo ];
400 # genericBuilderArgsModifier = args: args // { doCheck = true; doBenchmark = true };
403 # This will disable tests and benchmarks for everything in "haskellPkgs"
404 # (which will invalidate the binary cache), and then re-enable them
405 # for the "shellFor" environment (ensuring that any test/benchmark
406 # dependencies for "foo" will be available within the nix-shell).
407 , genericBuilderArgsModifier ? (args: args)
409 # Extra dependencies, in the form of cabal2nix build attributes.
411 # An example use case is when you have Haskell scripts that use
412 # libraries that don't occur in your packages' dependencies.
416 # extraDependencies = p: {
417 # libraryHaskellDepends = [ p.releaser ];
419 , extraDependencies ? p: {}
423 # A list of the packages we want to build a development shell for.
424 # This is a list of Haskell package derivations.
425 selected = packages self;
427 # This is a list of attribute sets, where each attribute set
428 # corresponds to the build inputs of one of the packages input to shellFor.
430 # Each attribute has keys like buildDepends, executableHaskellDepends,
431 # testPkgconfigDepends, etc. The values for the keys of the attribute
432 # set are lists of dependencies.
435 # cabalDepsForSelected
437 # # This may be the attribute set corresponding to the `backend`
438 # # package in the example above.
439 # { buildDepends = [ gcc ... ];
440 # libraryHaskellDepends = [ lens conduit ... ];
443 # # This may be the attribute set corresponding to the `common`
444 # # package in the example above.
445 # { testHaskellDepends = [ tasty hspec ... ];
446 # libraryHaskellDepends = [ lens aeson ];
447 # benchmarkHaskellDepends = [ criterion ... ];
452 cabalDepsForSelected = map (p: p.getCabalDeps) selected;
454 # A predicate that takes a derivation as input, and tests whether it is
455 # the same as any of the `selected` packages.
457 # Returns true if the input derivation is not in the list of `selected`
460 # isNotSelected :: Derivation -> Bool
464 # isNotSelected common [ frontend backend common ]
467 # isNotSelected lens [ frontend backend common ]
469 isNotSelected = input: pkgs.lib.all (p: input.outPath or null != p.outPath) selected;
471 # A function that takes a list of list of derivations, filters out all
472 # the `selected` packages from each list, and concats the results.
474 # zipperCombinedPkgs :: [[Derivation]] -> [Derivation]
477 # zipperCombinedPkgs [ [ lens conduit ] [ aeson frontend ] ]
478 # => [ lens conduit aeson ]
480 # Note: The reason this isn't just the function `pkgs.lib.concat` is
481 # that we need to be careful to remove dependencies that are in the
482 # `selected` packages.
484 # For instance, in the above example, if `common` is a dependency of
485 # `backend`, then zipperCombinedPkgs needs to be careful to filter out
486 # `common`, because cabal will end up ignoring that built version,
487 # assuming new-style commands.
488 zipperCombinedPkgs = vals:
490 (drvList: pkgs.lib.filter isNotSelected drvList)
493 # Zip `cabalDepsForSelected` into a single attribute list, combining
494 # the derivations in all the individual attributes.
498 # => # Assuming the value of cabalDepsForSelected is the same as
499 # # the example in cabalDepsForSelected:
500 # { buildDepends = [ gcc ... ];
501 # libraryHaskellDepends = [ lens conduit aeson ... ];
502 # testHaskellDepends = [ tasty hspec ... ];
503 # benchmarkHaskellDepends = [ criterion ... ];
507 # See the Note in `zipperCombinedPkgs` for what gets filtered out from
508 # each of these dependency lists.
510 pkgs.lib.zipAttrsWith (_name: zipperCombinedPkgs) (cabalDepsForSelected ++ [ (extraDependencies self) ]);
512 # A attribute set to pass to `haskellPackages.mkDerivation`.
514 # The important thing to note here is that all the fields from
515 # packageInputs are set correctly.
516 genericBuilderArgs = {
518 if pkgs.lib.length selected == 1
519 then (pkgs.lib.head selected).name
525 // pkgs.lib.optionalAttrs doBenchmark {
526 # `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.
530 # This is a pseudo Haskell package derivation that contains all the
531 # dependencies for the packages in `selected`.
533 # This is a derivation created with `haskellPackages.mkDerivation`.
535 # pkgWithCombinedDeps :: HaskellDerivation
536 pkgWithCombinedDeps = self.mkDerivation (genericBuilderArgsModifier genericBuilderArgs);
538 # The derivation returned from `envFunc` for `pkgWithCombinedDeps`.
540 # This is a derivation that can be run with `nix-shell`. It provides a
541 # GHC with a package database with all the dependencies of our
542 # `selected` packages.
544 # This is a derivation created with `stdenv.mkDerivation` (not
545 # `haskellPackages.mkDerivation`).
547 # pkgWithCombinedDepsDevDrv :: Derivation
548 pkgWithCombinedDepsDevDrv = pkgWithCombinedDeps.envFunc { inherit withHoogle; };
550 mkDerivationArgs = builtins.removeAttrs args [ "genericBuilderArgsModifier" "packages" "withHoogle" "doBenchmark" "extraDependencies" ];
552 in pkgWithCombinedDepsDevDrv.overrideAttrs (old: mkDerivationArgs // {
553 nativeBuildInputs = old.nativeBuildInputs ++ mkDerivationArgs.nativeBuildInputs or [];
554 buildInputs = old.buildInputs ++ mkDerivationArgs.buildInputs or [];
558 withPackages = self.ghcWithPackages;
559 withHoogle = self.ghcWithHoogle;
563 Run `cabal sdist` on a source.
565 Unlike `haskell.lib.sdistTarball`, this does not require any dependencies
566 to be present, as it uses `cabal-install` instead of building `Setup.hs`.
567 This makes `cabalSdist` faster than `sdistTarball`.
571 name ? if src?name then "${src.name}-sdist.tar.gz" else "source.tar.gz"
573 pkgs.runCommandLocal name
576 nativeBuildInputs = [
577 buildHaskellPackages.cabal-install
579 # TODO after https://github.com/haskell/cabal/issues/8352
586 cd "''${sourceRoot:-.}"
589 HOME=$PWD cabal sdist --output-directory out
594 Like `haskell.lib.buildFromSdist`, but using `cabal sdist` instead of
597 Unlike `haskell.lib.buildFromSdist`, this does not require any dependencies
598 to be present. This makes `buildFromCabalSdist` faster than `haskell.lib.buildFromSdist`.
600 buildFromCabalSdist = pkg:
601 haskellLib.overrideSrc
603 src = self.cabalSdist { inherit (pkg) src; };
604 version = pkg.version;
609 Modify a Haskell package to add shell completion scripts for the
610 given executables produced by it. These completion scripts will be
611 picked up automatically if the resulting derivation is installed,
612 e.g. by `nix-env -i`.
614 This depends on the `--*-completion` flag `optparse-applicative` provides
615 automatically. Since we need to invoke installed executables, completions
616 are not generated if we are cross-compiling.
618 commands: names of the executables built by the derivation
619 pkg: Haskell package that builds the executables
622 generateOptparseApplicativeCompletions [ "exec1" "exec2" ] pkg
624 Type: [str] -> drv -> drv
626 generateOptparseApplicativeCompletions =
633 if stdenv.buildPlatform.canExecute stdenv.hostPlatform
634 then lib.foldr haskellLib.__generateOptparseApplicativeCompletion pkg commands
639 Modify given Haskell package to force GHC to employ the LLVM
640 codegen backend when compiling. Useful when working around bugs
641 in a native codegen backend GHC defaults to.
644 forceLlvmCodegenBackend tls
648 forceLlvmCodegenBackend = overrideCabal (drv: {
649 configureFlags = drv.configureFlags or [ ] ++ [ "--ghc-option=-fllvm" ];
650 buildTools = drv.buildTools or [ ] ++ [ self.llvmPackages.llvm ];