Skip to content

Commit 69866b2

Browse files
Improve Nix flake app build flag support (#1221)
* Improve Nix flake app build flag support * Refine Nix wrapper matching and docs
1 parent 7ff8093 commit 69866b2

3 files changed

Lines changed: 181 additions & 20 deletions

File tree

README.md

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,7 @@ nix run .
6363

6464
This will automatically install all build dependencies and compile Redot if the binary doesn't exist.
6565

66-
For manual control over the build process:
67-
68-
```bash
69-
# Enter the Nix development environment
70-
nix develop
71-
72-
# Build Redot (use 'macos' on macOS, 'linuxbsd' on Linux)
73-
scons platform=linuxbsd # or: scons platform=macos
74-
75-
# Run the editor - binary name reflects your platform and architecture
76-
# Examples: redot.linuxbsd.editor.x86_64, redot.macos.editor.arm64
77-
./bin/redot.<platform>.editor.<arch>
78-
```
79-
80-
Nix works on Linux and macOS, and is available at [nixos.org/download.html](https://nixos.org/download.html). The `nix run .` command automatically detects your platform and architecture.
66+
Detailed Nix usage, including passing SCons build flags through `nix run`, forwarding runtime arguments, and manual `nix develop` workflows, is documented in the `Nix usage guide` at `doc/nix.md`.
8167

8268

8369
## Community and contributing

doc/nix.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Nix workflow
2+
3+
If you have the Nix package manager installed, you can build and run the editor in one command:
4+
5+
```bash
6+
nix run .
7+
```
8+
9+
This automatically installs the required build dependencies and compiles Redot if a matching binary does not already exist.
10+
11+
## Passing SCons build flags
12+
13+
You can pass SCons build flags directly through `nix run`:
14+
15+
```bash
16+
# Build with custom SCons flags, then run the editor.
17+
nix run . -- target=editor dev_build=yes num_jobs=12
18+
19+
# Build a release template.
20+
nix run . -- target=template_release production=yes
21+
22+
# Pass SCons build flags first, then `--`, then runtime args for the editor.
23+
nix run . -- target=editor dev_build=yes -- --path /tmp/project
24+
```
25+
26+
Argument handling works like this:
27+
28+
- The first `--` is consumed by Nix.
29+
- Arguments before the next `--` are passed to `scons`.
30+
- Arguments after the next `--` are passed to the built Redot binary.
31+
32+
For a quick reminder of the wrapper syntax, run:
33+
34+
```bash
35+
nix run . -- --help
36+
```
37+
38+
This supports the same SCons flags documented by the build system, such as `production=yes`, `target=template_release`, `module_mono_enabled=yes`, `precision=double`, `ccflags=...`, and more.
39+
40+
The wrapper only uses a subset of those flags when deciding which existing binary can be reused: `target`, `arch`, `dev_build`, `precision`, `threads`, and `extra_suffix`. Other SCons flags such as `production=yes`, `module_mono_enabled=yes`, or custom `ccflags` are still forwarded to `scons`, but they do not change the wrapper's reuse check.
41+
42+
## Manual builds with `nix develop`
43+
44+
For full manual control over the build process:
45+
46+
```bash
47+
# Enter the Nix development environment.
48+
nix develop
49+
50+
# Build Redot (use 'macos' on macOS, 'linuxbsd' on Linux).
51+
scons platform=linuxbsd # or: scons platform=macos
52+
53+
# Example with extra build flags.
54+
scons platform=linuxbsd target=template_release production=yes
55+
56+
# Run the editor - binary names can include optional suffixes.
57+
# Examples:
58+
# redot.linuxbsd.editor.x86_64
59+
# redot.linuxbsd.editor.dev.x86_64
60+
# redot.linuxbsd.editor.double.x86_64
61+
# redot.linuxbsd.editor.x86_64.nothreads
62+
./bin/redot.<platform>.<target>[.dev][.double].<arch>[.nothreads][.<extra_suffix>]
63+
```
64+
65+
## Notes
66+
67+
- `nix run .` only auto-builds when the expected binary for the wrapper-managed naming fields does not exist yet.
68+
- If you want to rebuild with different flags after a binary was already produced, remove the existing binary first or use `nix develop` and run `scons` manually.
69+
- The flake app automatically detects your platform and architecture, and `arch=auto` resolves to the host architecture.
70+
- Nix works on Linux and macOS, and is available at [nixos.org/download.html](https://nixos.org/download.html).

flake.nix

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
];
4747

4848
commonDeps = with pkgs; [
49+
stdenv.cc
50+
gcc-unwrapped
4951
pkg-config
5052
installShellFiles
5153
python3
@@ -64,25 +66,128 @@
6466
deps = if isDarwin then darwinDeps ++ commonDeps else linuxDeps ++ commonDeps;
6567
libraryPathVar = if isDarwin then "DYLD_LIBRARY_PATH" else "LD_LIBRARY_PATH";
6668
platform = if isDarwin then "macos" else "linuxbsd";
67-
binary = if isDarwin then "redot.macos.editor.${arch}" else "redot.linuxbsd.editor.${arch}";
6869
});
6970
in {
7071
apps = forEachSupportedSystem ({
7172
pkgs,
7273
deps,
7374
libraryPathVar,
7475
platform,
75-
binary,
7676
arch,
7777
...
7878
}: let
7979
script = pkgs.writeShellScript "redot" ''
80+
export PATH=${pkgs.lib.makeBinPath deps}:$PATH
8081
export ${libraryPathVar}=${pkgs.lib.makeLibraryPath deps}
81-
if [ ! -f ./bin/${binary} ]; then
82+
83+
scons_args=()
84+
runtime_args=()
85+
parsing_runtime_args=0
86+
show_help=0
87+
target=editor
88+
target_arch=${arch}
89+
scons_platform=${platform}
90+
dev_build=no
91+
precision=single
92+
threads=yes
93+
extra_suffix=
94+
95+
for arg in "$@"; do
96+
if [ "$parsing_runtime_args" -eq 0 ] && [ "$arg" = "--help" ]; then
97+
show_help=1
98+
continue
99+
fi
100+
101+
if [ "$parsing_runtime_args" -eq 0 ] && [ "$arg" = "--" ]; then
102+
parsing_runtime_args=1
103+
continue
104+
fi
105+
106+
if [ "$parsing_runtime_args" -eq 0 ]; then
107+
case "$arg" in
108+
target=*) target=''${arg#target=} ;;
109+
arch=*)
110+
target_arch=''${arg#arch=}
111+
if [ "$target_arch" = "auto" ]; then
112+
target_arch=${arch}
113+
fi
114+
;;
115+
platform=*)
116+
scons_platform=''${arg#platform=}
117+
continue
118+
;;
119+
dev_build=*) dev_build=''${arg#dev_build=} ;;
120+
precision=*) precision=''${arg#precision=} ;;
121+
threads=*) threads=''${arg#threads=} ;;
122+
extra_suffix=*) extra_suffix=''${arg#extra_suffix=} ;;
123+
esac
124+
125+
scons_args+=("$arg")
126+
else
127+
runtime_args+=("$arg")
128+
fi
129+
done
130+
131+
if [ "$show_help" -eq 1 ]; then
132+
cat <<EOF
133+
Usage:
134+
nix run .
135+
nix run . -- [scons build flags...]
136+
nix run . -- [scons build flags...] -- [redot runtime args...]
137+
138+
Examples:
139+
nix run .
140+
nix run . -- target=editor dev_build=yes num_jobs=12
141+
nix run . -- target=template_release production=yes
142+
nix run . -- target=editor dev_build=yes -- --path /tmp/project
143+
144+
Argument handling:
145+
- The first '--' is consumed by Nix.
146+
- Arguments before the next '--' are passed to SCons.
147+
- Arguments after the next '--' are passed to the Redot binary.
148+
149+
Notes:
150+
- Common SCons flags include target=..., dev_build=yes, production=yes,
151+
module_mono_enabled=yes, precision=double, num_jobs=..., and ccflags=...
152+
- The wrapper auto-builds only when a matching binary is not already present.
153+
Use 'nix develop' for full manual rebuild control.
154+
EOF
155+
exit 0
156+
fi
157+
158+
binary_pattern="redot.$scons_platform.''${target}"
159+
160+
if [ "$dev_build" = "yes" ]; then
161+
binary_pattern="$binary_pattern.dev"
162+
fi
163+
164+
if [ "$precision" = "double" ]; then
165+
binary_pattern="$binary_pattern.double"
166+
fi
167+
168+
binary_pattern="$binary_pattern.''${target_arch}"
169+
170+
if [ "$threads" = "no" ]; then
171+
binary_pattern="$binary_pattern.nothreads"
172+
fi
173+
174+
if [ -n "$extra_suffix" ]; then
175+
binary_pattern="$binary_pattern.$extra_suffix"
176+
fi
177+
178+
binary_path="./bin/$binary_pattern"
179+
180+
if [ ! -f "$binary_path" ]; then
82181
echo "Building Redot..."
83-
scons platform=${platform}
182+
scons "''${scons_args[@]}" platform=$scons_platform
84183
fi
85-
exec ./bin/${binary} "$@"
184+
185+
if [ ! -f "$binary_path" ]; then
186+
echo "Could not find a built Redot binary at $binary_path" >&2
187+
exit 1
188+
fi
189+
190+
exec "$binary_path" "''${runtime_args[@]}"
86191
'';
87192
in {
88193
default = {

0 commit comments

Comments
 (0)