Nix flake build for Lean 4.
Features:
- Lean overlay
- Automatically read toolchain version
- Convert
lake-manifest.jsoninto Lean build
The default template is a good starting point for projects requiring manual dependency management:
nix flake new --template github:lenianiva/lean4-nix ./minimalThe .#dependency template shows an example of using lake-manifest.json to
fetch dependencies automatically.
nix flake new --template github:lenianiva/lean4-nix#dependency ./dependencyThe user must decide on a Lean version to use as overlay. The minimal supported
version is v4.11.0, since it is the version when Lean's official Nix flake was
deprecated. There are a couple of ways to get an overlay. Each corresponds to a
flake output:
readSrc { src; bootstrap; }: Builds Lean from a source folder. A bootstrapping function must be provided.readFromGit{ args; bootstrap; }: Given parameters tobuiltins.fetchGit, download a git repositoryreadRev { rev; bootstrap; }: Reads a revision from the official Lean 4 repositoryreadToolchainFile: Reads the toolchain from a file. Due to Nix's pure evaluation principle, this only supportsleanprover/lean4:{tag}basedlean-toolchainfiles. For any other toolchains, usereadRevorreadFromGit.tags.{tag}: Lean4 tags. See the available tags inmanifests/
Then apply the overlay on pkgs:
pkgs = import nixpkgs {
inherit system;
overlays = [ (lean4-nix.readToolchainFile ./lean-toolchain) ];
};and pkgs.lean will be replaced by the chosen overlay.
This attribute set has properties
lean: The Lean executablelean-all:lean,lake, and the Lean library.example: Usenix run .#exampleto see an example of building a Lean program.Init,Std,Lean: Lean built-in libraries provided in the same format asbuildLeanPackage
and the function buildLeanPackage, which accepts a parameter set
{ name; roots; deps; src; }. The complete parameter set can be found in Lean
4's nix/buildLeanPackage.nix file. In general:
src: The source directoryroots: Lean modules at the root of the import tree.deps: A list of outputs of otherbuildLeanPackagecalls.
This is a form of manual dependency management.
Use lake2nix = lean4-nix.lake { inherit pkgs; } to generate the lake utilities.
lake2nix.mkPackage { src; roots; } automatically reads the
lake-manifest.json file and builds dependencies.
src: The source directorymanifestFile: Path to the manifest file. Defaults to${src}/lake-manifest.jsonroots: Lean modules at the root of the import tree. Defaults to the project name frommanifestFiledeps: Additional dependencies. Defaults to[ Init Std Lean ].
The buildLeanPackage and mkPackage functions output the built Lean package
in a non-derivation format. Generally, the attributes available are:
executable: ExecutablesharedLib: Shared librarymodRoot: Module root. SetLEAN_PATHto this to provide context for LSP.cTree,oTree,iTree: Trees of C files/.ofiles/.ileanfiles
If you see this error, add these packages to deps in either buildLeanPackage
or mkPackage.
{
deps = with pkgs.lean; [ Init Std Lean ];
}The Lean version is not listed in the manifests/ directory. Use readRev or
readFromGit instead.
Use nix flake check to check the template builds.
Update the template lean-toolchain files when new Lean versions come out.
All code must be formatted with alejandra before merging into main. To use it, execute
nix fmt