Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating shell.nix to flakes #114

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 51 additions & 53 deletions src/recipes/nix-build-system.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,69 @@
# Recipe: Nix as development environment
# Recipe: Flakes as development environment
jaoleal marked this conversation as resolved.
Show resolved Hide resolved

**Disclaimer**: _Currently the following steps are tested and confirmed to work on Linux only._
**Disclaimer**: _Currently the following steps are tested and confirmed to work on NixOS only._

[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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the introduction about NixOS? I don't think Flakes should be explained before NixOS itself.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a little redundant since the relation is with flakes feature not nix package manager. But this doesnt change nothing after all

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't remove this.

[Flakes](https://nixos.wiki/wiki/Flakes) Flakes is a feature of managing Nix packages to simplify usability and improve reproducibility of Nix installations.

This tutorial assumes that Nix is [installed](https://nixos.org/download.html#nix-quick-install) in your system.
This tutorial assumes that you are on a [NixOs](https://nixos.wiki/wiki/Main_Page) system and have [Flakes](https://nixos.wiki/wiki/Flakes) enabled.
jaoleal marked this conversation as resolved.
Show resolved Hide resolved

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.


## Specifying dependencies

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.
Now to the Nix part of the tutorial. In your project directory create a `flake.nix` file. Later on, this file will be used by Flakes to define the dependencies of your project. Below are the default content of `flake.nix` to run the sample project. We will also explain it in brief about the meaning each line of code.

```nix
let
# Get an up-to-date package for enabling OpenGL support in Nix
nixgl = import (fetchTarball "https://github.com/guibou/nixGL/archive/master.tar.gz") {};

# Pin the version of the nix package repository that has Godot 3.2.3 and compatible with godot-rust 0.9.3
# You might want to update the commit hash into the one that have your desired version of Godot
# You could search for the commit hash of a particular package by using this website https://lazamar.co.uk/nix-versions
pkgs = import (fetchTarball "https://github.com/nixos/nixpkgs/archive/5658fadedb748cb0bdbcb569a53bd6065a5704a9.tar.gz") {};
in
# Configure the dependency of your shell
# Add support for clang for bindgen in godot-rust
pkgs.mkShell.override { stdenv = pkgs.clangStdenv; } {
buildInputs = [
# Rust related dependencies
pkgs.rustc
pkgs.cargo
pkgs.rustfmt
pkgs.libclang

# Godot Engine Editor
pkgs.godot

# The support for OpenGL in Nix
nixgl.auto.nixGLDefault
{
# A string to define some description to the flakes
description = "GdNative Rust Shell";
# Will push all updated references to the deps
inputs = {
# Get the last available version of all nix packages
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
# Defines rust overlay so we can use it later on our shell
rust-overlay.url = "github:oxalica/rust-overlay";
# Some utils for reproducibility
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
#In flake-utils enabling reproducibility for the local system to be referenced
let
overlays = [
(import rust-overlay)
];

# Point bindgen to where the clang library would be
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
# Make clang aware of a few headers (stdbool.h, wchar.h)
BINDGEN_EXTRA_CLANG_ARGS = with pkgs; ''
-isystem ${llvmPackages.libclang.lib}/lib/clang/${lib.getVersion clang}/include
-isystem ${llvmPackages.libclang.out}/lib/clang/${lib.getVersion clang}/include
-isystem ${glibc.dev}/include
'';

# For Rust language server and rust-analyzer
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";

# Alias the godot engine to use nixGL
shellHook = ''
alias godot="nixGL godot -e"
'';
}
pkgs = import nixpkgs {
inherit system overlays;
};
in
with pkgs;
{
devShells.default = mkShell.override { stdenv = pkgs.clangStdenv; } {
# Point bindgen to where the clang library would be
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
# Make clang aware of a few headers (stdbool.h, wchar.h)
BINDGEN_EXTRA_CLANG_ARGS = with pkgs; ''
-isystem ${llvmPackages.libclang.lib}/lib/clang/${lib.getVersion clang}/include
-isystem ${llvmPackages.libclang.out}/lib/clang/${lib.getVersion clang}/include
-isystem ${glibc.dev}/include
''
# Setup all the packages that we will need to develop with gdnative.
buildInputs = [
openssl
pkg-config
rust-bin.stable.latest.default
godot3
];
};
}
);
}
```

If you get any errors about missing headers, you can use [`nix-locate`](https://github.com/bennofs/nix-index#usage) to search for them, e.g. `nix-locate 'include/wchar.h' | grep -v '^('` (the `grep -v` hides indirect packages), and then add the matching Nix package via the `BINDGEN_EXTRA_CLANG_ARGS` env var like above ([context](https://github.com/NixOS/nixpkgs/issues/52447#issuecomment-853429315)).


## Activating the Nix environment

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.

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.
## Activating the Flakes environment

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.
In the same directory where you created your flake.nix, you can use the comamnd [`nix develop`](https://nixos.wiki/wiki/Development_environment_with_nix-shell#nix-develop) to load our Flakes config and setup everything for your NixOS system.
jaoleal marked this conversation as resolved.
Show resolved Hide resolved