|
| 1 | +# Nix as A Development Environment |
| 2 | + |
| 3 | +**Disclaimer**: _Currently the following steps are tested and confirmed to work on Linux only._ |
| 4 | + |
| 5 | +[Nix](https://nixos.org/) is a package manager that employs a pure functional approach to dependency management. Nix packages are built and ran in isolated environments. It makes them more portable, but also harder to author. This tutorial will walk you through the process of setting up a package for Godot, GDNative and Rust with Nix. |
| 6 | + |
| 7 | +This tutorial assumes that Nix is [installed](https://nixos.org/download.html#nix-quick-install) in your system. |
| 8 | + |
| 9 | +To begin with, we are going to create a new project using the [Hello, world!](../getting-started/hello-world.md) guide in Getting Started. Note that the full source code for the project is available at https://github.com/godot-rust/godot-rust/tree/master/examples/hello_world. Because we aren't using the default build system explained in [setup](../getting-started/setup.md), you should only be worried about the content of the project rather than the dependencies. |
| 10 | + |
| 11 | +## Specifying Dependencies |
| 12 | + |
| 13 | +Now to the Nix part of the tutorial. In the root directory of the project (where `project.godot` is located), create a new file called `shell.nix`. Later on, this file will be evaluated by Nix to define the dependencies of your project. Below are the default content of `shell.nix` to run the sample project. We will also explain it in brief about the meaning each line of code. |
| 14 | + |
| 15 | +```nix |
| 16 | +let |
| 17 | + # Get an up-to-date package for enabling OpenGL support in Nix |
| 18 | + nixgl = import (fetchTarball "https://github.com/guibou/nixGL/archive/master.tar.gz") {}; |
| 19 | +
|
| 20 | + # Pin the version of the nix package repository that has Godot 3.2.3 and compatible with godot-rust 0.9.3 |
| 21 | + # You might want to update the commit hash into the one that have your desired version of Godot |
| 22 | + # You could search for the commit hash of a particular package by using this website https://lazamar.co.uk/nix-versions |
| 23 | + pkgs = import (fetchTarball "https://github.com/nixos/nixpkgs/archive/5658fadedb748cb0bdbcb569a53bd6065a5704a9.tar.gz") {}; |
| 24 | +in |
| 25 | + # Configure the dependency of your shell |
| 26 | + # Add support for clang for bindgen in godot-rust |
| 27 | + pkgs.mkShell.override { stdenv = pkgs.clangStdenv; } { |
| 28 | + buildInputs = [ |
| 29 | + # Rust related dependencies |
| 30 | + pkgs.rustc |
| 31 | + pkgs.cargo |
| 32 | + pkgs.rustfmt |
| 33 | + pkgs.libclang |
| 34 | +
|
| 35 | + # Godot Engine Editor |
| 36 | + pkgs.godot |
| 37 | +
|
| 38 | + # The support for OpenGL in Nix |
| 39 | + nixgl.nixGLDefault |
| 40 | + ]; |
| 41 | +
|
| 42 | + # Point bindgen to where the clang library would be |
| 43 | + LIBCLANG_PATH = "${pkgs.libclang.lib}/lib"; |
| 44 | +
|
| 45 | + # For rust language server and rust-analyzer |
| 46 | + RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; |
| 47 | +
|
| 48 | + # Alias the godot engine to use nixGL |
| 49 | + shellHook = '' |
| 50 | + alias godot="nixGL godot -e" |
| 51 | + ''; |
| 52 | + } |
| 53 | +``` |
| 54 | + |
| 55 | +## Activating The Nix Environment |
| 56 | + |
| 57 | +One of the simplest way to activate the nix environment is to use the `nix-shell` command. This program is installed automatically as you install Nix Package Manager. |
| 58 | + |
| 59 | +First, you need to open the root directory of your project. And then to activate your environment, run `nix-shell -v` into your terminal. The optional `-v` flag in the command will configure the command to be more verbose and display what kinds of things is getting installed. Because this is your first time using `nix-shell` on this particular project, it will take some time to download and install all the required dependencies. Subsequent run will be a lot faster after the installation. |
| 60 | + |
| 61 | +To run the project, first you need to compile the `hello-world` rust library using `cargo build`. After that, you can open the Godot Engine in your terminal using the command `godot`. As seen in `shell.nix`, this command is actually aliased to `nixGL godot -e` in which Godot will be opened using nixGL instead of opening it directly. After running the default scene, you should be able to see a single `hello, world.` printed in the Godot terminal. |
0 commit comments