Skip to content

Instantly share code, notes, and snippets.

@Woynert
Last active April 4, 2024 05:04
Show Gist options
  • Select an option

  • Save Woynert/3ced1ad594b0f8969c94a08bd21a2988 to your computer and use it in GitHub Desktop.

Select an option

Save Woynert/3ced1ad594b0f8969c94a08bd21a2988 to your computer and use it in GitHub Desktop.
godot-export.nix
{ project_path ? ./src }:
let
pkgs = import
(builtins.fetchTarball {
# this revision contains blender 4.0.2
# https://hydra.nixos.org/eval/1804905
name = "nixos-23.11-1804905";
url = "https://github.com/nixos/nixpkgs/tarball/bb183e5637b6d48804a3ee0fc5ab38df51122d65";
sha256 = "14zjy9h6z9a7pc9v08zddshm4xh5j3vw321n5d3d4fal8k9as9hm";
})
{ };
godot_version = "4.2.1-stable";
godot = pkgs.stdenv.mkDerivation {
name = "mygodot";
src = pkgs.fetchzip {
url = "https://github.com/godotengine/godot/releases/download/${godot_version}/Godot_v${godot_version}_linux.x86_64.zip";
hash = "sha256-dHlwkS2JHgLiXFPncLbSjZ7JqfcySS6k1pYPOyY59yk=";
};
nativeBuildInputs = [
pkgs.autoPatchelfHook
];
installPhase = ''
install -m755 -D Godot_v${godot_version}_linux.x86_64 $out/bin/godot
'';
};
rcedit = pkgs.fetchurl {
url = "https://github.com/electron/rcedit/releases/download/v2.0.0/rcedit-x86.exe";
hash = "sha256-OPtek118tY16mLTtj4dsg/XbAyvNAymwpN5OSh3odrY=";
};
createicon_gd = pkgs.fetchurl {
url = "https://raw.githubusercontent.com/pkowal1982/godoticon/6f390665ebdbe31d21141d0a570707b7ec3d0196/CreateIcon.gd";
hash = "sha256-OT1OIvRsGz6ixbqdA3Y+3V6RW7QHqETherjsoJpPVxA=";
};
setup_export_template = pkgs.writeShellScriptBin "setup-export-template" ''
#!/usr/bin/env sh
# GODOT_VERSION must be set, example: 4.2.1-stable
set -e
if [ -z "$GODOT_VERSION" ]; then
echo "Error: GODOT_VERSION is not set"; exit 1
fi
TEMPLATES_DIR=$HOME/.local/share/godot/export_templates
TEMPLATE_FILE=Godot_v''${GODOT_VERSION}_export_templates.tpz
VERSION_DIR=''${GODOT_VERSION//-/.}
if [ -e "$TEMPLATES_DIR/$VERSION_DIR" ]; then
echo "Export templates for $GODOT_VERSION already exists at $TEMPLATES_DIR/$VERSION_DIR"
exit 0
fi
mkdir -p $TEMPLATES_DIR
echo "Downloading export templates for $GODOT_VERSION at $TEMPLATES_DIR"
cd /tmp
wget https://github.com/godotengine/godot/releases/download/''${GODOT_VERSION}/$TEMPLATE_FILE
unzip $TEMPLATE_FILE -d $TEMPLATES_DIR
rm $TEMPLATE_FILE
mv $TEMPLATES_DIR/templates $TEMPLATES_DIR/$VERSION_DIR
echo "Installed export templates for $GODOT_VERSION at $TEMPLATES_DIR"
'';
setup_editor_config = pkgs.writeShellScriptBin "setup-editor-config" ''
#!/usr/bin/env sh
set -e
EDITOR_SETTINGS=godot-home/.config/godot/editor_settings-4.tres
mkdir -p $(dirname "$EDITOR_SETTINGS")
cat > $EDITOR_SETTINGS <<EOF
[gd_resource type="EditorSettings" format=3]
[resource]
filesystem/import/blender/blender3_path = "$(dirname `which blender`)"
export/windows/rcedit = "${rcedit}"
EOF
'';
godot_build_linux = pkgs.writeShellScriptBin "godot-build-linux" ''
#!/usr/bin/env sh
set -e
PROJECT_PATH=''${PROJECT_PATH:-.}
EXPORT_PATH=$(realpath --relative-to=$PROJECT_PATH $PWD)/export
mkdir -p export/linux
XDG_CONFIG_HOME=$PWD/godot-home/.config \
exec godot --headless $PROJECT_PATH/project.godot --export-release "Linux/X11" $EXPORT_PATH/linux/linux.x86_64
'';
godot_build_windows = pkgs.writeShellScriptBin "godot-build-windows" ''
#!/usr/bin/env sh
set -e
PROJECT_PATH=''${PROJECT_PATH:-.}
EXPORT_PATH=$(realpath --relative-to=$PROJECT_PATH $PWD)/export
if [ ! -e "$PROJECT_PATH/project.godot" ]; then
echo "Error: No project.godot at $PROJECT_PATH"
exit 1
fi
if ! grep -q windows_native_icon "$PROJECT_PATH/project.godot"; then
# change windows icon: creates a icon.ico form a icon.png then add it to the project config
godot --headless -s ${createicon_gd} $PROJECT_PATH/icon.ico $PROJECT_PATH/icon.png
sed -i '/\[application\]/a\config/windows_native_icon="res://icon.ico"' project.godot
fi
mkdir -p export/windows
# disable wine mono, gecko pop-ups
WINEDLLOVERRIDES="mscoree,mshtml=" \
XDG_CONFIG_HOME=$PWD/godot-home/.config \
exec godot --headless $PROJECT_PATH/project.godot --export-release "Windows Desktop" $EXPORT_PATH/windows/windows.exe
'';
in
pkgs.mkShell {
name = "dev-environment";
buildInputs = with pkgs; [
blender
godot
setup_export_template
setup_editor_config
godot_build_linux
godot_build_windows
lf
wget
which
unzip
cacert
coreutils
winePackages.minimal.out
];
shellHook = ''
export GODOT_VERSION=${godot_version}
# see where project.godot is located and save it to PROJECT_PATH
${ if (builtins.pathExists (builtins.toString (project_path + "/project.godot")))
then "export PROJECT_PATH=${builtins.toString project_path}"
else "export PROJECT_PATH=." }
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment