nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1# Rocq and rocq packages {#sec-language-rocq}
2
3Note that "The Rocq Prover" (Rocq for short) is the new name of the
4proof assistant formerly known as Coq. The `coq` and `coqPackages`
5derivations currently remain for both older versions of Coq, but also
6some versions of Rocq during the renaming transition. In the latter
7case, the `coq` derivation encompasses the compatibility binaries
8(`coqtop`, `coqc`, etc.) in addition to the `rocq` binary. The packages
9only in `coqPackages` are the ones which currently still depend on these
10compatibility binaries.
11
12## Rocq derivation: `rocq-core` {#rocq-derivation-rocq}
13
14The Rocq derivation is overridable through the `rocq-core.override overrides`, where overrides is an attribute set which contains the arguments to override. We recommend overriding either of the following:
15
16* `version` (optional, defaults to the latest version of Rocq selected for nixpkgs, see `pkgs/top-level/rocq-packages` to witness this choice), which follows the conventions explained in the `rocqPackages` section below,
17* `customOCamlPackages` (optional, defaults to `null`, which lets Rocq choose a version automatically), which can be set to any of the ocaml packages attribute of `ocaml-ng` (such as `ocaml-ng.ocamlPackages_4_14` which is the default for Rocq 9.1 for example).
18* `rocq-version` (optional, defaults to the short version e.g. "9.1"), is a version number of the form "x.y" that indicates which Rocq's version build behavior to mimic when using a source which is not a release. E.g. `rocq-core.override { version = "40be8435e132aab2231a79091f011ebc3e64a753"; rocq-version = "9.1"; }`.
19
20## Creating custom Coq environments with `coq.withPackages` {#coq-withPackages}
21
22The `coq.withPackages` function provides a convenient way to create a Coq environment that includes additional Coq packages. This is similar to how `python.withPackages` works for Python environments.
23
24The function takes a function that receives the Coq package set and returns a list of packages. It returns a wrapped Coq environment where all Coq binaries (`coqtop`, `coqc`, `coqdep`, `coqchk`, `coqide`, etc.) are configured with the appropriate environment variables to find the packages.
25
26### Usage {#coq-withPackages-usage}
27
28Here is an example of creating a Coq environment with specific packages.
29
30```nix
31coq.withPackages (
32 ps: with ps; [
33 mathcomp
34 bignums
35 ]
36)
37```
38
39If you install the `vsrocq-language-server` or `rocq-lsp` server, make sure to list them as part of the above `coq.withPackages` expression instead of installing them separately if you want them to find your Coq/Rocq packages.
40
41## Rocq packages attribute sets: `rocqPackages` {#rocq-packages-attribute-sets-rocqpackages}
42
43The recommended way of defining a derivation for a Rocq library, is to use the `rocqPackages.mkRocqDerivation` function, which is essentially a specialization of `mkDerivation` taking into account most of the specifics of Rocq libraries. The following attributes are supported:
44
45* `pname` (required) is the name of the package,
46* `version` (optional, defaults to `null`), is the version to fetch and build,
47 this attribute is interpreted in several ways depending on its type and pattern:
48 * if it is a known released version string, i.e. from the `release` attribute below, the according release is picked, and the `version` attribute of the resulting derivation is set to this release string,
49 * if it is a majorMinor `"x.y"` prefix of a known released version (as defined above), then the latest `"x.y.z"` known released version is selected (for the ordering given by `versionAtLeast`),
50 * if it is a path or a string representing an absolute path (i.e. starting with `"/"`), the provided path is selected as a source, and the `version` attribute of the resulting derivation is set to `"dev"`,
51 * if it is a string of the form `owner:branch` then it tries to download the `branch` of owner `owner` for a project of the same name using the same vcs, and the `version` attribute of the resulting derivation is set to `"dev"`, additionally if the owner is not provided (i.e. if the `owner:` prefix is missing), it defaults to the original owner of the package (see below),
52 * if it is a string of the form `"#N"`, and the domain is github, then it tries to download the current head of the pull request `#N` from github,
53* `defaultVersion` (optional). Rocq libraries may be compatible with some specific versions of Rocq only. The `defaultVersion` attribute is used when no `version` is provided (or if `version = null`) to select the version of the library to use by default, depending on the context. This selection will mainly depend on a `rocq-core` version number but also possibly on other packages versions (e.g. `mathcomp`). If its value ends up to be `null`, the package is marked for removal in end-user `rocqPackages` attribute set.
54* `release` (optional, defaults to `{}`), lists all the known releases of the library and for each of them provides an attribute set with at least a `hash` attribute (you may put the empty string `""` in order to automatically insert a fake hash, this will trigger an error which will allow you to find the correct hash), each attribute set of the list of releases also takes optional overloading arguments for the fetcher as below (i.e.`domain`, `owner`, `repo`, `rev`, `artifact` assuming the default fetcher is used) and optional overrides for the result of the fetcher (i.e. `version` and `src`).
55* `fetcher` (optional, defaults to a generic fetching mechanism supporting github or gitlab based infrastructures), is a function that takes at least an `owner`, a `repo`, a `rev`, and a `hash` and returns an attribute set with a `version` and `src`.
56* `repo` (optional, defaults to the value of `pname`),
57* `owner` (optional, defaults to `"rocq-community"`).
58* `domain` (optional, defaults to `"github.com"`), domains including the strings `"github"` or `"gitlab"` in their names are automatically supported, otherwise, one must change the `fetcher` argument to support them (cf `pkgs/development/rocq-modules/bignums/default.nix` for an example),
59* `releaseRev` (optional, defaults to `(v: v)`), provides a default mapping from release names to revision hashes/branch names/tags,
60* `releaseArtifact` (optional, defaults to `(v: null)`), provides a default mapping from release names to artifact names (only works for github artifact for now),
61* `displayVersion` (optional), provides a way to alter the computation of `name` from `pname`, by explaining how to display version numbers,
62* `namePrefix` (optional, defaults to `[ "rocq-core" ]`), provides a way to alter the computation of `name` from `pname`, by explaining which dependencies must occur in `name`,
63* `nativeBuildInputs` (optional), is a list of executables that are required to build the current derivation, in addition to the default ones (namely `which`, `dune` and `ocaml` depending on whether `useDune`, `useDuneifVersion` and `mlPlugin` are set).
64* `extraNativeBuildInputs` (optional, deprecated), an additional list of derivation to add to `nativeBuildInputs`,
65* `overrideNativeBuildInputs` (optional) replaces the default list of derivation to which `nativeBuildInputs` and `extraNativeBuildInputs` adds extra elements,
66* `buildInputs` (optional), is a list of libraries and dependencies that are required to build and run the current derivation, in addition to the default one `[ rocq-core ]`,
67* `extraBuildInputs` (optional, deprecated), an additional list of derivation to add to `buildInputs`,
68* `overrideBuildInputs` (optional) replaces the default list of derivation to which `buildInputs` and `extraBuildInputs` adds extras elements,
69* `propagatedBuildInputs` (optional) is passed as is to `mkDerivation`, we recommend to use this for Rocq libraries and Rocq plugin dependencies, as this makes sure the paths of the compiled libraries and plugins will always be added to the build environments of subsequent derivation, which is necessary for Rocq packages to work correctly,
70* `mlPlugin` (optional, defaults to `false`). Some extensions (plugins) might require OCaml and sometimes other OCaml packages. Standard dependencies can be added by setting the current option to `true`. For a finer grain control, the `rocq-core.ocamlPackages` attribute can be used in `nativeBuildInputs`, `buildInputs`, and `propagatedBuildInputs` to depend on the same package set Rocq was built against.
71* `useDuneifVersion` (optional, default to `(x: false)` uses Dune to build the package if the provided predicate evaluates to true on the version, e.g. `useDuneifVersion = versions.isGe "1.1"` will use dune if the version of the package is greater or equal to `"1.1"`,
72* `useDune` (optional, defaults to `false`) uses Dune to build the package if set to true, the presence of this attribute overrides the behavior of the previous one.
73* `opam-name` (optional, defaults to concatenating with a dash separator the components of `namePrefix` and `pname`), name of the Dune package to build.
74* `enableParallelBuilding` (optional, defaults to `true`), since it is activated by default, we provide a way to disable it.
75* `extraInstallFlags` (optional), allows to extend `installFlags` which initializes the variables `COQLIBINSTALL` and `COQPLUGININSTALL` so as to install in the proper subdirectory. Indeed Rocq libraries should be installed in `$(out)/lib/coq/${rocq-core.rocq-version}/user-contrib/`. Such directories are automatically added to the `$ROCQPATH` environment variable by the hook defined in the Rocq derivation.
76* `setROCQBIN` (optional, defaults to `true`), by default, the environment variable `$ROCQBIN` is set to the current Rocq's binary, but one can disable this behavior by setting it to `false`,
77* `useMelquiondRemake` (optional, default to `null`) is an attribute set, which, if given, overloads the `preConfigurePhases`, `configureFlags`, `buildPhase`, and `installPhase` attributes of the derivation for a specific use in libraries using `remake` as set up by Guillaume Melquiond for `flocq`, `gappalib`, `interval`, and `coquelicot` (see the corresponding derivation for concrete examples of use of this option). For backward compatibility, the attribute `useMelquiondRemake.logpath` must be set to the logical root of the library (otherwise, one can pass `useMelquiondRemake = {}` to activate this without backward compatibility).
78* `dropAttrs`, `keepAttrs`, `dropDerivationAttrs` are all optional and allow to tune which attribute is added or removed from the final call to `mkDerivation`.
79
80It also takes other standard `mkDerivation` attributes, they are added as such, except for `meta` which extends an automatically computed `meta` (where the `platform` is the same as `rocq-core` and the homepage is automatically computed).
81
82Here is a simple package example. It is a pure Rocq library, thus it depends on Rocq. It builds on the Mathematical Components library, thus it also takes some `mathcomp` derivations as `extraBuildInputs`.
83
84```nix
85{
86 lib,
87 mkRocqDerivation,
88 version ? null,
89 rocq-core,
90 mathcomp,
91 mathcomp-finmap,
92 mathcomp-bigenough,
93}:
94
95mkRocqDerivation {
96 # namePrefix leads to e.g. `name = rocq-core9.1-mathcomp2.5.0-multinomials-2.4.0`
97 namePrefix = [
98 "rocq-core"
99 "mathcomp"
100 ];
101 pname = "multinomials";
102 owner = "math-comp";
103 inherit version;
104 defaultVersion =
105 let
106 case = rocq: mc: out: {
107 cases = [
108 rocq-core
109 mc
110 ];
111 inherit out;
112 };
113 in
114 with lib.versions;
115 lib.switch
116 [ rocq-core.rocq-version mathcomp.version ]
117 [
118 (case (range "8.18" "9.1") (range "2.1.0" "2.5.0") "2.4.0")
119 (case (range "8.17" "9.0") (range "2.1.0" "2.3.0") "2.3.0")
120 ]
121 null;
122 release = {
123 "2.4.0".sha256 = "sha256-7zfIddRH+Sl4nhEPtS/lMZwRUZI45AVFpcC/UC8Z0Yo=";
124 "2.3.0".sha256 = "sha256-usIcxHOAuN+f/j3WjVbPrjz8Hl9ac8R6kYeAKi3CEts=";
125 };
126
127 propagatedBuildInputs = [
128 mathcomp.boot
129 mathcomp.algebra
130 mathcomp-finmap
131 mathcomp.fingroup
132 mathcomp-bigenough
133 ];
134
135 meta = {
136 description = "Coq/SSReflect Library for Monoidal Rings and Multinomials";
137 license = lib.licenses.cecill-c;
138 };
139}
140```
141
142## Three ways of overriding Rocq packages {#rocq-overriding-packages}
143
144There are three distinct ways of changing a Rocq package by overriding one of its values: `.override`, `overrideRocqDerivation`, and `.overrideAttrs`. This section explains what sort of values can be overridden with each of these methods.
145
146### `.override` {#rocq-override}
147
148`.override` lets you change arguments to a Rocq derivation. In the case of the `multinomials` package above, `.override` would let you override arguments like `mkRocqDerivation`, `version`, `rocq-core`, `mathcomp`, `mathcom-finmap`, etc.
149
150For example, assuming you have a special `mathcomp` dependency you want to use, here is how you could override the `mathcomp` dependency:
151
152```nix
153multinomials.override { mathcomp = my-special-mathcomp; }
154```
155
156In Nixpkgs, all Rocq derivations take a `version` argument. This can be overridden in order to easily use a different version:
157
158```nix
159rocqPackages.multinomials.override { version = "1.5.1"; }
160```
161
162Refer to [](#rocq-packages-attribute-sets-rocqpackages) for all the different formats that you can potentially pass to `version`, as well as the restrictions.
163
164### `overrideRocqDerivation` {#rocq-overrideRocqDerivation}
165
166The `overrideRocqDerivation` function lets you easily change arguments to `mkRocqDerivation`. These arguments are described in [](#rocq-packages-attribute-sets-rocqpackages).
167
168For example, here is how you could locally add a new release of the `multinomials` library, and set the `defaultVersion` to use this release:
169
170```nix
171rocqPackages.lib.overrideRocqDerivation {
172 defaultVersion = "2.0";
173 release."2.0".hash = "sha256-czoP11rtrIM7+OLdMisv2EF7n/IbGuwFxHiPtg3qCNM=";
174} rocqPackages.multinomials
175```
176
177### `.overrideAttrs` {#rocq-overrideAttrs}
178
179`.overrideAttrs` lets you override arguments to the underlying `stdenv.mkDerivation` call. Internally, `mkRocqDerivation` uses `stdenv.mkDerivation` to create derivations for Rocq libraries. You can override arguments to `stdenv.mkDerivation` with `.overrideAttrs`.
180
181For instance, here is how you could add some code to be performed in the derivation after installation is complete:
182
183```nix
184rocqPackages.multinomials.overrideAttrs (oldAttrs: {
185 postInstall = oldAttrs.postInstall or "" + ''
186 echo "you can do anything you want here"
187 '';
188})
189```