OCaml: Difference between revisions
imported>Mic92 fix markup |
overrideScope |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 10: | Line 10: | ||
This hook also sets the '''CAML_LD_LIBRARY_PATH''' environment variable that is used for locating the dynamically loaded shared libraries (aka stublibs). | This hook also sets the '''CAML_LD_LIBRARY_PATH''' environment variable that is used for locating the dynamically loaded shared libraries (aka stublibs). | ||
== Scripting with OCaml == | |||
OCaml can be used in nix-shell scripts as follows: | |||
<syntaxHighlight lang=ocaml> | |||
#!/usr/bin/env nix-shell | |||
(* | |||
#!nix-shell --pure -i ocaml -p ocaml | |||
*) | |||
print_string "Hello world! 🚀 \n";; | |||
</syntaxHighlight> | |||
== Using Emacs == | == Using Emacs == | ||
Line 18: | Line 29: | ||
Emacs must be configured to be able to find this package: you may need to add a line like this to your '''.emacs''' file, somewhere between <code>(require 'package)</code> and <code>(package-initialize)</code>: | Emacs must be configured to be able to find this package: you may need to add a line like this to your '''.emacs''' file, somewhere between <code>(require 'package)</code> and <code>(package-initialize)</code>: | ||
<syntaxHighlight lang=emacs> | |||
(add-to-list 'package-directory-list "~/.nix-profile/share/emacs/site-lisp/elpa") | |||
</syntaxHighlight> | |||
There is some more documentation in the [https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/emacs-packages.nix nixpkgs sources]. | There is some more documentation in the [https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/emacs-packages.nix nixpkgs sources]. | ||
Line 28: | Line 41: | ||
To configure Emacs and enable the merlin mode, add the following to your '''.emacs''': | To configure Emacs and enable the merlin mode, add the following to your '''.emacs''': | ||
<syntaxHighlight lang=emacs> | |||
(add-to-list 'load-path "~/.nix-profile/share/emacs/site-lisp") | |||
(require 'merlin) | |||
(add-hook 'tuareg-mode-hook 'merlin-mode t) | |||
</syntaxHighlight> | |||
Also, a specific variable ('''merlin-command''') of the merlin mode must be overridden (its default is '''opam'''): | Also, a specific variable ('''merlin-command''') of the merlin mode must be overridden (its default is '''opam'''): | ||
<syntaxHighlight lang=emacs> | |||
(custom-set-variables | |||
'(merlin-command "ocamlmerlin") | |||
) | |||
</syntaxHighlight> | |||
A useful complement to merlin is [https://www.typerex.org/ocp-indent.html '''ocp-indent'''], a customizable tool to indent OCaml code. It can be installed using <code>nix-env -iA nixpkgs.ocamlPackages.ocpIndent</code>. To enable it in Emacs, don’t forget to add the following line to your '''.emacs''': | A useful complement to merlin is [https://www.typerex.org/ocp-indent.html '''ocp-indent'''], a customizable tool to indent OCaml code. It can be installed using <code>nix-env -iA nixpkgs.ocamlPackages.ocpIndent</code>. To enable it in Emacs, don’t forget to add the following line to your '''.emacs''': | ||
<syntaxHighlight lang=emacs> | |||
(require 'ocp-indent) | |||
</syntaxHighlight> | |||
== Specific version of the OCaml compiler == | == Specific version of the OCaml compiler == | ||
Various versions of the '''ocamlPackage'' attribute set are available, corresponding to various versions of OCaml. For instance, the attribute set '''ocaml-ng.ocamlPackages_4_04''' contains the OCaml compiler at version 4.04 and OCaml libraries compiled with that particular compiler. | Various versions of the '''ocamlPackage''' attribute set are available, corresponding to various versions of OCaml. For instance, the attribute set '''ocaml-ng.ocamlPackages_4_04''' contains the OCaml compiler at version 4.04 and OCaml libraries compiled with that particular compiler. | ||
=== Custom version === | |||
So as to get the set of OCaml libraries built with/for a custom version of the OCaml compiler, e.g., to enable '''flambda''' support, you may use the '''ocamlPackages.overrideScope''' function: | |||
<syntaxHighlight lang=nix> | |||
ocamlPackagesFlambda = ocamlPackages.overrideScope (self: super: { | |||
ocaml = super.ocaml.override { flambdaSupport = true; }; | |||
}); | |||
</syntaxHighlight> | |||
More details: https://github.com/NixOS/nixpkgs/pull/53357#issuecomment-451727433 |
Latest revision as of 12:04, 12 July 2024
Most packages related to OCaml, in particular the OCaml compiler and many libraries, belong to the ocamlPackages attribute set of nixpkgs.
Findlib, ocamlfind
OCaml libraries are usually located using findlib and the associated ocamlfind tool. These tools are found under the ocamlPackages.findlib attribute and rely on the OCAMLPATH environment variable.
A hook in the findlib package will automatically populate this variable with the paths to the other libraries. For instance, when starting a shell with
nix-shell --packages ocamlPackages.findlib ocamlPackages.batteries
will set the OCAMLPATH variable so that ocamlfind can locate the batteries library.
This hook also sets the CAML_LD_LIBRARY_PATH environment variable that is used for locating the dynamically loaded shared libraries (aka stublibs).
Scripting with OCaml
OCaml can be used in nix-shell scripts as follows:
#!/usr/bin/env nix-shell
(*
#!nix-shell --pure -i ocaml -p ocaml
*)
print_string "Hello world! 🚀 \n";;
Using Emacs
Tuareg is an Emacs OCaml mode. It can be installed with
nix-env -iA nixpkgs.emacsPackagesNg.tuareg
.
Emacs must be configured to be able to find this package: you may need to add a line like this to your .emacs file, somewhere between (require 'package)
and (package-initialize)
:
(add-to-list 'package-directory-list "~/.nix-profile/share/emacs/site-lisp/elpa")
There is some more documentation in the nixpkgs sources.
Merlin provides additional functionality to Emacs. It can be installed through nix-env -iA nixpkgs.ocamlPackages.merlin
.
Beware that merlin is specific to one particular version of OCaml (it won’t work correctly with a compiler of a different version).
To configure Emacs and enable the merlin mode, add the following to your .emacs:
(add-to-list 'load-path "~/.nix-profile/share/emacs/site-lisp")
(require 'merlin)
(add-hook 'tuareg-mode-hook 'merlin-mode t)
Also, a specific variable (merlin-command) of the merlin mode must be overridden (its default is opam):
(custom-set-variables
'(merlin-command "ocamlmerlin")
)
A useful complement to merlin is ocp-indent, a customizable tool to indent OCaml code. It can be installed using nix-env -iA nixpkgs.ocamlPackages.ocpIndent
. To enable it in Emacs, don’t forget to add the following line to your .emacs:
(require 'ocp-indent)
Specific version of the OCaml compiler
Various versions of the ocamlPackage attribute set are available, corresponding to various versions of OCaml. For instance, the attribute set ocaml-ng.ocamlPackages_4_04 contains the OCaml compiler at version 4.04 and OCaml libraries compiled with that particular compiler.
Custom version
So as to get the set of OCaml libraries built with/for a custom version of the OCaml compiler, e.g., to enable flambda support, you may use the ocamlPackages.overrideScope function:
ocamlPackagesFlambda = ocamlPackages.overrideScope (self: super: {
ocaml = super.ocaml.override { flambdaSupport = true; };
});
More details: https://github.com/NixOS/nixpkgs/pull/53357#issuecomment-451727433