1# Nim {#nim}
2
3## Overview {#nim-overview}
4
5The Nim compiler, a builder function, and some packaged libraries are available
6in Nixpkgs. Until now each compiler release has been effectively backwards
7compatible so only the latest version is available.
8
9## Nim program packages in Nixpkgs {#nim-program-packages-in-nixpkgs}
10
11Nim programs can be built using `nimPackages.buildNimPackage`. In the
12case of packages not containing exported library code the attribute
13`nimBinOnly` should be set to `true`.
14
15The following example shows a Nim program that depends only on Nim libraries:
16
17```nix
18{ lib, nimPackages, fetchFromGitHub }:
19
20nimPackages.buildNimPackage (finalAttrs: {
21 pname = "ttop";
22 version = "1.0.1";
23 nimBinOnly = true;
24
25 src = fetchFromGitHub {
26 owner = "inv2004";
27 repo = "ttop";
28 rev = "v${finalAttrs.version}";
29 hash = "sha256-x4Uczksh6p3XX/IMrOFtBxIleVHdAPX9e8n32VAUTC4=";
30 };
31
32 buildInputs = with nimPackages; [ asciigraph illwill parsetoml zippy ];
33
34})
35```
36
37## Nim library packages in Nixpkgs {#nim-library-packages-in-nixpkgs}
38
39
40Nim libraries can also be built using `nimPackages.buildNimPackage`, but
41often the product of a fetcher is sufficient to satisfy a dependency.
42The `fetchgit`, `fetchFromGitHub`, and `fetchNimble` functions yield an
43output that can be discovered during the `configurePhase` of `buildNimPackage`.
44
45Nim library packages are listed in
46[pkgs/top-level/nim-packages.nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/nim-packages.nix) and implemented at
47[pkgs/development/nim-packages](https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/nim-packages).
48
49The following example shows a Nim library that propagates a dependency on a
50non-Nim package:
51```nix
52{ lib, buildNimPackage, fetchNimble, SDL2 }:
53
54buildNimPackage (finalAttrs: {
55 pname = "sdl2";
56 version = "2.0.4";
57 src = fetchNimble {
58 inherit (finalAttrs) pname version;
59 hash = "sha256-Vtcj8goI4zZPQs2TbFoBFlcR5UqDtOldaXSH/+/xULk=";
60 };
61 propagatedBuildInputs = [ SDL2 ];
62})
63```
64
65## `buildNimPackage` parameters {#buildnimpackage-parameters}
66
67All parameters from `stdenv.mkDerivation` function are still supported. The
68following are specific to `buildNimPackage`:
69
70* `nimBinOnly ? false`: If `true` then build only the programs listed in
71 the Nimble file in the packages sources.
72* `nimbleFile`: Specify the Nimble file location of the package being built
73 rather than discover the file at build-time.
74* `nimRelease ? true`: Build the package in *release* mode.
75* `nimDefines ? []`: A list of Nim defines. Key-value tuples are not supported.
76* `nimFlags ? []`: A list of command line arguments to pass to the Nim compiler.
77 Use this to specify defines with arguments in the form of `-d:${name}=${value}`.
78* `nimDoc` ? false`: Build and install HTML documentation.
79
80* `buildInputs` ? []: The packages listed here will be searched for `*.nimble`
81 files which are used to populate the Nim library path. Otherwise the standard
82 behavior is in effect.