nixlib

Alyssa's collection of Nix expressions
git clone git clone https://edef.eu/~qyliss/nixlib.git
Log | Files | Refs | README | LICENSE

crystal.section.md (2257B)


      1 # Crystal
      2 
      3 ## Building a Crystal package
      4 
      5 This section uses [Mint](https://github.com/mint-lang/mint) as an example for how to build a Crystal package.
      6 
      7 If 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 
     15 This should have generated a `shards.nix` file.
     16 
     17 Next create a Nix file for your derivation and use `pkgs.crystal.buildCrystalPackage` as follows:
     18 ```nix
     19 with import <nixpkgs> {};
     20 crystal.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 
     38 This 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 
     46 Additionally 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 
     52 Depending 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
     55 with import <nixpkgs> {};
     56 crystal.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_1_0_2 ];
     70 }
     71 ```