1# Crystal {#crystal}
2
3## Building a Crystal package
4
5This section uses [Mint](https://github.com/mint-lang/mint) as an example for how to build a Crystal package.
6
7If the Crystal project has any dependencies, the first step is to get a `shards.nix` file encoding those. Get a copy of the project and go to its root directory such that its `shard.lock` file is in the current directory, then run `crystal2nix` in it
8```bash
9$ git clone https://github.com/mint-lang/mint
10$ cd mint
11$ git checkout 0.5.0
12$ nix-shell -p crystal2nix --run crystal2nix
13```
14
15This should have generated a `shards.nix` file.
16
17Next create a Nix file for your derivation and use `pkgs.crystal.buildCrystalPackage` as follows:
18```nix
19with import <nixpkgs> {};
20crystal.buildCrystalPackage rec {
21 pname = "mint";
22 version = "0.5.0";
23
24 src = fetchFromGitHub {
25 owner = "mint-lang";
26 repo = "mint";
27 rev = version;
28 sha256 = "0vxbx38c390rd2ysvbwgh89v2232sh5rbsp3nk9wzb70jybpslvl";
29 };
30
31 # Insert the path to your shards.nix file here
32 shardsFile = ./shards.nix;
33
34 ...
35}
36```
37
38This won't build anything yet, because we haven't told it what files build. We can specify a mapping from binary names to source files with the `crystalBinaries` attribute. The project's compilation instructions should show this. For Mint, the binary is called "mint", which is compiled from the source file `src/mint.cr`, so we'll specify this as follows:
39
40```nix
41 crystalBinaries.mint.src = "src/mint.cr";
42
43 # ...
44```
45
46Additionally you can override the default `crystal build` options (which are currently `--release --progress --no-debug --verbose`) with
47
48```nix
49 crystalBinaries.mint.options = [ "--release" "--verbose" ];
50```
51
52Depending on the project, you might need additional steps to get it to compile successfully. In Mint's case, we need to link against openssl, so in the end the Nix file looks as follows:
53
54```nix
55with import <nixpkgs> {};
56crystal.buildCrystalPackage rec {
57 version = "0.5.0";
58 pname = "mint";
59 src = fetchFromGitHub {
60 owner = "mint-lang";
61 repo = "mint";
62 rev = version;
63 sha256 = "0vxbx38c390rd2ysvbwgh89v2232sh5rbsp3nk9wzb70jybpslvl";
64 };
65
66 shardsFile = ./shards.nix;
67 crystalBinaries.mint.src = "src/mint.cr";
68
69 buildInputs = [ openssl ];
70}
71```