···11# Nim {#nim}
2233-## Overview {#nim-overview}
44-55-The Nim compiler, a builder function, and some packaged libraries are available
66-in Nixpkgs. Until now each compiler release has been effectively backwards
77-compatible so only the latest version is available.
88-99-## Nim program packages in Nixpkgs {#nim-program-packages-in-nixpkgs}
1010-1111-Nim programs can be built using `nimPackages.buildNimPackage`. In the
1212-case of packages not containing exported library code the attribute
1313-`nimBinOnly` should be set to `true`.
33+The Nim compiler and a builder function is available.
44+Nim programs are built using `buildNimPackage` and a lockfile containing Nim dependencies.
145156The following example shows a Nim program that depends only on Nim libraries:
1616-177```nix
1818-{ lib, nimPackages, fetchFromGitHub }:
88+{ lib, buildNimPackage, fetchFromGitHub }:
1992020-nimPackages.buildNimPackage (finalAttrs: {
1010+buildNimPackage { } (finalAttrs: {
2111 pname = "ttop";
2222- version = "1.0.1";
2323- nimBinOnly = true;
1212+ version = "1.2.7";
24132514 src = fetchFromGitHub {
2615 owner = "inv2004";
2716 repo = "ttop";
2817 rev = "v${finalAttrs.version}";
2929- hash = "sha256-x4Uczksh6p3XX/IMrOFtBxIleVHdAPX9e8n32VAUTC4=";
1818+ hash = "sha256-oPdaUqh6eN1X5kAYVvevOndkB/xnQng9QVLX9bu5P5E=";
3019 };
31203232- buildInputs = with nimPackages; [ asciigraph illwill parsetoml zippy ];
3333-3434-})
3535-```
3636-3737-## Nim library packages in Nixpkgs {#nim-library-packages-in-nixpkgs}
3838-3939-4040-Nim libraries can also be built using `nimPackages.buildNimPackage`, but
4141-often the product of a fetcher is sufficient to satisfy a dependency.
4242-The `fetchgit`, `fetchFromGitHub`, and `fetchNimble` functions yield an
4343-output that can be discovered during the `configurePhase` of `buildNimPackage`.
2121+ lockFile = ./lock.json;
44224545-Nim library packages are listed in
4646-[pkgs/top-level/nim-packages.nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/nim-packages.nix) and implemented at
4747-[pkgs/development/nim-packages](https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/nim-packages).
4848-4949-The following example shows a Nim library that propagates a dependency on a
5050-non-Nim package:
5151-```nix
5252-{ lib, buildNimPackage, fetchNimble, SDL2 }:
5353-5454-buildNimPackage (finalAttrs: {
5555- pname = "sdl2";
5656- version = "2.0.4";
5757- src = fetchNimble {
5858- inherit (finalAttrs) pname version;
5959- hash = "sha256-Vtcj8goI4zZPQs2TbFoBFlcR5UqDtOldaXSH/+/xULk=";
6060- };
6161- propagatedBuildInputs = [ SDL2 ];
2323+ nimFlags = [
2424+ "-d:NimblePkgVersion=${finalAttrs.version}"
2525+ ];
6226})
6327```
64286529## `buildNimPackage` parameters {#buildnimpackage-parameters}
66306767-All parameters from `stdenv.mkDerivation` function are still supported. The
6868-following are specific to `buildNimPackage`:
3131+The `buildNimPackage` function takes an attrset of parameters that are passed on to `stdenv.mkDerivation`.
3232+3333+The following parameters are specific to `buildNimPackage`:
69347070-* `nimBinOnly ? false`: If `true` then build only the programs listed in
7171- the Nimble file in the packages sources.
3535+* `lockFile`: JSON formatted lockfile.
7236* `nimbleFile`: Specify the Nimble file location of the package being built
7337 rather than discover the file at build-time.
7438* `nimRelease ? true`: Build the package in *release* mode.
···7741 Use this to specify defines with arguments in the form of `-d:${name}=${value}`.
7842* `nimDoc` ? false`: Build and install HTML documentation.
79438080-* `buildInputs` ? []: The packages listed here will be searched for `*.nimble`
8181- files which are used to populate the Nim library path. Otherwise the standard
8282- behavior is in effect.
4444+## Lockfiles {#nim-lockfiles}
4545+Nim lockfiles are created with the `nim_lk` utility.
4646+Run `nim_lk` with the source directory as an argument and it will print a lockfile to stdout.
4747+```sh
4848+$ cd nixpkgs
4949+$ nix build -f . ttop.src
5050+$ nix run -f . nim_lk ./result | jq --sort-keys > pkgs/by-name/tt/ttop/lock.json
5151+```
5252+5353+## Lockfile dependency overrides {#nimoverrides}
5454+5555+The `buildNimPackage` function matches the libraries specified by `lockFile` to attrset of override functions that are then applied to the package derivation.
5656+The default overrides are maintained as the top-level `nimOverrides` attrset at `pkgs/top-level/nim-overrides.nix`.
5757+5858+For example, to propagate a dependency on SDL2 for lockfiles that select the Nim `sdl2` library, an overlay is added to the set in the `nim-overrides.nix` file:
5959+```nix
6060+{ lib
6161+/* … */
6262+, SDL2
6363+/* … */
6464+}:
6565+6666+{
6767+ /* … */
6868+ sdl2 =
6969+ lockAttrs:
7070+ finalAttrs:
7171+ { buildInputs ? [ ], ... }:
7272+ {
7373+ buildInputs = buildInputs ++ [ SDL2 ];
7474+ };
7575+ /* … */
7676+}
7777+```
7878+7979+The annotations in the `nim-overrides.nix` set are functions that take three arguments and return a new attrset to be overlayed on the package being built.
8080+- lockAttrs: the attrset for this library from within a lockfile. This can be used to implement library version constraints, such as marking libraries as broken or insecure.
8181+- finalAttrs: the final attrset passed by `buildNimPackage` to `stdenv.mkDerivation`.
8282+- prevAttrs: the attrset produced by initial arguments to `buildNimPackage` and any preceding lockfile overlays.
8383+8484+### Overriding an Nim library override {#nimoverrides-overrides}
8585+8686+The `nimOverrides` attrset makes it possible to modify overrides in a few different ways.
8787+8888+Override a package internal to its definition:
8989+```nix
9090+{ lib, buildNimPackage, nimOverrides, libressl }:
9191+9292+let
9393+ buildNimPackage' = buildNimPackage.override {
9494+ nimOverrides = nimOverrides.override { openssl = libressl; };
9595+ };
9696+in buildNimPackage' (finalAttrs: {
9797+ pname = "foo";
9898+ # …
9999+})
100100+101101+```
102102+103103+Override a package externally:
104104+```nix
105105+{ pkgs }: {
106106+ foo = pkgs.foo.override {
107107+ buildNimPackage = pkgs.buildNimPackage.override {
108108+ nimOverrides = pkgs.nimOverrides.override { openssl = libressl; };
109109+ };
110110+ };
111111+}
112112+```
···133133 if err != 0: quit("build phase failed", err)
134134135135proc installPhase*() =
136136- ## Install the Nim sources if ``nimBinOnly`` is not
136136+ ## Install the Nim sources if ``nimCopySources`` is
137137 ## set in the environment.
138138- if not getEnvBool"nimBinOnly":
138138+ if getEnvBool"nimCopySources":
139139 let
140140 nf = getNimbleFilePath()
141141 srcDir = nf.getNimbleValue("srcDir", ".")
···11+{ lib
22+, SDL2
33+}:
44+55+# The following is list of overrides that take three arguments each:
66+# - lockAttrs: - an attrset from a Nim lockfile, use this for making constraints on the locked library
77+# - finalAttrs: - final arguments to the depender package
88+# - prevAttrs: - preceding arguments to the depender package
99+{
1010+1111+ sdl2 = lockAttrs: finalAttrs:
1212+ { buildInputs ? [ ], ... }: {
1313+ buildInputs = buildInputs ++ [ SDL2 ];
1414+ };
1515+1616+}