From 2ee0488033927c53a41b6042d91b339c2ae84048 Mon Sep 17 00:00:00 2001 From: Joao Leal Date: Thu, 29 Feb 2024 09:43:28 -0300 Subject: [PATCH 1/9] Update nix-build-system.md Updating the shell.nix to a flake.nix. See the Issue on gdnative for more understanding https://github.com/godot-rust/gdnative/pull/1069#issuecomment-1971001997 --- src/recipes/nix-build-system.md | 102 ++++++++++++++++---------------- 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/src/recipes/nix-build-system.md b/src/recipes/nix-build-system.md index 8e5bf46..b7256c0 100644 --- a/src/recipes/nix-build-system.md +++ b/src/recipes/nix-build-system.md @@ -1,71 +1,69 @@ # Recipe: Nix as development environment -**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. +[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. 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 flake + 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 Flake 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 that you made your flake.nix, you can use the comand ['nix develop'](https://nixos.wiki/wiki/Development_environment_with_nix-shell#nix-develop) to load our flake config and setup everything for your NixOs system. From 4bae3086053c6a28a1bb844cbc07c18db749b593 Mon Sep 17 00:00:00 2001 From: Joao Leal Date: Thu, 29 Feb 2024 10:55:28 -0300 Subject: [PATCH 2/9] non-brazilian english Co-authored-by: Jan Haller --- src/recipes/nix-build-system.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/recipes/nix-build-system.md b/src/recipes/nix-build-system.md index b7256c0..5442ed7 100644 --- a/src/recipes/nix-build-system.md +++ b/src/recipes/nix-build-system.md @@ -66,4 +66,4 @@ If you get any errors about missing headers, you can use [`nix-locate`](https:// ## Activating the Flake environment -In the same directory that you made your flake.nix, you can use the comand ['nix develop'](https://nixos.wiki/wiki/Development_environment_with_nix-shell#nix-develop) to load our flake config and setup everything for your NixOs system. +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 Flake config and setup everything for your NixOS system. From 0315ea7017cdb45a9196ab366d033f1615c549b5 Mon Sep 17 00:00:00 2001 From: Joao Leal Date: Thu, 29 Feb 2024 10:58:30 -0300 Subject: [PATCH 3/9] Capitalization Co-authored-by: Jan Haller --- src/recipes/nix-build-system.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/recipes/nix-build-system.md b/src/recipes/nix-build-system.md index 5442ed7..9ae4708 100644 --- a/src/recipes/nix-build-system.md +++ b/src/recipes/nix-build-system.md @@ -1,6 +1,6 @@ # Recipe: Nix as development environment -**Disclaimer**: _Currently the following steps are tested and confirmed to work on NixOs only._ +**Disclaimer**: _Currently the following steps are tested and confirmed to work on NixOS only._ [Flakes](https://nixos.wiki/wiki/Flakes) Flakes is a feature of managing Nix packages to simplify usability and improve reproducibility of Nix installations. From e6fa85b286ff732fb831569b05b6d135e9582368 Mon Sep 17 00:00:00 2001 From: Joao Leal Date: Thu, 29 Feb 2024 11:04:31 -0300 Subject: [PATCH 4/9] naming pattern --- src/recipes/nix-build-system.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/recipes/nix-build-system.md b/src/recipes/nix-build-system.md index 9ae4708..6f5fab5 100644 --- a/src/recipes/nix-build-system.md +++ b/src/recipes/nix-build-system.md @@ -1,4 +1,4 @@ -# Recipe: Nix as development environment +# Recipe: Flakes as development environment **Disclaimer**: _Currently the following steps are tested and confirmed to work on NixOS only._ @@ -15,7 +15,7 @@ Now to the Nix part of the tutorial. In your project directory create a `flake.n ```nix { - # A string to define some description to the flake + # A string to define some description to the flakes description = "GdNative Rust Shell"; # Will push all updated references to the deps inputs = { @@ -64,6 +64,6 @@ Now to the Nix part of the tutorial. In your project directory create a `flake.n 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 Flake environment +## Activating the Flakes environment -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 Flake config and setup everything for your NixOS system. +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. From 35bcfe91e3a0bad2049f43782175ee86ba49d9c0 Mon Sep 17 00:00:00 2001 From: Joao Leal Date: Thu, 29 Feb 2024 17:04:40 -0300 Subject: [PATCH 5/9] some details Co-authored-by: Jose Storopoli --- src/recipes/nix-build-system.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/recipes/nix-build-system.md b/src/recipes/nix-build-system.md index 6f5fab5..26dcc82 100644 --- a/src/recipes/nix-build-system.md +++ b/src/recipes/nix-build-system.md @@ -1,4 +1,4 @@ -# Recipe: Flakes as development environment +# Recipe: Nix Flakes as development environment **Disclaimer**: _Currently the following steps are tested and confirmed to work on NixOS only._ From dd312ab6d5a6f6b44ec3c133ad95882e9dec15eb Mon Sep 17 00:00:00 2001 From: Joao Leal Date: Thu, 29 Feb 2024 17:08:06 -0300 Subject: [PATCH 6/9] Update src/recipes/nix-build-system.md Co-authored-by: Jose Storopoli --- src/recipes/nix-build-system.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/recipes/nix-build-system.md b/src/recipes/nix-build-system.md index 26dcc82..ffd98bf 100644 --- a/src/recipes/nix-build-system.md +++ b/src/recipes/nix-build-system.md @@ -4,7 +4,9 @@ [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 you are on a [NixOs](https://nixos.wiki/wiki/Main_Page) system and have [Flakes](https://nixos.wiki/wiki/Flakes) enabled. +This tutorial assumes that you are on a [NixOs](https://nixos.wiki/wiki/Main_Page) system +or that Nix is [installed](https://nixos.org/download.html#nix-quick-install). +Finally, you also need to make sure that [Flakes](https://nixos.wiki/wiki/Flakes) is enabled. 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. From c2a6012a55806311557045237d32bac9da55f363 Mon Sep 17 00:00:00 2001 From: Joao Leal Date: Thu, 29 Feb 2024 17:08:21 -0300 Subject: [PATCH 7/9] nix included Co-authored-by: Jose Storopoli --- src/recipes/nix-build-system.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/recipes/nix-build-system.md b/src/recipes/nix-build-system.md index ffd98bf..bb411bf 100644 --- a/src/recipes/nix-build-system.md +++ b/src/recipes/nix-build-system.md @@ -68,4 +68,4 @@ If you get any errors about missing headers, you can use [`nix-locate`](https:// ## Activating the Flakes environment -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. +In the same directory where you created your flake.nix, you can use the command [`nix develop`](https://nixos.wiki/wiki/Development_environment_with_nix-shell#nix-develop) to load our Flakes config and setup everything for your Nix-capable system. From 14a53bf96f8a72952158f04727799c1cb0d80c6c Mon Sep 17 00:00:00 2001 From: Joao Leal Date: Thu, 29 Feb 2024 17:19:40 -0300 Subject: [PATCH 8/9] ShellHook alias Prevent Errors... gdnative needs to godot be casted as godot --- src/recipes/nix-build-system.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/recipes/nix-build-system.md b/src/recipes/nix-build-system.md index bb411bf..2ef0635 100644 --- a/src/recipes/nix-build-system.md +++ b/src/recipes/nix-build-system.md @@ -57,6 +57,9 @@ Now to the Nix part of the tutorial. In your project directory create a `flake.n rust-bin.stable.latest.default godot3 ]; + shellHook = '' + alias godot="godot3" + ''; }; } ); From 0c3ae475baf0f075fdd3f7262e2f20b40f928287 Mon Sep 17 00:00:00 2001 From: joaoleal Date: Fri, 1 Mar 2024 16:36:14 -0300 Subject: [PATCH 9/9] Deps explanation --- src/recipes/nix-build-system.md | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/recipes/nix-build-system.md b/src/recipes/nix-build-system.md index 2ef0635..653f0cd 100644 --- a/src/recipes/nix-build-system.md +++ b/src/recipes/nix-build-system.md @@ -2,13 +2,28 @@ **Disclaimer**: _Currently the following steps are tested and confirmed to work on NixOS only._ -[Flakes](https://nixos.wiki/wiki/Flakes) Flakes is a feature of managing Nix packages to simplify usability and improve reproducibility of Nix installations. +[Flakes](https://nixos.wiki/wiki/Flakes) Flakes is a widelly used experimental feature of managing Nix packages to simplify usability and improve reproducibility of Nix installations. -This tutorial assumes that you are on a [NixOs](https://nixos.wiki/wiki/Main_Page) system -or that Nix is [installed](https://nixos.org/download.html#nix-quick-install). -Finally, you also need to make sure that [Flakes](https://nixos.wiki/wiki/Flakes) is enabled. +Please read carefully all this page before starting to use gdnative on NixOS. -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. +This tutorial assumes that you are on a [NixOs](https://nixos.wiki/wiki/Main_Page) system or that Nix is [installed](https://nixos.org/download.html#nix-quick-install). +And so on, you also need to make sure that [Flakes](https://nixos.wiki/wiki/Flakes) is enabled. + +To begin with, follow 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. + +## What we need ? + + Since [godot-rust](https://github.com/godot-rust/gdnative) use [gdnative](https://docs.godotengine.org/en/3.5/tutorials/scripting/gdnative/what_is_gdnative.html) to bind Rust and Godot, we need to install the following dependencies: + + - The [rust-overlay](https://github.com/oxalica/rust-overlay) to include some rusty toolchains like `rustc`, `cargo`, `rustup` and `rls`. + - [openssl](https://www.openssl.org/) that provide us some cryptographic functions for rust. + - [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/) to manage the compilation of the project. + - [godot3](https://godotengine.org/) aliased as godot. + - [clang](https://clang.llvm.org/) to compile and bind rust code to gdnative. + + We will use the unstable version of nixpkgs to get the latest version of all packages and the flakes feature. + + And we need to specify the `LIBCLANG_PATH` and `BINDGEN_EXTRA_CLANG_ARGS` to make the gdnative aware of the headers and the library. ## Specifying dependencies @@ -66,9 +81,9 @@ Now to the Nix part of the tutorial. In your project directory create a `flake.n } ``` -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 Flakes environment In the same directory where you created your flake.nix, you can use the command [`nix develop`](https://nixos.wiki/wiki/Development_environment_with_nix-shell#nix-develop) to load our Flakes config and setup everything for your Nix-capable system. +