···2233## How to use Agda
4455-Agda can be installed from `agda`:
66-```ShellSession
77-$ nix-env -iA agda
88-```
55+Agda is available as the [agda](https://search.nixos.org/packages?channel=unstable&show=agda&from=0&size=30&sort=relevance&query=agda)
66+package.
971010-To use Agda with libraries, the `agda.withPackages` function can be used. This function either takes:
88+The `agda` package installs an Agda-wrapper, which calls `agda` with `--library-file`
99+set to a generated library-file within the nix store, this means your library-file in
1010+`$HOME/.agda/libraries` will be ignored. By default the agda package installs Agda
1111+with no libraries, i.e. the generated library-file is empty. To use Agda with libraries,
1212+the `agda.withPackages` function can be used. This function either takes:
11131214* A list of packages,
1315* or a function which returns a list of packages when given the `agdaPackages` attribute set,
1416* or an attribute set containing a list of packages and a GHC derivation for compilation (see below).
1717+* or an attribute set containing a function which returns a list of packages when given the `agdaPackages` attribute set and a GHC derivation for compilation (see below).
15181619For example, suppose we wanted a version of Agda which has access to the standard library. This can be obtained with the expressions:
1720···27302831or can be called as in the [Compiling Agda](#compiling-agda) section.
29323030-If you want to use a library in your home directory (for instance if it is a development version) then typecheck it manually (using `agda.withPackages` if necessary) and then override the `src` attribute of the package to point to your local repository.
3333+If you want to use a different version of a library (for instance a development version)
3434+override the `src` attribute of the package to point to your local repository
3535+3636+```nix
3737+agda.withPackages (p: [
3838+ (p.standard-library.overrideAttrs (oldAttrs: {
3939+ version = "local version";
4040+ src = /path/to/local/repo/agda-stdlib;
4141+ }))
4242+])
4343+```
4444+4545+You can also reference a GitHub repository
4646+```nix
4747+agda.withPackages (p: [
4848+ (p.standard-library.overrideAttrs (oldAttrs: {
4949+ version = "1.5";
5050+ src = fetchFromGitHub {
5151+ repo = "agda-stdlib";
5252+ owner = "agda";
5353+ rev = "v1.5";
5454+ sha256 = "16fcb7ssj6kj687a042afaa2gq48rc8abihpm14k684ncihb2k4w";
5555+ };
5656+ }))
5757+])
5858+```
5959+6060+If you want to use a library not added to Nixpkgs, you can add a
6161+dependency to a local library by calling `agdaPackages.mkDerivation`.
6262+```nix
6363+agda.withPackages (p: [
6464+ (p.mkDerivation {
6565+ pname = "your-agda-lib";
6666+ version = "1.0.0";
6767+ src = /path/to/your-agda-lib;
6868+ })
6969+])
7070+```
7171+7272+Again you can reference GitHub
7373+7474+```nix
7575+agda.withPackages (p: [
7676+ (p.mkDerivation {
7777+ pname = "your-agda-lib";
7878+ version = "1.0.0";
7979+ src = fetchFromGitHub {
8080+ repo = "repo";
8181+ owner = "owner";
8282+ version = "...";
8383+ rev = "...";
8484+ sha256 = "...";
8585+ };
8686+ })
8787+])
8888+```
8989+9090+See [Building Agda Packages](#building-agda-packages) for more information on `mkDerivation`.
31913232-Agda will not by default use these libraries. To tell Agda to use the library we have some options:
9292+Agda will not by default use these libraries. To tell Agda to use a library we have some options:
33933494* Call `agda` with the library flag:
3595```ShellSession
···46106More information can be found in the [official Agda documentation on library management](https://agda.readthedocs.io/en/v2.6.1/tools/package-system.html).
4710748108## Compiling Agda
4949-Agda modules can be compiled with the `--compile` flag. A version of `ghc` with `ieee754` is made available to the Agda program via the `--with-compiler` flag.
109109+Agda modules can be compiled using the GHC backend with the `--compile` flag. A version of `ghc` with `ieee754` is made available to the Agda program via the `--with-compiler` flag.
50110This can be overridden by a different version of `ghc` as follows:
5111152112```nix
···65125* `libraryName` should be the name that appears in the `*.agda-lib` file, defaulting to `pname`.
66126* `libraryFile` should be the file name of the `*.agda-lib` file, defaulting to `${libraryName}.agda-lib`.
67127128128+Here is an example `default.nix`
129129+130130+```nix
131131+{ nixpkgs ? <nixpkgs> }:
132132+with (import nixpkgs {});
133133+agdaPackages.mkDerivation {
134134+ version = "1.0";
135135+ pname = "my-agda-lib";
136136+ src = ./.;
137137+ buildInputs = [
138138+ agdaPackages.standard-library
139139+ ];
140140+}
141141+```
142142+68143### Building Agda packages
69144The default build phase for `agdaPackages.mkDerivation` simply runs `agda` on the `Everything.agda` file.
70145If something else is needed to build the package (e.g. `make`) then the `buildPhase` should be overridden.
···80155## Adding Agda packages to Nixpkgs
8115682157To add an Agda package to `nixpkgs`, the derivation should be written to `pkgs/development/libraries/agda/${library-name}/` and an entry should be added to `pkgs/top-level/agda-packages.nix`. Here it is called in a scope with access to all other Agda libraries, so the top line of the `default.nix` can look like:
158158+83159```nix
84160{ mkDerivation, standard-library, fetchFromGitHub }:
85161```
8686-and `mkDerivation` should be called instead of `agdaPackages.mkDerivation`. Here is an example skeleton derivation for iowa-stdlib:
162162+163163+Note that the derivation function is called with `mkDerivation` set to `agdaPackages.mkDerivation`, therefore you
164164+could use a similar set as in your `default.nix` from [Writing Agda Packages](#writing-agda-packages) with
165165+`agdaPackages.mkDerivation` replaced with `mkDerivation`.
166166+167167+Here is an example skeleton derivation for iowa-stdlib:
8716888169```nix
89170mkDerivation {